1// Copyright 2020 ConsenSys Software Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Code generated by consensys/gnark-crypto DO NOT EDIT 16 17package bls12381 18 19import ( 20 "bytes" 21 "io" 22 "math/big" 23 "math/rand" 24 "testing" 25 26 "github.com/leanovate/gopter" 27 "github.com/leanovate/gopter/prop" 28 29 "github.com/consensys/gnark-crypto/ecc/bls12-381/fp" 30 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" 31) 32 33func TestEncoder(t *testing.T) { 34 35 // TODO need proper fuzz testing here 36 37 var inA uint64 38 var inB fr.Element 39 var inC fp.Element 40 var inD G1Affine 41 var inE G1Affine 42 var inF G2Affine 43 var inG []G1Affine 44 var inH []G2Affine 45 46 // set values of inputs 47 inA = rand.Uint64() 48 inB.SetRandom() 49 inC.SetRandom() 50 inD.ScalarMultiplication(&g1GenAff, new(big.Int).SetUint64(rand.Uint64())) 51 // inE --> infinity 52 inF.ScalarMultiplication(&g2GenAff, new(big.Int).SetUint64(rand.Uint64())) 53 inG = make([]G1Affine, 2) 54 inH = make([]G2Affine, 0) 55 inG[1] = inD 56 57 // encode them, compressed and raw 58 var buf, bufRaw bytes.Buffer 59 enc := NewEncoder(&buf) 60 encRaw := NewEncoder(&bufRaw, RawEncoding()) 61 toEncode := []interface{}{inA, &inB, &inC, &inD, &inE, &inF, inG, inH} 62 for _, v := range toEncode { 63 if err := enc.Encode(v); err != nil { 64 t.Fatal(err) 65 } 66 if err := encRaw.Encode(v); err != nil { 67 t.Fatal(err) 68 } 69 } 70 71 testDecode := func(t *testing.T, r io.Reader, n int64) { 72 dec := NewDecoder(r) 73 var outA uint64 74 var outB fr.Element 75 var outC fp.Element 76 var outD G1Affine 77 var outE G1Affine 78 outE.X.SetOne() 79 outE.Y.SetUint64(42) 80 var outF G2Affine 81 var outG []G1Affine 82 var outH []G2Affine 83 84 toDecode := []interface{}{&outA, &outB, &outC, &outD, &outE, &outF, &outG, &outH} 85 for _, v := range toDecode { 86 if err := dec.Decode(v); err != nil { 87 t.Fatal(err) 88 } 89 } 90 91 // compare values 92 if inA != outA { 93 t.Fatal("didn't encode/decode uint64 value properly") 94 } 95 96 if !inB.Equal(&outB) || !inC.Equal(&outC) { 97 t.Fatal("decode(encode(Element) failed") 98 } 99 if !inD.Equal(&outD) || !inE.Equal(&outE) { 100 t.Fatal("decode(encode(G1Affine) failed") 101 } 102 if !inF.Equal(&outF) { 103 t.Fatal("decode(encode(G2Affine) failed") 104 } 105 if (len(inG) != len(outG)) || (len(inH) != len(outH)) { 106 t.Fatal("decode(encode(slice(points))) failed") 107 } 108 for i := 0; i < len(inG); i++ { 109 if !inG[i].Equal(&outG[i]) { 110 t.Fatal("decode(encode(slice(points))) failed") 111 } 112 } 113 if n != dec.BytesRead() { 114 t.Fatal("bytes read don't match bytes written") 115 } 116 } 117 118 // decode them 119 testDecode(t, &buf, enc.BytesWritten()) 120 testDecode(t, &bufRaw, encRaw.BytesWritten()) 121 122} 123 124func TestIsCompressed(t *testing.T) { 125 var g1Inf, g1 G1Affine 126 var g2Inf, g2 G2Affine 127 128 g1 = g1GenAff 129 g2 = g2GenAff 130 131 { 132 b := g1Inf.Bytes() 133 if !isCompressed(b[0]) { 134 t.Fatal("g1Inf.Bytes() should be compressed") 135 } 136 } 137 138 { 139 b := g1Inf.RawBytes() 140 if isCompressed(b[0]) { 141 t.Fatal("g1Inf.RawBytes() should be uncompressed") 142 } 143 } 144 145 { 146 b := g1.Bytes() 147 if !isCompressed(b[0]) { 148 t.Fatal("g1.Bytes() should be compressed") 149 } 150 } 151 152 { 153 b := g1.RawBytes() 154 if isCompressed(b[0]) { 155 t.Fatal("g1.RawBytes() should be uncompressed") 156 } 157 } 158 159 { 160 b := g2Inf.Bytes() 161 if !isCompressed(b[0]) { 162 t.Fatal("g2Inf.Bytes() should be compressed") 163 } 164 } 165 166 { 167 b := g2Inf.RawBytes() 168 if isCompressed(b[0]) { 169 t.Fatal("g2Inf.RawBytes() should be uncompressed") 170 } 171 } 172 173 { 174 b := g2.Bytes() 175 if !isCompressed(b[0]) { 176 t.Fatal("g2.Bytes() should be compressed") 177 } 178 } 179 180 { 181 b := g2.RawBytes() 182 if isCompressed(b[0]) { 183 t.Fatal("g2.RawBytes() should be uncompressed") 184 } 185 } 186 187} 188 189func TestG1AffineSerialization(t *testing.T) { 190 191 // test round trip serialization of infinity 192 { 193 // compressed 194 { 195 var p1, p2 G1Affine 196 p2.X.SetRandom() 197 p2.Y.SetRandom() 198 buf := p1.Bytes() 199 n, err := p2.SetBytes(buf[:]) 200 if err != nil { 201 t.Fatal(err) 202 } 203 if n != SizeOfG1AffineCompressed { 204 t.Fatal("invalid number of bytes consumed in buffer") 205 } 206 if !(p2.X.IsZero() && p2.Y.IsZero()) { 207 t.Fatal("deserialization of uncompressed infinity point is not infinity") 208 } 209 } 210 211 // uncompressed 212 { 213 var p1, p2 G1Affine 214 p2.X.SetRandom() 215 p2.Y.SetRandom() 216 buf := p1.RawBytes() 217 n, err := p2.SetBytes(buf[:]) 218 if err != nil { 219 t.Fatal(err) 220 } 221 if n != SizeOfG1AffineUncompressed { 222 t.Fatal("invalid number of bytes consumed in buffer") 223 } 224 if !(p2.X.IsZero() && p2.Y.IsZero()) { 225 t.Fatal("deserialization of uncompressed infinity point is not infinity") 226 } 227 } 228 } 229 230 parameters := gopter.DefaultTestParameters() 231 if testing.Short() { 232 parameters.MinSuccessfulTests = 100 233 } else { 234 parameters.MinSuccessfulTests = 1000 235 } 236 237 properties := gopter.NewProperties(parameters) 238 239 properties.Property("[G1] Affine SetBytes(RawBytes) should stay the same", prop.ForAll( 240 func(a fp.Element) bool { 241 var start, end G1Affine 242 var ab big.Int 243 a.ToBigIntRegular(&ab) 244 start.ScalarMultiplication(&g1GenAff, &ab) 245 246 buf := start.RawBytes() 247 n, err := end.SetBytes(buf[:]) 248 if err != nil { 249 return false 250 } 251 if n != SizeOfG1AffineUncompressed { 252 return false 253 } 254 return start.X.Equal(&end.X) && start.Y.Equal(&end.Y) 255 }, 256 GenFp(), 257 )) 258 259 properties.Property("[G1] Affine SetBytes(Bytes()) should stay the same", prop.ForAll( 260 func(a fp.Element) bool { 261 var start, end G1Affine 262 var ab big.Int 263 a.ToBigIntRegular(&ab) 264 start.ScalarMultiplication(&g1GenAff, &ab) 265 266 buf := start.Bytes() 267 n, err := end.SetBytes(buf[:]) 268 if err != nil { 269 return false 270 } 271 if n != SizeOfG1AffineCompressed { 272 return false 273 } 274 return start.X.Equal(&end.X) && start.Y.Equal(&end.Y) 275 }, 276 GenFp(), 277 )) 278 279 properties.TestingRun(t, gopter.ConsoleReporter(false)) 280} 281 282func TestG2AffineSerialization(t *testing.T) { 283 284 // test round trip serialization of infinity 285 { 286 // compressed 287 { 288 var p1, p2 G2Affine 289 p2.X.SetRandom() 290 p2.Y.SetRandom() 291 buf := p1.Bytes() 292 n, err := p2.SetBytes(buf[:]) 293 if err != nil { 294 t.Fatal(err) 295 } 296 if n != SizeOfG2AffineCompressed { 297 t.Fatal("invalid number of bytes consumed in buffer") 298 } 299 if !(p2.X.IsZero() && p2.Y.IsZero()) { 300 t.Fatal("deserialization of uncompressed infinity point is not infinity") 301 } 302 } 303 304 // uncompressed 305 { 306 var p1, p2 G2Affine 307 p2.X.SetRandom() 308 p2.Y.SetRandom() 309 buf := p1.RawBytes() 310 n, err := p2.SetBytes(buf[:]) 311 if err != nil { 312 t.Fatal(err) 313 } 314 if n != SizeOfG2AffineUncompressed { 315 t.Fatal("invalid number of bytes consumed in buffer") 316 } 317 if !(p2.X.IsZero() && p2.Y.IsZero()) { 318 t.Fatal("deserialization of uncompressed infinity point is not infinity") 319 } 320 } 321 } 322 323 parameters := gopter.DefaultTestParameters() 324 if testing.Short() { 325 parameters.MinSuccessfulTests = 100 326 } else { 327 parameters.MinSuccessfulTests = 1000 328 } 329 330 properties := gopter.NewProperties(parameters) 331 332 properties.Property("[G2] Affine SetBytes(RawBytes) should stay the same", prop.ForAll( 333 func(a fp.Element) bool { 334 var start, end G2Affine 335 var ab big.Int 336 a.ToBigIntRegular(&ab) 337 start.ScalarMultiplication(&g2GenAff, &ab) 338 339 buf := start.RawBytes() 340 n, err := end.SetBytes(buf[:]) 341 if err != nil { 342 return false 343 } 344 if n != SizeOfG2AffineUncompressed { 345 return false 346 } 347 return start.X.Equal(&end.X) && start.Y.Equal(&end.Y) 348 }, 349 GenFp(), 350 )) 351 352 properties.Property("[G2] Affine SetBytes(Bytes()) should stay the same", prop.ForAll( 353 func(a fp.Element) bool { 354 var start, end G2Affine 355 var ab big.Int 356 a.ToBigIntRegular(&ab) 357 start.ScalarMultiplication(&g2GenAff, &ab) 358 359 buf := start.Bytes() 360 n, err := end.SetBytes(buf[:]) 361 if err != nil { 362 return false 363 } 364 if n != SizeOfG2AffineCompressed { 365 return false 366 } 367 return start.X.Equal(&end.X) && start.Y.Equal(&end.Y) 368 }, 369 GenFp(), 370 )) 371 372 properties.TestingRun(t, gopter.ConsoleReporter(false)) 373} 374