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 "crypto/rand" 9 "encoding/hex" 10 "fmt" 11 "math/big" 12 "testing" 13 ) 14 15 func TestOnCurve(t *testing.T) { 16 p224 := P224() 17 if !p224.IsOnCurve(p224.Params().Gx, p224.Params().Gy) { 18 t.Errorf("FAIL") 19 } 20 } 21 22 func TestOffCurve(t *testing.T) { 23 p224 := P224() 24 x, y := new(big.Int).SetInt64(1), new(big.Int).SetInt64(1) 25 if p224.IsOnCurve(x, y) { 26 t.Errorf("FAIL: point off curve is claimed to be on the curve") 27 } 28 b := Marshal(p224, x, y) 29 x1, y1 := Unmarshal(p224, b) 30 if x1 != nil || y1 != nil { 31 t.Errorf("FAIL: unmarshaling a point not on the curve succeeded") 32 } 33 } 34 35 type baseMultTest struct { 36 k string 37 x, y string 38 } 39 40 var p224BaseMultTests = []baseMultTest{ 41 { 42 "1", 43 "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 44 "bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", 45 }, 46 { 47 "2", 48 "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6", 49 "1c2b76a7bc25e7702a704fa986892849fca629487acf3709d2e4e8bb", 50 }, 51 { 52 "3", 53 "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04", 54 "a3f7f03cadd0be444c0aa56830130ddf77d317344e1af3591981a925", 55 }, 56 { 57 "4", 58 "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301", 59 "482580a0ec5bc47e88bc8c378632cd196cb3fa058a7114eb03054c9", 60 }, 61 { 62 "5", 63 "31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa", 64 "27e8bff1745635ec5ba0c9f1c2ede15414c6507d29ffe37e790a079b", 65 }, 66 { 67 "6", 68 "1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408", 69 "89faf0ccb750d99b553c574fad7ecfb0438586eb3952af5b4b153c7e", 70 }, 71 { 72 "7", 73 "db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28", 74 "f3a30085497f2f611ee2517b163ef8c53b715d18bb4e4808d02b963", 75 }, 76 { 77 "8", 78 "858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550", 79 "46dcd3ea5c43898c5c5fc4fdac7db39c2f02ebee4e3541d1e78047a", 80 }, 81 { 82 "9", 83 "2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d", 84 "371732e4f41bf4f7883035e6a79fcedc0e196eb07b48171697517463", 85 }, 86 { 87 "10", 88 "aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd", 89 "39bb30eab337e0a521b6cba1abe4b2b3a3e524c14a3fe3eb116b655f", 90 }, 91 { 92 "11", 93 "ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c", 94 "20b510004092e96636cfb7e32efded8265c266dfb754fa6d6491a6da", 95 }, 96 { 97 "12", 98 "6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a", 99 "207dddf0385bfdeab6e9acda8da06b3bbef224a93ab1e9e036109d13", 100 }, 101 { 102 "13", 103 "34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca", 104 "252819f71c7fb7fbcb159be337d37d3336d7feb963724fdfb0ecb767", 105 }, 106 { 107 "14", 108 "a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa", 109 "d5814cd724199c4a5b974a43685fbf5b8bac69459c9469bc8f23ccaf", 110 }, 111 { 112 "15", 113 "baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9", 114 "979a5f4759f80f4fb4ec2e34f5566d595680a11735e7b61046127989", 115 }, 116 { 117 "16", 118 "b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d", 119 "3399d464345906b11b00e363ef429221f2ec720d2f665d7dead5b482", 120 }, 121 { 122 "17", 123 "b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc", 124 "ff149efa6606a6bd20ef7d1b06bd92f6904639dce5174db6cc554a26", 125 }, 126 { 127 "18", 128 "c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc", 129 "ea98d60e5ffc9b8fcf999fab1df7e7ef7084f20ddb61bb045a6ce002", 130 }, 131 { 132 "19", 133 "a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c", 134 "dcf1f6c3db09c70acc25391d492fe25b4a180babd6cea356c04719cd", 135 }, 136 { 137 "20", 138 "fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455", 139 "d5d7110274cba7cdee90e1a8b0d394c376a5573db6be0bf2747f530", 140 }, 141 { 142 "112233445566778899", 143 "61f077c6f62ed802dad7c2f38f5c67f2cc453601e61bd076bb46179e", 144 "2272f9e9f5933e70388ee652513443b5e289dd135dcc0d0299b225e4", 145 }, 146 { 147 "112233445566778899112233445566778899", 148 "29895f0af496bfc62b6ef8d8a65c88c613949b03668aab4f0429e35", 149 "3ea6e53f9a841f2019ec24bde1a75677aa9b5902e61081c01064de93", 150 }, 151 { 152 "6950511619965839450988900688150712778015737983940691968051900319680", 153 "ab689930bcae4a4aa5f5cb085e823e8ae30fd365eb1da4aba9cf0379", 154 "3345a121bbd233548af0d210654eb40bab788a03666419be6fbd34e7", 155 }, 156 { 157 "13479972933410060327035789020509431695094902435494295338570602119423", 158 "bdb6a8817c1f89da1c2f3dd8e97feb4494f2ed302a4ce2bc7f5f4025", 159 "4c7020d57c00411889462d77a5438bb4e97d177700bf7243a07f1680", 160 }, 161 { 162 "13479971751745682581351455311314208093898607229429740618390390702079", 163 "d58b61aa41c32dd5eba462647dba75c5d67c83606c0af2bd928446a9", 164 "d24ba6a837be0460dd107ae77725696d211446c5609b4595976b16bd", 165 }, 166 { 167 "13479972931865328106486971546324465392952975980343228160962702868479", 168 "dc9fa77978a005510980e929a1485f63716df695d7a0c18bb518df03", 169 "ede2b016f2ddffc2a8c015b134928275ce09e5661b7ab14ce0d1d403", 170 }, 171 { 172 "11795773708834916026404142434151065506931607341523388140225443265536", 173 "499d8b2829cfb879c901f7d85d357045edab55028824d0f05ba279ba", 174 "bf929537b06e4015919639d94f57838fa33fc3d952598dcdbb44d638", 175 }, 176 { 177 "784254593043826236572847595991346435467177662189391577090", 178 "8246c999137186632c5f9eddf3b1b0e1764c5e8bd0e0d8a554b9cb77", 179 "e80ed8660bc1cb17ac7d845be40a7a022d3306f116ae9f81fea65947", 180 }, 181 { 182 "13479767645505654746623887797783387853576174193480695826442858012671", 183 "6670c20afcceaea672c97f75e2e9dd5c8460e54bb38538ebb4bd30eb", 184 "f280d8008d07a4caf54271f993527d46ff3ff46fd1190a3f1faa4f74", 185 }, 186 { 187 "205688069665150753842126177372015544874550518966168735589597183", 188 "eca934247425cfd949b795cb5ce1eff401550386e28d1a4c5a8eb", 189 "d4c01040dba19628931bc8855370317c722cbd9ca6156985f1c2e9ce", 190 }, 191 { 192 "13479966930919337728895168462090683249159702977113823384618282123295", 193 "ef353bf5c73cd551b96d596fbc9a67f16d61dd9fe56af19de1fba9cd", 194 "21771b9cdce3e8430c09b3838be70b48c21e15bc09ee1f2d7945b91f", 195 }, 196 { 197 "50210731791415612487756441341851895584393717453129007497216", 198 "4036052a3091eb481046ad3289c95d3ac905ca0023de2c03ecd451cf", 199 "d768165a38a2b96f812586a9d59d4136035d9c853a5bf2e1c86a4993", 200 }, 201 { 202 "26959946667150639794667015087019625940457807714424391721682722368041", 203 "fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455", 204 "f2a28eefd8b345832116f1e574f2c6b2c895aa8c24941f40d8b80ad1", 205 }, 206 { 207 "26959946667150639794667015087019625940457807714424391721682722368042", 208 "a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c", 209 "230e093c24f638f533dac6e2b6d01da3b5e7f45429315ca93fb8e634", 210 }, 211 { 212 "26959946667150639794667015087019625940457807714424391721682722368043", 213 "c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc", 214 "156729f1a003647030666054e208180f8f7b0df2249e44fba5931fff", 215 }, 216 { 217 "26959946667150639794667015087019625940457807714424391721682722368044", 218 "b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc", 219 "eb610599f95942df1082e4f9426d086fb9c6231ae8b24933aab5db", 220 }, 221 { 222 "26959946667150639794667015087019625940457807714424391721682722368045", 223 "b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d", 224 "cc662b9bcba6f94ee4ff1c9c10bd6ddd0d138df2d099a282152a4b7f", 225 }, 226 { 227 "26959946667150639794667015087019625940457807714424391721682722368046", 228 "baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9", 229 "6865a0b8a607f0b04b13d1cb0aa992a5a97f5ee8ca1849efb9ed8678", 230 }, 231 { 232 "26959946667150639794667015087019625940457807714424391721682722368047", 233 "a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa", 234 "2a7eb328dbe663b5a468b5bc97a040a3745396ba636b964370dc3352", 235 }, 236 { 237 "26959946667150639794667015087019625940457807714424391721682722368048", 238 "34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca", 239 "dad7e608e380480434ea641cc82c82cbc92801469c8db0204f13489a", 240 }, 241 { 242 "26959946667150639794667015087019625940457807714424391721682722368049", 243 "6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a", 244 "df82220fc7a4021549165325725f94c3410ddb56c54e161fc9ef62ee", 245 }, 246 { 247 "26959946667150639794667015087019625940457807714424391721682722368050", 248 "ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c", 249 "df4aefffbf6d1699c930481cd102127c9a3d992048ab05929b6e5927", 250 }, 251 { 252 "26959946667150639794667015087019625940457807714424391721682722368051", 253 "aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd", 254 "c644cf154cc81f5ade49345e541b4d4b5c1adb3eb5c01c14ee949aa2", 255 }, 256 { 257 "26959946667150639794667015087019625940457807714424391721682722368052", 258 "2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d", 259 "c8e8cd1b0be40b0877cfca1958603122f1e6914f84b7e8e968ae8b9e", 260 }, 261 { 262 "26959946667150639794667015087019625940457807714424391721682722368053", 263 "858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550", 264 "fb9232c15a3bc7673a3a03b0253824c53d0fd1411b1cabe2e187fb87", 265 }, 266 { 267 "26959946667150639794667015087019625940457807714424391721682722368054", 268 "db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28", 269 "f0c5cff7ab680d09ee11dae84e9c1072ac48ea2e744b1b7f72fd469e", 270 }, 271 { 272 "26959946667150639794667015087019625940457807714424391721682722368055", 273 "1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408", 274 "76050f3348af2664aac3a8b05281304ebc7a7914c6ad50a4b4eac383", 275 }, 276 { 277 "26959946667150639794667015087019625940457807714424391721682722368056", 278 "31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa", 279 "d817400e8ba9ca13a45f360e3d121eaaeb39af82d6001c8186f5f866", 280 }, 281 { 282 "26959946667150639794667015087019625940457807714424391721682722368057", 283 "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301", 284 "fb7da7f5f13a43b81774373c879cd32d6934c05fa758eeb14fcfab38", 285 }, 286 { 287 "26959946667150639794667015087019625940457807714424391721682722368058", 288 "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04", 289 "5c080fc3522f41bbb3f55a97cfecf21f882ce8cbb1e50ca6e67e56dc", 290 }, 291 { 292 "26959946667150639794667015087019625940457807714424391721682722368059", 293 "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6", 294 "e3d4895843da188fd58fb0567976d7b50359d6b78530c8f62d1b1746", 295 }, 296 { 297 "26959946667150639794667015087019625940457807714424391721682722368060", 298 "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 299 "42c89c774a08dc04b3dd201932bc8a5ea5f8b89bbb2a7e667aff81cd", 300 }, 301 } 302 303 func TestBaseMult(t *testing.T) { 304 p224 := P224() 305 for i, e := range p224BaseMultTests { 306 k, ok := new(big.Int).SetString(e.k, 10) 307 if !ok { 308 t.Errorf("%d: bad value for k: %s", i, e.k) 309 } 310 x, y := p224.ScalarBaseMult(k.Bytes()) 311 if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y { 312 t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y) 313 } 314 if testing.Short() && i > 5 { 315 break 316 } 317 } 318 } 319 320 func TestGenericBaseMult(t *testing.T) { 321 // We use the P224 CurveParams directly in order to test the generic implementation. 322 p224 := P224().Params() 323 for i, e := range p224BaseMultTests { 324 k, ok := new(big.Int).SetString(e.k, 10) 325 if !ok { 326 t.Errorf("%d: bad value for k: %s", i, e.k) 327 } 328 x, y := p224.ScalarBaseMult(k.Bytes()) 329 if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y { 330 t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y) 331 } 332 if testing.Short() && i > 5 { 333 break 334 } 335 } 336 } 337 338 func TestP256BaseMult(t *testing.T) { 339 p256 := P256() 340 p256Generic := p256.Params() 341 342 scalars := make([]*big.Int, 0, len(p224BaseMultTests)+1) 343 for _, e := range p224BaseMultTests { 344 k, _ := new(big.Int).SetString(e.k, 10) 345 scalars = append(scalars, k) 346 } 347 k := new(big.Int).SetInt64(1) 348 k.Lsh(k, 500) 349 scalars = append(scalars, k) 350 351 for i, k := range scalars { 352 x, y := p256.ScalarBaseMult(k.Bytes()) 353 x2, y2 := p256Generic.ScalarBaseMult(k.Bytes()) 354 if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 { 355 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, x, y, x2, y2) 356 } 357 358 if testing.Short() && i > 5 { 359 break 360 } 361 } 362 } 363 364 func TestP256Mult(t *testing.T) { 365 p256 := P256() 366 p256Generic := p256.Params() 367 368 for i, e := range p224BaseMultTests { 369 x, _ := new(big.Int).SetString(e.x, 16) 370 y, _ := new(big.Int).SetString(e.y, 16) 371 k, _ := new(big.Int).SetString(e.k, 10) 372 373 xx, yy := p256.ScalarMult(x, y, k.Bytes()) 374 xx2, yy2 := p256Generic.ScalarMult(x, y, k.Bytes()) 375 if xx.Cmp(xx2) != 0 || yy.Cmp(yy2) != 0 { 376 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, xx, yy, xx2, yy2) 377 } 378 if testing.Short() && i > 5 { 379 break 380 } 381 } 382 } 383 384 func TestInfinity(t *testing.T) { 385 tests := []struct { 386 name string 387 curve Curve 388 }{ 389 {"p224", P224()}, 390 {"p256", P256()}, 391 } 392 393 for _, test := range tests { 394 curve := test.curve 395 x, y := curve.ScalarBaseMult(nil) 396 if x.Sign() != 0 || y.Sign() != 0 { 397 t.Errorf("%s: x^0 != ", test.name) 398 } 399 x.SetInt64(0) 400 y.SetInt64(0) 401 402 x2, y2 := curve.Double(x, y) 403 if x2.Sign() != 0 || y2.Sign() != 0 { 404 t.Errorf("%s: 2 != ", test.name) 405 } 406 407 baseX := curve.Params().Gx 408 baseY := curve.Params().Gy 409 410 x3, y3 := curve.Add(baseX, baseY, x, y) 411 if x3.Cmp(baseX) != 0 || y3.Cmp(baseY) != 0 { 412 t.Errorf("%s: x+ != x", test.name) 413 } 414 415 x4, y4 := curve.Add(x, y, baseX, baseY) 416 if x4.Cmp(baseX) != 0 || y4.Cmp(baseY) != 0 { 417 t.Errorf("%s: +x != x", test.name) 418 } 419 } 420 } 421 422 func BenchmarkBaseMult(b *testing.B) { 423 b.ResetTimer() 424 p224 := P224() 425 e := p224BaseMultTests[25] 426 k, _ := new(big.Int).SetString(e.k, 10) 427 b.StartTimer() 428 for i := 0; i < b.N; i++ { 429 p224.ScalarBaseMult(k.Bytes()) 430 } 431 } 432 433 func BenchmarkBaseMultP256(b *testing.B) { 434 b.ResetTimer() 435 p256 := P256() 436 e := p224BaseMultTests[25] 437 k, _ := new(big.Int).SetString(e.k, 10) 438 b.StartTimer() 439 for i := 0; i < b.N; i++ { 440 p256.ScalarBaseMult(k.Bytes()) 441 } 442 } 443 444 func BenchmarkScalarMultP256(b *testing.B) { 445 b.ResetTimer() 446 p256 := P256() 447 _, x, y, _ := GenerateKey(p256, rand.Reader) 448 priv, _, _, _ := GenerateKey(p256, rand.Reader) 449 450 b.StartTimer() 451 for i := 0; i < b.N; i++ { 452 p256.ScalarMult(x, y, priv) 453 } 454 } 455 456 func TestMarshal(t *testing.T) { 457 p224 := P224() 458 _, x, y, err := GenerateKey(p224, rand.Reader) 459 if err != nil { 460 t.Error(err) 461 return 462 } 463 serialized := Marshal(p224, x, y) 464 xx, yy := Unmarshal(p224, serialized) 465 if xx == nil { 466 t.Error("failed to unmarshal") 467 return 468 } 469 if xx.Cmp(x) != 0 || yy.Cmp(y) != 0 { 470 t.Error("unmarshal returned different values") 471 return 472 } 473 } 474 475 func TestP224Overflow(t *testing.T) { 476 // This tests for a specific bug in the P224 implementation. 477 p224 := P224() 478 pointData, _ := hex.DecodeString("049B535B45FB0A2072398A6831834624C7E32CCFD5A4B933BCEAF77F1DD945E08BBE5178F5EDF5E733388F196D2A631D2E075BB16CBFEEA15B") 479 x, y := Unmarshal(p224, pointData) 480 if !p224.IsOnCurve(x, y) { 481 t.Error("P224 failed to validate a correct point") 482 } 483 } 484