Skip to content

Commit 55b0e54

Browse files
authored
fix: use the little-endian representation for serialized values in KZG Pointeval precompile (#1614)
1 parent 8432c7c commit 55b0e54

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

std/evmprecompiles/10-kzg_point_evaluation.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func KzgPointEvaluation(
123123
// versioned hash
124124
var versionedHashBytes [sha256.Size]uints.U8
125125
for i := range versionedHash {
126-
res, err := conversion.NativeToBytes(api, versionedHash[len(versionedHash)-1-i])
126+
res, err := conversion.NativeToBytes(api, versionedHash[i])
127127
if err != nil {
128128
return fmt.Errorf("convert versioned hash element %d to bytes: %w", i, err)
129129
}
@@ -133,7 +133,7 @@ func KzgPointEvaluation(
133133
// commitment
134134
var comSerializedBytes [bls12381.SizeOfG1AffineCompressed]uints.U8
135135
for i := range commitmentCompressed {
136-
res, err := conversion.NativeToBytes(api, commitmentCompressed[len(commitmentCompressed)-1-i])
136+
res, err := conversion.NativeToBytes(api, commitmentCompressed[i])
137137
if err != nil {
138138
return fmt.Errorf("convert commitment element %d to bytes: %w", i, err)
139139
}
@@ -142,7 +142,7 @@ func KzgPointEvaluation(
142142
// proof
143143
var proofSerialisedBytes [bls12381.SizeOfG1AffineCompressed]uints.U8
144144
for i := range proofCompressed {
145-
res, err := conversion.NativeToBytes(api, proofCompressed[len(proofCompressed)-1-i])
145+
res, err := conversion.NativeToBytes(api, proofCompressed[i])
146146
if err != nil {
147147
return fmt.Errorf("convert proof element %d to bytes: %w", i, err)
148148
}
@@ -289,8 +289,8 @@ func KzgPointEvaluationFailure(
289289
Y: *fp.NewElement(0),
290290
}
291291
dummyVersionedHash := []frontend.Variable{
292-
"0xdef7ab966d7b770905398eba3c444014",
293292
"0x010657f37554c781402a22917dee2f75",
293+
"0xdef7ab966d7b770905398eba3c444014",
294294
}
295295
// -- check that the masks of compressed commitment and proof are correct
296296
// (infinity, small y, large y). If either of them is not correct, then we
@@ -300,15 +300,15 @@ func KzgPointEvaluationFailure(
300300
// - first we unpack the packed commitment and proof into bytes
301301
var comSerializedBytes [bls12381.SizeOfG1AffineCompressed]uints.U8
302302
for i := range commitmentCompressed {
303-
res, err := conversion.NativeToBytes(api, commitmentCompressed[len(commitmentCompressed)-1-i])
303+
res, err := conversion.NativeToBytes(api, commitmentCompressed[i])
304304
if err != nil {
305305
return fmt.Errorf("convert commitment element %d to bytes: %w", i, err)
306306
}
307307
copy(comSerializedBytes[i*16:(i+1)*16], res[16:])
308308
}
309309
var proofSerialisedBytes [bls12381.SizeOfG1AffineCompressed]uints.U8
310310
for i := range proofCompressed {
311-
res, err := conversion.NativeToBytes(api, proofCompressed[len(proofCompressed)-1-i])
311+
res, err := conversion.NativeToBytes(api, proofCompressed[i])
312312
if err != nil {
313313
return fmt.Errorf("convert proof element %d to bytes: %w", i, err)
314314
}
@@ -458,7 +458,7 @@ func KzgPointEvaluationFailure(
458458
// - first map the versioned hash to bytes
459459
var versionedHashBytes [sha256.Size]uints.U8
460460
for i := range versionedHash {
461-
res, err := conversion.NativeToBytes(api, versionedHash[len(versionedHash)-1-i])
461+
res, err := conversion.NativeToBytes(api, versionedHash[i])
462462
if err != nil {
463463
return fmt.Errorf("convert versioned hash element %d to bytes: %w", i, err)
464464
}

std/evmprecompiles/10-kzg_point_evaluation_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ func TestKzgPointEvaluationPrecompile(t *testing.T) {
8080
h[0] = blobCommitmentVersionKZG
8181

8282
witnessHash := [2]frontend.Variable{
83-
encode(h[16:32]),
8483
encode(h[0:16]),
84+
encode(h[16:32]),
8585
}
8686
// - commitment into 3 limbs
8787
witnessCommitment := [3]frontend.Variable{
88-
encode(commitmentBytes[32:48]),
89-
encode(commitmentBytes[16:32]),
9088
encode(commitmentBytes[0:16]),
89+
encode(commitmentBytes[16:32]),
90+
encode(commitmentBytes[32:48]),
9191
}
9292
// - proof into 3 limbs
9393
proofUncompressed := kzgProof.H.Bytes()
9494
witnessProof := [3]frontend.Variable{
95-
encode(proofUncompressed[32:48]),
96-
encode(proofUncompressed[16:32]),
9795
encode(proofUncompressed[0:16]),
96+
encode(proofUncompressed[16:32]),
97+
encode(proofUncompressed[32:48]),
9898
}
9999

100100
// prepare the constant return values
@@ -141,20 +141,20 @@ func (c *kzgPointEvalFailureCircuit) Define(api frontend.API) error {
141141

142142
func runFailureCircuit(_ *test.Assert, evaluationPoint fr.Element, claimedValue fr.Element, hashBytes []byte, commitmentBytes [48]byte, proofBytes [48]byte, blobSize []int, blsModulus []string) error {
143143
witnessHash := [2]frontend.Variable{
144-
encode(hashBytes[16:32]),
145144
encode(hashBytes[0:16]),
145+
encode(hashBytes[16:32]),
146146
}
147147
// - commitment into 3 limbs
148148
witnessCommitment := [3]frontend.Variable{
149-
encode(commitmentBytes[32:48]),
150-
encode(commitmentBytes[16:32]),
151149
encode(commitmentBytes[0:16]),
150+
encode(commitmentBytes[16:32]),
151+
encode(commitmentBytes[32:48]),
152152
}
153153
// - proof into 3 limbs
154154
witnessProof := [3]frontend.Variable{
155-
encode(proofBytes[32:48]),
156-
encode(proofBytes[16:32]),
157155
encode(proofBytes[0:16]),
156+
encode(proofBytes[16:32]),
157+
encode(proofBytes[32:48]),
158158
}
159159

160160
// prepare the constant return values

0 commit comments

Comments
 (0)