github.com/insolar/x-crypto@v0.0.0-20191031140942-75fab8a325f6/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 "encoding/hex" 9 "fmt" 10 "github.com/insolar/x-crypto/rand" 11 "math/big" 12 "testing" 13 ) 14 15 func TestOnCurve(t *testing.T) { 16 for _, curve := range [...]Curve{ 17 P224(), P256(), P384(), P521(), P256K(), 18 } { 19 curveParams := curve.Params() 20 t.Logf("Testing G is on curve %v", curveParams.Name) 21 if !curve.IsOnCurve(curveParams.Gx, curveParams.Gy) { 22 t.Errorf("FAIL") 23 } 24 if !curveParams.IsOnCurve(curveParams.Gx, curveParams.Gy) { 25 t.Errorf("FAIL") 26 } 27 } 28 } 29 30 func TestOffCurve(t *testing.T) { 31 p224 := P224() 32 x, y := new(big.Int).SetInt64(1), new(big.Int).SetInt64(1) 33 if p224.IsOnCurve(x, y) { 34 t.Errorf("FAIL: point off curve is claimed to be on the curve") 35 } 36 b := Marshal(p224, x, y) 37 x1, y1 := Unmarshal(p224, b) 38 if x1 != nil || y1 != nil { 39 t.Errorf("FAIL: unmarshaling a point not on the curve succeeded") 40 } 41 } 42 43 type baseMultTest struct { 44 k string 45 x, y string 46 } 47 48 var p224BaseMultTests = []baseMultTest{ 49 { 50 "1", 51 "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 52 "bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", 53 }, 54 { 55 "2", 56 "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6", 57 "1c2b76a7bc25e7702a704fa986892849fca629487acf3709d2e4e8bb", 58 }, 59 { 60 "3", 61 "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04", 62 "a3f7f03cadd0be444c0aa56830130ddf77d317344e1af3591981a925", 63 }, 64 { 65 "4", 66 "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301", 67 "482580a0ec5bc47e88bc8c378632cd196cb3fa058a7114eb03054c9", 68 }, 69 { 70 "5", 71 "31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa", 72 "27e8bff1745635ec5ba0c9f1c2ede15414c6507d29ffe37e790a079b", 73 }, 74 { 75 "6", 76 "1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408", 77 "89faf0ccb750d99b553c574fad7ecfb0438586eb3952af5b4b153c7e", 78 }, 79 { 80 "7", 81 "db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28", 82 "f3a30085497f2f611ee2517b163ef8c53b715d18bb4e4808d02b963", 83 }, 84 { 85 "8", 86 "858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550", 87 "46dcd3ea5c43898c5c5fc4fdac7db39c2f02ebee4e3541d1e78047a", 88 }, 89 { 90 "9", 91 "2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d", 92 "371732e4f41bf4f7883035e6a79fcedc0e196eb07b48171697517463", 93 }, 94 { 95 "10", 96 "aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd", 97 "39bb30eab337e0a521b6cba1abe4b2b3a3e524c14a3fe3eb116b655f", 98 }, 99 { 100 "11", 101 "ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c", 102 "20b510004092e96636cfb7e32efded8265c266dfb754fa6d6491a6da", 103 }, 104 { 105 "12", 106 "6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a", 107 "207dddf0385bfdeab6e9acda8da06b3bbef224a93ab1e9e036109d13", 108 }, 109 { 110 "13", 111 "34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca", 112 "252819f71c7fb7fbcb159be337d37d3336d7feb963724fdfb0ecb767", 113 }, 114 { 115 "14", 116 "a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa", 117 "d5814cd724199c4a5b974a43685fbf5b8bac69459c9469bc8f23ccaf", 118 }, 119 { 120 "15", 121 "baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9", 122 "979a5f4759f80f4fb4ec2e34f5566d595680a11735e7b61046127989", 123 }, 124 { 125 "16", 126 "b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d", 127 "3399d464345906b11b00e363ef429221f2ec720d2f665d7dead5b482", 128 }, 129 { 130 "17", 131 "b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc", 132 "ff149efa6606a6bd20ef7d1b06bd92f6904639dce5174db6cc554a26", 133 }, 134 { 135 "18", 136 "c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc", 137 "ea98d60e5ffc9b8fcf999fab1df7e7ef7084f20ddb61bb045a6ce002", 138 }, 139 { 140 "19", 141 "a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c", 142 "dcf1f6c3db09c70acc25391d492fe25b4a180babd6cea356c04719cd", 143 }, 144 { 145 "20", 146 "fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455", 147 "d5d7110274cba7cdee90e1a8b0d394c376a5573db6be0bf2747f530", 148 }, 149 { 150 "112233445566778899", 151 "61f077c6f62ed802dad7c2f38f5c67f2cc453601e61bd076bb46179e", 152 "2272f9e9f5933e70388ee652513443b5e289dd135dcc0d0299b225e4", 153 }, 154 { 155 "112233445566778899112233445566778899", 156 "29895f0af496bfc62b6ef8d8a65c88c613949b03668aab4f0429e35", 157 "3ea6e53f9a841f2019ec24bde1a75677aa9b5902e61081c01064de93", 158 }, 159 { 160 "6950511619965839450988900688150712778015737983940691968051900319680", 161 "ab689930bcae4a4aa5f5cb085e823e8ae30fd365eb1da4aba9cf0379", 162 "3345a121bbd233548af0d210654eb40bab788a03666419be6fbd34e7", 163 }, 164 { 165 "13479972933410060327035789020509431695094902435494295338570602119423", 166 "bdb6a8817c1f89da1c2f3dd8e97feb4494f2ed302a4ce2bc7f5f4025", 167 "4c7020d57c00411889462d77a5438bb4e97d177700bf7243a07f1680", 168 }, 169 { 170 "13479971751745682581351455311314208093898607229429740618390390702079", 171 "d58b61aa41c32dd5eba462647dba75c5d67c83606c0af2bd928446a9", 172 "d24ba6a837be0460dd107ae77725696d211446c5609b4595976b16bd", 173 }, 174 { 175 "13479972931865328106486971546324465392952975980343228160962702868479", 176 "dc9fa77978a005510980e929a1485f63716df695d7a0c18bb518df03", 177 "ede2b016f2ddffc2a8c015b134928275ce09e5661b7ab14ce0d1d403", 178 }, 179 { 180 "11795773708834916026404142434151065506931607341523388140225443265536", 181 "499d8b2829cfb879c901f7d85d357045edab55028824d0f05ba279ba", 182 "bf929537b06e4015919639d94f57838fa33fc3d952598dcdbb44d638", 183 }, 184 { 185 "784254593043826236572847595991346435467177662189391577090", 186 "8246c999137186632c5f9eddf3b1b0e1764c5e8bd0e0d8a554b9cb77", 187 "e80ed8660bc1cb17ac7d845be40a7a022d3306f116ae9f81fea65947", 188 }, 189 { 190 "13479767645505654746623887797783387853576174193480695826442858012671", 191 "6670c20afcceaea672c97f75e2e9dd5c8460e54bb38538ebb4bd30eb", 192 "f280d8008d07a4caf54271f993527d46ff3ff46fd1190a3f1faa4f74", 193 }, 194 { 195 "205688069665150753842126177372015544874550518966168735589597183", 196 "eca934247425cfd949b795cb5ce1eff401550386e28d1a4c5a8eb", 197 "d4c01040dba19628931bc8855370317c722cbd9ca6156985f1c2e9ce", 198 }, 199 { 200 "13479966930919337728895168462090683249159702977113823384618282123295", 201 "ef353bf5c73cd551b96d596fbc9a67f16d61dd9fe56af19de1fba9cd", 202 "21771b9cdce3e8430c09b3838be70b48c21e15bc09ee1f2d7945b91f", 203 }, 204 { 205 "50210731791415612487756441341851895584393717453129007497216", 206 "4036052a3091eb481046ad3289c95d3ac905ca0023de2c03ecd451cf", 207 "d768165a38a2b96f812586a9d59d4136035d9c853a5bf2e1c86a4993", 208 }, 209 { 210 "26959946667150639794667015087019625940457807714424391721682722368041", 211 "fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455", 212 "f2a28eefd8b345832116f1e574f2c6b2c895aa8c24941f40d8b80ad1", 213 }, 214 { 215 "26959946667150639794667015087019625940457807714424391721682722368042", 216 "a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c", 217 "230e093c24f638f533dac6e2b6d01da3b5e7f45429315ca93fb8e634", 218 }, 219 { 220 "26959946667150639794667015087019625940457807714424391721682722368043", 221 "c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc", 222 "156729f1a003647030666054e208180f8f7b0df2249e44fba5931fff", 223 }, 224 { 225 "26959946667150639794667015087019625940457807714424391721682722368044", 226 "b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc", 227 "eb610599f95942df1082e4f9426d086fb9c6231ae8b24933aab5db", 228 }, 229 { 230 "26959946667150639794667015087019625940457807714424391721682722368045", 231 "b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d", 232 "cc662b9bcba6f94ee4ff1c9c10bd6ddd0d138df2d099a282152a4b7f", 233 }, 234 { 235 "26959946667150639794667015087019625940457807714424391721682722368046", 236 "baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9", 237 "6865a0b8a607f0b04b13d1cb0aa992a5a97f5ee8ca1849efb9ed8678", 238 }, 239 { 240 "26959946667150639794667015087019625940457807714424391721682722368047", 241 "a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa", 242 "2a7eb328dbe663b5a468b5bc97a040a3745396ba636b964370dc3352", 243 }, 244 { 245 "26959946667150639794667015087019625940457807714424391721682722368048", 246 "34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca", 247 "dad7e608e380480434ea641cc82c82cbc92801469c8db0204f13489a", 248 }, 249 { 250 "26959946667150639794667015087019625940457807714424391721682722368049", 251 "6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a", 252 "df82220fc7a4021549165325725f94c3410ddb56c54e161fc9ef62ee", 253 }, 254 { 255 "26959946667150639794667015087019625940457807714424391721682722368050", 256 "ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c", 257 "df4aefffbf6d1699c930481cd102127c9a3d992048ab05929b6e5927", 258 }, 259 { 260 "26959946667150639794667015087019625940457807714424391721682722368051", 261 "aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd", 262 "c644cf154cc81f5ade49345e541b4d4b5c1adb3eb5c01c14ee949aa2", 263 }, 264 { 265 "26959946667150639794667015087019625940457807714424391721682722368052", 266 "2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d", 267 "c8e8cd1b0be40b0877cfca1958603122f1e6914f84b7e8e968ae8b9e", 268 }, 269 { 270 "26959946667150639794667015087019625940457807714424391721682722368053", 271 "858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550", 272 "fb9232c15a3bc7673a3a03b0253824c53d0fd1411b1cabe2e187fb87", 273 }, 274 { 275 "26959946667150639794667015087019625940457807714424391721682722368054", 276 "db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28", 277 "f0c5cff7ab680d09ee11dae84e9c1072ac48ea2e744b1b7f72fd469e", 278 }, 279 { 280 "26959946667150639794667015087019625940457807714424391721682722368055", 281 "1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408", 282 "76050f3348af2664aac3a8b05281304ebc7a7914c6ad50a4b4eac383", 283 }, 284 { 285 "26959946667150639794667015087019625940457807714424391721682722368056", 286 "31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa", 287 "d817400e8ba9ca13a45f360e3d121eaaeb39af82d6001c8186f5f866", 288 }, 289 { 290 "26959946667150639794667015087019625940457807714424391721682722368057", 291 "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301", 292 "fb7da7f5f13a43b81774373c879cd32d6934c05fa758eeb14fcfab38", 293 }, 294 { 295 "26959946667150639794667015087019625940457807714424391721682722368058", 296 "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04", 297 "5c080fc3522f41bbb3f55a97cfecf21f882ce8cbb1e50ca6e67e56dc", 298 }, 299 { 300 "26959946667150639794667015087019625940457807714424391721682722368059", 301 "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6", 302 "e3d4895843da188fd58fb0567976d7b50359d6b78530c8f62d1b1746", 303 }, 304 { 305 "26959946667150639794667015087019625940457807714424391721682722368060", 306 "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 307 "42c89c774a08dc04b3dd201932bc8a5ea5f8b89bbb2a7e667aff81cd", 308 }, 309 } 310 311 var secp256k1BaseMultTests = []baseMultTest{ 312 // Thanks Chuck Batson for the test vectors: 313 // https://chuckbatson.wordpress.com/2014/11/26/secp256k1-test-vectors/ 314 { 315 "1", 316 "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", 317 "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8", 318 }, 319 { 320 "2", 321 "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5", 322 "1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a", 323 }, 324 { 325 "3", 326 "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9", 327 "388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672", 328 }, 329 { 330 "4", 331 "e493dbf1c10d80f3581e4904930b1404cc6c13900ee0758474fa94abe8c4cd13", 332 "51ed993ea0d455b75642e2098ea51448d967ae33bfbdfe40cfe97bdc47739922", 333 }, 334 { 335 "5", 336 "2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4", 337 "d8ac222636e5e3d6d4dba9dda6c9c426f788271bab0d6840dca87d3aa6ac62d6", 338 }, 339 { 340 "6", 341 "fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556", 342 "ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297", 343 }, 344 { 345 "7", 346 "5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc", 347 "6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da", 348 }, 349 { 350 "8", 351 "2f01e5e15cca351daff3843fb70f3c2f0a1bdd05e5af888a67784ef3e10a2a01", 352 "5c4da8a741539949293d082a132d13b4c2e213d6ba5b7617b5da2cb76cbde904", 353 }, 354 { 355 "9", 356 "acd484e2f0c7f65309ad178a9f559abde09796974c57e714c35f110dfc27ccbe", 357 "cc338921b0a7d9fd64380971763b61e9add888a4375f8e0f05cc262ac64f9c37", 358 }, 359 { 360 "10", 361 "a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7", 362 "893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7", 363 }, 364 { 365 "11", 366 "774ae7f858a9411e5ef4246b70c65aac5649980be5c17891bbec17895da008cb", 367 "d984a032eb6b5e190243dd56d7b7b365372db1e2dff9d6a8301d74c9c953c61b", 368 }, 369 { 370 "12", 371 "d01115d548e7561b15c38f004d734633687cf4419620095bc5b0f47070afe85a", 372 "a9f34ffdc815e0d7a8b64537e17bd81579238c5dd9a86d526b051b13f4062327", 373 }, 374 { 375 "13", 376 "f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8", 377 "ab0902e8d880a89758212eb65cdaf473a1a06da521fa91f29b5cb52db03ed81", 378 }, 379 { 380 "14", 381 "499fdf9e895e719cfd64e67f07d38e3226aa7b63678949e6e49b241a60e823e4", 382 "cac2f6c4b54e855190f044e4a7b3d464464279c27a3f95bcc65f40d403a13f5b", 383 }, 384 { 385 "15", 386 "d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e", 387 "581e2872a86c72a683842ec228cc6defea40af2bd896d3a5c504dc9ff6a26b58", 388 }, 389 { 390 "16", 391 "e60fce93b59e9ec53011aabc21c23e97b2a31369b87a5ae9c44ee89e2a6dec0a", 392 "f7e3507399e595929db99f34f57937101296891e44d23f0be1f32cce69616821", 393 }, 394 { 395 "17", 396 "defdea4cdb677750a420fee807eacf21eb9898ae79b9768766e4faa04a2d4a34", 397 "4211ab0694635168e997b0ead2a93daeced1f4a04a95c0f6cfb199f69e56eb77", 398 }, 399 { 400 "18", 401 "5601570cb47f238d2b0286db4a990fa0f3ba28d1a319f5e7cf55c2a2444da7cc", 402 "c136c1dc0cbeb930e9e298043589351d81d8e0bc736ae2a1f5192e5e8b061d58", 403 }, 404 { 405 "19", 406 "2b4ea0a797a443d293ef5cff444f4979f06acfebd7e86d277475656138385b6c", 407 "85e89bc037945d93b343083b5a1c86131a01f60c50269763b570c854e5c09b7a", 408 }, 409 { 410 "20", 411 "4ce119c96e2fa357200b559b2f7dd5a5f02d5290aff74b03f3e471b273211c97", 412 "12ba26dcb10ec1625da61fa10a844c676162948271d96967450288ee9233dc3a", 413 }, 414 { 415 "112233445566778899", 416 "a90cc3d3f3e146daadfc74ca1372207cb4b725ae708cef713a98edd73d99ef29", 417 "5a79d6b289610c68bc3b47f3d72f9788a26a06868b4d8e433e1e2ad76fb7dc76", 418 }, 419 { 420 "112233445566778899112233445566778899", 421 "e5a2636bcfd412ebf36ec45b19bfb68a1bc5f8632e678132b885f7df99c5e9b3", 422 "736c1ce161ae27b405cafd2a7520370153c2c861ac51d6c1d5985d9606b45f39", 423 }, 424 { 425 "28948022309329048855892746252171976963209391069768726095651290785379540373584", 426 "a6b594b38fb3e77c6edf78161fade2041f4e09fd8497db776e546c41567feb3c", 427 "71444009192228730cd8237a490feba2afe3d27d7cc1136bc97e439d13330d55", 428 }, 429 { 430 "57896044618658097711785492504343953926418782139537452191302581570759080747168", 431 "3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63", 432 "3f3979bf72ae8202983dc989aec7f2ff2ed91bdd69ce02fc0700ca100e59ddf3", 433 }, 434 { 435 "86844066927987146567678238756515930889628173209306178286953872356138621120752", 436 "e24ce4beee294aa6350faa67512b99d388693ae4e7f53d19882a6ea169fc1ce1", 437 "8b71e83545fc2b5872589f99d948c03108d36797c4de363ebd3ff6a9e1a95b10", 438 }, 439 { 440 "115792089237316195423570985008687907852837564279074904382605163141518161494317", 441 "4ce119c96e2fa357200b559b2f7dd5a5f02d5290aff74b03f3e471b273211c97", 442 "ed45d9234ef13e9da259e05ef57bb3989e9d6b7d8e269698bafd77106dcc1ff5", 443 }, 444 { 445 "115792089237316195423570985008687907852837564279074904382605163141518161494318", 446 "2b4ea0a797a443d293ef5cff444f4979f06acfebd7e86d277475656138385b6c", 447 "7a17643fc86ba26c4cbcf7c4a5e379ece5fe09f3afd9689c4a8f37aa1a3f60b5", 448 }, 449 { 450 "115792089237316195423570985008687907852837564279074904382605163141518161494319", 451 "5601570cb47f238d2b0286db4a990fa0f3ba28d1a319f5e7cf55c2a2444da7cc", 452 "3ec93e23f34146cf161d67fbca76cae27e271f438c951d5e0ae6d1a074f9ded7", 453 }, 454 { 455 "115792089237316195423570985008687907852837564279074904382605163141518161494320", 456 "defdea4cdb677750a420fee807eacf21eb9898ae79b9768766e4faa04a2d4a34", 457 "bdee54f96b9cae9716684f152d56c251312e0b5fb56a3f09304e660861a910b8", 458 }, 459 { 460 "115792089237316195423570985008687907852837564279074904382605163141518161494321", 461 "e60fce93b59e9ec53011aabc21c23e97b2a31369b87a5ae9c44ee89e2a6dec0a", 462 "81caf8c661a6a6d624660cb0a86c8efed6976e1bb2dc0f41e0cd330969e940e", 463 }, 464 { 465 "115792089237316195423570985008687907852837564279074904382605163141518161494322", 466 "d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e", 467 "a7e1d78d57938d597c7bd13dd733921015bf50d427692c5a3afb235f095d90d7", 468 }, 469 { 470 "115792089237316195423570985008687907852837564279074904382605163141518161494323", 471 "499fdf9e895e719cfd64e67f07d38e3226aa7b63678949e6e49b241a60e823e4", 472 "353d093b4ab17aae6f0fbb1b584c2b9bb9bd863d85c06a4339a0bf2afc5ebcd4", 473 }, 474 { 475 "115792089237316195423570985008687907852837564279074904382605163141518161494324", 476 "f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8", 477 "f54f6fd17277f5768a7ded149a3250b8c5e5f925ade056e0d64a34ac24fc0eae", 478 }, 479 { 480 "115792089237316195423570985008687907852837564279074904382605163141518161494325", 481 "d01115d548e7561b15c38f004d734633687cf4419620095bc5b0f47070afe85a", 482 "560cb00237ea1f285749bac81e8427ea86dc73a2265792ad94fae4eb0bf9d908", 483 }, 484 { 485 "115792089237316195423570985008687907852837564279074904382605163141518161494326", 486 "774ae7f858a9411e5ef4246b70c65aac5649980be5c17891bbec17895da008cb", 487 "267b5fcd1494a1e6fdbc22a928484c9ac8d24e1d20062957cfe28b3536ac3614", 488 }, 489 { 490 "115792089237316195423570985008687907852837564279074904382605163141518161494327", 491 "a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7", 492 "76c545bdabe643d85c4938196c5db3969086b3d127885ea6c3411ac3fc8c9358", 493 }, 494 { 495 "115792089237316195423570985008687907852837564279074904382605163141518161494328", 496 "acd484e2f0c7f65309ad178a9f559abde09796974c57e714c35f110dfc27ccbe", 497 "33cc76de4f5826029bc7f68e89c49e165227775bc8a071f0fa33d9d439b05ff8", 498 }, 499 { 500 "115792089237316195423570985008687907852837564279074904382605163141518161494329", 501 "2f01e5e15cca351daff3843fb70f3c2f0a1bdd05e5af888a67784ef3e10a2a01", 502 "a3b25758beac66b6d6c2f7d5ecd2ec4b3d1dec2945a489e84a25d3479342132b", 503 }, 504 { 505 "115792089237316195423570985008687907852837564279074904382605163141518161494330", 506 "5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc", 507 "951435bf45daa69f5ce8729279e5ab2457ec2f47ec02184a5af7d9d6f78d9755", 508 }, 509 { 510 "115792089237316195423570985008687907852837564279074904382605163141518161494331", 511 "fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556", 512 "51ed8885530449df0c4169fe80ba3a9f217f0f09ae701b5fc378f3c84f8a0998", 513 }, 514 { 515 "115792089237316195423570985008687907852837564279074904382605163141518161494332", 516 "2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4", 517 "2753ddd9c91a1c292b24562259363bd90877d8e454f297bf235782c459539959", 518 }, 519 { 520 "115792089237316195423570985008687907852837564279074904382605163141518161494333", 521 "e493dbf1c10d80f3581e4904930b1404cc6c13900ee0758474fa94abe8c4cd13", 522 "ae1266c15f2baa48a9bd1df6715aebb7269851cc404201bf30168422b88c630d", 523 }, 524 { 525 "115792089237316195423570985008687907852837564279074904382605163141518161494334", 526 "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9", 527 "c77084f09cd217ebf01cc819d5c80ca99aff5666cb3ddce4934602897b4715bd", 528 }, 529 { 530 "115792089237316195423570985008687907852837564279074904382605163141518161494335", 531 "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5", 532 "e51e970159c23cc65c3a7be6b99315110809cd9acd992f1edc9bce55af301705", 533 }, 534 { 535 "115792089237316195423570985008687907852837564279074904382605163141518161494336", 536 "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", 537 "b7c52588d95c3b9aa25b0403f1eef75702e84bb7597aabe663b82f6f04ef2777", 538 }, 539 } 540 541 type scalarMultTest struct { 542 k string 543 xIn, yIn string 544 xOut, yOut string 545 } 546 547 var p256MultTests = []scalarMultTest{ 548 { 549 "2a265f8bcbdcaf94d58519141e578124cb40d64a501fba9c11847b28965bc737", 550 "023819813ac969847059028ea88a1f30dfbcde03fc791d3a252c6b41211882ea", 551 "f93e4ae433cc12cf2a43fc0ef26400c0e125508224cdb649380f25479148a4ad", 552 "4d4de80f1534850d261075997e3049321a0864082d24a917863366c0724f5ae3", 553 "a22d2b7f7818a3563e0f7a76c9bf0921ac55e06e2e4d11795b233824b1db8cc0", 554 }, 555 { 556 "313f72ff9fe811bf573176231b286a3bdb6f1b14e05c40146590727a71c3bccd", 557 "cc11887b2d66cbae8f4d306627192522932146b42f01d3c6f92bd5c8ba739b06", 558 "a2f08a029cd06b46183085bae9248b0ed15b70280c7ef13a457f5af382426031", 559 "831c3f6b5f762d2f461901577af41354ac5f228c2591f84f8a6e51e2e3f17991", 560 "93f90934cd0ef2c698cc471c60a93524e87ab31ca2412252337f364513e43684", 561 }, 562 } 563 564 func TestBaseMult(t *testing.T) { 565 type curveBaseMultTest struct { 566 curve Curve 567 cases []baseMultTest 568 } 569 for _, tt := range []curveBaseMultTest{ 570 {P224(), p224BaseMultTests}, 571 {P256K(), secp256k1BaseMultTests}, 572 } { 573 name := tt.curve.Params().Name 574 for i, e := range tt.cases { 575 k, ok := new(big.Int).SetString(e.k, 10) 576 if !ok { 577 t.Errorf("%s %d: bad value for k: %s", name, i, e.k) 578 } 579 x, y := tt.curve.ScalarBaseMult(k.Bytes()) 580 if !tt.curve.IsOnCurve(x, y) { 581 t.Errorf("%s %d: (%x, %x) not on curve", name, i, x, y) 582 } 583 if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y { 584 t.Errorf("%s %d: bad output for k=%s: got (%x, %x), want (%s, %s)", name, i, e.k, x, y, e.x, e.y) 585 } 586 if testing.Short() && i > 5 { 587 break 588 } 589 } 590 } 591 } 592 593 func TestGenericBaseMult(t *testing.T) { 594 // We use the P224 CurveParams directly in order to test the generic implementation. 595 p224 := P224().Params() 596 for i, e := range p224BaseMultTests { 597 k, ok := new(big.Int).SetString(e.k, 10) 598 if !ok { 599 t.Errorf("%d: bad value for k: %s", i, e.k) 600 } 601 x, y := p224.ScalarBaseMult(k.Bytes()) 602 if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y { 603 t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y) 604 } 605 if testing.Short() && i > 5 { 606 break 607 } 608 } 609 } 610 611 func TestP256BaseMult(t *testing.T) { 612 p256 := P256() 613 p256Generic := p256.Params() 614 615 scalars := make([]*big.Int, 0, len(p224BaseMultTests)+1) 616 for _, e := range p224BaseMultTests { 617 k, _ := new(big.Int).SetString(e.k, 10) 618 scalars = append(scalars, k) 619 } 620 k := new(big.Int).SetInt64(1) 621 k.Lsh(k, 500) 622 scalars = append(scalars, k) 623 624 for i, k := range scalars { 625 x, y := p256.ScalarBaseMult(k.Bytes()) 626 x2, y2 := p256Generic.ScalarBaseMult(k.Bytes()) 627 if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 { 628 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, x, y, x2, y2) 629 } 630 631 if testing.Short() && i > 5 { 632 break 633 } 634 } 635 } 636 637 func TestP256Mult(t *testing.T) { 638 p256 := P256() 639 p256Generic := p256.Params() 640 641 for i, e := range p224BaseMultTests { 642 x, _ := new(big.Int).SetString(e.x, 16) 643 y, _ := new(big.Int).SetString(e.y, 16) 644 k, _ := new(big.Int).SetString(e.k, 10) 645 646 xx, yy := p256.ScalarMult(x, y, k.Bytes()) 647 xx2, yy2 := p256Generic.ScalarMult(x, y, k.Bytes()) 648 if xx.Cmp(xx2) != 0 || yy.Cmp(yy2) != 0 { 649 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, xx, yy, xx2, yy2) 650 } 651 if testing.Short() && i > 5 { 652 break 653 } 654 } 655 656 for i, e := range p256MultTests { 657 x, _ := new(big.Int).SetString(e.xIn, 16) 658 y, _ := new(big.Int).SetString(e.yIn, 16) 659 k, _ := new(big.Int).SetString(e.k, 16) 660 expectedX, _ := new(big.Int).SetString(e.xOut, 16) 661 expectedY, _ := new(big.Int).SetString(e.yOut, 16) 662 663 xx, yy := p256.ScalarMult(x, y, k.Bytes()) 664 if xx.Cmp(expectedX) != 0 || yy.Cmp(expectedY) != 0 { 665 t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, xx, yy, expectedX, expectedY) 666 } 667 } 668 } 669 670 func TestInfinity(t *testing.T) { 671 tests := []struct { 672 name string 673 curve Curve 674 }{ 675 {"p224", P224()}, 676 {"p256", P256()}, 677 } 678 679 for _, test := range tests { 680 curve := test.curve 681 x, y := curve.ScalarBaseMult(nil) 682 if x.Sign() != 0 || y.Sign() != 0 { 683 t.Errorf("%s: x^0 != ∞", test.name) 684 } 685 x.SetInt64(0) 686 y.SetInt64(0) 687 688 x2, y2 := curve.Double(x, y) 689 if x2.Sign() != 0 || y2.Sign() != 0 { 690 t.Errorf("%s: 2∞ != ∞", test.name) 691 } 692 693 baseX := curve.Params().Gx 694 baseY := curve.Params().Gy 695 696 x3, y3 := curve.Add(baseX, baseY, x, y) 697 if x3.Cmp(baseX) != 0 || y3.Cmp(baseY) != 0 { 698 t.Errorf("%s: x+∞ != x", test.name) 699 } 700 701 x4, y4 := curve.Add(x, y, baseX, baseY) 702 if x4.Cmp(baseX) != 0 || y4.Cmp(baseY) != 0 { 703 t.Errorf("%s: ∞+x != x", test.name) 704 } 705 } 706 } 707 708 type synthCombinedMult struct { 709 Curve 710 } 711 712 func (s synthCombinedMult) CombinedMult(bigX, bigY *big.Int, baseScalar, scalar []byte) (x, y *big.Int) { 713 x1, y1 := s.ScalarBaseMult(baseScalar) 714 x2, y2 := s.ScalarMult(bigX, bigY, scalar) 715 return s.Add(x1, y1, x2, y2) 716 } 717 718 func TestCombinedMult(t *testing.T) { 719 type combinedMult interface { 720 Curve 721 CombinedMult(bigX, bigY *big.Int, baseScalar, scalar []byte) (x, y *big.Int) 722 } 723 724 p256, ok := P256().(combinedMult) 725 if !ok { 726 p256 = &synthCombinedMult{P256()} 727 } 728 729 gx := p256.Params().Gx 730 gy := p256.Params().Gy 731 732 zero := make([]byte, 32) 733 one := make([]byte, 32) 734 one[31] = 1 735 two := make([]byte, 32) 736 two[31] = 2 737 738 // 0×G + 0×G = ∞ 739 x, y := p256.CombinedMult(gx, gy, zero, zero) 740 if x.Sign() != 0 || y.Sign() != 0 { 741 t.Errorf("0×G + 0×G = (%d, %d), should be ∞", x, y) 742 } 743 744 // 1×G + 0×G = G 745 x, y = p256.CombinedMult(gx, gy, one, zero) 746 if x.Cmp(gx) != 0 || y.Cmp(gy) != 0 { 747 t.Errorf("1×G + 0×G = (%d, %d), should be (%d, %d)", x, y, gx, gy) 748 } 749 750 // 0×G + 1×G = G 751 x, y = p256.CombinedMult(gx, gy, zero, one) 752 if x.Cmp(gx) != 0 || y.Cmp(gy) != 0 { 753 t.Errorf("0×G + 1×G = (%d, %d), should be (%d, %d)", x, y, gx, gy) 754 } 755 756 // 1×G + 1×G = 2×G 757 x, y = p256.CombinedMult(gx, gy, one, one) 758 ggx, ggy := p256.ScalarBaseMult(two) 759 if x.Cmp(ggx) != 0 || y.Cmp(ggy) != 0 { 760 t.Errorf("1×G + 1×G = (%d, %d), should be (%d, %d)", x, y, ggx, ggy) 761 } 762 763 minusOne := new(big.Int).Sub(p256.Params().N, big.NewInt(1)) 764 // 1×G + (-1)×G = ∞ 765 x, y = p256.CombinedMult(gx, gy, one, minusOne.Bytes()) 766 if x.Sign() != 0 || y.Sign() != 0 { 767 t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y) 768 } 769 } 770 771 func BenchmarkBaseMult(b *testing.B) { 772 b.ResetTimer() 773 p224 := P224() 774 e := p224BaseMultTests[25] 775 k, _ := new(big.Int).SetString(e.k, 10) 776 b.ReportAllocs() 777 b.StartTimer() 778 b.RunParallel(func(pb *testing.PB) { 779 for pb.Next() { 780 p224.ScalarBaseMult(k.Bytes()) 781 } 782 }) 783 } 784 785 func BenchmarkBaseMultP256(b *testing.B) { 786 b.ResetTimer() 787 p256 := P256() 788 e := p224BaseMultTests[25] 789 k, _ := new(big.Int).SetString(e.k, 10) 790 b.ReportAllocs() 791 b.StartTimer() 792 b.RunParallel(func(pb *testing.PB) { 793 for pb.Next() { 794 p256.ScalarBaseMult(k.Bytes()) 795 } 796 }) 797 } 798 799 func BenchmarkScalarMultP256(b *testing.B) { 800 b.ResetTimer() 801 p256 := P256() 802 _, x, y, _ := GenerateKey(p256, rand.Reader) 803 priv, _, _, _ := GenerateKey(p256, rand.Reader) 804 805 b.ReportAllocs() 806 b.StartTimer() 807 b.RunParallel(func(pb *testing.PB) { 808 for pb.Next() { 809 p256.ScalarMult(x, y, priv) 810 } 811 }) 812 } 813 814 func TestMarshal(t *testing.T) { 815 p224 := P224() 816 _, x, y, err := GenerateKey(p224, rand.Reader) 817 if err != nil { 818 t.Error(err) 819 return 820 } 821 serialized := Marshal(p224, x, y) 822 xx, yy := Unmarshal(p224, serialized) 823 if xx == nil { 824 t.Error("failed to unmarshal") 825 return 826 } 827 if xx.Cmp(x) != 0 || yy.Cmp(y) != 0 { 828 t.Error("unmarshal returned different values") 829 return 830 } 831 } 832 833 func TestP224Overflow(t *testing.T) { 834 // This tests for a specific bug in the P224 implementation. 835 p224 := P224() 836 pointData, _ := hex.DecodeString("049B535B45FB0A2072398A6831834624C7E32CCFD5A4B933BCEAF77F1DD945E08BBE5178F5EDF5E733388F196D2A631D2E075BB16CBFEEA15B") 837 x, y := Unmarshal(p224, pointData) 838 if !p224.IsOnCurve(x, y) { 839 t.Error("P224 failed to validate a correct point") 840 } 841 } 842 843 // See https://golang.org/issues/20482 844 func TestUnmarshalToLargeCoordinates(t *testing.T) { 845 curve := P256() 846 p := curve.Params().P 847 848 invalidX, invalidY := make([]byte, 65), make([]byte, 65) 849 invalidX[0], invalidY[0] = 4, 4 // uncompressed encoding 850 851 // Set x to be greater than curve's parameter P – specifically, to P+5. 852 // Set y to mod_sqrt(x^3 - 3x + B)) so that (x mod P = 5 , y) is on the 853 // curve. 854 x := new(big.Int).Add(p, big.NewInt(5)) 855 y, _ := new(big.Int).SetString("31468013646237722594854082025316614106172411895747863909393730389177298123724", 10) 856 857 copy(invalidX[1:], x.Bytes()) 858 copy(invalidX[33:], y.Bytes()) 859 860 if X, Y := Unmarshal(curve, invalidX); X != nil || Y != nil { 861 t.Errorf("Unmarshal accepts invalid X coordinate") 862 } 863 864 // 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. 865 x, _ = new(big.Int).SetString("31931927535157963707678568152204072984517581467226068221761862915403492091210", 10) 866 y, _ = new(big.Int).SetString("5208467867388784005506817585327037698770365050895731383201516607147", 10) 867 y.Add(y, p) 868 869 if p.Cmp(y) > 0 || y.BitLen() != 256 { 870 t.Fatal("y not within expected range") 871 } 872 873 // marshal 874 copy(invalidY[1:], x.Bytes()) 875 copy(invalidY[33:], y.Bytes()) 876 877 if X, Y := Unmarshal(curve, invalidY); X != nil || Y != nil { 878 t.Errorf("Unmarshal accepts invalid Y coordinate") 879 } 880 }