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