Skip to content

Commit dc3f350

Browse files
committed
tests/fuzzers/bn256: add PairingCheck fuzzer (ethereum#27252)
* tests/fuzzers/bn256: scale gnark result by constant * tests/fuzzers/bn256: scale gnark result by constant
1 parent 2448a03 commit dc3f350

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

tests/fuzzers/bn256/bn256_fuzz.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,29 @@ func FuzzPair(data []byte) int {
153153
if !bytes.Equal(clPair, gPair) {
154154
panic("pairing mismatch: cloudflare/google")
155155
}
156-
157156
cPair, err := bn254.Pair([]bn254.G1Affine{*ps}, []bn254.G2Affine{*ts})
158157
if err != nil {
159158
panic(fmt.Sprintf("gnark/bn254 encountered error: %v", err))
160159
}
161-
if !bytes.Equal(clPair, cPair.Marshal()) {
160+
161+
// gnark uses a different pairing algorithm which might produce
162+
// different but also correct outputs, we need to scale the output by s
163+
164+
u, _ := new(big.Int).SetString("0x44e992b44a6909f1", 0)
165+
u_exp2 := new(big.Int).Exp(u, big.NewInt(2), nil) // u^2
166+
u_6_exp2 := new(big.Int).Mul(big.NewInt(6), u_exp2) // 6*u^2
167+
u_3 := new(big.Int).Mul(big.NewInt(3), u) // 3*u
168+
inner := u_6_exp2.Add(u_6_exp2, u_3) // 6*u^2 + 3*u
169+
inner.Add(inner, big.NewInt(1)) // 6*u^2 + 3*u + 1
170+
u_2 := new(big.Int).Mul(big.NewInt(2), u) // 2*u
171+
s := u_2.Mul(u_2, inner) // 2*u(6*u^2 + 3*u + 1)
172+
173+
gRes := new(bn254.GT)
174+
if err := gRes.SetBytes(clPair); err != nil {
175+
panic(err)
176+
}
177+
gRes = gRes.Exp(*gRes, s)
178+
if !bytes.Equal(cPair.Marshal(), gRes.Marshal()) {
162179
panic("pairing mismatch: cloudflare/gnark")
163180
}
164181

0 commit comments

Comments
 (0)