github.com/emmansun/gmsm@v0.29.1/sm9/bn256/gfp6_test.go (about) 1 package bn256 2 3 import ( 4 "math/big" 5 "testing" 6 ) 7 8 func TestMulS(t *testing.T) { 9 x := &gfP6{ 10 gfP2{ 11 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 12 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 13 }, 14 gfP2{ 15 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 16 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 17 }, 18 gfP2{ 19 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 20 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 21 }, 22 } 23 s := &gfP6{} 24 s.SetS() 25 26 xmuls1, xmuls2 := &gfP6{}, &gfP6{} 27 xmuls1.MulS(x) 28 xmuls1 = gfP6Decode(xmuls1) 29 xmuls2.Mul(x, s) 30 xmuls2 = gfP6Decode(xmuls2) 31 32 if *xmuls1 != *xmuls2 { 33 t.Errorf("xmulx=%v, x2=%v", xmuls1, xmuls2) 34 } 35 } 36 37 func testGfP6Square(t *testing.T, x *gfP6) { 38 xmulx := &gfP6{} 39 xmulx.Mul(x, x) 40 xmulx = gfP6Decode(xmulx) 41 42 x2 := &gfP6{} 43 x2.Square(x) 44 x2 = gfP6Decode(x2) 45 46 if xmulx.x != x2.x || xmulx.y != x2.y { 47 t.Errorf("xmulx=%v, x2=%v", xmulx, x2) 48 } 49 } 50 51 func Test_gfP6Square(t *testing.T) { 52 gfp2Zero := (&gfP2{}).SetZero() 53 x := &gfP6{ 54 gfP2{ 55 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 56 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 57 }, 58 gfP2{ 59 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 60 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 61 }, 62 gfP2{ 63 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 64 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 65 }, 66 } 67 testGfP6Square(t, x) 68 x = &gfP6{ 69 gfP2{ 70 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 71 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 72 }, 73 *gfp2Zero, 74 gfP2{ 75 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 76 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 77 }, 78 } 79 testGfP6Square(t, x) 80 } 81 82 func testGfP6Invert(t *testing.T, x *gfP6) { 83 xInv := &gfP6{} 84 xInv.Invert(x) 85 86 y := &gfP6{} 87 y.Mul(x, xInv) 88 if !y.IsOne() { 89 t.Fail() 90 } 91 } 92 93 func Test_gfP6Invert(t *testing.T) { 94 gfp2Zero := (&gfP2{}).SetZero() 95 x := &gfP6{ 96 gfP2{ 97 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 98 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 99 }, 100 gfP2{ 101 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 102 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 103 }, 104 gfP2{ 105 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 106 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 107 }, 108 } 109 testGfP6Invert(t, x) 110 111 x = &gfP6{ 112 gfP2{ 113 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 114 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 115 }, 116 gfP2{ 117 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 118 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 119 }, 120 *gfp2Zero, 121 } 122 testGfP6Invert(t, x) 123 124 x = &gfP6{ 125 *gfp2Zero, 126 gfP2{ 127 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 128 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 129 }, 130 gfP2{ 131 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 132 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 133 }, 134 } 135 testGfP6Invert(t, x) 136 137 138 x = &gfP6{ 139 gfP2{ 140 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 141 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 142 }, 143 *gfp2Zero, 144 gfP2{ 145 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 146 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 147 }, 148 } 149 testGfP6Invert(t, x) 150 } 151 152 // sToPMinus1 = s^(p-1) = u ^ ((p-1) / 3) 153 // sToPMinus1 ^ 3 = -1 154 // sToPMinus1 = 0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65334 155 func TestSToPMinus1(t *testing.T) { 156 expected := &gfP2{} 157 expected.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65334"))) 158 expected.x.Set(zero) 159 160 s := &gfP6{} 161 s.SetS() 162 s.Exp(s, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457c")) 163 if !(s.x.IsZero() && s.y.IsZero() && s.z == *expected) { 164 t.Error("not same as expected") 165 } 166 } 167 168 // s2ToPMinus1 = (s^2)^(p-1) = sToPMinus1 ^ 2 169 // s2ToPMinus1 = sToPMinus1^2 170 // s2ToPMinus1 = 0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65333 171 func TestS2ToPMinus1(t *testing.T) { 172 expected := &gfP2{} 173 expected.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65333"))) 174 expected.x.Set(zero) 175 176 s := &gfP6{} 177 s.SetS() 178 s.Square(s) 179 s.Exp(s, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457c")) 180 if !(s.x.IsZero() && s.y.IsZero() && s.z == *expected) { 181 t.Error("not same as expected") 182 } 183 184 s2 := &gfP2{} 185 s2.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65334"))) 186 s2.x.Set(zero) 187 s2.Square(s2) 188 189 if *s2 != *expected { 190 t.Errorf("not same as expected") 191 } 192 } 193 194 func Test_gfP6Frobenius(t *testing.T) { 195 x := &gfP6{ 196 gfP2{ 197 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 198 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 199 }, 200 gfP2{ 201 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 202 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 203 }, 204 gfP2{ 205 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 206 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 207 }, 208 } 209 expected := &gfP6{} 210 expected.Exp(x, p) 211 got := &gfP6{} 212 got.Frobenius(x) 213 if expected.x != got.x || expected.y != got.y || expected.z != got.z { 214 t.Errorf("got %v, expected %v", got, expected) 215 } 216 } 217 218 func TestSToPSquaredMinus1(t *testing.T) { 219 s := &gfP6{} 220 s.SetS() 221 p2 := new(big.Int).Mul(p, p) 222 p2 = new(big.Int).Sub(p2, big.NewInt(1)) 223 s.Exp(s, p2) 224 225 expected := &gfP2{} 226 expected.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65333"))) 227 expected.x.Set(zero) 228 229 if !(s.x.IsZero() && s.y.IsZero() && s.z == *expected) { 230 t.Error("not same as expected") 231 } 232 } 233 234 func TestSTo2PSquaredMinus2(t *testing.T) { 235 expected := &gfP2{} 236 expected.y.Set(fromBigInt(bigFromHex("b640000002a3a6f0e303ab4ff2eb2052a9f02115caef75e70f738991676af249"))) 237 expected.x.Set(zero) 238 239 s2 := &gfP2{} 240 s2.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65333"))) 241 s2.x.Set(zero) 242 s2.Square(s2) 243 244 if *s2 != *expected { 245 s2 = gfP2Decode(s2) 246 t.Errorf("not same as expected: %v", s2) 247 } 248 } 249 250 func Test_gfP6FrobeniusP2(t *testing.T) { 251 x := &gfP6{ 252 gfP2{ 253 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 254 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 255 }, 256 gfP2{ 257 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 258 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 259 }, 260 gfP2{ 261 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 262 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 263 }, 264 } 265 expected := &gfP6{} 266 p2 := new(big.Int).Mul(p, p) 267 expected.Exp(x, p2) 268 got := &gfP6{} 269 got.FrobeniusP2(x) 270 if expected.x != got.x || expected.y != got.y || expected.z != got.z { 271 t.Errorf("got %v, expected %v", got, expected) 272 } 273 } 274 275 func Test_gfP6FrobeniusP4(t *testing.T) { 276 x := &gfP6{ 277 gfP2{ 278 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 279 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 280 }, 281 gfP2{ 282 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 283 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 284 }, 285 gfP2{ 286 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 287 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 288 }, 289 } 290 expected := &gfP6{} 291 p4 := new(big.Int).Mul(p, p) 292 p4.Mul(p4, p4) 293 expected.Exp(x, p4) 294 got := &gfP6{} 295 got.FrobeniusP4(x) 296 if expected.x != got.x || expected.y != got.y || expected.z != got.z { 297 t.Errorf("got %v, expected %v", got, expected) 298 } 299 }