@@ -20,42 +20,52 @@ package bn256
2020
2121import (
2222 "bytes"
23+ "fmt"
24+ "io"
2325 "math/big"
2426
2527 cloudflare "github.com/XinFinOrg/XDPoSChain/crypto/bn256/cloudflare"
2628 google "github.com/XinFinOrg/XDPoSChain/crypto/bn256/google"
2729)
2830
29- // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
30- func FuzzAdd ( data [] byte ) int {
31- // Ensure we have enough data in the first place
32- if len ( data ) != 128 {
33- return 0
31+ func getG1Points ( input io. Reader ) ( * cloudflare. G1 , * google. G1 ) {
32+ _ , xc , err := cloudflare . RandomG1 ( input )
33+ if err != nil {
34+ // insufficient input
35+ return nil , nil
3436 }
35- // Ensure both libs can parse the first curve point
36- xc := new (cloudflare.G1 )
37- _ , errc := xc .Unmarshal (data [:64 ])
38-
3937 xg := new (google.G1 )
40- _ , errg := xg .Unmarshal (data [:64 ])
41-
42- if (errc == nil ) != (errg == nil ) {
43- panic ("parse mismatch" )
44- } else if errc != nil {
45- return 0
38+ if _ , err := xg .Unmarshal (xc .Marshal ()); err != nil {
39+ panic (fmt .Sprintf ("Could not marshal cloudflare -> google:" , err ))
4640 }
47- // Ensure both libs can parse the second curve point
48- yc := new (cloudflare.G1 )
49- _ , errc = yc .Unmarshal (data [64 :])
41+ return xc , xg
42+ }
5043
51- yg := new (google.G1 )
52- _ , errg = yg .Unmarshal (data [64 :])
44+ func getG2Points (input io.Reader ) (* cloudflare.G2 , * google.G2 ) {
45+ _ , xc , err := cloudflare .RandomG2 (input )
46+ if err != nil {
47+ // insufficient input
48+ return nil , nil
49+ }
50+ xg := new (google.G2 )
51+ if _ , err := xg .Unmarshal (xc .Marshal ()); err != nil {
52+ panic (fmt .Sprintf ("Could not marshal cloudflare -> google:" , err ))
53+ }
54+ return xc , xg
55+ }
5356
54- if (errc == nil ) != (errg == nil ) {
55- panic ("parse mismatch" )
56- } else if errc != nil {
57+ // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
58+ func FuzzAdd (data []byte ) int {
59+ input := bytes .NewReader (data )
60+ xc , xg := getG1Points (input )
61+ if xc == nil {
5762 return 0
5863 }
64+ yc , yg := getG1Points (input )
65+ if yc == nil {
66+ return 0
67+ }
68+ // Ensure both libs can parse the second curve point
5969 // Add the two points and ensure they result in the same output
6070 rc := new (cloudflare.G1 )
6171 rc .Add (xc , yc )
@@ -66,73 +76,50 @@ func FuzzAdd(data []byte) int {
6676 if ! bytes .Equal (rc .Marshal (), rg .Marshal ()) {
6777 panic ("add mismatch" )
6878 }
69- return 0
79+ return 1
7080}
7181
7282// FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare
7383// libraries.
7484func FuzzMul (data []byte ) int {
75- // Ensure we have enough data in the first place
76- if len (data ) != 96 {
85+ input := bytes .NewReader (data )
86+ pc , pg := getG1Points (input )
87+ if pc == nil {
7788 return 0
7889 }
79- // Ensure both libs can parse the curve point
80- pc := new (cloudflare.G1 )
81- _ , errc := pc .Unmarshal (data [:64 ])
82-
83- pg := new (google.G1 )
84- _ , errg := pg .Unmarshal (data [:64 ])
85-
86- if (errc == nil ) != (errg == nil ) {
87- panic ("parse mismatch" )
88- } else if errc != nil {
90+ // Add the two points and ensure they result in the same output
91+ remaining := input .Len ()
92+ if remaining == 0 {
8993 return 0
9094 }
91- // Add the two points and ensure they result in the same output
95+ buf := make ([]byte , remaining )
96+ input .Read (buf )
97+
9298 rc := new (cloudflare.G1 )
93- rc .ScalarMult (pc , new (big.Int ).SetBytes (data [ 64 :] ))
99+ rc .ScalarMult (pc , new (big.Int ).SetBytes (buf ))
94100
95101 rg := new (google.G1 )
96- rg .ScalarMult (pg , new (big.Int ).SetBytes (data [ 64 :] ))
102+ rg .ScalarMult (pg , new (big.Int ).SetBytes (buf ))
97103
98104 if ! bytes .Equal (rc .Marshal (), rg .Marshal ()) {
99105 panic ("scalar mul mismatch" )
100106 }
101- return 0
107+ return 1
102108}
103109
104110func FuzzPair (data []byte ) int {
105- // Ensure we have enough data in the first place
106- if len (data ) != 192 {
111+ input := bytes .NewReader (data )
112+ pc , pg := getG1Points (input )
113+ if pc == nil {
107114 return 0
108115 }
109- // Ensure both libs can parse the curve point
110- pc := new (cloudflare.G1 )
111- _ , errc := pc .Unmarshal (data [:64 ])
112-
113- pg := new (google.G1 )
114- _ , errg := pg .Unmarshal (data [:64 ])
115-
116- if (errc == nil ) != (errg == nil ) {
117- panic ("parse mismatch" )
118- } else if errc != nil {
119- return 0
120- }
121- // Ensure both libs can parse the twist point
122- tc := new (cloudflare.G2 )
123- _ , errc = tc .Unmarshal (data [64 :])
124-
125- tg := new (google.G2 )
126- _ , errg = tg .Unmarshal (data [64 :])
127-
128- if (errc == nil ) != (errg == nil ) {
129- panic ("parse mismatch" )
130- } else if errc != nil {
116+ tc , tg := getG2Points (input )
117+ if tc == nil {
131118 return 0
132119 }
133120 // Pair the two points and ensure thet result in the same output
134121 if cloudflare .PairingCheck ([]* cloudflare.G1 {pc }, []* cloudflare.G2 {tc }) != google .PairingCheck ([]* google.G1 {pg }, []* google.G2 {tg }) {
135122 panic ("pair mismatch" )
136123 }
137- return 0
124+ return 1
138125}
0 commit comments