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 and cp-wjhan committed Jun 2, 2023
1 parent 5e42ef1 commit 65051f3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
5 changes: 3 additions & 2 deletions go.mod
Expand Up @@ -58,8 +58,11 @@ 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
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
github.com/yahoo/coname v0.0.0-20170609175141-84592ddf8673
github.com/yoseplee/vrf v0.0.0-20210814110709-d1caf509310b
go.etcd.io/etcd/api/v3 v3.5.2
go.etcd.io/etcd/client/v3 v3.5.2
go.etcd.io/etcd/server/v3 v3.5.2
Expand All @@ -72,8 +75,6 @@ require (
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
gopkg.in/urfave/cli.v1 v1.20.0
github.com/yoseplee/vrf v0.0.0-20210814110709-d1caf509310b
github.com/yahoo/coname v0.0.0-20170609175141-84592ddf8673
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -561,6 +561,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
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
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 65051f3

Please sign in to comment.