gitee.com/liu-zhao234568/cntest@v1.0.0/tests/fuzzers/bn256/bn256_fuzz.go (about)

     1  // Copyright 2018 Péter Szilágyi. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be found
     3  // in the LICENSE file.
     4  
     5  //go:build gofuzz
     6  // +build gofuzz
     7  
     8  package bn256
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"io"
    14  	"math/big"
    15  
    16  	cloudflare "gitee.com/liu-zhao234568/cntest/crypto/bn256/cloudflare"
    17  	google "gitee.com/liu-zhao234568/cntest/crypto/bn256/google"
    18  	"github.com/consensys/gnark-crypto/ecc/bn254"
    19  )
    20  
    21  func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1, *bn254.G1Affine) {
    22  	_, xc, err := cloudflare.RandomG1(input)
    23  	if err != nil {
    24  		// insufficient input
    25  		return nil, nil, nil
    26  	}
    27  	xg := new(google.G1)
    28  	if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
    29  		panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
    30  	}
    31  	xs := new(bn254.G1Affine)
    32  	if err := xs.Unmarshal(xc.Marshal()); err != nil {
    33  		panic(fmt.Sprintf("Could not marshal cloudflare -> gnark: %v", err))
    34  	}
    35  	return xc, xg, xs
    36  }
    37  
    38  func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2, *bn254.G2Affine) {
    39  	_, xc, err := cloudflare.RandomG2(input)
    40  	if err != nil {
    41  		// insufficient input
    42  		return nil, nil, nil
    43  	}
    44  	xg := new(google.G2)
    45  	if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
    46  		panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
    47  	}
    48  	xs := new(bn254.G2Affine)
    49  	if err := xs.Unmarshal(xc.Marshal()); err != nil {
    50  		panic(fmt.Sprintf("Could not marshal cloudflare -> gnark: %v", err))
    51  	}
    52  	return xc, xg, xs
    53  }
    54  
    55  // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
    56  func FuzzAdd(data []byte) int {
    57  	input := bytes.NewReader(data)
    58  	xc, xg, xs := getG1Points(input)
    59  	if xc == nil {
    60  		return 0
    61  	}
    62  	yc, yg, ys := getG1Points(input)
    63  	if yc == nil {
    64  		return 0
    65  	}
    66  	// Ensure both libs can parse the second curve point
    67  	// Add the two points and ensure they result in the same output
    68  	rc := new(cloudflare.G1)
    69  	rc.Add(xc, yc)
    70  
    71  	rg := new(google.G1)
    72  	rg.Add(xg, yg)
    73  
    74  	tmpX := new(bn254.G1Jac).FromAffine(xs)
    75  	tmpY := new(bn254.G1Jac).FromAffine(ys)
    76  	rs := new(bn254.G1Affine).FromJacobian(tmpX.AddAssign(tmpY))
    77  
    78  	if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
    79  		panic("add mismatch: cloudflare/google")
    80  	}
    81  
    82  	if !bytes.Equal(rc.Marshal(), rs.Marshal()) {
    83  		panic("add mismatch: cloudflare/gnark")
    84  	}
    85  	return 1
    86  }
    87  
    88  // FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare
    89  // libraries.
    90  func FuzzMul(data []byte) int {
    91  	input := bytes.NewReader(data)
    92  	pc, pg, ps := getG1Points(input)
    93  	if pc == nil {
    94  		return 0
    95  	}
    96  	// Add the two points and ensure they result in the same output
    97  	remaining := input.Len()
    98  	if remaining == 0 {
    99  		return 0
   100  	}
   101  	if remaining > 128 {
   102  		// The evm only ever uses 32 byte integers, we need to cap this otherwise
   103  		// we run into slow exec. A 236Kb byte integer cause oss-fuzz to report it as slow.
   104  		// 128 bytes should be fine though
   105  		return 0
   106  	}
   107  	buf := make([]byte, remaining)
   108  	input.Read(buf)
   109  
   110  	rc := new(cloudflare.G1)
   111  	rc.ScalarMult(pc, new(big.Int).SetBytes(buf))
   112  
   113  	rg := new(google.G1)
   114  	rg.ScalarMult(pg, new(big.Int).SetBytes(buf))
   115  
   116  	rs := new(bn254.G1Jac)
   117  	psJac := new(bn254.G1Jac).FromAffine(ps)
   118  	rs.ScalarMultiplication(psJac, new(big.Int).SetBytes(buf))
   119  	rsAffine := new(bn254.G1Affine).FromJacobian(rs)
   120  
   121  	if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
   122  		panic("scalar mul mismatch: cloudflare/google")
   123  	}
   124  	if !bytes.Equal(rc.Marshal(), rsAffine.Marshal()) {
   125  		panic("scalar mul mismatch: cloudflare/gnark")
   126  	}
   127  	return 1
   128  }
   129  
   130  func FuzzPair(data []byte) int {
   131  	input := bytes.NewReader(data)
   132  	pc, pg, ps := getG1Points(input)
   133  	if pc == nil {
   134  		return 0
   135  	}
   136  	tc, tg, ts := getG2Points(input)
   137  	if tc == nil {
   138  		return 0
   139  	}
   140  
   141  	// Pair the two points and ensure they result in the same output
   142  	clPair := cloudflare.Pair(pc, tc).Marshal()
   143  	gPair := google.Pair(pg, tg).Marshal()
   144  	if !bytes.Equal(clPair, gPair) {
   145  		panic("pairing mismatch: cloudflare/google")
   146  	}
   147  
   148  	cPair, err := bn254.Pair([]bn254.G1Affine{*ps}, []bn254.G2Affine{*ts})
   149  	if err != nil {
   150  		panic(fmt.Sprintf("gnark/bn254 encountered error: %v", err))
   151  	}
   152  	if !bytes.Equal(clPair, cPair.Marshal()) {
   153  		panic("pairing mismatch: cloudflare/gnark")
   154  	}
   155  
   156  	return 1
   157  }