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