github.com/emmansun/gmsm@v0.29.1/sm9/bn256/bn_pair_test.go (about) 1 package bn256 2 3 import ( 4 "crypto/rand" 5 "math/big" 6 "testing" 7 ) 8 9 var expected1 = &gfP12{} 10 var expected_b2 = &gfP12{} 11 var expected_b2_2 = &gfP12{} 12 13 func init() { 14 expected1.x.x.x = *fromBigInt(bigFromHex("4e378fb5561cd0668f906b731ac58fee25738edf09cadc7a29c0abc0177aea6d")) 15 expected1.x.x.y = *fromBigInt(bigFromHex("28b3404a61908f5d6198815c99af1990c8af38655930058c28c21bb539ce0000")) 16 expected1.x.y.x = *fromBigInt(bigFromHex("38bffe40a22d529a0c66124b2c308dac9229912656f62b4facfced408e02380f")) 17 expected1.x.y.y = *fromBigInt(bigFromHex("a01f2c8bee81769609462c69c96aa923fd863e209d3ce26dd889b55e2e3873db")) 18 expected1.y.x.x = *fromBigInt(bigFromHex("67e0e0c2eed7a6993dce28fe9aa2ef56834307860839677f96685f2b44d0911f")) 19 expected1.y.x.y = *fromBigInt(bigFromHex("5a1ae172102efd95df7338dbc577c66d8d6c15e0a0158c7507228efb078f42a6")) 20 expected1.y.y.x = *fromBigInt(bigFromHex("1604a3fcfa9783e667ce9fcb1062c2a5c6685c316dda62de0548baa6ba30038b")) 21 expected1.y.y.y = *fromBigInt(bigFromHex("93634f44fa13af76169f3cc8fbea880adaff8475d5fd28a75deb83c44362b439")) 22 expected1.z.x.x = *fromBigInt(bigFromHex("b3129a75d31d17194675a1bc56947920898fbf390a5bf5d931ce6cbb3340f66d")) 23 expected1.z.x.y = *fromBigInt(bigFromHex("4c744e69c4a2e1c8ed72f796d151a17ce2325b943260fc460b9f73cb57c9014b")) 24 expected1.z.y.x = *fromBigInt(bigFromHex("84b87422330d7936eaba1109fa5a7a7181ee16f2438b0aeb2f38fd5f7554e57a")) 25 expected1.z.y.y = *fromBigInt(bigFromHex("aab9f06a4eeba4323a7833db202e4e35639d93fa3305af73f0f071d7d284fcfb")) 26 27 expected_b2.x.x.x = *fromBigInt(bigFromHex("28542FB6954C84BE6A5F2988A31CB6817BA0781966FA83D9673A9577D3C0C134")) 28 expected_b2.x.x.y = *fromBigInt(bigFromHex("5E27C19FC02ED9AE37F5BB7BE9C03C2B87DE027539CCF03E6B7D36DE4AB45CD1")) 29 expected_b2.x.y.x = *fromBigInt(bigFromHex("A1ABFCD30C57DB0F1A838E3A8F2BF823479C978BD137230506EA6249C891049E")) 30 expected_b2.x.y.y = *fromBigInt(bigFromHex("3497477913AB89F5E2960F382B1B5C8EE09DE0FA498BA95C4409D630D343DA40")) 31 expected_b2.y.x.x = *fromBigInt(bigFromHex("4FEC93472DA33A4DB6599095C0CF895E3A7B993EE5E4EBE3B9AB7D7D5FF2A3D1")) 32 expected_b2.y.x.y = *fromBigInt(bigFromHex("647BA154C3E8E185DFC33657C1F128D480F3F7E3F16801208029E19434C733BB")) 33 expected_b2.y.y.x = *fromBigInt(bigFromHex("73F21693C66FC23724DB26380C526223C705DAF6BA18B763A68623C86A632B05")) 34 expected_b2.y.y.y = *fromBigInt(bigFromHex("0F63A071A6D62EA45B59A1942DFF5335D1A232C9C5664FAD5D6AF54C11418B0D")) 35 expected_b2.z.x.x = *fromBigInt(bigFromHex("8C8E9D8D905780D50E779067F2C4B1C8F83A8B59D735BB52AF35F56730BDE5AC")) 36 expected_b2.z.x.y = *fromBigInt(bigFromHex("861CCD9978617267CE4AD9789F77739E62F2E57B48C2FF26D2E90A79A1D86B93")) 37 expected_b2.z.y.x = *fromBigInt(bigFromHex("9B1CA08F64712E33AEDA3F44BD6CB633E0F722211E344D73EC9BBEBC92142765")) 38 expected_b2.z.y.y = *fromBigInt(bigFromHex("6BA584CE742A2A3AB41C15D3EF94EDEB8EF74A2BDCDAAECC09ABA567981F6437")) 39 40 expected_b2_2.x.x.x = *fromBigInt(bigFromHex("1052D6E9D13E381909DFF7B2B41E13C987D0A9068423B769480DACCE6A06F492")) 41 expected_b2_2.x.x.y = *fromBigInt(bigFromHex("5FFEB92AD870F97DC0893114DA22A44DBC9E7A8B6CA31A0CF0467265A1FB48C7")) 42 expected_b2_2.x.y.x = *fromBigInt(bigFromHex("2C5C3B37E4F2FF83DB33D98C0317BCBBBBF4AC6DF6B89ECA58268B280045E612")) 43 expected_b2_2.x.y.y = *fromBigInt(bigFromHex("6CED9E2D7C9CD3D5AD630DEFAB0B831506218037EE0F861CF9B43C78434AEC38")) 44 expected_b2_2.y.x.x = *fromBigInt(bigFromHex("0AE7BF3E1AEC0CB67A03440906C7DFB3BCD4B6EEEBB7E371F0094AD4A816088D")) 45 expected_b2_2.y.x.y = *fromBigInt(bigFromHex("98DBC791D0671CACA12236CDF8F39E15AEB96FAEB39606D5B04AC581746A663D")) 46 expected_b2_2.y.y.x = *fromBigInt(bigFromHex("00DD2B7416BAA91172E89D5309D834F78C1E31B4483BB97185931BAD7BE1B9B5")) 47 expected_b2_2.y.y.y = *fromBigInt(bigFromHex("7EBAC0349F8544469E60C32F6075FB0468A68147FF013537DF792FFCE024F857")) 48 expected_b2_2.z.x.x = *fromBigInt(bigFromHex("10CC2B561A62B62DA36AEFD60850714F49170FD94A0010C6D4B651B64F3A3A5E")) 49 expected_b2_2.z.x.y = *fromBigInt(bigFromHex("58C9687BEDDCD9E4FEDAB16B884D1FE6DFA117B2AB821F74E0BF7ACDA2269859")) 50 expected_b2_2.z.y.x = *fromBigInt(bigFromHex("2A430968F16086061904CE201847934B11CA0F9E9528F5A9D0CE8F015C9AEA79")) 51 expected_b2_2.z.y.y = *fromBigInt(bigFromHex("934FDDA6D3AB48C8571CE2354B79742AA498CB8CDDE6BD1FA5946345A1A652F6")) 52 } 53 54 func Test_Pairing_A2(t *testing.T) { 55 pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4") 56 g2 := &G2{} 57 _, err := g2.ScalarBaseMult(NormalizeScalar(pk.Bytes())) 58 if err != nil { 59 t.Fatal(err) 60 } 61 ret := pairing(g2.p, curveGen) 62 if ret.x != expected1.x || ret.y != expected1.y || ret.z != expected1.z { 63 t.Errorf("not expected") 64 } 65 } 66 67 func Test_Pairing_B2(t *testing.T) { 68 deB := &twistPoint{} 69 deB.x.x = *fromBigInt(bigFromHex("74CCC3AC9C383C60AF083972B96D05C75F12C8907D128A17ADAFBAB8C5A4ACF7")) 70 deB.x.y = *fromBigInt(bigFromHex("01092FF4DE89362670C21711B6DBE52DCD5F8E40C6654B3DECE573C2AB3D29B2")) 71 deB.y.x = *fromBigInt(bigFromHex("44B0294AA04290E1524FF3E3DA8CFD432BB64DE3A8040B5B88D1B5FC86A4EBC1")) 72 deB.y.y = *fromBigInt(bigFromHex("8CFC48FB4FF37F1E27727464F3C34E2153861AD08E972D1625FC1A7BD18D5539")) 73 deB.z.SetOne() 74 deB.t.SetOne() 75 76 rA := &curvePoint{} 77 rA.x = *fromBigInt(bigFromHex("7CBA5B19069EE66AA79D490413D11846B9BA76DD22567F809CF23B6D964BB265")) 78 rA.y = *fromBigInt(bigFromHex("A9760C99CB6F706343FED05637085864958D6C90902ABA7D405FBEDF7B781599")) 79 rA.z = *one 80 rA.t = *one 81 82 ret := pairing(deB, rA) 83 if ret.x != expected_b2.x || ret.y != expected_b2.y || ret.z != expected_b2.z { 84 t.Errorf("not expected") 85 } 86 } 87 88 func Test_Pairing_B2_2(t *testing.T) { 89 pubE := &curvePoint{} 90 pubE.x = *fromBigInt(bigFromHex("9174542668E8F14AB273C0945C3690C66E5DD09678B86F734C4350567ED06283")) 91 pubE.y = *fromBigInt(bigFromHex("54E598C6BF749A3DACC9FFFEDD9DB6866C50457CFC7AA2A4AD65C3168FF74210")) 92 pubE.z = *one 93 pubE.t = *one 94 95 ret := pairing(twistGen, pubE) 96 ret.Exp(ret, bigFromHex("00018B98C44BEF9F8537FB7D071B2C928B3BC65BD3D69E1EEE213564905634FE")) 97 if ret.x != expected_b2_2.x || ret.y != expected_b2_2.y || ret.z != expected_b2_2.z { 98 t.Errorf("not expected") 99 } 100 } 101 102 var testGfp12 = &gfP12{ 103 gfP4{ 104 gfP2{ 105 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 106 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 107 }, 108 gfP2{ 109 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 110 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 111 }, 112 }, 113 gfP4{ 114 gfP2{ 115 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 116 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 117 }, 118 gfP2{ 119 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 120 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 121 }, 122 }, 123 gfP4{ 124 gfP2{ 125 *fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")), 126 *fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")), 127 }, 128 gfP2{ 129 *fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")), 130 *fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")), 131 }, 132 }, 133 } 134 135 func Test_finalExponentiation(t *testing.T) { 136 x := testGfp12 137 got := finalExponentiation(x) 138 139 exp := new(big.Int).Exp(p, big.NewInt(12), nil) 140 exp.Sub(exp, big.NewInt(1)) 141 exp.Div(exp, Order) 142 expected := (&gfP12{}).Exp(x, exp) 143 144 if got.x != expected.x || got.y != expected.y || got.z != expected.z { 145 t.Errorf("got %v, expected %v\n", got, expected) 146 } 147 } 148 149 func TestBilinearity(t *testing.T) { 150 for i := 0; i < 2; i++ { 151 a, p1, _ := RandomG1(rand.Reader) 152 b, p2, _ := RandomG2(rand.Reader) 153 e1 := Pair(p1, p2) 154 155 e2 := Pair(&G1{curveGen}, &G2{twistGen}) 156 e2.ScalarMult(e2, a) 157 e2.ScalarMult(e2, b) 158 159 if *e1.p != *e2.p { 160 t.Fatalf("bad pairing result: %s", e1) 161 } 162 } 163 } 164 165 func BenchmarkFinalExponentiation(b *testing.B) { 166 x := testGfp12 167 exp := new(big.Int).Exp(p, big.NewInt(12), nil) 168 exp.Sub(exp, big.NewInt(1)) 169 exp.Div(exp, Order) 170 expected := (&gfP12{}).Exp(x, exp) 171 172 b.ReportAllocs() 173 b.ResetTimer() 174 for i := 0; i < b.N; i++ { 175 got := finalExponentiation(x) 176 if *got != *expected { 177 b.Errorf("got %v, expected %v\n", got, expected) 178 } 179 } 180 } 181 182 func BenchmarkMiller(b *testing.B) { 183 pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4") 184 g2 := &G2{} 185 _, err := g2.ScalarBaseMult(NormalizeScalar(pk.Bytes())) 186 if err != nil { 187 b.Fatal(err) 188 } 189 b.ReportAllocs() 190 b.ResetTimer() 191 for i := 0; i < b.N; i++ { 192 miller(g2.p, curveGen) 193 } 194 } 195 196 func BenchmarkPairingB4(b *testing.B) { 197 pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4") 198 g2 := &G2{} 199 _, err := g2.ScalarBaseMult(NormalizeScalar(pk.Bytes())) 200 if err != nil { 201 b.Fatal(err) 202 } 203 b.ReportAllocs() 204 b.ResetTimer() 205 for i := 0; i < b.N; i++ { 206 ret := pairing(g2.p, curveGen) 207 if *ret != *expected1 { 208 b.Errorf("not expected") 209 } 210 } 211 }