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