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