tests/fuzzers: crypto/bn256 and crypto/bls12381 tests against gnark-crypto (#22755)
Add more cross-fuzzers to fuzz bls with gnark versus geth's own bls12-381 library
This commit is contained in:
@ -12,12 +12,12 @@ import (
|
||||
"io"
|
||||
"math/big"
|
||||
|
||||
gurvy "github.com/consensys/gurvy/bn256"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254"
|
||||
cloudflare "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
|
||||
google "github.com/ethereum/go-ethereum/crypto/bn256/google"
|
||||
)
|
||||
|
||||
func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1, *gurvy.G1Affine) {
|
||||
func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1, *bn254.G1Affine) {
|
||||
_, xc, err := cloudflare.RandomG1(input)
|
||||
if err != nil {
|
||||
// insufficient input
|
||||
@ -25,16 +25,16 @@ func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1, *gurvy.G1Affine)
|
||||
}
|
||||
xg := new(google.G1)
|
||||
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
|
||||
}
|
||||
xs := new(gurvy.G1Affine)
|
||||
xs := new(bn254.G1Affine)
|
||||
if err := xs.Unmarshal(xc.Marshal()); err != nil {
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> consensys:", err))
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> gnark: %v", err))
|
||||
}
|
||||
return xc, xg, xs
|
||||
}
|
||||
|
||||
func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2, *gurvy.G2Affine) {
|
||||
func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2, *bn254.G2Affine) {
|
||||
_, xc, err := cloudflare.RandomG2(input)
|
||||
if err != nil {
|
||||
// insufficient input
|
||||
@ -42,11 +42,11 @@ func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2, *gurvy.G2Affine)
|
||||
}
|
||||
xg := new(google.G2)
|
||||
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
|
||||
}
|
||||
xs := new(gurvy.G2Affine)
|
||||
xs := new(bn254.G2Affine)
|
||||
if err := xs.Unmarshal(xc.Marshal()); err != nil {
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> consensys:", err))
|
||||
panic(fmt.Sprintf("Could not marshal cloudflare -> gnark: %v", err))
|
||||
}
|
||||
return xc, xg, xs
|
||||
}
|
||||
@ -70,16 +70,16 @@ func FuzzAdd(data []byte) int {
|
||||
rg := new(google.G1)
|
||||
rg.Add(xg, yg)
|
||||
|
||||
tmpX := new(gurvy.G1Jac).FromAffine(xs)
|
||||
tmpY := new(gurvy.G1Jac).FromAffine(ys)
|
||||
rs := new(gurvy.G1Affine).FromJacobian(tmpX.AddAssign(tmpY))
|
||||
tmpX := new(bn254.G1Jac).FromAffine(xs)
|
||||
tmpY := new(bn254.G1Jac).FromAffine(ys)
|
||||
rs := new(bn254.G1Affine).FromJacobian(tmpX.AddAssign(tmpY))
|
||||
|
||||
if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
|
||||
panic("add mismatch: cloudflare/google")
|
||||
}
|
||||
|
||||
if !bytes.Equal(rc.Marshal(), rs.Marshal()) {
|
||||
panic("add mismatch: cloudflare/consensys")
|
||||
panic("add mismatch: cloudflare/gnark")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@ -112,16 +112,16 @@ func FuzzMul(data []byte) int {
|
||||
rg := new(google.G1)
|
||||
rg.ScalarMult(pg, new(big.Int).SetBytes(buf))
|
||||
|
||||
rs := new(gurvy.G1Jac)
|
||||
psJac := new(gurvy.G1Jac).FromAffine(ps)
|
||||
rs := new(bn254.G1Jac)
|
||||
psJac := new(bn254.G1Jac).FromAffine(ps)
|
||||
rs.ScalarMultiplication(psJac, new(big.Int).SetBytes(buf))
|
||||
rsAffine := new(gurvy.G1Affine).FromJacobian(rs)
|
||||
rsAffine := new(bn254.G1Affine).FromJacobian(rs)
|
||||
|
||||
if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
|
||||
panic("scalar mul mismatch: cloudflare/google")
|
||||
}
|
||||
if !bytes.Equal(rc.Marshal(), rsAffine.Marshal()) {
|
||||
panic("scalar mul mismatch: cloudflare/consensys")
|
||||
panic("scalar mul mismatch: cloudflare/gnark")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@ -136,18 +136,20 @@ func FuzzPair(data []byte) int {
|
||||
if tc == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Pair the two points and ensure they result in the same output
|
||||
clPair := cloudflare.PairingCheck([]*cloudflare.G1{pc}, []*cloudflare.G2{tc})
|
||||
if clPair != google.PairingCheck([]*google.G1{pg}, []*google.G2{tg}) {
|
||||
clPair := cloudflare.Pair(pc, tc).Marshal()
|
||||
gPair := google.Pair(pg, tg).Marshal()
|
||||
if !bytes.Equal(clPair, gPair) {
|
||||
panic("pairing mismatch: cloudflare/google")
|
||||
}
|
||||
|
||||
coPair, err := gurvy.PairingCheck([]gurvy.G1Affine{*ps}, []gurvy.G2Affine{*ts})
|
||||
cPair, err := bn254.Pair([]bn254.G1Affine{*ps}, []bn254.G2Affine{*ts})
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("gurvy encountered error: %v", err))
|
||||
panic(fmt.Sprintf("gnark/bn254 encountered error: %v", err))
|
||||
}
|
||||
if clPair != coPair {
|
||||
panic("pairing mismatch: cloudflare/consensys")
|
||||
if !bytes.Equal(clPair, cPair.Marshal()) {
|
||||
panic("pairing mismatch: cloudflare/gnark")
|
||||
}
|
||||
|
||||
return 1
|
||||
|
Reference in New Issue
Block a user