github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/crypto/elliptic/elliptic_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package elliptic 6 7 import ( 8 "bytes" 9 "crypto/rand" 10 "encoding/hex" 11 "fmt" 12 "math/big" 13 "testing" 14 ) 15 16 func TestOnCurve(t *testing.T) { 17 p224 := P224() 18 if !p224.IsOnCurve(p224.Params().Gx, p224.Params().Gy) { 19 t.Errorf("FAIL") 20 } 21 } 22 23 func TestOffCurve(t *testing.T) { 24 p224 := P224() 25 x, y := new(big.Int).SetInt64(1), new(big.Int).SetInt64(1) 26 if p224.IsOnCurve(x, y) { 27 t.Errorf("FAIL: point off curve is claimed to be on the curve") 28 } 29 b := Marshal(p224, x, y) 30 x1, y1 := Unmarshal(p224, b) 31 if x1 != nil || y1 != nil { 32 t.Errorf("FAIL: unmarshaling a point not on the curve succeeded") 33 } 34 } 35 36 type baseMultTest struct { 37 k string 38 x, y string 39 } 40 41 var p224BaseMultTests = []baseMultTest{ 42 { 43 "1", 44 "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 45 "bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", 46 }, 47 { 48 "2", 49 "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6", 50 "1c2b76a7bc25e7702a704fa986892849fca629487acf3709d2e4e8bb", 51 }, 52 { 53 "3", 54 "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04", 55 "a3f7f03cadd0be444c0aa56830130ddf77d317344e1af3591981a925", 56 }, 57 { 58 "4", 59 "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301", 60 "482580a0ec5bc47e88bc8c378632cd196cb3fa058a7114eb03054c9", 61 }, 62 { 63 "5", 64 "31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa", 65 "27e8bff1745635ec5ba0c9f1c2ede15414c6507d29ffe37e790a079b", 66 }, 67 { 68 "6", 69 "1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408", 70 "89faf0ccb750d99b553c574fad7ecfb0438586eb3952af5b4b153c7e", 71 }, 72 { 73 "7", 74 "db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28", 75 "f3a30085497f2f611ee2517b163ef8c53b715d18bb4e4808d02b963", 76 }, 77 { 78 "8", 79 "858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550", 80 "46dcd3ea5c43898c5c5fc4fdac7db39c2f02ebee4e3541d1e78047a", 81 }, 82 { 83 "9", 84 "2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d", 85 "371732e4f41bf4f7883035e6a79fcedc0e196eb07b48171697517463", 86 }, 87 { 88 "10", 89 "aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd", 90 "39bb30eab337e0a521b6cba1abe4b2b3a3e524c14a3fe3eb116b655f", 91 }, 92 { 93 "11", 94 "ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c", 95 "20b510004092e96636cfb7e32efded8265c266dfb754fa6d6491a6da", 96 }, 97 { 98 "12", 99 "6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a", 100 "207dddf0385bfdeab6e9acda8da06b3bbef224a93ab1e9e036109d13", 101 }, 102 { 103 "13", 104 "34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca", 105 "252819f71c7fb7fbcb159be337d37d3336d7feb963724fdfb0ecb767", 106 }, 107 { 108 "14", 109 "a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa", 110 "d5814cd724199c4a5b974a43685fbf5b8bac69459c9469bc8f23ccaf", 111 }, 112 { 113 "15", 114 "baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9", 115 "979a5f4759f80f4fb4ec2e34f5566d595680a11735e7b61046127989", 116 }, 117 { 118 "16", 119 "b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d", 120 "3399d464345906b11b00e363ef429221f2ec720d2f665d7dead5b482", 121 }, 122 { 123 "17", 124 "b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc", 125 "ff149efa6606a6bd20ef7d1b06bd92f6904639dce5174db6cc554a26", 126 }, 127 { 128 "18", 129 "c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc", 130 "ea98d60e5ffc9b8fcf999fab1df7e7ef7084f20ddb61bb045a6ce002", 131 }, 132 { 133 "19", 134 "a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c", 135 "dcf1f6c3db09c70acc25391d492fe25b4a180babd6cea356c04719cd", 136 }, 137 { 138 "20", 139 "fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455", 140 "d5d7110274cba7cdee90e1a8b0d394c376a5573db6be0bf2747f530", 141 }, 142 { 143 "112233445566778899", 144 "61f077c6f62ed802dad7c2f38f5c67f2cc453601e61bd076bb46179e", 145 "2272f9e9f5933e70388ee652513443b5e289dd135dcc0d0299b225e4", 146 }, 147 { 148 "112233445566778899112233445566778899", 149 "29895f0af496bfc62b6ef8d8a65c88c613949b03668aab4f0429e35", 150 "3ea6e53f9a841f2019ec24bde1a75677aa9b5902e61081c01064de93", 151 }, 152 { 153 "6950511619965839450988900688150712778015737983940691968051900319680", 154 "ab689930bcae4a4aa5f5cb085e823e8ae30fd365eb1da4aba9cf0379", 155 "3345a121bbd233548af0d210654eb40bab788a03666419be6fbd34e7", 156 }, 157 { 158 "13479972933410060327035789020509431695094902435494295338570602119423", 159 "bdb6a8817c1f89da1c2f3dd8e97feb4494f2ed302a4ce2bc7f5f4025", 160 "4c7020d57c00411889462d77a5438bb4e97d177700bf7243a07f1680", 161 }, 162 { 163 "13479971751745682581351455311314208093898607229429740618390390702079", 164 "d58b61aa41c32dd5eba462647dba75c5d67c83606c0af2bd928446a9", 165 "d24ba6a837be0460dd107ae77725696d211446c5609b4595976b16bd", 166 }, 167 { 168 "13479972931865328106486971546324465392952975980343228160962702868479", 169 "dc9fa77978a005510980e929a1485f63716df695d7a0c18bb518df03", 170 "ede2b016f2ddffc2a8c015b134928275ce09e5661b7ab14ce0d1d403", 171 }, 172 { 173 "11795773708834916026404142434151065506931607341523388140225443265536", 174 "499d8b2829cfb879c901f7d85d357045edab55028824d0f05ba279ba", 175 "bf929537b06e4015919639d94f57838fa33fc3d952598dcdbb44d638", 176 }, 177 { 178 "784254593043826236572847595991346435467177662189391577090", 179 "8246c999137186632c5f9eddf3b1b0e1764c5e8bd0e0d8a554b9cb77", 180 "e80ed8660bc1cb17ac7d845be40a7a022d3306f116ae9f81fea65947", 181 }, 182 { 183 "13479767645505654746623887797783387853576174193480695826442858012671", 184 "6670c20afcceaea672c97f75e2e9dd5c8460e54bb38538ebb4bd30eb", 185 "f280d8008d07a4caf54271f993527d46ff3ff46fd1190a3f1faa4f74", 186 }, 187 { 188 "205688069665150753842126177372015544874550518966168735589597183", 189 "eca934247425cfd949b795cb5ce1eff401550386e28d1a4c5a8eb", 190 "d4c01040dba19628931bc8855370317c722cbd9ca6156985f1c2e9ce", 191 }, 192 { 193 "13479966930919337728895168462090683249159702977113823384618282123295", 194 "ef353bf5c73cd551b96d596fbc9a67f16d61dd9fe56af19de1fba9cd", 195 "21771b9cdce3e8430c09b3838be70b48c21e15bc09ee1f2d7945b91f", 196 }, 197 { 198 "50210731791415612487756441341851895584393717453129007497216", 199 "4036052a3091eb481046ad3289c95d3ac905ca0023de2c03ecd451cf", 200 "d768165a38a2b96f812586a9d59d4136035d9c853a5bf2e1c86a4993", 201 }, 202 { 203 "26959946667150639794667015087019625940457807714424391721682722368041", 204 "fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455", 205 "f2a28eefd8b345832116f1e574f2c6b2c895aa8c24941f40d8b80ad1", 206 }, 207 { 208 "26959946667150639794667015087019625940457807714424391721682722368042", 209 "a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c", 210 "230e093c24f638f533dac6e2b6d01da3b5e7f45429315ca93fb8e634", 211 }, 212 { 213 "26959946667150639794667015087019625940457807714424391721682722368043", 214 "c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc", 215 "156729f1a003647030666054e208180f8f7b0df2249e44fba5931fff", 216 }, 217 { 218 "26959946667150639794667015087019625940457807714424391721682722368044", 219 "b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc", 220 "eb610599f95942df1082e4f9426d086fb9c6231ae8b24933aab5db", 221 }, 222 { 223 "26959946667150639794667015087019625940457807714424391721682722368045", 224 "b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d", 225 "cc662b9bcba6f94ee4ff1c9c10bd6ddd0d138df2d099a282152a4b7f", 226 }, 227 { 228 "26959946667150639794667015087019625940457807714424391721682722368046", 229 "baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9", 230 "6865a0b8a607f0b04b13d1cb0aa992a5a97f5ee8ca1849efb9ed8678", 231 }, 232 { 233 "26959946667150639794667015087019625940457807714424391721682722368047", 234 "a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa", 235 "2a7eb328dbe663b5a468b5bc97a040a3745396ba636b964370dc3352", 236 }, 237 { 238 "26959946667150639794667015087019625940457807714424391721682722368048", 239 "34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca", 240 "dad7e608e380480434ea641cc82c82cbc92801469c8db0204f13489a", 241 }, 242 { 243 "26959946667150639794667015087019625940457807714424391721682722368049", 244 "6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a", 245 "df82220fc7a4021549165325725f94c3410ddb56c54e161fc9ef62ee", 246 }, 247 { 248 "26959946667150639794667015087019625940457807714424391721682722368050", 249 "ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c", 250 "df4aefffbf6d1699c930481cd102127c9a3d992048ab05929b6e5927", 251 }, 252 { 253 "26959946667150639794667015087019625940457807714424391721682722368051", 254 "aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd", 255 "c644cf154cc81f5ade49345e541b4d4b5c1adb3eb5c01c14ee949aa2", 256 }, 257 { 258 "26959946667150639794667015087019625940457807714424391721682722368052", 259 "2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d", 260 "c8e8cd1b0be40b0877cfca1958603122f1e6914f84b7e8e968ae8b9e", 261 }, 262 { 263 "26959946667150639794667015087019625940457807714424391721682722368053", 264 "858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550", 265 "fb9232c15a3bc7673a3a03b0253824c53d0fd1411b1cabe2e187fb87", 266 }, 267 { 268 "26959946667150639794667015087019625940457807714424391721682722368054", 269 "db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28", 270 "f0c5cff7ab680d09ee11dae84e9c1072ac48ea2e744b1b7f72fd469e", 271 }, 272 { 273 "26959946667150639794667015087019625940457807714424391721682722368055", 274 "1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408", 275 "76050f3348af2664aac3a8b05281304ebc7a7914c6ad50a4b4eac383", 276 }, 277 { 278 "26959946667150639794667015087019625940457807714424391721682722368056", 279 "31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa", 280 "d817400e8ba9ca13a45f360e3d121eaaeb39af82d6001c8186f5f866", 281 }, 282 { 283 "26959946667150639794667015087019625940457807714424391721682722368057", 284 "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301", 285 "fb7da7f5f13a43b81774373c879cd32d6934c05fa758eeb14fcfab38", 286 }, 287 { 288 "26959946667150639794667015087019625940457807714424391721682722368058", 289 "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04", 290 "5c080fc3522f41bbb3f55a97cfecf21f882ce8cbb1e50ca6e67e56dc", 291 }, 292 { 293 "26959946667150639794667015087019625940457807714424391721682722368059", 294 "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6", 295 "e3d4895843da188fd58fb0567976d7b50359d6b78530c8f62d1b1746", 296 }, 297 { 298 "26959946667150639794667015087019625940457807714424391721682722368060", 299 "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 300 "42c89c774a08dc04b3dd201932bc8a5ea5f8b89bbb2a7e667aff81cd", 301 }, 302 } 303 304 type scalarMultTest struct { 305 k string 306 xIn, yIn string 307 xOut, yOut string 308 } 309 310 var p256MultTests = []scalarMultTest{ 311 { 312 "2a265f8bcbdcaf94d58519141e578124cb40d64a501fba9c11847b28965bc737", 313 "023819813ac969847059028ea88a1f30dfbcde03fc791d3a252c6b41211882ea", 314 "f93e4ae433cc12cf2a43fc0ef26400c0e125508224cdb649380f25479148a4ad", 315 "4d4de80f1534850d261075997e3049321a0864082d24a917863366c0724f5ae3", 316 "a22d2b7f7818a3563e0f7a76c9bf0921ac55e06e2e4d11795b233824b1db8cc0", 317 }, 318 { 319 "313f72ff9fe811bf573176231b286a3bdb6f1b14e05c40146590727a71c3bccd", 320 "cc11887b2d66cbae8f4d306627192522932146b42f01d3c6f92bd5c8ba739b06", 321 "a2f08a029cd06b46183085bae9248b0ed15b70280c7ef13a457f5af382426031", 322 "831c3f6b5f762d2f461901577af41354ac5f228c2591f84f8a6e51e2e3f17991", 323 "93f90934cd0ef2c698cc471c60a93524e87ab31ca2412252337f364513e43684", 324 }, 325 } 326 327 func TestBaseMult(t *testing.T) { 328 p224 := P224() 329 for i, e := range p224BaseMultTests { 330 k, ok := new(big.Int).SetString(e.k, 10) 331 if !ok { 332 t.Errorf("%d: bad value for k: %s", i, e.k) 333 } 334 x, y := p224.ScalarBaseMult(k.Bytes()) 335 if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y { 336 t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y) 337 } 338 if testing.Short() && i > 5 { 339 break 340 } 341 } 342 } 343 344 func TestGenericBaseMult(t *testing.T) { 345 // We use the P224 CurveParams directly in order to test the generic implementation. 346 p224 := P224().Params() 347 for i, e := range p224BaseMultTests { 348 k, ok := new(big.Int).SetString(e.k, 10) 349 if !ok { 350 t.Errorf("%d: bad value for k: %s", i, e.k) 351 } 352 x, y := p224.ScalarBaseMult(k.Bytes()) 353 if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y { 354 t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y) 355 } 356 if testing.Short() && i > 5 { 357 break 358 } 359 } 360 } 361 362 func TestP256BaseMult(t *testing.T) { 363 p256 := P256() 364 p256Generic := p256.Params() 365 366 scalars := make([]*big.Int, 0, len(p224BaseMultTests)+1) 367 for _, e := range p224BaseMultTests { 368 k, _ := new(big.Int).SetString(e.k, 10) 369 scalars = append(scalars, k) 370 } 371 k := new(big.Int).SetInt64(1) 372 k.Lsh(k, 500) 373 scalars = append(scalars, k) 374 375 for i, k := range scalars { 376 x, y := p256.ScalarBaseMult(k.Bytes()) 377 x2, y2 := p256Generic.ScalarBaseMult(k.Bytes()) 378 if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 { 379 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, x, y, x2, y2) 380 } 381 382 if testing.Short() && i > 5 { 383 break 384 } 385 } 386 } 387 388 func TestP256Mult(t *testing.T) { 389 p256 := P256() 390 p256Generic := p256.Params() 391 392 for i, e := range p224BaseMultTests { 393 x, _ := new(big.Int).SetString(e.x, 16) 394 y, _ := new(big.Int).SetString(e.y, 16) 395 k, _ := new(big.Int).SetString(e.k, 10) 396 397 xx, yy := p256.ScalarMult(x, y, k.Bytes()) 398 xx2, yy2 := p256Generic.ScalarMult(x, y, k.Bytes()) 399 if xx.Cmp(xx2) != 0 || yy.Cmp(yy2) != 0 { 400 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, xx, yy, xx2, yy2) 401 } 402 if testing.Short() && i > 5 { 403 break 404 } 405 } 406 407 for i, e := range p256MultTests { 408 x, _ := new(big.Int).SetString(e.xIn, 16) 409 y, _ := new(big.Int).SetString(e.yIn, 16) 410 k, _ := new(big.Int).SetString(e.k, 16) 411 expectedX, _ := new(big.Int).SetString(e.xOut, 16) 412 expectedY, _ := new(big.Int).SetString(e.yOut, 16) 413 414 xx, yy := p256.ScalarMult(x, y, k.Bytes()) 415 if xx.Cmp(expectedX) != 0 || yy.Cmp(expectedY) != 0 { 416 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, xx, yy, expectedX, expectedY) 417 } 418 } 419 } 420 421 func testInfinity(t *testing.T, curve Curve) { 422 _, x, y, _ := GenerateKey(curve, rand.Reader) 423 x, y = curve.ScalarMult(x, y, curve.Params().N.Bytes()) 424 if x.Sign() != 0 || y.Sign() != 0 { 425 t.Errorf("x^q != ∞") 426 } 427 428 x, y = curve.ScalarBaseMult([]byte{0}) 429 if x.Sign() != 0 || y.Sign() != 0 { 430 t.Errorf("b^0 != ∞") 431 x.SetInt64(0) 432 y.SetInt64(0) 433 } 434 435 x2, y2 := curve.Double(x, y) 436 if x2.Sign() != 0 || y2.Sign() != 0 { 437 t.Errorf("2∞ != ∞") 438 } 439 440 baseX := curve.Params().Gx 441 baseY := curve.Params().Gy 442 443 x3, y3 := curve.Add(baseX, baseY, x, y) 444 if x3.Cmp(baseX) != 0 || y3.Cmp(baseY) != 0 { 445 t.Errorf("x+∞ != x") 446 } 447 448 x4, y4 := curve.Add(x, y, baseX, baseY) 449 if x4.Cmp(baseX) != 0 || y4.Cmp(baseY) != 0 { 450 t.Errorf("∞+x != x") 451 } 452 453 if curve.IsOnCurve(x, y) { 454 t.Errorf("IsOnCurve(∞) == true") 455 } 456 } 457 458 func TestInfinity(t *testing.T) { 459 tests := []struct { 460 name string 461 curve Curve 462 }{ 463 {"P-224", P224()}, 464 {"P-256", P256()}, 465 {"P-256/Generic", P256().Params()}, 466 {"P-384", P384()}, 467 {"P-521", P521()}, 468 } 469 if testing.Short() { 470 tests = tests[:1] 471 } 472 for _, test := range tests { 473 curve := test.curve 474 t.Run(test.name, func(t *testing.T) { 475 testInfinity(t, curve) 476 }) 477 } 478 } 479 480 type synthCombinedMult struct { 481 Curve 482 } 483 484 func (s synthCombinedMult) CombinedMult(bigX, bigY *big.Int, baseScalar, scalar []byte) (x, y *big.Int) { 485 x1, y1 := s.ScalarBaseMult(baseScalar) 486 x2, y2 := s.ScalarMult(bigX, bigY, scalar) 487 return s.Add(x1, y1, x2, y2) 488 } 489 490 func TestCombinedMult(t *testing.T) { 491 type combinedMult interface { 492 Curve 493 CombinedMult(bigX, bigY *big.Int, baseScalar, scalar []byte) (x, y *big.Int) 494 } 495 496 p256, ok := P256().(combinedMult) 497 if !ok { 498 p256 = &synthCombinedMult{P256()} 499 } 500 501 gx := p256.Params().Gx 502 gy := p256.Params().Gy 503 504 zero := make([]byte, 32) 505 one := make([]byte, 32) 506 one[31] = 1 507 two := make([]byte, 32) 508 two[31] = 2 509 510 // 0×G + 0×G = ∞ 511 x, y := p256.CombinedMult(gx, gy, zero, zero) 512 if x.Sign() != 0 || y.Sign() != 0 { 513 t.Errorf("0×G + 0×G = (%d, %d), should be ∞", x, y) 514 } 515 516 // 1×G + 0×G = G 517 x, y = p256.CombinedMult(gx, gy, one, zero) 518 if x.Cmp(gx) != 0 || y.Cmp(gy) != 0 { 519 t.Errorf("1×G + 0×G = (%d, %d), should be (%d, %d)", x, y, gx, gy) 520 } 521 522 // 0×G + 1×G = G 523 x, y = p256.CombinedMult(gx, gy, zero, one) 524 if x.Cmp(gx) != 0 || y.Cmp(gy) != 0 { 525 t.Errorf("0×G + 1×G = (%d, %d), should be (%d, %d)", x, y, gx, gy) 526 } 527 528 // 1×G + 1×G = 2×G 529 x, y = p256.CombinedMult(gx, gy, one, one) 530 ggx, ggy := p256.ScalarBaseMult(two) 531 if x.Cmp(ggx) != 0 || y.Cmp(ggy) != 0 { 532 t.Errorf("1×G + 1×G = (%d, %d), should be (%d, %d)", x, y, ggx, ggy) 533 } 534 535 minusOne := new(big.Int).Sub(p256.Params().N, big.NewInt(1)) 536 // 1×G + (-1)×G = ∞ 537 x, y = p256.CombinedMult(gx, gy, one, minusOne.Bytes()) 538 if x.Sign() != 0 || y.Sign() != 0 { 539 t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y) 540 } 541 } 542 543 func BenchmarkBaseMult(b *testing.B) { 544 b.ResetTimer() 545 p224 := P224() 546 e := p224BaseMultTests[25] 547 k, _ := new(big.Int).SetString(e.k, 10) 548 b.ReportAllocs() 549 b.StartTimer() 550 b.RunParallel(func(pb *testing.PB) { 551 for pb.Next() { 552 p224.ScalarBaseMult(k.Bytes()) 553 } 554 }) 555 } 556 557 func BenchmarkBaseMultP256(b *testing.B) { 558 b.ResetTimer() 559 p256 := P256() 560 e := p224BaseMultTests[25] 561 k, _ := new(big.Int).SetString(e.k, 10) 562 b.ReportAllocs() 563 b.StartTimer() 564 b.RunParallel(func(pb *testing.PB) { 565 for pb.Next() { 566 p256.ScalarBaseMult(k.Bytes()) 567 } 568 }) 569 } 570 571 func BenchmarkScalarMultP256(b *testing.B) { 572 b.ResetTimer() 573 p256 := P256() 574 _, x, y, _ := GenerateKey(p256, rand.Reader) 575 priv, _, _, _ := GenerateKey(p256, rand.Reader) 576 577 b.ReportAllocs() 578 b.StartTimer() 579 b.RunParallel(func(pb *testing.PB) { 580 for pb.Next() { 581 p256.ScalarMult(x, y, priv) 582 } 583 }) 584 } 585 586 func TestMarshal(t *testing.T) { 587 p224 := P224() 588 _, x, y, err := GenerateKey(p224, rand.Reader) 589 if err != nil { 590 t.Error(err) 591 return 592 } 593 serialized := Marshal(p224, x, y) 594 xx, yy := Unmarshal(p224, serialized) 595 if xx == nil { 596 t.Error("failed to unmarshal") 597 return 598 } 599 if xx.Cmp(x) != 0 || yy.Cmp(y) != 0 { 600 t.Error("unmarshal returned different values") 601 return 602 } 603 } 604 605 func TestP224Overflow(t *testing.T) { 606 // This tests for a specific bug in the P224 implementation. 607 p224 := P224() 608 pointData, _ := hex.DecodeString("049B535B45FB0A2072398A6831834624C7E32CCFD5A4B933BCEAF77F1DD945E08BBE5178F5EDF5E733388F196D2A631D2E075BB16CBFEEA15B") 609 x, y := Unmarshal(p224, pointData) 610 if !p224.IsOnCurve(x, y) { 611 t.Error("P224 failed to validate a correct point") 612 } 613 } 614 615 // See https://golang.org/issues/20482 616 func TestUnmarshalToLargeCoordinates(t *testing.T) { 617 curve := P256() 618 p := curve.Params().P 619 620 invalidX, invalidY := make([]byte, 65), make([]byte, 65) 621 invalidX[0], invalidY[0] = 4, 4 // uncompressed encoding 622 623 // Set x to be greater than curve's parameter P – specifically, to P+5. 624 // Set y to mod_sqrt(x^3 - 3x + B)) so that (x mod P = 5 , y) is on the 625 // curve. 626 x := new(big.Int).Add(p, big.NewInt(5)) 627 y, _ := new(big.Int).SetString("31468013646237722594854082025316614106172411895747863909393730389177298123724", 10) 628 629 copy(invalidX[1:], x.Bytes()) 630 copy(invalidX[33:], y.Bytes()) 631 632 if X, Y := Unmarshal(curve, invalidX); X != nil || Y != nil { 633 t.Errorf("Unmarshal accepts invalid X coordinate") 634 } 635 636 // This is a point on the curve with a small y value, small enough that we can add p and still be within 32 bytes. 637 x, _ = new(big.Int).SetString("31931927535157963707678568152204072984517581467226068221761862915403492091210", 10) 638 y, _ = new(big.Int).SetString("5208467867388784005506817585327037698770365050895731383201516607147", 10) 639 y.Add(y, p) 640 641 if p.Cmp(y) > 0 || y.BitLen() != 256 { 642 t.Fatal("y not within expected range") 643 } 644 645 // marshal 646 copy(invalidY[1:], x.Bytes()) 647 copy(invalidY[33:], y.Bytes()) 648 649 if X, Y := Unmarshal(curve, invalidY); X != nil || Y != nil { 650 t.Errorf("Unmarshal accepts invalid Y coordinate") 651 } 652 } 653 654 func TestMarshalCompressed(t *testing.T) { 655 t.Run("P-256/03", func(t *testing.T) { 656 data, _ := hex.DecodeString("031e3987d9f9ea9d7dd7155a56a86b2009e1e0ab332f962d10d8beb6406ab1ad79") 657 x, _ := new(big.Int).SetString("13671033352574878777044637384712060483119675368076128232297328793087057702265", 10) 658 y, _ := new(big.Int).SetString("66200849279091436748794323380043701364391950689352563629885086590854940586447", 10) 659 testMarshalCompressed(t, P256(), x, y, data) 660 }) 661 t.Run("P-256/02", func(t *testing.T) { 662 data, _ := hex.DecodeString("021e3987d9f9ea9d7dd7155a56a86b2009e1e0ab332f962d10d8beb6406ab1ad79") 663 x, _ := new(big.Int).SetString("13671033352574878777044637384712060483119675368076128232297328793087057702265", 10) 664 y, _ := new(big.Int).SetString("49591239931264812013903123569363872165694192725937750565648544718012157267504", 10) 665 testMarshalCompressed(t, P256(), x, y, data) 666 }) 667 668 t.Run("Invalid", func(t *testing.T) { 669 data, _ := hex.DecodeString("02fd4bf61763b46581fd9174d623516cf3c81edd40e29ffa2777fb6cb0ae3ce535") 670 X, Y := UnmarshalCompressed(P256(), data) 671 if X != nil || Y != nil { 672 t.Error("expected an error for invalid encoding") 673 } 674 }) 675 676 if testing.Short() { 677 t.Skip("skipping other curves on short test") 678 } 679 680 t.Run("P-224", func(t *testing.T) { 681 _, x, y, err := GenerateKey(P224(), rand.Reader) 682 if err != nil { 683 t.Fatal(err) 684 } 685 testMarshalCompressed(t, P224(), x, y, nil) 686 }) 687 t.Run("P-384", func(t *testing.T) { 688 _, x, y, err := GenerateKey(P384(), rand.Reader) 689 if err != nil { 690 t.Fatal(err) 691 } 692 testMarshalCompressed(t, P384(), x, y, nil) 693 }) 694 t.Run("P-521", func(t *testing.T) { 695 _, x, y, err := GenerateKey(P521(), rand.Reader) 696 if err != nil { 697 t.Fatal(err) 698 } 699 testMarshalCompressed(t, P521(), x, y, nil) 700 }) 701 } 702 703 func testMarshalCompressed(t *testing.T, curve Curve, x, y *big.Int, want []byte) { 704 if !curve.IsOnCurve(x, y) { 705 t.Fatal("invalid test point") 706 } 707 got := MarshalCompressed(curve, x, y) 708 if want != nil && !bytes.Equal(got, want) { 709 t.Errorf("got unexpected MarshalCompressed result: got %x, want %x", got, want) 710 } 711 712 X, Y := UnmarshalCompressed(curve, got) 713 if X == nil || Y == nil { 714 t.Fatalf("UnmarshalCompressed failed unexpectedly") 715 } 716 717 if !curve.IsOnCurve(X, Y) { 718 t.Error("UnmarshalCompressed returned a point not on the curve") 719 } 720 if X.Cmp(x) != 0 || Y.Cmp(y) != 0 { 721 t.Errorf("point did not round-trip correctly: got (%v, %v), want (%v, %v)", X, Y, x, y) 722 } 723 }