github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-381/internal/fptower/e6_test.go (about) 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 17 package fptower 18 19 import ( 20 "testing" 21 22 "github.com/leanovate/gopter" 23 "github.com/leanovate/gopter/prop" 24 ) 25 26 // ------------------------------------------------------------ 27 // tests 28 29 func TestE6ReceiverIsOperand(t *testing.T) { 30 31 t.Parallel() 32 parameters := gopter.DefaultTestParameters() 33 if testing.Short() { 34 parameters.MinSuccessfulTests = nbFuzzShort 35 } else { 36 parameters.MinSuccessfulTests = nbFuzz 37 } 38 39 properties := gopter.NewProperties(parameters) 40 41 genA := GenE6() 42 genB := GenE6() 43 genE2 := GenE2() 44 45 properties.Property("[BLS12-381] Having the receiver as operand (addition) should output the same result", prop.ForAll( 46 func(a, b *E6) bool { 47 var c, d E6 48 d.Set(a) 49 c.Add(a, b) 50 a.Add(a, b) 51 b.Add(&d, b) 52 return a.Equal(b) && a.Equal(&c) && b.Equal(&c) 53 }, 54 genA, 55 genB, 56 )) 57 58 properties.Property("[BLS12-381] Having the receiver as operand (sub) should output the same result", prop.ForAll( 59 func(a, b *E6) bool { 60 var c, d E6 61 d.Set(a) 62 c.Sub(a, b) 63 a.Sub(a, b) 64 b.Sub(&d, b) 65 return a.Equal(b) && a.Equal(&c) && b.Equal(&c) 66 }, 67 genA, 68 genB, 69 )) 70 71 properties.Property("[BLS12-381] Having the receiver as operand (mul) should output the same result", prop.ForAll( 72 func(a, b *E6) bool { 73 var c, d E6 74 d.Set(a) 75 c.Mul(a, b) 76 a.Mul(a, b) 77 b.Mul(&d, b) 78 return a.Equal(b) && a.Equal(&c) && b.Equal(&c) 79 }, 80 genA, 81 genB, 82 )) 83 84 properties.Property("[BLS12-381] Having the receiver as operand (square) should output the same result", prop.ForAll( 85 func(a *E6) bool { 86 var b E6 87 b.Square(a) 88 a.Square(a) 89 return a.Equal(&b) 90 }, 91 genA, 92 )) 93 94 properties.Property("[BLS12-381] Having the receiver as operand (neg) should output the same result", prop.ForAll( 95 func(a *E6) bool { 96 var b E6 97 b.Neg(a) 98 a.Neg(a) 99 return a.Equal(&b) 100 }, 101 genA, 102 )) 103 104 properties.Property("[BLS12-381] Having the receiver as operand (double) should output the same result", prop.ForAll( 105 func(a *E6) bool { 106 var b E6 107 b.Double(a) 108 a.Double(a) 109 return a.Equal(&b) 110 }, 111 genA, 112 )) 113 114 properties.Property("[BLS12-381] Having the receiver as operand (mul by non residue) should output the same result", prop.ForAll( 115 func(a *E6) bool { 116 var b E6 117 b.MulByNonResidue(a) 118 a.MulByNonResidue(a) 119 return a.Equal(&b) 120 }, 121 genA, 122 )) 123 124 properties.Property("[BLS12-381] Having the receiver as operand (Inverse) should output the same result", prop.ForAll( 125 func(a *E6) bool { 126 var b E6 127 b.Inverse(a) 128 a.Inverse(a) 129 return a.Equal(&b) 130 }, 131 genA, 132 )) 133 134 properties.Property("[BLS12-381] Having the receiver as operand (mul by E2) should output the same result", prop.ForAll( 135 func(a *E6, b *E2) bool { 136 var c E6 137 c.MulByE2(a, b) 138 a.MulByE2(a, b) 139 return a.Equal(&c) 140 }, 141 genA, 142 genE2, 143 )) 144 145 properties.TestingRun(t, gopter.ConsoleReporter(false)) 146 } 147 148 func TestE6Ops(t *testing.T) { 149 150 t.Parallel() 151 parameters := gopter.DefaultTestParameters() 152 if testing.Short() { 153 parameters.MinSuccessfulTests = nbFuzzShort 154 } else { 155 parameters.MinSuccessfulTests = nbFuzz 156 } 157 158 properties := gopter.NewProperties(parameters) 159 160 genA := GenE6() 161 genB := GenE6() 162 genE2 := GenE2() 163 164 properties.Property("[BLS12-381] sub & add should leave an element invariant", prop.ForAll( 165 func(a, b *E6) bool { 166 var c E6 167 c.Set(a) 168 c.Add(&c, b).Sub(&c, b) 169 return c.Equal(a) 170 }, 171 genA, 172 genB, 173 )) 174 175 properties.Property("[BLS12-381] mul & inverse should leave an element invariant", prop.ForAll( 176 func(a, b *E6) bool { 177 var c, d E6 178 d.Inverse(b) 179 c.Set(a) 180 c.Mul(&c, b).Mul(&c, &d) 181 return c.Equal(a) 182 }, 183 genA, 184 genB, 185 )) 186 187 properties.Property("[BLS12-381] inverse twice should leave an element invariant", prop.ForAll( 188 func(a *E6) bool { 189 var b E6 190 b.Inverse(a).Inverse(&b) 191 return a.Equal(&b) 192 }, 193 genA, 194 )) 195 196 properties.Property("[BLS12-381] BatchInvertE6 should output the same result as Inverse", prop.ForAll( 197 func(a, b, c *E6) bool { 198 199 batch := BatchInvertE6([]E6{*a, *b, *c}) 200 a.Inverse(a) 201 b.Inverse(b) 202 c.Inverse(c) 203 return a.Equal(&batch[0]) && b.Equal(&batch[1]) && c.Equal(&batch[2]) 204 }, 205 genA, 206 genA, 207 genA, 208 )) 209 210 properties.Property("[BLS12-381] neg twice should leave an element invariant", prop.ForAll( 211 func(a *E6) bool { 212 var b E6 213 b.Neg(a).Neg(&b) 214 return a.Equal(&b) 215 }, 216 genA, 217 )) 218 219 properties.Property("[BLS12-381] square and mul should output the same result", prop.ForAll( 220 func(a *E6) bool { 221 var b, c E6 222 b.Mul(a, a) 223 c.Square(a) 224 return b.Equal(&c) 225 }, 226 genA, 227 )) 228 229 properties.Property("[BLS12-381] Double and add twice should output the same result", prop.ForAll( 230 func(a *E6) bool { 231 var b E6 232 b.Add(a, a) 233 a.Double(a) 234 return a.Equal(&b) 235 }, 236 genA, 237 )) 238 239 properties.Property("[BLS12-381] Mul by non residue should be the same as multiplying by (0,1,0)", prop.ForAll( 240 func(a *E6) bool { 241 var b, c E6 242 b.B1.A0.SetOne() 243 c.Mul(a, &b) 244 a.MulByNonResidue(a) 245 return a.Equal(&c) 246 }, 247 genA, 248 )) 249 250 properties.Property("[BLS12-381] MulByE2 MulByE2 inverse should leave an element invariant", prop.ForAll( 251 func(a *E6, b *E2) bool { 252 var c E6 253 var d E2 254 d.Inverse(b) 255 c.MulByE2(a, b).MulByE2(&c, &d) 256 return c.Equal(a) 257 }, 258 genA, 259 genE2, 260 )) 261 262 properties.Property("[BLS12-381] Mul and MulBy01 should output the same result", prop.ForAll( 263 func(a *E6, c0, c1 *E2) bool { 264 var b E6 265 b.B0.Set(c0) 266 b.B1.Set(c1) 267 b.Mul(&b, a) 268 a.MulBy01(c0, c1) 269 return b.Equal(a) 270 }, 271 genA, 272 genE2, 273 genE2, 274 )) 275 276 properties.Property("[BLS12-381] Mul and MulBy1 should output the same result", prop.ForAll( 277 func(a *E6, c1 *E2) bool { 278 var b E6 279 b.B1.Set(c1) 280 b.Mul(&b, a) 281 a.MulBy1(c1) 282 return b.Equal(a) 283 }, 284 genA, 285 genE2, 286 )) 287 288 properties.TestingRun(t, gopter.ConsoleReporter(false)) 289 290 } 291 292 // ------------------------------------------------------------ 293 // benches 294 295 func BenchmarkE6Add(b *testing.B) { 296 var a, c E6 297 _, _ = a.SetRandom() 298 _, _ = c.SetRandom() 299 b.ResetTimer() 300 for i := 0; i < b.N; i++ { 301 a.Add(&a, &c) 302 } 303 } 304 305 func BenchmarkE6Sub(b *testing.B) { 306 var a, c E6 307 _, _ = a.SetRandom() 308 _, _ = c.SetRandom() 309 b.ResetTimer() 310 for i := 0; i < b.N; i++ { 311 a.Sub(&a, &c) 312 } 313 } 314 315 func BenchmarkE6Mul(b *testing.B) { 316 var a, c E6 317 _, _ = a.SetRandom() 318 _, _ = c.SetRandom() 319 b.ResetTimer() 320 for i := 0; i < b.N; i++ { 321 a.Mul(&a, &c) 322 } 323 } 324 325 func BenchmarkE6Square(b *testing.B) { 326 var a E6 327 _, _ = a.SetRandom() 328 b.ResetTimer() 329 for i := 0; i < b.N; i++ { 330 a.Square(&a) 331 } 332 } 333 334 func BenchmarkE6Inverse(b *testing.B) { 335 var a E6 336 _, _ = a.SetRandom() 337 b.ResetTimer() 338 for i := 0; i < b.N; i++ { 339 a.Inverse(&a) 340 } 341 } 342 343 func TestE6Div(t *testing.T) { 344 345 parameters := gopter.DefaultTestParameters() 346 properties := gopter.NewProperties(parameters) 347 348 genA := GenE6() 349 genB := GenE6() 350 351 properties.Property("[BLS12-381] dividing then multiplying by the same element does nothing", prop.ForAll( 352 func(a, b *E6) bool { 353 var c E6 354 c.Div(a, b) 355 c.Mul(&c, b) 356 return c.Equal(a) 357 }, 358 genA, 359 genB, 360 )) 361 362 properties.TestingRun(t, gopter.ConsoleReporter(false)) 363 }