github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/crypto/bn256/bn256_fuzz.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // +build gofuzz
    18  
    19  package bn256
    20  
    21  import (
    22  	"bytes"
    23  	"math/big"
    24  
    25  	cloudflare "github.com/vntchain/go-vnt/crypto/bn256/cloudflare"
    26  	google "github.com/vntchain/go-vnt/crypto/bn256/google"
    27  )
    28  
    29  // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
    30  func FuzzAdd(data []byte) int {
    31  	// Ensure we have enough data in the first place
    32  	if len(data) != 128 {
    33  		return 0
    34  	}
    35  	// Ensure both libs can parse the first curve point
    36  	xc := new(cloudflare.G1)
    37  	_, errc := xc.Unmarshal(data[:64])
    38  
    39  	xg := new(google.G1)
    40  	_, errg := xg.Unmarshal(data[:64])
    41  
    42  	if (errc == nil) != (errg == nil) {
    43  		panic("parse mismatch")
    44  	} else if errc != nil {
    45  		return 0
    46  	}
    47  	// Ensure both libs can parse the second curve point
    48  	yc := new(cloudflare.G1)
    49  	_, errc = yc.Unmarshal(data[64:])
    50  
    51  	yg := new(google.G1)
    52  	_, errg = yg.Unmarshal(data[64:])
    53  
    54  	if (errc == nil) != (errg == nil) {
    55  		panic("parse mismatch")
    56  	} else if errc != nil {
    57  		return 0
    58  	}
    59  	// Add the two points and ensure they result in the same output
    60  	rc := new(cloudflare.G1)
    61  	rc.Add(xc, yc)
    62  
    63  	rg := new(google.G1)
    64  	rg.Add(xg, yg)
    65  
    66  	if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
    67  		panic("add mismatch")
    68  	}
    69  	return 0
    70  }
    71  
    72  // FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare
    73  // libraries.
    74  func FuzzMul(data []byte) int {
    75  	// Ensure we have enough data in the first place
    76  	if len(data) != 96 {
    77  		return 0
    78  	}
    79  	// Ensure both libs can parse the curve point
    80  	pc := new(cloudflare.G1)
    81  	_, errc := pc.Unmarshal(data[:64])
    82  
    83  	pg := new(google.G1)
    84  	_, errg := pg.Unmarshal(data[:64])
    85  
    86  	if (errc == nil) != (errg == nil) {
    87  		panic("parse mismatch")
    88  	} else if errc != nil {
    89  		return 0
    90  	}
    91  	// Add the two points and ensure they result in the same output
    92  	rc := new(cloudflare.G1)
    93  	rc.ScalarMult(pc, new(big.Int).SetBytes(data[64:]))
    94  
    95  	rg := new(google.G1)
    96  	rg.ScalarMult(pg, new(big.Int).SetBytes(data[64:]))
    97  
    98  	if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
    99  		panic("scalar mul mismatch")
   100  	}
   101  	return 0
   102  }
   103  
   104  func FuzzPair(data []byte) int {
   105  	// Ensure we have enough data in the first place
   106  	if len(data) != 192 {
   107  		return 0
   108  	}
   109  	// Ensure both libs can parse the curve point
   110  	pc := new(cloudflare.G1)
   111  	_, errc := pc.Unmarshal(data[:64])
   112  
   113  	pg := new(google.G1)
   114  	_, errg := pg.Unmarshal(data[:64])
   115  
   116  	if (errc == nil) != (errg == nil) {
   117  		panic("parse mismatch")
   118  	} else if errc != nil {
   119  		return 0
   120  	}
   121  	// Ensure both libs can parse the twist point
   122  	tc := new(cloudflare.G2)
   123  	_, errc = tc.Unmarshal(data[64:])
   124  
   125  	tg := new(google.G2)
   126  	_, errg = tg.Unmarshal(data[64:])
   127  
   128  	if (errc == nil) != (errg == nil) {
   129  		panic("parse mismatch")
   130  	} else if errc != nil {
   131  		return 0
   132  	}
   133  	// Pair the two points and ensure thet result in the same output
   134  	if cloudflare.PairingCheck([]*cloudflare.G1{pc}, []*cloudflare.G2{tc}) != google.PairingCheck([]*google.G1{pg}, []*google.G2{tg}) {
   135  		panic("pair mismatch")
   136  	}
   137  	return 0
   138  }