Skip to content

Commit

Permalink
add tool to generate test cases for secp256r1
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jun 5, 2023
1 parent 92201d7 commit 97ced76
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
41 changes: 41 additions & 0 deletions op-tests/test-secp256r1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; This file was generated by tools/generate-secp256r1-tests.py

secp256r1_verify 0x028f0fc3f787816de614ab421b95042a27efe4383dad64eab149ef91e689067053 0x85932e4d075615be881398cc765f9f78204033f0ef5f832ac37e732f5f0cbda2 0xedbd7b15db0bcbca2b183de5a3000981d5e388267f0012f01d0ad9206fd655d5eb1f8279bf748fc1874e6ddcb3f685332d416ed3fbdfca79b7589364927c83e0 => 0 | 1000000
secp256r1_verify 0x028a090a5dc1762b5dc4a08dec57dda596b28b3c9f34e8c841a3c2525947c94afa 0x23a02777185712876787e32ba4b9800fa8dff0ad846e5769d9f5d57ecde10ab3 0xe752c9d67a3fe31358b612de117b5dc0bd75d94036a18e6f38f00505fc91cc752575c624f8b63b7a63e035172546bf36f9ab300d77703e84d39511c6e5efeb9e => 0 | 1000000
secp256r1_verify 0x02dff7e3c33ea23dc031a3656d456de34419f2c56beaaa303a1d75c732251ca640 0x222b25aef8c0c85aa429f3efc19027743bad43615a8dd4e84e435c55af352e7a 0xc53d4c2fa3c72ba78b3e877f3b3db5b6f24481d313d5b75dbea717c02d1487c24a387dd8e589bc899bd0a9c1bff56def39a90f6bc44550a15f0958884366199a => 0 | 1000000
secp256r1_verify 0x0349c178e99dbb70e7cdeaae0a3d25fdc4e99f5a2f731b1684209fe7b84ac8341f 0x87c4d4aec1bc8cd850199319dde457ecc47af37bff4fed285c0d8acc501ef1cc 0x6b5cab560d9a396e48c0b0ab6b47bf6680064eea692d519dac26ad6be65c727adcce7c5c6632dc0e16d0e1f09477259d80773dcd451b89e84a12b83e04672d4a => 0 | 1000000
secp256r1_verify 0x02faaf48b2a739dea541ea32155e9e60a6dae1d2fbe2ebf3e71d80fb3f006e70c5 0xd36eaf1c8f6f9b25a0008b44d49027818f421687187cd4a121e66f1cb8a67e0a 0xed83866e13ffd995355b5817a89434939fb6ba7b8859a7ee60974c51ff349b79ed5079cbbae39470c6afe63e1977344c1cc4b8038025dcc6f96f7ab06d18b191 => 0 | 1000000
secp256r1_verify 0x026a760a572c782292e775de9578315b61886e659a4a04ae169b57cf0410695387 0x147ec91f141bc05c902084c80649d64eef0bd2b5c32fdfc568a35436bea540e4 0xe6a6ad25d897e68e65e178fee0bc91ad35d4a1e7417becc784f79345205b836751122ca20f5c0399ff0595ca549b2ce40adb0c779aadeddbfd87bfdfb759a46d => 0 | 1000000
secp256r1_verify 0x02dde0450734841522baf92935e44ba9020a77d3c499e2074ca0bd372e3ccf1831 0x369215c4a473669292fed192a5ae1a9aa641afe10d3ec4d0b455cfad5791d462 0xad3e8b4dc6a1d20c67b98f7a5ecdf128df0590bd177e4593650c3df94057914e12dc858834b32e0bdf6a00aa5ab857f5a4ee0f608a14d51ecc8934fe7e812621 => 0 | 1000000
secp256r1_verify 0x02aecaaaa76d741373a810ac6259ba7fa17afb5c38d05dec9fbdcb1aff192f5153 0x08d5b309f5897d54cd3d1eb4ea9165af063d3aff27bd2756fbc64e844f38afbe 0xe5d336e316b135246ee657adbe3e17194cf83df4c2030fd996ef0acf9247b8e14795a84af27629a07f01e5f98a487a7497f65fcbf20d7c987348f3f530f13066 => 0 | 1000000
secp256r1_verify 0x02033b7d579e864c9568539d923658217c26d7af0a0d8fa2617f561f91cc3f58f4 0xaffd750fb25272c2e8670b1231f0acd61e0539eadb47cf23efa4fc4984fde036 0x8b486012e17a77d7bd72a846e9f1243edd59262b1f848e5fae7ac3caf0559edf77b4e9907083762dc5e8d846e99b3a18d5e2b95144676db5bbceadc83e0527d4 => 0 | 1000000
secp256r1_verify 0x02d1233717da48ecf69b43002e904d91e30ba2f952fe15ec61621fefcb1c467521 0x2c090ec4147adb7af6d2fcfeadb95bb7f0256e26a6cab4ef83aaf005491e7da3 0x9e38fe0cc7283855aa42e2856087af60bf563be85d6efb4590b71b269704a4ec9a7bce765866a0eb93723311b0598fa5e82450900f917f913160d44c36ea4111 => 0 | 1000000
secp256r1_verify 0x02bc34c899f417113605efa1f515620be5eea7f1bf2ec20d1a95ffbc0497a1a41b 0xdb104e9c6fe422bfad4efb4d60f6b69703f59fe139c1bccbb4ad8e26c59bbdfc 0xd952babdf6a0826375b9e876c06e27f711de35ee5f50dec232f2540433014b5fbd475201600ed9420ccd79662b42afe92b3b6907ea75cfc2758b49134bbb4194 => 0 | 1000000
secp256r1_verify 0x02a1c3300768b31101553d2ad6ec74ebcb3a2c7643fba74bbb40f9c27d37e2f9cf 0x41bd0a80ef7761bf998254cf56f1daf89460a66edc6b40689ed4a660b4d5ec0a 0x44104a6b47e62a508e3165c44056cbd90d7e132e479ecec7fb473c90b5f848426e88368812b291480000d7cf5e9df093bca213f4c039a08df142fe1e6272d226 => 0 | 1000000
secp256r1_verify 0x036116269ae91eb5599ef24ed7cc2132e7a34af61e2b020522034f8abc306ea508 0xdc3c455519f7d3206b8ee435c944b7de1f99fa7f5b645f6d79cec4658cd3f313 0xde85399c7d14194781f114c7ffa382ba8901212955c8a532c59a6aa337d8895ce772ee56670be8dc7d96188dec31a5ac7a96ed6d17e80a5e91038ecf3aebb181 => 0 | 1000000
secp256r1_verify 0x02210c7930c06185c1964c07161da345a541423b7ac5c3603d267692184b0d9850 0xc8f9a4a27bbc1af837fd415e6241285e4b95c0ce74600c81ded086ebcc74547f 0x37376d477c2bdccd0e1f111f787e716aaa047b9aab85430e77bca268db828da2096a5c18ac4aa671b9ff7b3cebcf572d7da92ae1a3038f669a21f2fe95633e84 => 0 | 1000000
secp256r1_verify 0x03be25350ce101d522f2ed7a4034c68daa19fc0bca968f2f04d37da99e4a814669 0xea89d66edf25352d44ca5b1670a0c46255d51ef20ca5d0649b1b6450c1df4984 0x4c6219bc2c64d39d92be8e8b100d69d50c5bcbcc09dae3e137a630abe1d0524d52c8e56441d909979a4d7077724f581924c3bb1ee912a260a85654da89ab9309 => 0 | 1000000
secp256r1_verify 0x0253f055918432f3483043d610e452ccff712b7f935ba737bd061c6d8a6ed61608 0xc32749548ab386ad3930f6f1dd2a85c5063563eeb99ef633ed1fd648aabb22a2 0x1bfa42fab513aed266ea9d6b4bb99287cd7b7563585a3306bea452b43efb4f0019a089f6e747893584b43ed69321ce3063807313db760e03e8a4008023139f12 => 0 | 1000000
secp256r1_verify 0x03558f48ddb055a79507e8f6b6ad074985a389342f42c50a69208df000162b81ee 0x728395357573930ab93762774ccd98edf66db24039c08e59f76d1bbaeefe6379 0xd8fe5bc11e0a12e6a93743100b09aae6bf8ac4408949581ccb343c396e027af5d938a7b8d8498a48b516fcf183b7d5b2c5144b1a9498bcbb9972f9d05271e0b2 => 0 | 1000000
secp256r1_verify 0x0340c8072cdacfb938eac3f214bec5960b6f0c88c8d3413df7f0bc8272aed271f4 0x4531ac1e89c712279be237a9da1f9eda8c4ed21a3112ef5c521d1cff849b4772 0x58d8cfb6c4287a31d2c3361fae4e9bdd07a907e5a0833328c4ed97fe603b66d5380b9e930056c17a8dadc367012fd702946103b02d94a27866595fb4bd9283a9 => 0 | 1000000
secp256r1_verify 0x0224b15ba2d1d7dcf45884193e4e1a8afdcc19a5c4e17625faca03720166b1ed53 0xb0124a8df2a9d2c52f1ff75b6fd0b26189f963fc0dbbff9bfa64ac3efdfa7efd 0xa3d691ea5ce4a72735c93f636a16502806be279af0f9732ccbad6c22b7973d98dc05cf5da33cb7a4528a203408bcb0f05f063079c3b9dd3cad5043f19336ede2 => 0 | 1000000
secp256r1_verify 0x022a66c28efef148c8168c2baad03754b7532841281a53c8deeb073eec489ae612 0xa4a82d915744b1646bec9d8de7c1261d2f8034594da88c07d5b644e12fb78fa8 0x76220dcc73bdfa1eb300bce9fd8987ba93263a122c9d6aaaacd07e7065b663b3c56dc926390cf12cbd7cb2962ef6579b95c679a3fdbd89e7691fed4a45399465 => 0 | 1000000
secp256r1_verify 0x02ae65c0b4e104accf2285b88e235e8bc64e7bc1d94c3acf3fe30c6a70a89d5682 0x0e6d6511259dd00c6894752091ae31c1e92eb594cec5bfce874ddeb1074c4a8e 0xb7b43e7668356e134407611452917f98c767decdb1e989f985345621d627b2db8c4e6caaae02becf41528d81efccd6e39dcf2ace0cf0cfedad14c646e3a00fbc => 0 | 1000000
secp256r1_verify 0x03b3f1313ec38d6625785ef0125bea6d8e39b61a4f6dd1ae1289118e9dd6e008b8 0x4742e3be54ab22db1ffcb63ac90a34384b1b2da07968d816556c9488c6342ff7 0x746c663be08e6f889c685c5fd258f68d74615d0c9b5f40100b016a64ad043d956d76631c3de0820e44fff3aea1dec8fae0b2d9af2d756bed86a6aa28e7d46e27 => 0 | 1000000
secp256r1_verify 0x030a5c7f61439a8eb04bef0d0224ba204a2c24deb854656f8ab0c7fc97271d201f 0x764930461dc2636cbad399be0ab3a0484f12b2289bc62c9c65b227d3d15d9331 0x2d9a085d11f5b050a37501355ab43fe9fc492e87e48d3ed8e5e60c0b7ae3929bc69cf7afb929aa452c8a6f129cd63d4ff65d43286054508534aec88ffaaa7857 => 0 | 1000000
secp256r1_verify 0x0218245aee291e7302d3b5b3fe8dc2137db05eb3eef033ccdface4d1f5a5730b05 0x25624809df04f74c21af8bd16b33c3922490caf0920fe7ce0890b1c12c2b1f7e 0xcb1fd41c1e46427a0a90774baec1fbfaf1fb2023bb9e91096b6fcd2bcd622856d45be4f523c59c4dc7c42e3860c0df677c38657243c9da0b8e1f12a2a6d3ee56 => 0 | 1000000
secp256r1_verify 0x035b13fd90a871e1a2dce438c9cc46b636b1da269445a47495ac0346ebfa5a7089 0x0374d1fc889af2526b24fc3e09068eff4403c28d482b240e3fac512482198f38 0x30cd7bfbc54de779047d373515332208791bea50968c530b34855fbe6175623b5606caaa25276bc006a44f48b07dc3212e3cfe336713ee05a25f59460f51a423 => 0 | 1000000
secp256r1_verify 0x032152e2ec96d34c8220a56f37e8e47aea3843f5e043baa10eaa9d30365d57e76d 0x44b54aed8cffdbf091cebf733b3b448da3ea3bdecf4c4245616031fe51cb22c4 0x5dcac0e821aa2a53843a705a2ccdc25de76f8d156f011a00da6d565cfbe2d562ec94c2b67aacd17739c5440620ec627a2dd186c634a37641b0ac3c1a75607f7a => 0 | 1000000
secp256r1_verify 0x03ec9eab63c4f55a6fff3148a50d42e371346ff86ad3b2f338408c50e28f003d83 0xc9eaff05fa43de1dea678859575344cc9497f8efbd7903ceb5ecec00525d9eae 0xcfe873b9b57f702a79e6d33f96f456e5ae5bc999a76f81860ea6672aabc4c615e56a1f53f58d85a2c9efcd5c4a5fdf83c83a1512b80e651733e037b02e33c98f => 0 | 1000000
secp256r1_verify 0x036e56521dfdc382ff42568a868ffdc81b944b252a1e96c60f5250495fe81b7593 0xd3b91c33942efacc55cb620e31dc020d3d3a585b44a7eee2c42623dad052cf5d 0xbbd362a283dad92154c842db24717f3d433bc715ed459bd91873c6e10ad7529a55f3c23849822780182db5ee753d7f92d13c98bd10886fbb81287d52cdcb8214 => 0 | 1000000
secp256r1_verify 0x033affbb5acad1e531ddfa720b4e3580c911fc837f2a9aa0656d5e68ad471cfee3 0x9243e5958a207cc63725f619124483b9fd99ca24a155089fd9e037f25c58acce 0x16df2b8670d73c714b76e09034704f160d92c20ebd8d93fd699b5f5787b4fbaa4f679a362063797c375a2e8099d4bef88f2249500a3416600c453545b379bb41 => 0 | 1000000
secp256r1_verify 0x0221a28a4c2598ce035e43b234ca8867ec803dc38320f4713ea65c01d6f2602b07 0x9940a483e428c608f1f9aa2f7816f6caa23f6044f1d37dff304f89a650ee734e 0xa95cecb2213fa45e050852634ceb11ce4980156948d972528d25b0283ae82f2c1b31d87270f133890aec33c8afa0b962baedeb0a3cc4e64e734d07743c35b4f8 => 0 | 1000000
secp256r1_verify 0x02d123b717da48ecf69b43002e904d91e30ba2f952fe15ec61621fefcb1c467521 0x935d863e2d28d8e5d399ea8af7393ef11fdffc7d862dcc6b5217a8ef15fb5442 0x75745c7aa2a9722777fe69e03520563e2b6b7df7bb8c197f8268eb7f0dbf3a3e04f3cd5a1e3d76060dc9a0c5cebdbf3aba85d6c0e2ea29b72be6d4da57c5ab26 => FAIL
secp256r1_verify 0x0221a28a4c2598ce035e43b234ca8867ec823dc38320f4713ea65c01d6f2602b07 0x0847bede50a2174a361c292ae89c6a7f2b511ea957d379e25cae1d4dbc3a9039 0x3d74ac622476ff2af23d0150fa167f6396fc8b4af33cf7bcd874d76e6c4e7b85b1582a3c5a96a4674c30a4667fdb4f60dec45ed174aa5d0ba73f220234dcd23a => FAIL
secp256r1_verify 0x02537055918432f3483043d610e452ccff712b7f935ba737bd061c6d8a6ed61608 0x4f2f997d37ef9978e517f61da2ead60b422ef1ff3b537e84ba9aa7a7afd9a3ee 0x9cfd1e7fa361c9a61d2ee9df1bf22c4026a6a1bc6bbf2aa0c2aafee87639796d5611c9b8d24d51cf5729cb0080ec7f02cdd3d0ef7b8b0377579a2fcd156c9ec2 => FAIL
secp256r1_verify 0x02a1c3300768b31101553d2ad6ec74ebcb3a2c7643fba74bbb40f9c27d37e2f9cf 0xc89e4fe7c56483c6d9a4fe8b5fdf983697db0b027a8daab81e908282e39f3401 0x8b4df071416b15e4bf948e64062391627465af919ce00a063a681329cd8dd5ff41bb0daeb8049e4d1d4158d798ed1b18208df2b64735aec70e119fc26b067e50 => FAIL
secp256r1_verify 0x03ec9eab63c4f55a6fff3148a50d42e371346ff86ad3b2f338408c50e28f003d83 0x7e06c2a825c043f6fd8400ddaed28cdb9a297aa8aa71d8ba004cf685e34cbbbe 0x7fa605aa30ee9e5ca0853addb1e6715b0295fc4c49964fe8e6e654b743877ac9133940a5cc668440d8b130eefbb75a51267f2ac6a656c86f04348b40382696bf => FAIL
secp256r1_verify 0x0221a28a4c2598ce035e43b234ca8867ec803dc38320f4713ea65c01d6f2602b07 0x63da4d356c8712477fa28d5d0bf2a5ae8da1b0fdbec3541c2b9e2e6ce9bf5548 0xfd20565ffe4bb9748519b8a09eb45bf9e6a19e910932098afbfca746c33ffe32fdcf1f762d344fddcf6cf830f144eef80f75fbeb6fca5a668b36e0de91c255c1 => FAIL
secp256r1_verify 0x02dff7e3c33ea23dc031a3656d456de34419f2c56beaaa303a1d75c732251ca640 0x5090b6b9f28182a3ccc1c967b22bf23493f6f31c35d05602b51335d9da70ac59 0x6b66298b1ae122ea606f2d95eaa262ffa94e2bd6734819b022fa417e55e9741f868f0328b30d54e6875ad6706b4185cf918827b3bad26a15ad4cdc40197052a4 => FAIL
secp256r1_verify 0x026a760a572c782292e775de9578315b61886e659a4a04ae169b57cf0410695387 0xbe9bed82fa8fce0fd4aaaff5aab93617b95a0778c08916ec01d77beb0e39bd10 0x905147eeb7a7e4e6ed7380bc0bf96c9449e17a03876ddf74381feecdeec47f5fc47b3e02885d0c3f022c7637f927412e05fb7e882442698727c98cdecc2a9f30 => FAIL
secp256r1_verify 0x02d1233717da48ecf69b43002e904d91e30ba2f952fe15ec61621fefcb1c467521 0xdac269ea2ee6aff43216dee49c8a7049c3124162c7ae9d5fc4b7dd7e5c776440 0x06370335413dda59d8b5d16a4d310234359864d8b0312ce8d4d124bd07f82786bfe41731d8f91b4f4d1df75d0f7ddf2a4d914eae04af49e971f14b3baa63b762 => FAIL
1 change: 1 addition & 0 deletions src/test_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
54 changes: 54 additions & 0 deletions tools/generate-secp256r1-tests.py
Original file line number Diff line number Diff line change
@@ -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 = 1000000
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")

0 comments on commit 97ced76

Please sign in to comment.