From d6b55749e6dd1c679e3fec4e7b6802159b8d786f Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 6 Jun 2022 17:01:59 +0200 Subject: [PATCH] tests/fuzzers/bls12381: fix blst deserializing (#25036) * tests/fuzzers/bls12381: fix blst deserializing * tests/fuzzers/bls12381: fix blst deserializing --- tests/fuzzers/bls12381/bls12381_fuzz.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/fuzzers/bls12381/bls12381_fuzz.go b/tests/fuzzers/bls12381/bls12381_fuzz.go index c511c6501..8f5761cf5 100644 --- a/tests/fuzzers/bls12381/bls12381_fuzz.go +++ b/tests/fuzzers/bls12381/bls12381_fuzz.go @@ -29,6 +29,7 @@ import ( gnark "github.com/consensys/gnark-crypto/ecc/bls12-381" "github.com/consensys/gnark-crypto/ecc/bls12-381/fp" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto/bls12381" blst "github.com/supranational/blst/bindings/go" ) @@ -70,7 +71,9 @@ func FuzzCrossPairing(data []byte) int { blst.PairingRawAggregate(ctx, blG2, blG1) blstResult := blst.PairingAsFp12(ctx) if !(bytes.Equal(blstResult.ToBendian(), bls12381.NewGT().ToBytes(kResult))) { - panic("pairing mismatch blst / geth ") + fmt.Printf("geth: %v\n", common.Bytes2Hex(bls12381.NewGT().ToBytes(kResult))) + fmt.Printf("blst: %v\n", common.Bytes2Hex(blstResult.ToBendian())) + panic("pairing mismatch blst / geth") } return 1 @@ -227,10 +230,8 @@ func getG1Points(input io.Reader) (*bls12381.PointG1, *gnark.G1Affine, *blst.P1A } // marshal gnark point -> blst point - var p1 *blst.P1Affine - var scalar *blst.Scalar - scalar.Deserialize(s.Bytes()) - p1.From(scalar) + scalar := new(blst.Scalar).FromBEndian(common.LeftPadBytes(s.Bytes(), 32)) + p1 := new(blst.P1Affine).From(scalar) if !bytes.Equal(p1.Serialize(), cpBytes) { panic("bytes(blst.G1) != bytes(geth.G1)") } @@ -262,10 +263,9 @@ func getG2Points(input io.Reader) (*bls12381.PointG2, *gnark.G2Affine, *blst.P2A } // marshal gnark point -> blst point - var p2 *blst.P2Affine - var scalar *blst.Scalar - scalar.Deserialize(s.Bytes()) - p2.From(scalar) + // Left pad the scalar to 32 bytes + scalar := new(blst.Scalar).FromBEndian(common.LeftPadBytes(s.Bytes(), 32)) + p2 := new(blst.P2Affine).From(scalar) if !bytes.Equal(p2.Serialize(), cpBytes) { panic("bytes(blst.G2) != bytes(geth.G2)") }