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