Skip to content

Commit

Permalink
tests/fuzzers/bls12381: Add BLST to fuzzing support (ethereum#24249)
Browse files Browse the repository at this point in the history
* tests/fuzzers/bls12381: added blst library

* go.mod: added blst dependency

* tests/fuzzers/bls12381: stuff

* tests/fuzzers/bls12381: added blst to pairing fuzzer
  • Loading branch information
MariusVanDerWijden authored Jun 2, 2022
1 parent d8a2305 commit 3c6d6f7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
github.com/stretchr/testify v1.7.0
github.com/supranational/blst v0.3.8-0.20220526154634-513d2456b344 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/supranational/blst v0.3.6 h1:a24cPQB0qYpXPMZx177aapCM50/YrTMt/TKAUa7TzdM=
github.com/supranational/blst v0.3.6/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/supranational/blst v0.3.7 h1:QObqTzlW30Z947JMe0MH12mVhFOxgtDapuWvPvCEGDE=
github.com/supranational/blst v0.3.7/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/supranational/blst v0.3.8-0.20220526154634-513d2456b344 h1:m+8fKfQwCAy1QjzINvKe/pYtLjo2dl59x2w9YSEJxuY=
github.com/supranational/blst v0.3.8-0.20220526154634-513d2456b344/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
Expand Down
64 changes: 51 additions & 13 deletions tests/fuzzers/bls12381/bls12381_fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ import (
"github.com/consensys/gnark-crypto/ecc/bls12-381/fp"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/ethereum/go-ethereum/crypto/bls12381"
blst "github.com/supranational/blst/bindings/go"
)

func FuzzCrossPairing(data []byte) int {
input := bytes.NewReader(data)

// get random G1 points
kpG1, cpG1, err := getG1Points(input)
kpG1, cpG1, blG1, err := getG1Points(input)
if err != nil {
return 0
}

// get random G2 points
kpG2, cpG2, err := getG2Points(input)
kpG2, cpG2, blG2, err := getG2Points(input)
if err != nil {
return 0
}
Expand All @@ -63,20 +64,29 @@ func FuzzCrossPairing(data []byte) int {
panic("pairing mismatch gnark / geth ")
}

var b []byte
ctx := blst.PairingCtx(false, b)
// compute pairing using blst
blst.PairingRawAggregate(ctx, blG2, blG1)
blstResult := blst.PairingAsFp12(ctx)
if !(bytes.Equal(blstResult.ToBendian(), bls12381.NewGT().ToBytes(kResult))) {
panic("pairing mismatch blst / geth ")
}

return 1
}

func FuzzCrossG1Add(data []byte) int {
input := bytes.NewReader(data)

// get random G1 points
kp1, cp1, err := getG1Points(input)
kp1, cp1, bl1, err := getG1Points(input)
if err != nil {
return 0
}

// get random G1 points
kp2, cp2, err := getG1Points(input)
kp2, cp2, bl2, err := getG1Points(input)
if err != nil {
return 0
}
Expand All @@ -96,20 +106,25 @@ func FuzzCrossG1Add(data []byte) int {
panic("G1 point addition mismatch gnark / geth ")
}

bl3 := blst.P1AffinesAdd([]*blst.P1Affine{bl1, bl2})
if !(bytes.Equal(cp.Marshal(), bl3.Serialize())) {
panic("G1 point addition mismatch blst / geth ")
}

return 1
}

func FuzzCrossG2Add(data []byte) int {
input := bytes.NewReader(data)

// get random G2 points
kp1, cp1, err := getG2Points(input)
kp1, cp1, bl1, err := getG2Points(input)
if err != nil {
return 0
}

// get random G2 points
kp2, cp2, err := getG2Points(input)
kp2, cp2, bl2, err := getG2Points(input)
if err != nil {
return 0
}
Expand All @@ -129,6 +144,11 @@ func FuzzCrossG2Add(data []byte) int {
panic("G2 point addition mismatch gnark / geth ")
}

bl3 := blst.P2AffinesAdd([]*blst.P2Affine{bl1, bl2})
if !(bytes.Equal(cp.Marshal(), bl3.Serialize())) {
panic("G1 point addition mismatch blst / geth ")
}

return 1
}

Expand All @@ -148,7 +168,7 @@ func FuzzCrossG1MultiExp(data []byte) int {
break
}
// get a random G1 point as basis
kp1, cp1, err := getG1Points(input)
kp1, cp1, _, err := getG1Points(input)
if err != nil {
break
}
Expand Down Expand Up @@ -183,11 +203,11 @@ func FuzzCrossG1MultiExp(data []byte) int {
return 1
}

func getG1Points(input io.Reader) (*bls12381.PointG1, *gnark.G1Affine, error) {
func getG1Points(input io.Reader) (*bls12381.PointG1, *gnark.G1Affine, *blst.P1Affine, error) {
// sample a random scalar
s, err := randomScalar(input, fp.Modulus())
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

// compute a random point
Expand All @@ -206,14 +226,23 @@ func getG1Points(input io.Reader) (*bls12381.PointG1, *gnark.G1Affine, error) {
panic("bytes(gnark.G1) != bytes(geth.G1)")
}

return kp, cp, nil
// marshal gnark point -> blst point
var p1 *blst.P1Affine
var scalar *blst.Scalar
scalar.Deserialize(s.Bytes())
p1.From(scalar)
if !bytes.Equal(p1.Serialize(), cpBytes) {
panic("bytes(blst.G1) != bytes(geth.G1)")
}

return kp, cp, p1, nil
}

func getG2Points(input io.Reader) (*bls12381.PointG2, *gnark.G2Affine, error) {
func getG2Points(input io.Reader) (*bls12381.PointG2, *gnark.G2Affine, *blst.P2Affine, error) {
// sample a random scalar
s, err := randomScalar(input, fp.Modulus())
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

// compute a random point
Expand All @@ -232,7 +261,16 @@ func getG2Points(input io.Reader) (*bls12381.PointG2, *gnark.G2Affine, error) {
panic("bytes(gnark.G2) != bytes(geth.G2)")
}

return kp, cp, nil
// marshal gnark point -> blst point
var p2 *blst.P2Affine
var scalar *blst.Scalar
scalar.Deserialize(s.Bytes())
p2.From(scalar)
if !bytes.Equal(p2.Serialize(), cpBytes) {
panic("bytes(blst.G2) != bytes(geth.G2)")
}

return kp, cp, p2, nil
}

func randomScalar(r io.Reader, max *big.Int) (k *big.Int, err error) {
Expand Down

0 comments on commit 3c6d6f7

Please sign in to comment.