github.com/consensys/gnark-crypto@v0.14.0/ecc/bls24-315/internal/fptower/e12_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 package fptower 16 17 import ( 18 "math/big" 19 "testing" 20 21 "github.com/leanovate/gopter" 22 "github.com/leanovate/gopter/prop" 23 ) 24 25 // ------------------------------------------------------------ 26 // tests 27 28 func TestE12ReceiverIsOperand(t *testing.T) { 29 30 parameters := gopter.DefaultTestParameters() 31 parameters.MinSuccessfulTests = 100 32 33 properties := gopter.NewProperties(parameters) 34 35 genA := GenE12() 36 genB := GenE12() 37 38 properties.Property("[BLS24-315] Having the receiver as operand (addition) should output the same result", prop.ForAll( 39 func(a, b *E12) bool { 40 var c, d E12 41 d.Set(a) 42 c.Add(a, b) 43 a.Add(a, b) 44 b.Add(&d, b) 45 return a.Equal(b) && a.Equal(&c) && b.Equal(&c) 46 }, 47 genA, 48 genB, 49 )) 50 51 properties.Property("[BLS24-315] Having the receiver as operand (sub) should output the same result", prop.ForAll( 52 func(a, b *E12) bool { 53 var c, d E12 54 d.Set(a) 55 c.Sub(a, b) 56 a.Sub(a, b) 57 b.Sub(&d, b) 58 return a.Equal(b) && a.Equal(&c) && b.Equal(&c) 59 }, 60 genA, 61 genB, 62 )) 63 64 properties.Property("[BLS24-315] Having the receiver as operand (mul) should output the same result", prop.ForAll( 65 func(a, b *E12) bool { 66 var c, d E12 67 d.Set(a) 68 c.Mul(a, b) 69 a.Mul(a, b) 70 b.Mul(&d, b) 71 return a.Equal(b) && a.Equal(&c) && b.Equal(&c) 72 }, 73 genA, 74 genB, 75 )) 76 77 properties.Property("[BLS24-315] Having the receiver as operand (square) should output the same result", prop.ForAll( 78 func(a *E12) bool { 79 var b E12 80 b.Square(a) 81 a.Square(a) 82 return a.Equal(&b) 83 }, 84 genA, 85 )) 86 87 properties.Property("[BLS24-315] Having the receiver as operand (neg) should output the same result", prop.ForAll( 88 func(a *E12) bool { 89 var b E12 90 b.Neg(a) 91 a.Neg(a) 92 return a.Equal(&b) 93 }, 94 genA, 95 )) 96 97 properties.Property("[BLS24-315] Having the receiver as operand (double) should output the same result", prop.ForAll( 98 func(a *E12) bool { 99 var b E12 100 b.Double(a) 101 a.Double(a) 102 return a.Equal(&b) 103 }, 104 genA, 105 )) 106 107 properties.Property("[BLS24-315] Having the receiver as operand (Inverse) should output the same result", prop.ForAll( 108 func(a *E12) bool { 109 var b E12 110 b.Inverse(a) 111 a.Inverse(a) 112 return a.Equal(&b) 113 }, 114 genA, 115 )) 116 117 properties.TestingRun(t, gopter.ConsoleReporter(false)) 118 } 119 120 func TestE12Ops(t *testing.T) { 121 t.Parallel() 122 123 parameters := gopter.DefaultTestParameters() 124 parameters.MinSuccessfulTests = 100 125 126 properties := gopter.NewProperties(parameters) 127 128 genA := GenE12() 129 genB := GenE12() 130 131 properties.Property("[BLS24-315] sub & add should leave an element invariant", prop.ForAll( 132 func(a, b *E12) bool { 133 var c E12 134 c.Set(a) 135 c.Add(&c, b).Sub(&c, b) 136 return c.Equal(a) 137 }, 138 genA, 139 genB, 140 )) 141 142 properties.Property("[BLS24-315] mul & inverse should leave an element invariant", prop.ForAll( 143 func(a, b *E12) bool { 144 var c, d E12 145 d.Inverse(b) 146 c.Set(a) 147 c.Mul(&c, b).Mul(&c, &d) 148 return c.Equal(a) 149 }, 150 genA, 151 genB, 152 )) 153 154 properties.Property("[BLS24-315] inverse twice should leave an element invariant", prop.ForAll( 155 func(a *E12) bool { 156 var b E12 157 b.Inverse(a).Inverse(&b) 158 return a.Equal(&b) 159 }, 160 genA, 161 )) 162 163 properties.Property("[BLS24-315] neg twice should leave an element invariant", prop.ForAll( 164 func(a *E12) bool { 165 var b E12 166 b.Neg(a).Neg(&b) 167 return a.Equal(&b) 168 }, 169 genA, 170 )) 171 172 properties.Property("[BLS24-315] square and mul should output the same result", prop.ForAll( 173 func(a *E12) bool { 174 var b, c E12 175 b.Mul(a, a) 176 c.Square(a) 177 return b.Equal(&c) 178 }, 179 genA, 180 )) 181 182 properties.Property("[BLS24-315] Double and add twice should output the same result", prop.ForAll( 183 func(a *E12) bool { 184 var b E12 185 b.Add(a, a) 186 a.Double(a) 187 return a.Equal(&b) 188 }, 189 genA, 190 )) 191 properties.TestingRun(t, gopter.ConsoleReporter(false)) 192 } 193 194 // ------------------------------------------------------------ 195 // benches 196 197 func BenchmarkE12Add(b *testing.B) { 198 var a, c E12 199 _, _ = a.SetRandom() 200 _, _ = c.SetRandom() 201 b.ResetTimer() 202 for i := 0; i < b.N; i++ { 203 a.Add(&a, &c) 204 } 205 } 206 207 func BenchmarkE12Sub(b *testing.B) { 208 var a, c E12 209 _, _ = a.SetRandom() 210 _, _ = c.SetRandom() 211 b.ResetTimer() 212 for i := 0; i < b.N; i++ { 213 a.Sub(&a, &c) 214 } 215 } 216 217 func BenchmarkE12Mul(b *testing.B) { 218 var a, c E12 219 _, _ = a.SetRandom() 220 _, _ = c.SetRandom() 221 b.ResetTimer() 222 for i := 0; i < b.N; i++ { 223 a.Mul(&a, &c) 224 } 225 } 226 227 func BenchmarkE12Square(b *testing.B) { 228 var a E12 229 _, _ = a.SetRandom() 230 b.ResetTimer() 231 for i := 0; i < b.N; i++ { 232 a.Square(&a) 233 } 234 } 235 236 func BenchmarkE12Inverse(b *testing.B) { 237 var a E12 238 _, _ = a.SetRandom() 239 b.ResetTimer() 240 for i := 0; i < b.N; i++ { 241 a.Inverse(&a) 242 } 243 } 244 245 func BenchmarkE12ExpBySeed(b *testing.B) { 246 var a E12 247 var seed big.Int 248 seed.SetString("3218079743", 10) 249 _, _ = a.SetRandom() 250 b.ResetTimer() 251 for i := 0; i < b.N; i++ { 252 a.Exp(a, &seed).Conjugate(&a) 253 } 254 }