github.com/emmansun/gmsm@v0.29.1/sm9/bn256/gfp4_test.go (about) 1 package bn256 2 3 import ( 4 "math/big" 5 "testing" 6 ) 7 8 func TestGfp4BasicOperations(t *testing.T) { 9 x := &gfP4{ 10 gfP2{ 11 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 12 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 13 }, 14 gfP2{ 15 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 16 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 17 }, 18 } 19 y := &gfP4{} 20 y.x.Set(&x.y) 21 y.y.Set(&x.x) 22 23 expectedAdd := "((5bf55bb67d25f098609a367366d94d6599da7405db44c388edb64706908922e4, 728185f97d3df3a01d3ad2a0e140d12011e10fa47d50fd12e6413a361e549cd9), (5bf55bb67d25f098609a367366d94d6599da7405db44c388edb64706908922e4, 728185f97d3df3a01d3ad2a0e140d12011e10fa47d50fd12e6413a361e549cd9))" 24 expectedSub := "((0e6cca2ef0f4dce3fa4a249bb48a25d84dbf1f63ac843004e3b586d5dac6e8eb, 51785a37fb519603d4b026648151d768ebe9b9193a9c83c365c31316fb711845), (a7d335d111aeca0ddbb986b44104a16cd43373e76df6bed701ba1452088a5c92, 64c7a5c8075210ee015384eb743cefdc3608da31dfde6b187fac8810e7e02d38))" 25 expectedMul := "((5f318c234b817377df2179ff82a0759c6b926330853e5abd919e45a6a93e658e, 3c9db0f3bbdb89a9a407dfec4f8f4d6b8ef35b2a3f05e7bcc9bb6a956876faf7), (3ef93f2e9fa8c29914fd823d04d243503646107711ec6068eb28c59946d24878, 2caf5e47bc5be242917002b1f89afaf5ff27ebafcb9a7bcdab917c82b6a4cb41))" 26 expectedMulV := "((3ef93f2e9fa8c29914fd823d04d243503646107711ec6068eb28c59946d24878, 2caf5e47bc5be242917002b1f89afaf5ff27ebafcb9a7bcdab917c82b6a4cb41), (3c9db0f3bbdb89a9a407dfec4f8f4d6b8ef35b2a3f05e7bcc9bb6a956876faf7, ae1ce7b96e4466f3edc462a0e5dca3516cc060352a79283ca7a2ab027425bfde))" 27 28 t.Parallel() 29 t.Run("Add", func(t *testing.T) { 30 got := &gfP4{} 31 got.Set(x) 32 got.Add(got, y) 33 if got.String() != expectedAdd { 34 t.Errorf("got %v, expected %v", got, expectedAdd) 35 } 36 }) 37 38 t.Run("Sub", func(t *testing.T) { 39 got := &gfP4{} 40 got.Set(x) 41 got.Sub(got, y) 42 if got.String() != expectedSub { 43 t.Errorf("got %v, expected %v", got, expectedSub) 44 } 45 }) 46 47 t.Run("Mul", func(t *testing.T) { 48 got := &gfP4{} 49 got.Set(x) 50 got.Mul(got, y) 51 if got.String() != expectedMul { 52 t.Errorf("got %v, expected %v", got, expectedMul) 53 } 54 }) 55 56 t.Run("MulV", func(t *testing.T) { 57 got := &gfP4{} 58 got.Set(x) 59 got.MulV(got, y) 60 if got.String() != expectedMulV { 61 t.Errorf("got %v, expected %v", got, expectedMulV) 62 } 63 }) 64 65 t.Run("Double", func(t *testing.T) { 66 got := &gfP4{} 67 got.Set(x) 68 got.Double(got) 69 70 expected := &gfP4{} 71 expected.Add(x, x) 72 if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 { 73 t.Errorf("got %v, expected %v", got, expected) 74 } 75 }) 76 77 t.Run("Triple", func(t *testing.T) { 78 got := &gfP4{} 79 got.Set(x) 80 got.Triple(got) 81 82 expected := &gfP4{} 83 expected.Add(x, x) 84 expected.Add(expected, x) 85 if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 { 86 t.Errorf("got %v, expected %v", got, expected) 87 } 88 }) 89 90 t.Run("Square", func(t *testing.T) { 91 got := &gfP4{} 92 got.Set(x) 93 got.Square(got) 94 95 expected := &gfP4{} 96 expected.Mul(x, x) 97 if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 { 98 t.Errorf("got %v, expected %v", got, expected) 99 } 100 }) 101 102 t.Run("SquareV", func(t *testing.T) { 103 got := &gfP4{} 104 got.Set(x) 105 got.SquareV(got) 106 107 expected := &gfP4{} 108 expected.MulV(x, x) 109 if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 { 110 t.Errorf("got %v, expected %v", got, expected) 111 } 112 }) 113 } 114 115 func Test_gfP4Square(t *testing.T) { 116 x := &gfP4{ 117 gfP2{ 118 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 119 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 120 }, 121 gfP2{ 122 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 123 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 124 }, 125 } 126 xmulx := &gfP4{} 127 xmulx.Mul(x, x) 128 xmulx = gfP4Decode(xmulx) 129 130 x2 := &gfP4{} 131 x2.Square(x) 132 x2 = gfP4Decode(x2) 133 134 if xmulx.x != x2.x || xmulx.y != x2.y { 135 t.Errorf("xmulx=%v, x2=%v", xmulx, x2) 136 } 137 } 138 139 func Test_gfP4Invert(t *testing.T) { 140 gfp2Zero := (&gfP2{}).SetZero() 141 x := &gfP4{ 142 gfP2{ 143 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 144 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 145 }, 146 gfP2{ 147 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 148 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 149 }, 150 } 151 152 xInv := &gfP4{} 153 xInv.Invert(x) 154 155 y := &gfP4{} 156 y.Mul(x, xInv) 157 if !y.IsOne() { 158 t.Fail() 159 } 160 161 x = &gfP4{ 162 gfP2{ 163 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 164 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 165 }, 166 *gfp2Zero, 167 } 168 169 xInv.Invert(x) 170 171 y.Mul(x, xInv) 172 if !y.IsOne() { 173 t.Fail() 174 } 175 176 x = &gfP4{ 177 *gfp2Zero, 178 gfP2{ 179 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 180 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 181 }, 182 } 183 184 xInv.Invert(x) 185 186 y.Mul(x, xInv) 187 if !y.IsOne() { 188 t.Fail() 189 } 190 } 191 192 func Test_gfP4Frobenius(t *testing.T) { 193 x := &gfP4{ 194 gfP2{ 195 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 196 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 197 }, 198 gfP2{ 199 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 200 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 201 }, 202 } 203 expected := &gfP4{} 204 expected.Exp(x, p) 205 got := &gfP4{} 206 got.Frobenius(x) 207 if expected.x != got.x || expected.y != got.y { 208 t.Errorf("got %v, expected %v", got, expected) 209 } 210 } 211 212 // Generate vToPMinus1 213 func Test_gfP4Frobenius_Case1(t *testing.T) { 214 expected := &gfP4{} 215 i := &gfP4{} 216 i.SetV() 217 pMinus1 := new(big.Int).Sub(p, big.NewInt(1)) 218 i.Exp(i, pMinus1) 219 i = gfP4Decode(i) 220 expected.y.x.Set(zero) 221 expected.y.y.Set(fromBigInt(bigFromHex("6c648de5dc0a3f2cf55acc93ee0baf159f9d411806dc5177f5b21fd3da24d011"))) 222 expected.x.SetZero() 223 expected = gfP4Decode(expected) 224 if expected.x != i.x || expected.y != i.y { 225 t.Errorf("got %v, expected %v", i, expected) 226 } 227 } 228 229 func Test_gfP4FrobeniusP2(t *testing.T) { 230 x := &gfP4{ 231 gfP2{ 232 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 233 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 234 }, 235 gfP2{ 236 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 237 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 238 }, 239 } 240 expected := &gfP4{} 241 p2 := new(big.Int).Mul(p, p) 242 expected.Exp(x, p2) 243 got := &gfP4{} 244 got.FrobeniusP2(x) 245 if expected.x != got.x || expected.y != got.y { 246 t.Errorf("got %v, expected %v", got, expected) 247 } 248 } 249 250 func Test_gfP4FrobeniusP2_Case1(t *testing.T) { 251 expected := &gfP4{} 252 i := &gfP4{} 253 i.SetV() 254 p2 := new(big.Int).Mul(p, p) 255 p2 = new(big.Int).Sub(p2, big.NewInt(1)) 256 i.Exp(i, p2) 257 i = gfP4Decode(i) 258 expected.y.x.Set(zero) 259 expected.y.y.Set(newGFp(-1)) 260 expected.x.SetZero() 261 expected = gfP4Decode(expected) 262 if expected.x != i.x || expected.y != i.y { 263 t.Errorf("got %v, expected %v", i, expected) 264 } 265 } 266 267 func Test_gfP4FrobeniusP3(t *testing.T) { 268 x := &gfP4{ 269 gfP2{ 270 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 271 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 272 }, 273 gfP2{ 274 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 275 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 276 }, 277 } 278 expected := &gfP4{} 279 p3 := new(big.Int).Mul(p, p) 280 p3 = p3.Mul(p3, p) 281 expected.Exp(x, p3) 282 got := &gfP4{} 283 got.FrobeniusP3(x) 284 if expected.x != got.x || expected.y != got.y { 285 t.Errorf("got %v, expected %v", got, expected) 286 } 287 } 288 289 func BenchmarkGfP4Mul(b *testing.B) { 290 x := &gfP4{ 291 gfP2{ 292 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 293 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 294 }, 295 gfP2{ 296 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 297 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 298 }, 299 } 300 y := &gfP4{ 301 gfP2{ 302 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 303 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 304 }, 305 gfP2{ 306 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 307 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 308 }, 309 } 310 t := &gfP4{} 311 b.ReportAllocs() 312 b.ResetTimer() 313 for i := 0; i < b.N; i++ { 314 t.Mul(x, y) 315 } 316 } 317 318 func BenchmarkGfP4Square(b *testing.B) { 319 x := &gfP4{ 320 gfP2{ 321 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 322 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 323 }, 324 gfP2{ 325 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 326 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 327 }, 328 } 329 t := &gfP4{} 330 b.ReportAllocs() 331 b.ResetTimer() 332 for i := 0; i < b.N; i++ { 333 t.Square(x) 334 } 335 }