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