github.com/emmansun/gmsm@v0.29.1/sm2/sm2ec/sm2ec_test.go (about) 1 package sm2ec 2 3 import ( 4 "crypto/elliptic" 5 "math/big" 6 "testing" 7 ) 8 9 type baseMultTest struct { 10 k string 11 } 12 13 var baseMultTests = []baseMultTest{ 14 { 15 "112233445566778899", 16 }, 17 { 18 "112233445566778899112233445566778899", 19 }, 20 { 21 "6950511619965839450988900688150712778015737983940691968051900319680", 22 }, 23 { 24 "13479972933410060327035789020509431695094902435494295338570602119423", 25 }, 26 { 27 "13479971751745682581351455311314208093898607229429740618390390702079", 28 }, 29 { 30 "13479972931865328106486971546324465392952975980343228160962702868479", 31 }, 32 { 33 "11795773708834916026404142434151065506931607341523388140225443265536", 34 }, 35 { 36 "784254593043826236572847595991346435467177662189391577090", 37 }, 38 { 39 "13479767645505654746623887797783387853576174193480695826442858012671", 40 }, 41 { 42 "205688069665150753842126177372015544874550518966168735589597183", 43 }, 44 { 45 "13479966930919337728895168462090683249159702977113823384618282123295", 46 }, 47 { 48 "50210731791415612487756441341851895584393717453129007497216", 49 }, 50 { 51 "26959946667150639794667015087019625940457807714424391721682722368041", 52 }, 53 { 54 "26959946667150639794667015087019625940457807714424391721682722368042", 55 }, 56 { 57 "26959946667150639794667015087019625940457807714424391721682722368043", 58 }, 59 { 60 "26959946667150639794667015087019625940457807714424391721682722368044", 61 }, 62 { 63 "26959946667150639794667015087019625940457807714424391721682722368045", 64 }, 65 { 66 "26959946667150639794667015087019625940457807714424391721682722368046", 67 }, 68 { 69 "26959946667150639794667015087019625940457807714424391721682722368047", 70 }, 71 { 72 "26959946667150639794667015087019625940457807714424391721682722368048", 73 }, 74 { 75 "26959946667150639794667015087019625940457807714424391721682722368049", 76 }, 77 { 78 "26959946667150639794667015087019625940457807714424391721682722368050", 79 }, 80 { 81 "26959946667150639794667015087019625940457807714424391721682722368051", 82 }, 83 { 84 "26959946667150639794667015087019625940457807714424391721682722368052", 85 }, 86 { 87 "26959946667150639794667015087019625940457807714424391721682722368053", 88 }, 89 { 90 "26959946667150639794667015087019625940457807714424391721682722368054", 91 }, 92 { 93 "26959946667150639794667015087019625940457807714424391721682722368055", 94 }, 95 { 96 "26959946667150639794667015087019625940457807714424391721682722368056", 97 }, 98 { 99 "26959946667150639794667015087019625940457807714424391721682722368057", 100 }, 101 { 102 "26959946667150639794667015087019625940457807714424391721682722368058", 103 }, 104 { 105 "26959946667150639794667015087019625940457807714424391721682722368059", 106 }, 107 { 108 "26959946667150639794667015087019625940457807714424391721682722368060", 109 }, 110 } 111 112 type scalarMultTest struct { 113 k string 114 xIn, yIn string 115 xOut, yOut string 116 } 117 118 var p256MultTests = []scalarMultTest{ 119 { 120 "9e9e0dfa7b29bd78a381e5ad3c3ef3154080bf8198b4f6d4dc4b13a04e49a979", 121 "0a5351c475d8f8c5dab77b688b17fa1d6f2a9aed187b3a6cb670647c1a1b2369", 122 "aba5ace91a313f0d4468a44f66617e7f497f3508c6f2c0273dc6c133c9a59df0", 123 "f64634b9eb2b0feb5bfdcb882a365041437da717dfb4156e7b3f3b22784889a9", 124 "84d36430a453396b047494e6a74c43abf193c13ce17dd60b614b22de97139d09", 125 }, 126 { 127 "dd242eb66c7be62f2d3173185b6875f66d0d0bc75df8900c69d48630ef60faff", 128 "0f5e36b3eaa03868bccfd0f7e5f0189ee5d58b0946420ee0672797620f4856df", 129 "35032c2d743a0df6d838b01034402db85d3ad4b07f316612cfc8902434dedd29", 130 "1ffe871e928012e14dfad0ec1d54a8198c6830dd283703a42c21f2367c72d10f", 131 "fbf401d0729d2b38a925d2d2b750293239ea74065a28279710e5fc8a7c86b3b7", 132 }, 133 { 134 "38f2411d1cad8c1b026e731a85dcc2eca79f472369233ae204aa5d6a2f6542f1", 135 "1fb1c5de8ef2fdecf9a729ed4eb9ce0f363e75fed95400dbd25c333c26393bc3", 136 "e9250c58d7200783aa9ec9814c13f252ba368bf52d6fd8f2e9397e603972e55d", 137 "b5301fcc9818019651e8f56a265fb254ad864d9001b21ebd6b1a6ec0e6f6e07a", 138 "2e29cd8f8697360d0b60d730d073793d41bc3c99f00c99875f5d22ed0b32ea6a", 139 }, 140 } 141 142 func TestP256BaseMult(t *testing.T) { 143 p256 := P256() 144 p256Generic := genericParamsForCurve(p256) 145 146 scalars := make([]*big.Int, 0, len(baseMultTests)+1) 147 for i := 1; i <= 20; i++ { 148 k := new(big.Int).SetInt64(int64(i)) 149 scalars = append(scalars, k) 150 } 151 for _, e := range baseMultTests { 152 k, _ := new(big.Int).SetString(e.k, 10) 153 scalars = append(scalars, k) 154 } 155 k := new(big.Int).SetInt64(1) 156 k.Lsh(k, 500) 157 scalars = append(scalars, k) 158 159 for i, k := range scalars { 160 x, y := p256.ScalarBaseMult(k.Bytes()) 161 x2, y2 := p256Generic.ScalarBaseMult(k.Bytes()) 162 if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 { 163 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, x, y, x2, y2) 164 } 165 166 if testing.Short() && i > 5 { 167 break 168 } 169 } 170 } 171 172 /* 173 func generateP256MultTests() { 174 p256 := P256() 175 p256Generic := genericParamsForCurve(p256) 176 for i := 0; i < 3; i++ { 177 k1, err := randFieldElement(p256Generic, rand.Reader) 178 if err != nil { 179 fmt.Printf("%v\n", err) 180 } 181 x1, y1 := p256Generic.ScalarBaseMult(k1.Bytes()) 182 k2, err := randFieldElement(p256Generic, rand.Reader) 183 if err != nil { 184 fmt.Printf("%v\n", err) 185 } 186 x2, y2 := p256Generic.ScalarMult(x1, y1, k2.Bytes()) 187 fmt.Printf("%s\n", hex.EncodeToString(k2.Bytes())) 188 fmt.Printf("%s\n", hex.EncodeToString(x1.Bytes())) 189 fmt.Printf("%s\n", hex.EncodeToString(y1.Bytes())) 190 fmt.Printf("%s\n", hex.EncodeToString(x2.Bytes())) 191 fmt.Printf("%s\n", hex.EncodeToString(y2.Bytes())) 192 } 193 } 194 */ 195 func TestP256Mult(t *testing.T) { 196 p256 := P256() 197 for i, e := range p256MultTests { 198 x, _ := new(big.Int).SetString(e.xIn, 16) 199 y, _ := new(big.Int).SetString(e.yIn, 16) 200 k, _ := new(big.Int).SetString(e.k, 16) 201 expectedX, _ := new(big.Int).SetString(e.xOut, 16) 202 expectedY, _ := new(big.Int).SetString(e.yOut, 16) 203 204 xx, yy := p256.ScalarMult(x, y, k.Bytes()) 205 if xx.Cmp(expectedX) != 0 || yy.Cmp(expectedY) != 0 { 206 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, xx, yy, expectedX, expectedY) 207 } 208 } 209 } 210 211 type synthCombinedMult struct { 212 elliptic.Curve 213 } 214 215 func (s synthCombinedMult) CombinedMult(bigX, bigY *big.Int, baseScalar, scalar []byte) (x, y *big.Int) { 216 x1, y1 := s.ScalarBaseMult(baseScalar) 217 x2, y2 := s.ScalarMult(bigX, bigY, scalar) 218 return s.Add(x1, y1, x2, y2) 219 } 220 221 func TestP256CombinedMult(t *testing.T) { 222 type combinedMult interface { 223 elliptic.Curve 224 CombinedMult(bigX, bigY *big.Int, baseScalar, scalar []byte) (x, y *big.Int) 225 } 226 227 p256, ok := P256().(combinedMult) 228 if !ok { 229 p256 = &synthCombinedMult{P256()} 230 } 231 232 gx := p256.Params().Gx 233 gy := p256.Params().Gy 234 235 zero := make([]byte, 32) 236 one := make([]byte, 32) 237 one[31] = 1 238 two := make([]byte, 32) 239 two[31] = 2 240 241 // 0×G + 0×G = ∞ 242 x, y := p256.CombinedMult(gx, gy, zero, zero) 243 if x.Sign() != 0 || y.Sign() != 0 { 244 t.Errorf("0×G + 0×G = (%d, %d), should be ∞", x, y) 245 } 246 247 // 1×G + 0×G = G 248 x, y = p256.CombinedMult(gx, gy, one, zero) 249 if x.Cmp(gx) != 0 || y.Cmp(gy) != 0 { 250 t.Errorf("1×G + 0×G = (%d, %d), should be (%d, %d)", x, y, gx, gy) 251 } 252 253 // 0×G + 1×G = G 254 x, y = p256.CombinedMult(gx, gy, zero, one) 255 if x.Cmp(gx) != 0 || y.Cmp(gy) != 0 { 256 t.Errorf("0×G + 1×G = (%d, %d), should be (%d, %d)", x, y, gx, gy) 257 } 258 259 // 1×G + 1×G = 2×G 260 x, y = p256.CombinedMult(gx, gy, one, one) 261 ggx, ggy := p256.ScalarBaseMult(two) 262 if x.Cmp(ggx) != 0 || y.Cmp(ggy) != 0 { 263 t.Errorf("1×G + 1×G = (%d, %d), should be (%d, %d)", x, y, ggx, ggy) 264 } 265 266 minusOne := new(big.Int).Sub(p256.Params().N, big.NewInt(1)) 267 // 1×G + (-1)×G = ∞ 268 x, y = p256.CombinedMult(gx, gy, one, minusOne.Bytes()) 269 if x.Sign() != 0 || y.Sign() != 0 { 270 t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y) 271 } 272 } 273 274 func TestIssue52075(t *testing.T) { 275 Gx, Gy := P256().Params().Gx, P256().Params().Gy 276 scalar := make([]byte, 33) 277 scalar[32] = 1 278 x, y := P256().ScalarBaseMult(scalar) 279 if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 { 280 t.Errorf("unexpected output (%v,%v)", x, y) 281 } 282 x, y = P256().ScalarMult(Gx, Gy, scalar) 283 if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 { 284 t.Errorf("unexpected output (%v,%v)", x, y) 285 } 286 }