github.com/luckypickle/go-ethereum-vet@v1.14.2/crypto/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 "math/big" 13 14 cloudflare "github.com/luckypickle/go-ethereum-vet/crypto/bn256/cloudflare" 15 google "github.com/luckypickle/go-ethereum-vet/crypto/bn256/google" 16 ) 17 18 // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries. 19 func FuzzAdd(data []byte) int { 20 // Ensure we have enough data in the first place 21 if len(data) != 128 { 22 return 0 23 } 24 // Ensure both libs can parse the first curve point 25 xc := new(cloudflare.G1) 26 _, errc := xc.Unmarshal(data[:64]) 27 28 xg := new(google.G1) 29 _, errg := xg.Unmarshal(data[:64]) 30 31 if (errc == nil) != (errg == nil) { 32 panic("parse mismatch") 33 } else if errc != nil { 34 return 0 35 } 36 // Ensure both libs can parse the second curve point 37 yc := new(cloudflare.G1) 38 _, errc = yc.Unmarshal(data[64:]) 39 40 yg := new(google.G1) 41 _, errg = yg.Unmarshal(data[64:]) 42 43 if (errc == nil) != (errg == nil) { 44 panic("parse mismatch") 45 } else if errc != nil { 46 return 0 47 } 48 // Add the two points and ensure they result in the same output 49 rc := new(cloudflare.G1) 50 rc.Add(xc, yc) 51 52 rg := new(google.G1) 53 rg.Add(xg, yg) 54 55 if !bytes.Equal(rc.Marshal(), rg.Marshal()) { 56 panic("add mismatch") 57 } 58 return 0 59 } 60 61 // FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare 62 // libraries. 63 func FuzzMul(data []byte) int { 64 // Ensure we have enough data in the first place 65 if len(data) != 96 { 66 return 0 67 } 68 // Ensure both libs can parse the curve point 69 pc := new(cloudflare.G1) 70 _, errc := pc.Unmarshal(data[:64]) 71 72 pg := new(google.G1) 73 _, errg := pg.Unmarshal(data[:64]) 74 75 if (errc == nil) != (errg == nil) { 76 panic("parse mismatch") 77 } else if errc != nil { 78 return 0 79 } 80 // Add the two points and ensure they result in the same output 81 rc := new(cloudflare.G1) 82 rc.ScalarMult(pc, new(big.Int).SetBytes(data[64:])) 83 84 rg := new(google.G1) 85 rg.ScalarMult(pg, new(big.Int).SetBytes(data[64:])) 86 87 if !bytes.Equal(rc.Marshal(), rg.Marshal()) { 88 panic("scalar mul mismatch") 89 } 90 return 0 91 } 92 93 func FuzzPair(data []byte) int { 94 // Ensure we have enough data in the first place 95 if len(data) != 192 { 96 return 0 97 } 98 // Ensure both libs can parse the curve point 99 pc := new(cloudflare.G1) 100 _, errc := pc.Unmarshal(data[:64]) 101 102 pg := new(google.G1) 103 _, errg := pg.Unmarshal(data[:64]) 104 105 if (errc == nil) != (errg == nil) { 106 panic("parse mismatch") 107 } else if errc != nil { 108 return 0 109 } 110 // Ensure both libs can parse the twist point 111 tc := new(cloudflare.G2) 112 _, errc = tc.Unmarshal(data[64:]) 113 114 tg := new(google.G2) 115 _, errg = tg.Unmarshal(data[64:]) 116 117 if (errc == nil) != (errg == nil) { 118 panic("parse mismatch") 119 } else if errc != nil { 120 return 0 121 } 122 // Pair the two points and ensure thet result in the same output 123 if cloudflare.PairingCheck([]*cloudflare.G1{pc}, []*cloudflare.G2{tc}) != google.PairingCheck([]*google.G1{pg}, []*google.G2{tg}) { 124 panic("pair mismatch") 125 } 126 return 0 127 }