Skip to content

Commit 6e77459

Browse files
tests/fuzzers/bn256: scale gnark result by constant
1 parent 4a9fa31 commit 6e77459

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tests/fuzzers/bn256/bn256_fuzz.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,34 @@ func FuzzPair(data []byte) int {
151151
}
152152

153153
// Pair the two points and ensure they result in the same output
154-
clPair := cloudflare.Pair(pc, tc).Marshal()
154+
clPair := cloudflare.Pair(pc, tc).Marshal() // e_cloudflare(P, Q)
155155
gPair := google.Pair(pg, tg).Marshal()
156156
if !bytes.Equal(clPair, gPair) {
157157
panic("pairing mismatch: cloudflare/google")
158158
}
159-
160-
cPair, err := bn254.Pair([]bn254.G1Affine{*ps}, []bn254.G2Affine{*ts})
159+
cPair, err := bn254.Pair([]bn254.G1Affine{*ps}, []bn254.G2Affine{*ts}) // e_gnark(-P, Q)
161160
if err != nil {
162161
panic(fmt.Sprintf("gnark/bn254 encountered error: %v", err))
163162
}
164-
if !bytes.Equal(clPair, cPair.Marshal()) {
163+
164+
// gnark uses a different pairing algorithm which might produce
165+
// different but also correct outputs, we need to scale the output by s
166+
167+
u, _ := new(big.Int).SetString("0x44e992b44a6909f1", 0)
168+
u_exp2 := new(big.Int).Exp(u, big.NewInt(2), nil) // u^2
169+
u_6_exp2 := new(big.Int).Mul(big.NewInt(6), u_exp2) // 6*u^2
170+
u_3 := new(big.Int).Mul(big.NewInt(3), u) // 3*u
171+
inner := u_6_exp2.Add(u_6_exp2, u_3) // 6*u^2 + 3*u
172+
inner.Add(inner, big.NewInt(1)) // 6*u^2 + 3*u + 1
173+
u_2 := new(big.Int).Mul(big.NewInt(2), u) // 2*u
174+
s := u_2.Mul(u_2, inner) // 2*u(6*u^2 + 3*u + 1)
175+
176+
gRes := new(bn254.GT)
177+
if err := gRes.SetBytes(clPair); err != nil {
178+
panic(err)
179+
}
180+
gRes = gRes.Exp(*gRes, s)
181+
if !bytes.Equal(cPair.Marshal(), gRes.Marshal()) {
165182
panic("pairing mismatch: cloudflare/gnark")
166183
}
167184

0 commit comments

Comments
 (0)