Skip to content

Commit 084a110

Browse files
committed
add naive reference implementation of MultiExponentiation for cross-validation with gnark fast impl
1 parent 1d33584 commit 084a110

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

tests/fuzzers/bls12381/bls12381_fuzz.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,10 @@ func fuzzCrossG1MultiExp(data []byte) int {
203203
cp := new(bls12381.G1Affine)
204204
cp.MultiExp(gnarkPoints, gnarkScalars, ecc.MultiExpConfig{})
205205

206-
/* TODO: add back but compare against reference impl
207-
// compare result
208-
if !(bytes.Equal(cp.Marshal(), g1.ToBytes(&kp))) {
209-
panic("G1 multi exponentiation mismatch gnark / geth ")
206+
expected := multiExpG1(gnarkPoints, gnarkScalars)
207+
if !bytes.Equal(cp.Marshal(), expected.Marshal()) {
208+
panic("g1 multi exponentiation mismatch")
210209
}
211-
*/
212210
return 1
213211
}
214212

@@ -267,3 +265,16 @@ func randomScalar(r io.Reader, max *big.Int) (k *big.Int, err error) {
267265
}
268266
}
269267
}
268+
269+
// multiExpG1 is a naive implementation of G1 multi-exponentiation
270+
func multiExpG1(gs []bls12381.G1Affine, scalars []fr.Element) bls12381.G1Affine {
271+
_, _, res, _ := bls12381.Generators()
272+
for i := 0; i < len(gs); i++ {
273+
tmp := new(bls12381.G1Affine)
274+
sb := scalars[i].Bytes()
275+
scalarBytes := new(big.Int).SetBytes(sb[:])
276+
tmp.ScalarMultiplication(&gs[i], scalarBytes)
277+
res.Add(&res, tmp)
278+
}
279+
return res
280+
}

0 commit comments

Comments
 (0)