diff --git a/op-tests/test-secp256r1.txt b/op-tests/test-secp256r1.txt new file mode 100644 index 00000000..0595c5be --- /dev/null +++ b/op-tests/test-secp256r1.txt @@ -0,0 +1,41 @@ +; This file was generated by tools/generate-secp256r1-tests.py + +secp256r1_verify 0x033e1a1b2ccbc35883c60fdfc3f4a02175096ade6271fe85517ca5772594bbd0dc 0x85932e4d075615be881398cc765f9f78204033f0ef5f832ac37e732f5f0cbda2 0xeae2f488080919bd0a7069c24cdd9c6ce2db423861b0c9d4236cdadbd0005f6d8f3709e6eb19249fd9c8bea664aba35218e67ea4b0f2239488dc3147f336e1e6 => 0 | 1850000 +secp256r1_verify 0x029b30ca509d72121608b1037a4f3463c3e1b581dc097ece14e4017ec301e8b78c 0x23a02777185712876787e32ba4b9800fa8dff0ad846e5769d9f5d57ecde10ab3 0x7437ffdf9c46f4e5f4940c5a6908dcd5289ed307b6d6495617186ef2539b9ced8c0791cfd4febee604c0d9986792e94ee87e03fe4e9bd3c14e797a17f5121836 => 0 | 1850000 +secp256r1_verify 0x033f70805937c836fe08be2a0e27673a1f2b876ab6e9d9cb61ddce151800da8114 0x222b25aef8c0c85aa429f3efc19027743bad43615a8dd4e84e435c55af352e7a 0xf47fdb819a73318cb0569e5851cb149de83cc4d94927c77fc67a8fd93e50535506a406538feb27b81ab0d64e8b1e4e47e6a7bab15625b1251ac3c2f4febc11df => 0 | 1850000 +secp256r1_verify 0x031280e44d9d901344faa2a8ef15550ee256a20a7946c0d94501733dcf8b1354e8 0x87c4d4aec1bc8cd850199319dde457ecc47af37bff4fed285c0d8acc501ef1cc 0x3c5387b557dadf80fdcacf599c4f83849406185de0a616ef34191cb1ca1ae8ee23e2e97c1869c69ed2b3c4d9f77e9154008326b31092361bbcba1238d881f633 => 0 | 1850000 +secp256r1_verify 0x03ce560844002d82f594451b62ef6a894fab39a7d12755940ddc9bb8dde5c42171 0xd36eaf1c8f6f9b25a0008b44d49027818f421687187cd4a121e66f1cb8a67e0a 0xbcf3580170255dba72dde806f0fb098e802438396534a6ba9d02787b8f73fe58ffc5417675864bff6b2e54856166d492c6f8bbc99a7b21dc9e64b2722b8c5b76 => 0 | 1850000 +secp256r1_verify 0x03034c53c812f939cc40b4a06632eefd2ec12fc8dbd0ea915e64913e62f2ba4944 0x147ec91f141bc05c902084c80649d64eef0bd2b5c32fdfc568a35436bea540e4 0x164d80346288fbd99d24089c975c42a4e48bfb770af603df1c7a18d4b75d7b2e3e080fa5f77b74540e677cf1cbf125925610f6a24cd8a4526f0ff3adda5f16b4 => 0 | 1850000 +secp256r1_verify 0x03a68f5523ebd25001e2a93ac2217ee948ba957d997494585c0306c53e37319d38 0x369215c4a473669292fed192a5ae1a9aa641afe10d3ec4d0b455cfad5791d462 0x30e714daf4ca2d580aa3ef78e1353bca814bb00b22e7aff0369fed9d5b56f63ab553299e9eca4975b41e47fbe443303a7ded4937fe8c13329b484b713fab794a => 0 | 1850000 +secp256r1_verify 0x03436408d48234dfc3d070cc305c98082a095b0f86435506723cadc8fbb40d6c79 0x08d5b309f5897d54cd3d1eb4ea9165af063d3aff27bd2756fbc64e844f38afbe 0xe2ef64baf7108bf7be3f9a0b74c6120218d9dfecd078aee1371d083e28e6333db51c4ac6f72d4e04f1a200e42a3fbc1e66bf86110739c702e2b3aa0b199565eb => 0 | 1850000 +secp256r1_verify 0x03bb6f94c6b5f8839d435ccdabeb5ac958075eb073b808326f1ab914b44e5acc28 0xaffd750fb25272c2e8670b1231f0acd61e0539eadb47cf23efa4fc4984fde036 0xfdfd0b16bbc1e241f883a901da3ba51c67cd1eef87030c9407c36cf20683e3168e4babdd718a6fb95a260bd50ca2f046b9979a20cc82b6bd453e9f717185ff4b => 0 | 1850000 +secp256r1_verify 0x0251955b74b1902d53758a62ccd9dd01837aa5ae755e878eb0aeccbe8fe477c543 0x2c090ec4147adb7af6d2fcfeadb95bb7f0256e26a6cab4ef83aaf005491e7da3 0x47f678f2f311de625bbd8da795f462420c58475d899122e616014b40c4f518c6d07467f0fc761a7bf2ee33783042cf4467f44391caf488cade45dd50238e3326 => 0 | 1850000 +secp256r1_verify 0x028125b54a52c3c47fdc7bdaac3258f0eaf8b79aafceca7b50dc3446663f1cb3da 0xdb104e9c6fe422bfad4efb4d60f6b69703f59fe139c1bccbb4ad8e26c59bbdfc 0xd15601d49ef4839488d64c2dbfe146785a256d369370002d85497df724c2d5d98210c8f2c5327e68badc709366e5e2ec66d220351856f3f421cd40a7c7831d7a => 0 | 1850000 +secp256r1_verify 0x025cdba10ee6b29e6d93e5818234f2638b3825a2565c19c7f40f426e3678d99bb0 0x41bd0a80ef7761bf998254cf56f1daf89460a66edc6b40689ed4a660b4d5ec0a 0x55eb523d44ce270a6c182adabad061197718a52fba073fe6f00ca905e1030e417a3344251c89cb6a94c5aed62a6e5b0a2c709f76bc006cb72a7560a029028cd7 => 0 | 1850000 +secp256r1_verify 0x03be78eb8719db611c4babe088340980df5a68ce67f214541d470de814b2610fb5 0xdc3c455519f7d3206b8ee435c944b7de1f99fa7f5b645f6d79cec4658cd3f313 0xfda9946c934065bfabfaf202ccf7849092e174c54c9e5b4b5b586647b9bde2ed61f759b479973daf34214300045ab8f3091cd76deca9e043bb2343e4ec06b7e8 => 0 | 1850000 +secp256r1_verify 0x0230e567e48f235751d617576f61dd6fa3c715b40d82a058c253fa302d85eed434 0xc8f9a4a27bbc1af837fd415e6241285e4b95c0ce74600c81ded086ebcc74547f 0xd9462aedbde5cd98f3f65c96a8d4f8102b8f02f0518987be2a7e45054f569eb9f6f6a0da0676257b2ec96c89d508074823f2c92f143be18360481036fdb4d96a => 0 | 1850000 +secp256r1_verify 0x02fc40cefe32fedad137e03a3787bfbac38a06d07feb83fbc714d90795c9b602f8 0xea89d66edf25352d44ca5b1670a0c46255d51ef20ca5d0649b1b6450c1df4984 0x65488caadcf423a4c993be9bec5626be1e55ebb82bf16f939b21e9c413e66b29a73f3d932d1f7fb95680750c0173cb1dfbb1eb7fc7a94cc74f3db9ef83d318d6 => 0 | 1850000 +secp256r1_verify 0x030230d48c08ef26c3a77a3f89f19d38ecff2707dd554b0511b70a82373715a276 0xc32749548ab386ad3930f6f1dd2a85c5063563eeb99ef633ed1fd648aabb22a2 0xf849ca7d62d79637422d2aaa9d44bd4f1a143fd41ee0a752c99690257bc092f60ab13ede71efe39b4376f64c82d46a7f1115b21966eb955a705535acae800116 => 0 | 1850000 +secp256r1_verify 0x036ba08725bb0ef7ad8b60fe5ed86e86738d38e7d63a948ac85ffb507ed79a54bf 0x728395357573930ab93762774ccd98edf66db24039c08e59f76d1bbaeefe6379 0xa55a213bfadd87fad9993cb3adc3e03e5973cdf7aa768e215d36801b70d306a950fd58cfcc828057d3bd62d88825bd4610a175bacdf0fafa9880c8e8a46062ab => 0 | 1850000 +secp256r1_verify 0x026e4dbb9f416cbda2e948237f284112defba53bc5becae8bb47bd977e0a762a8f 0x4531ac1e89c712279be237a9da1f9eda8c4ed21a3112ef5c521d1cff849b4772 0xa555bdda6941242959bb418d78a572ca7d8df2cfd9a13af0f1cc2700eeb7eca968285a42adfa34fee61222451016e5f04038f74db47d4781705d39289cae705f => 0 | 1850000 +secp256r1_verify 0x035fb09d4f5ae49b8d76ed8face217c0b9992b832f5a97d9a6700f2dbf68e14eaf 0xb0124a8df2a9d2c52f1ff75b6fd0b26189f963fc0dbbff9bfa64ac3efdfa7efd 0x32e2f70af60d134e3a388de3e63ea68187ec8fb68f403598e4713bf5400b1f4a78b171758cdb5112ad1d9d44598318e6045460b54076d076716d5e3ea1c97b26 => 0 | 1850000 +secp256r1_verify 0x0211e9b729e9c8edd603407d7654801baf6048ea2642b5f98e5b6fc807999cd50f 0xa4a82d915744b1646bec9d8de7c1261d2f8034594da88c07d5b644e12fb78fa8 0x1c8e71b1b73ed8f396d1d28598dc190898f8677f41b2c3ab4ec3fe7ce13293e7378859f4b9bc4408e401a02515adddd20087b3003cd0fb0e34f0dca5e17303c3 => 0 | 1850000 +secp256r1_verify 0x0201f9a0fe217fa682d767e5dd6cc21185bd0dc1ecc76cd975a9551a38a0ab0916 0x0e6d6511259dd00c6894752091ae31c1e92eb594cec5bfce874ddeb1074c4a8e 0xf8af3cf6120f867506ee3e963a3854e27ba2870e68a3d5fe49929778fc8824fe03b57e3b49f05415c54838f45c8012a7956d2c7690b23798c86745514e8e55d7 => 0 | 1850000 +secp256r1_verify 0x03da1ea461a9ffdc83bcc260d1d697f79a46ef8d08a81aa28440fce281596c187c 0x4742e3be54ab22db1ffcb63ac90a34384b1b2da07968d816556c9488c6342ff7 0xc370c3b0d62534af7ad422156b6fdf1f9fc1a19a6b81b8c4abff316783f98ee2849aea2e0a53955e6699628716eae6c67fb6eef97f7050f07a4fa82ccb8f5d8e => 0 | 1850000 +secp256r1_verify 0x038cd87b69559f61cac5d3ff08827ec04d5abb99d00ed312bf82f1ec38817d05fb 0x764930461dc2636cbad399be0ab3a0484f12b2289bc62c9c65b227d3d15d9331 0xd8e89c5222e8c12edea7b5780aac8943fb84e03108ba9bcbbc1da98d8f1239af0b31493df9c0538252c65ced6ded82f661ba39463335f5aa39db0d01386dc6ae => 0 | 1850000 +secp256r1_verify 0x02901b96a87aa70acf6d7f1284abf1440d5fc342df0430f1fde5b160424c0a3b6b 0x25624809df04f74c21af8bd16b33c3922490caf0920fe7ce0890b1c12c2b1f7e 0xb38cb72138b2f04941421527ef4e3d9c5309ddf1a7918fd76f43ff12661f93f63b1a8be8e95479eaf42b24076dd0d8cb0ef072bedca3add74da1d616a6cb0022 => 0 | 1850000 +secp256r1_verify 0x023977390275ccc9191973f7b5600ba55d68fe5e50f664dd6a63d09ceeb92210cb 0x0374d1fc889af2526b24fc3e09068eff4403c28d482b240e3fac512482198f38 0x40b2485638057b128e9b08ffc48cc28ad546b564730061c1fd6a7d6550af10da1b8c3b8457c6dcc23fc3f9d98717da523bf3f884f00d2c0dfd9a6fb934cf40a3 => 0 | 1850000 +secp256r1_verify 0x03862a98fbe6da4c6f89c8e0801497d7ba0d9207d485f3d15495e057fb823b2773 0x44b54aed8cffdbf091cebf733b3b448da3ea3bdecf4c4245616031fe51cb22c4 0xc987fae255f067392250d5f22e20b3037356941a314701fa9e715cae5bbcbe742d26c21c58d25aaf63e50b25064a07d7b0b6a2f55e6a244279726bfe9c351dbb => 0 | 1850000 +secp256r1_verify 0x036f6fb1f8534da1f12b2ecbcb2a7baaf9d17109c1026bac25b83ce7aedabdcccf 0xc9eaff05fa43de1dea678859575344cc9497f8efbd7903ceb5ecec00525d9eae 0x147722139c94474e5b76b186f9859a6eb381177603ae7d2bfed6f379b13ec090f737db3a235d41ed820b13bbfaef46c6087c5f561b3a0067b51d5dd1ed1a0ea7 => 0 | 1850000 +secp256r1_verify 0x031cbbd349fb0faaad9fc3b4259ccfb190cc16e3ab88121077fdf544b61c2aca79 0xd3b91c33942efacc55cb620e31dc020d3d3a585b44a7eee2c42623dad052cf5d 0x34d058e8e638640d092e6879585991b46f280b3bf48892dc31ed4b8c5a5b3c5d5eabfa1c2826e40a2c06e32ae85485a8ca4d1fc1eeabb914a31d6a156f878ffe => 0 | 1850000 +secp256r1_verify 0x02bce812e6002b59404b6bc9222d75ae473ff50516c09a89666e2e75b794162141 0x9243e5958a207cc63725f619124483b9fd99ca24a155089fd9e037f25c58acce 0x896fe5cc79da0d0c902bbae38fcd9f88d875ac0906bf0313bee1fe0ae9a5da039ada3ab27ec89e969c659d8d64bb52591c7a211ff612432fedb419ce4beb1e7b => 0 | 1850000 +secp256r1_verify 0x0377260529fa20d810ca40425a87d89e6d774d839960bc2b440e51fd36de8cb2a6 0x9940a483e428c608f1f9aa2f7816f6caa23f6044f1d37dff304f89a650ee734e 0x35d786243a01011d0da816b5ae421e6f434bf64d4c8ac5df66f43716b8fe39260ca4597257f0239ee8a346366925591261d32b3c882dc88bf13c034050bbb25f => 0 | 1850000 +secp256r1_verify 0x025195db74b1902d53758a62ccd9dd01837aa5ae755e878eb0aeccbe8fe477c543 0x935d863e2d28d8e5d399ea8af7393ef11fdffc7d862dcc6b5217a8ef15fb5442 0xecef274a7408e6cb0196eac64d2ae32fc54c2537f8a9efd5b75a4e8a53b0b156c64564306f38bade4adceac1073d464e4db3d0332141a7203dfd113ad36e393d => FAIL +secp256r1_verify 0x0377260529fa20d810ca40425a87d89e6d754d839960bc2b440e51fd36de8cb2a6 0x0847bede50a2174a361c292ae89c6a7f2b511ea957d379e25cae1d4dbc3a9039 0x2cb24e63e259058d16ad660fe400a05d0bfceeb8971c2dd20ab84071eb3b8877618ce130b786918b64ce7e89e13c69b641d77900bef4f2d06cd5eaaada0a6437 => FAIL +secp256r1_verify 0x0302b0d48c08ef26c3a77a3f89f19d38ecff2707dd554b0511b70a82373715a276 0x4f2f997d37ef9978e517f61da2ead60b422ef1ff3b537e84ba9aa7a7afd9a3ee 0x8e4058aa3a36ed1d25823efe9cadfc5c038ac75a2299f845616cf05050254fd100c5449153d39d7fa17e1f7962b50b8cf0be8209d68af84a9f693790fc8cb415 => FAIL +secp256r1_verify 0x025cdba10ee6b29e6d93e5818234f2638b3825a2565c19c7f40f426e3678d99bb0 0xc89e4fe7c56483c6d9a4fe8b5fdf983697db0b027a8daab81e908282e39f3401 0x3d4f7014e4c98798df57e25a3c478c5a94a7d6149288ea9d4e3e325863dc0f02c038527b9ff1bc38212504a5ed6dfb3f861cfa3c3f88ff8b0abb7a5303937ab1 => FAIL +secp256r1_verify 0x036f6fb1f8534da1f12b2ecbcb2a7baaf9d17109c1026bac25b83ce7aedabdcccf 0x7e06c2a825c043f6fd8400ddaed28cdb9a297aa8aa71d8ba004cf685e34cbbbe 0xe0a1fe37e945b58e490a46fbabdbcb3d0412b03fc7e26f08dc324e6a9a29a68119145fa590f29f0f963b98493371d48755b4b695775d32334b48cfd069a84b71 => FAIL +secp256r1_verify 0x0377260529fa20d810ca40425a87d89e6d774d839960bc2b440e51fd36de8cb2a6 0x63da4d356c8712477fa28d5d0bf2a5ae8da1b0fdbec3541c2b9e2e6ce9bf5548 0x43515529be3fb0c34ea9db017d33a907012f2645986a34e2948b7edc0269ca3fcf3bd693f0269899b787b9782f5e20aede52ee14e8ac471a6d7866fe89b1b731 => FAIL +secp256r1_verify 0x033f70805937c836fe08be2a0e27673a1f2b876ab6e9d9cb61ddce151800da8114 0x5090b6b9f28182a3ccc1c967b22bf23493f6f31c35d05602b51335d9da70ac59 0x4b08ebfa30794c5a092d47d0d57eb57ae5e6efedf82fa660cc530ebc45adb90bee7451fc951d80fb92cdec1ce02abd8ceea0aea2e5c34b29a7f98d6c369353f9 => FAIL +secp256r1_verify 0x03034c53c812f939cc40b4a06632eefd2ec12fc8dbd0ea915e64913e62f2ba4944 0xbe9bed82fa8fce0fd4aaaff5aab93617b95a0778c08916ec01d77beb0e39bd10 0x14020caf0e9f56f8427d8e879a798b995ede2cfe672db6e90521b8884d8a46447ee49e571238fb068f0a18f6c7bdacd1e626a8d678a37ac6866fded64f90fffa => FAIL +secp256r1_verify 0x0251955b74b1902d53758a62ccd9dd01837aa5ae755e878eb0aeccbe8fe477c543 0xdac269ea2ee6aff43216dee49c8a7049c3124162c7ae9d5fc4b7dd7e5c776440 0x3802b16f2401082ba9e526eccc0b3a90332755579012683e1ddd1348129d065f7fdd7888af4091b50a35bec40997b389877af264fdf8d2c2997868ae3bcde793 => FAIL diff --git a/src/test_ops.rs b/src/test_ops.rs index f2587258..5e989f59 100644 --- a/src/test_ops.rs +++ b/src/test_ops.rs @@ -232,6 +232,7 @@ use rstest::rstest; #[case("test-blspy-verify")] #[case("test-secp-verify")] #[case("test-secp256k1")] +#[case("test-secp256r1")] fn test_ops(#[case] filename: &str) { use std::fs::read_to_string; diff --git a/tools/generate-secp256r1-tests.py b/tools/generate-secp256r1-tests.py new file mode 100644 index 00000000..70c497e7 --- /dev/null +++ b/tools/generate-secp256r1-tests.py @@ -0,0 +1,54 @@ +from ecdsa import SigningKey, NIST256p +from hashlib import sha256 +from random import randbytes, randint, seed, sample + +def flip_bit(b: bytes) -> bytearray: + idx = randint(0, len(b) - 1) + bit = 1 << randint(0, 7) + ret = bytearray(b) + ret[idx] ^= bit + return ret + +def print_validation_test_case(f, num_cases, filter_pk, filter_msg, filter_sig, expect: str): + sks = sample(secret_keys, num_cases) + cost = 1850000 + sigs = [] + + args = "" + for sk in sks: + pk = sk.verifying_key + msg = randbytes(randint(3,40)) + sig = sk.sign_deterministic(msg) + sha = sha256() + sha.update(msg) + f.write(f"secp256r1_verify 0x{bytes(filter_pk(pk.to_string('compressed'))).hex()} 0x{filter_msg(sha.digest()).hex()} 0x{bytes(filter_sig(sig)).hex()}") + + f.write(f" => {expect}") + if expect != "FAIL": + f.write(f" | {cost}") + f.write("\n") + + +seed(1337) + +SIZE = 30 + +# generate a bunch of keys +secret_keys = [] +for i in range(SIZE): + secret_keys.append(SigningKey.generate(curve=NIST256p, hashfunc=sha256)) + + +with open("../op-tests/test-secp256r1.txt", "w+") as f: + f.write("; This file was generated by tools/generate-secp256r1-tests.py\n\n") + + print_validation_test_case(f, SIZE, lambda pk: pk, lambda msg: msg, lambda sig: sig, "0") + + # negative tests (alter public key) + print_validation_test_case(f, 3, flip_bit, lambda msg: msg, lambda sig: sig, "FAIL") + + # negative tests (alter message) + print_validation_test_case(f, 3, lambda pk: pk, flip_bit, lambda sig: sig, "FAIL") + + # negative tests (alter signature) + print_validation_test_case(f, 3, lambda pk: pk, lambda msg: msg, flip_bit, "FAIL")