github.com/emmansun/gmsm@v0.29.1/sm9/bn256/gfp2_test.go (about) 1 package bn256 2 3 import ( 4 "math/big" 5 "testing" 6 ) 7 8 func TestGfp2BasicOperations(t *testing.T) { 9 x := &gfP2{ 10 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 11 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 12 } 13 y := &gfP2{ 14 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 15 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 16 } 17 expectedAdd := "(5bf55bb67d25f098609a367366d94d6599da7405db44c388edb64706908922e4, 728185f97d3df3a01d3ad2a0e140d12011e10fa47d50fd12e6413a361e549cd9)" 18 expectedSub := "(0e6cca2ef0f4dce3fa4a249bb48a25d84dbf1f63ac843004e3b586d5dac6e8eb, 51785a37fb519603d4b026648151d768ebe9b9193a9c83c365c31316fb711845)" 19 expectedMul := "(7f98a04cf83164be0fdc4763a7c6f24c2901191f2917eb71037cd5221cf002bb, 75a09ee1aa1b04ccdb24e629529a18492f378aa3034f63d3cd1b8b9f0d338b3a)" 20 expectedMulU := "(75a09ee1aa1b04ccdb24e629529a18492f378aa3034f63d3cd1b8b9f0d338b3a, 6d4ebf6614e484678c4ec7d89b8fa9f1f1e2f457e2c606d5c3e58c0b8cc28584)" 21 22 t.Parallel() 23 t.Run("Add", func(t *testing.T) { 24 ret := &gfP2{} 25 ret.Add(x, y) 26 if ret.String() != expectedAdd { 27 t.Errorf("expected %v, got %v", expectedAdd, ret) 28 } 29 ret.Set(x) 30 ret.Add(ret, y) 31 if ret.String() != expectedAdd { 32 t.Errorf("add self fail, expected %v, got %v", expectedAdd, ret) 33 } 34 ret.Set(y) 35 ret.Add(x, ret) 36 if ret.String() != expectedAdd { 37 t.Errorf("add self fail, expected %v, got %v", expectedAdd, ret) 38 } 39 }) 40 41 t.Run("Sub", func(t *testing.T) { 42 ret := &gfP2{} 43 ret.Sub(x, y) 44 if ret.String() != expectedSub { 45 t.Errorf("expected %v, got %v", expectedSub, ret) 46 } 47 ret.Set(x) 48 ret.Sub(ret, y) 49 if ret.String() != expectedSub { 50 t.Errorf("sub self fail, expected %v, got %v", expectedSub, ret) 51 } 52 ret.Set(y) 53 ret.Sub(x, ret) 54 if ret.String() != expectedSub { 55 t.Errorf("sub self fail, expected %v, got %v", expectedSub, ret) 56 } 57 }) 58 59 t.Run("Double", func(t *testing.T) { 60 expected := &gfP2{} 61 expected.Set(x) 62 expected.Add(expected, expected) 63 64 got := &gfP2{} 65 got.Set(x) 66 got.Double(got) 67 if got.Equal(expected) != 1 { 68 t.Errorf("got %v, expected %v", got, expected) 69 } 70 }) 71 72 t.Run("Triple", func(t *testing.T) { 73 expected := &gfP2{} 74 expected.Set(x) 75 expected.Add(expected, expected) 76 expected.Add(expected, x) 77 got := &gfP2{} 78 got.Set(x) 79 got.Triple(got) 80 if got.Equal(expected) != 1 { 81 t.Errorf("got %v, expected %v", got, expected) 82 } 83 }) 84 85 t.Run("Mul", func(t *testing.T) { 86 got := &gfP2{} 87 got.Set(x) 88 got.Mul(got, y) 89 if got.String() != expectedMul { 90 t.Errorf("got %v, expected %v", got, expectedMul) 91 } 92 }) 93 94 t.Run("MulU", func(t *testing.T) { 95 got := &gfP2{} 96 got.Set(x) 97 got.MulU(got, y) 98 if got.String() != expectedMulU { 99 t.Errorf("got %v, expected %v", got, expectedMulU) 100 } 101 }) 102 103 t.Run("Square", func(t *testing.T) { 104 expected := &gfP2{} 105 expected.Mul(x, x) 106 got := &gfP2{} 107 got.Set(x) 108 got.Square(got) 109 if got.Equal(expected) != 1 { 110 t.Errorf("got %v, expected %v", got, expected) 111 } 112 }) 113 114 t.Run("SquareU", func(t *testing.T) { 115 expected := &gfP2{} 116 expected.MulU(x, x) 117 got := &gfP2{} 118 got.Set(x) 119 got.SquareU(got) 120 if got.Equal(expected) != 1 { 121 t.Errorf("got %v, expected %v", got, expected) 122 } 123 }) 124 } 125 126 func Test_gfP2Invert(t *testing.T) { 127 x := &gfP2{ 128 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 129 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 130 } 131 132 xInv := &gfP2{} 133 xInv.Invert(x) 134 135 y := &gfP2{} 136 y.Mul(x, xInv) 137 expected := (&gfP2{}).SetOne() 138 139 if y.x != expected.x || y.y != expected.y { 140 t.Errorf("got %v, expected %v", y, expected) 141 } 142 143 x = &gfP2{ 144 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 145 *zero, 146 } 147 148 xInv.Invert(x) 149 150 y.Mul(x, xInv) 151 152 if y.x != expected.x || y.y != expected.y { 153 t.Errorf("got %v, expected %v", y, expected) 154 } 155 156 x = &gfP2{ 157 *zero, 158 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 159 } 160 161 xInv.Invert(x) 162 163 y.Mul(x, xInv) 164 165 if y.x != expected.x || y.y != expected.y { 166 t.Errorf("got %v, expected %v", y, expected) 167 } 168 } 169 170 func Test_gfP2Exp(t *testing.T) { 171 x := &gfP2{ 172 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 173 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 174 } 175 got := &gfP2{} 176 got.Exp(x, big.NewInt(1)) 177 if x.x != got.x || x.y != got.y { 178 t.Errorf("got %v, expected %v", got, x) 179 } 180 } 181 182 func Test_gfP2Frobenius(t *testing.T) { 183 x := &gfP2{ 184 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 185 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 186 } 187 expected := &gfP2{} 188 expected.Exp(x, p) 189 got := &gfP2{} 190 got.Frobenius(x) 191 if expected.x != got.x || expected.y != got.y { 192 t.Errorf("got %v, expected %v", got, x) 193 } 194 195 // make sure i^(p-1) = -1 196 i := &gfP2{} 197 i.SetU() 198 i.Exp(i, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457c")) 199 i = gfP2Decode(i) 200 expected.y.Set(newGFp(-1)) 201 expected.x.Set(zero) 202 expected = gfP2Decode(expected) 203 if expected.x != i.x || expected.y != i.y { 204 t.Errorf("got %v, expected %v", i, expected) 205 } 206 } 207 208 func Test_gfP2Sqrt(t *testing.T) { 209 x := &gfP2{ 210 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 211 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 212 } 213 x2, x3, sqrt, sqrtNeg := &gfP2{}, &gfP2{}, &gfP2{}, &gfP2{} 214 x2.Mul(x, x) 215 sqrt.Sqrt(x2) 216 sqrtNeg.Neg(sqrt) 217 x3.Mul(sqrt, sqrt) 218 219 if *x3 != *x2 { 220 t.Errorf("not correct") 221 } 222 223 if *sqrt != *x && *sqrtNeg != *x { 224 t.Errorf("sqrt not expected") 225 } 226 } 227 228 func BenchmarkGfP2Mul(b *testing.B) { 229 x := &gfP2{ 230 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 231 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 232 } 233 y := &gfP2{ 234 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 235 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 236 } 237 t := &gfP2{} 238 b.ReportAllocs() 239 b.ResetTimer() 240 for i := 0; i < b.N; i++ { 241 t.Mul(x, y) 242 } 243 } 244 245 func BenchmarkGfP2MulScalar(b *testing.B) { 246 x := &gfP2{ 247 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 248 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 249 } 250 251 t := &gfP2{} 252 b.ReportAllocs() 253 b.ResetTimer() 254 for i := 0; i < b.N; i++ { 255 t.MulScalar(x, &x.x) 256 } 257 } 258 259 func BenchmarkGfP2MulU1(b *testing.B) { 260 x := &gfP2{ 261 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 262 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 263 } 264 265 t := &gfP2{} 266 b.ReportAllocs() 267 b.ResetTimer() 268 for i := 0; i < b.N; i++ { 269 t.MulU1(x) 270 } 271 } 272 273 func BenchmarkGfP2MulU(b *testing.B) { 274 x := &gfP2{ 275 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 276 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 277 } 278 y := &gfP2{ 279 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 280 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 281 } 282 283 t := &gfP2{} 284 b.ReportAllocs() 285 b.ResetTimer() 286 for i := 0; i < b.N; i++ { 287 t.MulU(x, y) 288 } 289 } 290 291 func BenchmarkGfP2Square(b *testing.B) { 292 x := &gfP2{ 293 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 294 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 295 } 296 b.ReportAllocs() 297 b.ResetTimer() 298 for i := 0; i < b.N; i++ { 299 x.Square(x) 300 } 301 } 302 303 func BenchmarkGfP2SquareU(b *testing.B) { 304 x := &gfP2{ 305 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 306 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 307 } 308 b.ReportAllocs() 309 b.ResetTimer() 310 for i := 0; i < b.N; i++ { 311 x.SquareU(x) 312 } 313 } 314 315 func BenchmarkGfP2Invert(b *testing.B) { 316 x := &gfP2{ 317 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 318 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 319 } 320 t := &gfP2{} 321 b.ReportAllocs() 322 b.ResetTimer() 323 for i := 0; i < b.N; i++ { 324 t.Invert(x) 325 } 326 } 327 328 func BenchmarkGfP2Neg(b *testing.B) { 329 x := &gfP2{ 330 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 331 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 332 } 333 b.ReportAllocs() 334 b.ResetTimer() 335 for i := 0; i < b.N; i++ { 336 gfpNeg(&x.x, &x.x) 337 gfpNeg(&x.y, &x.y) 338 } 339 } 340 341 func BenchmarkGfP2Neg2(b *testing.B) { 342 x := &gfP2{ 343 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 344 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 345 } 346 b.ReportAllocs() 347 b.ResetTimer() 348 for i := 0; i < b.N; i++ { 349 gfpSub(&x.x, zero, &x.x) 350 gfpSub(&x.y, zero, &x.y) 351 } 352 } 353 354 /* 355 func Test_gfP2QuadraticResidue(t *testing.T) { 356 x := &gfP2{ 357 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 358 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 359 } 360 n := bigFromHex("40df880001e10199aa9f985292a7740a5f3e998ff60a2401e81d08b99ba6f8ff691684e427df891a9250c20f55961961fe81f6fc785a9512ad93e28f5cfb4f84") 361 y := &gfP2{} 362 x2 := &gfP2{} 363 x2.Exp(x, n) 364 x2 = gfP2Decode(x2) 365 fmt.Printf("%v\n", x2) 366 for { 367 k, err := randomK(rand.Reader) 368 if err != nil { 369 t.Fatal(err) 370 } 371 372 x2.Exp(x, k) 373 y.Exp(x2, n) 374 if y.x == *zero && y.y == *one { 375 break 376 } 377 } 378 x2 = gfP2Decode(x2) 379 fmt.Printf("%v\n", x2) 380 } 381 */