github.com/consensys/gnark-crypto@v0.14.0/ecc/bls24-317/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-317] 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-317] 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-317] 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-317] 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-317] 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-317] 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-317] 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 122 parameters := gopter.DefaultTestParameters() 123 parameters.MinSuccessfulTests = 100 124 125 properties := gopter.NewProperties(parameters) 126 127 genA := GenE12() 128 genB := GenE12() 129 130 properties.Property("[BLS24-317] sub & add should leave an element invariant", prop.ForAll( 131 func(a, b *E12) bool { 132 var c E12 133 c.Set(a) 134 c.Add(&c, b).Sub(&c, b) 135 return c.Equal(a) 136 }, 137 genA, 138 genB, 139 )) 140 141 properties.Property("[BLS24-317] mul & inverse should leave an element invariant", prop.ForAll( 142 func(a, b *E12) bool { 143 var c, d E12 144 d.Inverse(b) 145 c.Set(a) 146 c.Mul(&c, b).Mul(&c, &d) 147 return c.Equal(a) 148 }, 149 genA, 150 genB, 151 )) 152 153 properties.Property("[BLS24-317] inverse twice should leave an element invariant", prop.ForAll( 154 func(a *E12) bool { 155 var b E12 156 b.Inverse(a).Inverse(&b) 157 return a.Equal(&b) 158 }, 159 genA, 160 )) 161 162 properties.Property("[BLS24-317] neg twice should leave an element invariant", prop.ForAll( 163 func(a *E12) bool { 164 var b E12 165 b.Neg(a).Neg(&b) 166 return a.Equal(&b) 167 }, 168 genA, 169 )) 170 171 properties.Property("[BLS24-317] square and mul should output the same result", prop.ForAll( 172 func(a *E12) bool { 173 var b, c E12 174 b.Mul(a, a) 175 c.Square(a) 176 return b.Equal(&c) 177 }, 178 genA, 179 )) 180 181 properties.Property("[BLS24-317] Double and add twice should output the same result", prop.ForAll( 182 func(a *E12) bool { 183 var b E12 184 b.Add(a, a) 185 a.Double(a) 186 return a.Equal(&b) 187 }, 188 genA, 189 )) 190 properties.TestingRun(t, gopter.ConsoleReporter(false)) 191 } 192 193 // ------------------------------------------------------------ 194 // benches 195 196 func BenchmarkE12Add(b *testing.B) { 197 var a, c E12 198 _, _ = a.SetRandom() 199 _, _ = c.SetRandom() 200 b.ResetTimer() 201 for i := 0; i < b.N; i++ { 202 a.Add(&a, &c) 203 } 204 } 205 206 func BenchmarkE12Sub(b *testing.B) { 207 var a, c E12 208 _, _ = a.SetRandom() 209 _, _ = c.SetRandom() 210 b.ResetTimer() 211 for i := 0; i < b.N; i++ { 212 a.Sub(&a, &c) 213 } 214 } 215 216 func BenchmarkE12Mul(b *testing.B) { 217 var a, c E12 218 _, _ = a.SetRandom() 219 _, _ = c.SetRandom() 220 b.ResetTimer() 221 for i := 0; i < b.N; i++ { 222 a.Mul(&a, &c) 223 } 224 } 225 226 func BenchmarkE12Square(b *testing.B) { 227 var a E12 228 _, _ = a.SetRandom() 229 b.ResetTimer() 230 for i := 0; i < b.N; i++ { 231 a.Square(&a) 232 } 233 } 234 235 func BenchmarkE12Inverse(b *testing.B) { 236 var a E12 237 _, _ = a.SetRandom() 238 b.ResetTimer() 239 for i := 0; i < b.N; i++ { 240 a.Inverse(&a) 241 } 242 } 243 244 func BenchmarkE12ExpBySeed(b *testing.B) { 245 var a E12 246 var seed big.Int 247 seed.SetString("3218079743", 10) 248 _, _ = a.SetRandom() 249 b.ResetTimer() 250 for i := 0; i < b.N; i++ { 251 a.Exp(a, &seed).Conjugate(&a) 252 } 253 }