Skip to content

Commit

Permalink
add tool to generate test cases for secp256k1
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jun 8, 2023
1 parent 6ec0077 commit d12fa00
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-secp256k1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; This file was generated by tools/generate-secp256k1-tests.py

secp256k1_verify 0x028bb490a35003c5e7ad4b22c70bfe209e7318a993b64744125a8d03f5894b310e 0x85932e4d075615be881398cc765f9f78204033f0ef5f832ac37e732f5f0cbda2 0x9213730efc55264645506993b3c17a3d6f228a9116de7866c2eca53eef5ef0ba591400683c5741402416b887c1716441d038c3614a6a76206076eea1e2b29cbd => 0 | 850000
secp256k1_verify 0x030763619f00c856f3b2a8c3841d84da30e45cbb2f84a59016c4107dd7a5915a73 0x23a02777185712876787e32ba4b9800fa8dff0ad846e5769d9f5d57ecde10ab3 0xaa598f576a7a740560d82dca58c7846e3c3634d4cd67769620e0ab3f5313d0c72d884d64cc42e1650e2d3f5e4eb79ee9fe56559d45584cc1fe4d831cf1ac7b06 => 0 | 850000
secp256k1_verify 0x025b76e818db00f938392d0a4031b80ca0af0105d3c2b72b03114115d1584059be 0x222b25aef8c0c85aa429f3efc19027743bad43615a8dd4e84e435c55af352e7a 0x43e59f19ae6c613811769b81d4b2768960c0240b1faa9ff792564c012fddc56957331f9c3dd5eecd2d7d818f03bddff78873c9a259ec8a0671b5833e625e9ca5 => 0 | 850000
secp256k1_verify 0x0315c699d5a3515609a67a5f6755fd8153efbe0fd9cb1b4a454fd6c9fadb97a7cf 0x87c4d4aec1bc8cd850199319dde457ecc47af37bff4fed285c0d8acc501ef1cc 0x3afd8b6392b72c276f9b19e8d42e5ae6b7f473b4196d3427f57c0b4cb4e05d0c1185df3caf14d445966be4d2b5ec6a40d8478af42f9c930b0959a3f24e6f51df => 0 | 850000
secp256k1_verify 0x02bd204c3d266eeb3288af023e455f19e889ae8c0ca4935b196dec72d02940d68c 0xd36eaf1c8f6f9b25a0008b44d49027818f421687187cd4a121e66f1cb8a67e0a 0x3a2344da7aa72eb596ee43f5df314ef1514be757b564a6d17fc9c1dc18b086457bed1d51bad1f8587554ef973f80f42dd47c2244736070dc42725dd0cb9b1150 => 0 | 850000
secp256k1_verify 0x031afa2fac2fb07f98a8f2f158cc886a2003dcebead5ffeff5a6b38366b2cb11aa 0x147ec91f141bc05c902084c80649d64eef0bd2b5c32fdfc568a35436bea540e4 0x8c95233f99c72648e4dbe7c256914b39f953a1be51ffe2862cee1cfbd96fc6a641c7b02d9384b6b4fd30f7de54ef7b3022b71bb3074141e02ebce04312819311 => 0 | 850000
secp256k1_verify 0x038aed4fbf23bc59c030cff8b0291b20fadacf6bcf4c0d8b97b2ba0e5683751343 0x369215c4a473669292fed192a5ae1a9aa641afe10d3ec4d0b455cfad5791d462 0xc0dfe41a5d0c0e34a4138133c3aa7659157eac6a5a7e77f8b041db5f1dd8578f54f0469f6178c9605a09aa231d9cae2267c33f5304a44979e97017f7d0dd6299 => 0 | 850000
secp256k1_verify 0x02ac0ac696843fdcce44ef1237635cc8edcb78e3efdff55ec7829aca83817f471d 0x08d5b309f5897d54cd3d1eb4ea9165af063d3aff27bd2756fbc64e844f38afbe 0xeb5b619b0250a1ccd5d931e9a2c4a6ad951186bfeb674af3d674b5270ed655bb4f048a390efba8168ea52d933c1afda53fba692acd8b13ccadfcbaa9015688a1 => 0 | 850000
secp256k1_verify 0x02364a773be0321384d540009ba2441e768481e6c08bbe0408756f746c852af8ee 0xaffd750fb25272c2e8670b1231f0acd61e0539eadb47cf23efa4fc4984fde036 0xc8f498aae0c3083946bbe392d5fcbfc86c54dfaf153580e30fd10152b4c495e617f027b5bc1cf5b4f156b7a6679b5194e3fffef67e87813e14884632cb265738 => 0 | 850000
secp256k1_verify 0x039a785d7f1fc9833467a89c5f6d1a4eff7c5df647a637c6257a1f0bd222b99fa3 0x2c090ec4147adb7af6d2fcfeadb95bb7f0256e26a6cab4ef83aaf005491e7da3 0x805bb93a1883b471dd8fbd04b1808e029d22d6c65895f80a8d3f5c1db8a0d2ef0120ddbc8c0bd31c07933532de58306ac94c3578736819c3efa86bd132adac87 => 0 | 850000
secp256k1_verify 0x02afe7eee5e0f6b4d8f3555bc4e49af414db818f35f4d3fbe857408b441d6d99cb 0xdb104e9c6fe422bfad4efb4d60f6b69703f59fe139c1bccbb4ad8e26c59bbdfc 0xa46d08ef84e3737ba7e6a8988930166944f04d476ea7831fbaaa5ff105260c1913518e20c9ef9d2ff3e1742c42cb43564d4e343758461ddbc039290a1b8d81bd => 0 | 850000
secp256k1_verify 0x03f8b1ffaab6100ad049ed9a2b881a14bc921355616ca932acf6995e59336a2362 0x41bd0a80ef7761bf998254cf56f1daf89460a66edc6b40689ed4a660b4d5ec0a 0xe67d34e19221be4200c83186c73b93361982edf6cd50bd645ce5edf3572371392dc2f4bb3a323acd5d05cd6bc486bfb19041d172746621f276c9c0fa4d652e2d => 0 | 850000
secp256k1_verify 0x02e75a5fbdb58cb0db7e11969f190326dc1bb2d5b40d32b47f104a295e53f9bebe 0xdc3c455519f7d3206b8ee435c944b7de1f99fa7f5b645f6d79cec4658cd3f313 0xa5cc79c6acde175921a57c6132ae06ef22c38440afa12dfb6da42e57437d7a803c86878b07e7e7e040aba0d8f98bd7c2e7706a58d50473f64b70ab0ac8d4aeb1 => 0 | 850000
secp256k1_verify 0x023d6276af9da5e6a9910a5f52feb30843afcbaff8833f7f000bc63b6453a23690 0xc8f9a4a27bbc1af837fd415e6241285e4b95c0ce74600c81ded086ebcc74547f 0x9ec2ad2d679a50d90fa4e6d9097d98ed4e9ddea8800b38b55d01fd4caed8b8a86849f3d7feb9cc965e19b088cc34e167a1be04b57a68dcc61a880c4ba831cfe9 => 0 | 850000
secp256k1_verify 0x022497b8aa90db61882c610f41877a3c94e30a51c9cc6e1bfa20abba71b04fdab8 0xea89d66edf25352d44ca5b1670a0c46255d51ef20ca5d0649b1b6450c1df4984 0x6a5ad7658c523a44ab238aff8b17368994dcbddbe9848036eca08bd286d825d20b6ca7209c081a75025c0d351aadb9ec85475a825ce8b008e46ec6c75b71f0c3 => 0 | 850000
secp256k1_verify 0x03a9647301153f3a673231b4eb4bf8ce3ac3eb47a46d4ecc7d8b2b513128473930 0xc32749548ab386ad3930f6f1dd2a85c5063563eeb99ef633ed1fd648aabb22a2 0xf63d3f55662d414b439b66aad45dac0652c235e4b7d9f9ba056ed855ab32cab07c1852de59878bd33a96483cde4acb0e4b9bd5583836ad5ca08c22d2bb32488d => 0 | 850000
secp256k1_verify 0x0288e7e29d9ea7abe2d8c7f2f8ae5670553861d248a0462f8a7b8c65148ba4c107 0x728395357573930ab93762774ccd98edf66db24039c08e59f76d1bbaeefe6379 0x1773bf5088f801b00e0ea5ebce25ce2875c41bdaff0a19d895a818131241542d1e31b0ea04221bdc5e4f88926b8714138dca607f7d1ad22453b52721353ad4db => 0 | 850000
secp256k1_verify 0x03f0ba054b09ed58f510081ef87edf62193d8d3cece12b43749890fa53fc2146ba 0x4531ac1e89c712279be237a9da1f9eda8c4ed21a3112ef5c521d1cff849b4772 0xfc899da2725161a2e7ac4cde7c2170bc31a971c25e1b269d5cdb29642f323d2754fb3a93d060603c5e5f8582e96791cff4d8323cce0d04a7866b241cac1ee116 => 0 | 850000
secp256k1_verify 0x02cf07e9a0a85afcb65d2cc4e0958a33fca3d2d665c1cf08b9cdb62875078bc90d 0xb0124a8df2a9d2c52f1ff75b6fd0b26189f963fc0dbbff9bfa64ac3efdfa7efd 0x4dc90dff0ef84f03efa7a970b30555e4ce6d367263afd38dcbfdbb2618e5c6130d507430cdad63ef546568e031b4009923e15348a2785f4d9f4acc8d1201463d => 0 | 850000
secp256k1_verify 0x033085019044fd85e567f06b96a572e940fd7312df96c1c8ebac97f02ae5e405d0 0xa4a82d915744b1646bec9d8de7c1261d2f8034594da88c07d5b644e12fb78fa8 0x95bc79b70e262d0828a8f81eac758755785cc0f5a242d1d06160dd25ca52b15d42a166775f2dcdc7232b1cf9152f3f24778b6915f329ae8b9b7e53b12ec43eaa => 0 | 850000
secp256k1_verify 0x03c601576c80ac0f0c0c6bf87780f0239a64ef9081cc93d08001636127a8878264 0x0e6d6511259dd00c6894752091ae31c1e92eb594cec5bfce874ddeb1074c4a8e 0xbac28c9f8046f2b8be38824315d40c25686fd69c142b0cb0edb8022ccce80978286ec7cdd88b43707dcb8d9867a4a733a0c1637961e5608c640385993bc80452 => 0 | 850000
secp256k1_verify 0x03ba36e64074c0ea7781c421f89241222486a4eacbd1b17b9d2ea0d340f9e0df84 0x4742e3be54ab22db1ffcb63ac90a34384b1b2da07968d816556c9488c6342ff7 0x23de22f7cd51939a39935b657c3eeb2d3ad727c2a0f5ea974b453b96839357e618d54effa3e000a05dfe1a3b2348fc175caeedcebdfee9381b9b93a489aba566 => 0 | 850000
secp256k1_verify 0x0261bdffdf9f2128b9e30706d21d16f394dad15a8ba6b11c83e2e89954573f8fb8 0x764930461dc2636cbad399be0ab3a0484f12b2289bc62c9c65b227d3d15d9331 0x5e374aad58c8f33dfa8f1b76ae0afb2d87979a3ef295e198933191c2071185a569320cbaa5de6a7c4dc1f89ab10c14f5dbd490e109bf32c0a6cf8f4ce2a04b9c => 0 | 850000
secp256k1_verify 0x024c486edf4db677c576ba3a93fc55560107db03a2d840aef2d42583c8bc5d79f0 0x25624809df04f74c21af8bd16b33c3922490caf0920fe7ce0890b1c12c2b1f7e 0x3f19b909b887776227df84a58f38ab1d309d1cca0ff55daec79397b71570d3fa23731ce8aac5a38a3031da95c9321551408f561ca266da297192fd84e7827296 => 0 | 850000
secp256k1_verify 0x034199e60bfc9d57c33e6ccc7e8ba407560c4b13262c25e0ac5fecf6df07f1d298 0x0374d1fc889af2526b24fc3e09068eff4403c28d482b240e3fac512482198f38 0xd64eee71ee781211263120b071930d19357acdf6377cf6cd8b7621a5f48985b15313bc745ddabcbb8b14836f5418f9ccbe63f66ce19ac3bfef55e492494592c0 => 0 | 850000
secp256k1_verify 0x03d97dc47b6ae6d74ee013e0511cf1a14e768fdd47af0edda270b49c3034bcdab6 0x44b54aed8cffdbf091cebf733b3b448da3ea3bdecf4c4245616031fe51cb22c4 0x5b8f0272ba43a930efb1bc4e5b2be449cd3a45ee0c07f963a6477037271654aa33e6736f9b24084944987a52a54aef99a7ede47a5a44e88942694b3bfe88a6a0 => 0 | 850000
secp256k1_verify 0x0203e94824ac4475813e3ca5412b0352b8cc061f30501f68892047b109bb4d6a51 0xc9eaff05fa43de1dea678859575344cc9497f8efbd7903ceb5ecec00525d9eae 0xa52eaf75bfc94371ed821295870d53fc6b8ecc55999af2faae6284b4846e33ec02671e480a158227d4f499fa87a51b87d9f8de8690935208ba3509746b87747f => 0 | 850000
secp256k1_verify 0x02ff73c028db3318345aeff3d29f72e98e4d73beecfee1d0322b26b4c4a1e2b7dd 0xd3b91c33942efacc55cb620e31dc020d3d3a585b44a7eee2c42623dad052cf5d 0x8ec6f72515b8131559970102d356aac70ef471aec26bd4b6d19ee20dcaa9250b56963419942f2503d55742310d17941e0e5d2844887e87d4d86b3b6d25f43052 => 0 | 850000
secp256k1_verify 0x0244913945a534e0f4a7fcd5da183f65badc1168447f5e3a70082fced142dcb79e 0x9243e5958a207cc63725f619124483b9fd99ca24a155089fd9e037f25c58acce 0xba9178b04cecffbd87da8397b7ccac84254631ab3bae763e2bc9f1b5783217bc7b892ec7cb98f0bba1cc6b5da6162b9a32583fb0490f97501bee5a9175668156 => 0 | 850000
secp256k1_verify 0x021e023532f625fa4b80e35831f07976d046629f5ffc5f9bdc32a6328521847295 0x9940a483e428c608f1f9aa2f7816f6caa23f6044f1d37dff304f89a650ee734e 0x378bd9bbb548f2bb01d8d8824193ee9d5a4b4f0c7d6372af3a3fb5d2446ab3ef256a4f900989c35cb419fe6d55a856cc0fe99b234418ff4e1ceba60b3c58e648 => 0 | 850000
secp256k1_verify 0x039a78dd7f1fc9833467a89c5f6d1a4eff7c5df647a637c6257a1f0bd222b99fa3 0x935d863e2d28d8e5d399ea8af7393ef11fdffc7d862dcc6b5217a8ef15fb5442 0x9048ea133c5089410edad375ca3b0f1af6b762ac7f6c5f471b038fc1b0ecee8c588ad1bd8728122f7524098e7e1b9ab709c0104e24843e044179666df8f82922 => FAIL
secp256k1_verify 0x021e023532f625fa4b80e35831f07976d044629f5ffc5f9bdc32a6328521847295 0x0847bede50a2174a361c292ae89c6a7f2b511ea957d379e25cae1d4dbc3a9039 0xb3ac654913e3701de7f05daa516965fa548b26f0883011d6374bc465289b01ac0da571a16fdcd25aa164a79cc3caa140738d9a9a4a65268181ef8fab6f01a68a => FAIL
secp256k1_verify 0x03a9e47301153f3a673231b4eb4bf8ce3ac3eb47a46d4ecc7d8b2b513128473930 0x4f2f997d37ef9978e517f61da2ead60b422ef1ff3b537e84ba9aa7a7afd9a3ee 0xc3b98932d14f32004a62474af1046efe00df4b611c3a7ebbfc3dd955fd29fa155c8295dbf8a3b2d9f1d862615e7ac9fbba26a5635aa9a3f999e42254d7fbc85e => FAIL
secp256k1_verify 0x03f8b1ffaab6100ad049ed9a2b881a14bc921355616ca932acf6995e59336a2362 0xc89e4fe7c56483c6d9a4fe8b5fdf983697db0b027a8daab81e908282e39f3401 0x96901ed43e39825f7a21c379187a6c793a1f1dca0ee0f167111bfc774f81e3c6354fa5d29e3a6be879cc12dbf86807f47b397812f667fb487b0e5a3a88592033 => FAIL
secp256k1_verify 0x0203e94824ac4475813e3ca5412b0352b8cc061f30501f68892047b109bb4d6a51 0x7e06c2a825c043f6fd8400ddaed28cdb9a297aa8aa71d8ba004cf685e34cbbbe 0xb90ef95be55d0c0f9c6ad3bdf3c465196ab8e041076348e480950b278764802302b3d2ab67c5638c845aa7428c3c3bf6d29635a9e066107a282f18485e88d54d => FAIL
secp256k1_verify 0x021e023532f625fa4b80e35831f07976d046629f5ffc5f9bdc32a6328521847295 0x63da4d356c8712477fa28d5d0bf2a5ae8da1b0fdbec3541c2b9e2e6ce9bf5548 0x7dfae5d2ec1a4d146d3c363d0f911fc7a009d23c696537d170151f538056e7995c65207a664f84d9367601612791fe0a943961c0f7e60e2a0b21357cae5e8cd3 => FAIL
secp256k1_verify 0x025b76e818db00f938392d0a4031b80ca0af0105d3c2b72b03114115d1584059be 0x5090b6b9f28182a3ccc1c967b22bf23493f6f31c35d05602b51335d9da70ac59 0xcb1feb36656b7fdf71c078ad8091e864bcc75fe3c0997cfe3645049b2ddde6aa450fe8135a3db1dda72de309927b3671d7ef4c09ea514cbfa9332b9e372e2840 => FAIL
secp256k1_verify 0x031afa2fac2fb07f98a8f2f158cc886a2003dcebead5ffeff5a6b38366b2cb11aa 0xbe9bed82fa8fce0fd4aaaff5aab93617b95a0778c08916ec01d77beb0e39bd10 0x08bc83e323583d4a5c51402a660a26227c41cbae1190e904662e176eb244bbbe5e3856f6fef63eb16ca8cdd4a254baa8ec903e54f172f609a26b64555a3d3537 => FAIL
secp256k1_verify 0x039a785d7f1fc9833467a89c5f6d1a4eff7c5df647a637c6257a1f0bd222b99fa3 0xdac269ea2ee6aff43216dee49c8a7049c3124162c7ae9d5fc4b7dd7e5c776440 0xcfd9583d84cb52d792836de5523c689b3f9ca540fffa9c5091fe659f1e1257b568fa0670c570c790cbe673854e3639f5a3d96cf766eb19707d0a4cab0ab891b5 => FAIL
1 change: 1 addition & 0 deletions src/test_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ use rstest::rstest;
#[case("test-blspy-pairing")]
#[case("test-blspy-verify")]
#[case("test-secp-verify")]
#[case("test-secp256k1")]
fn test_ops(#[case] filename: &str) {
use std::fs::read_to_string;

Expand Down
54 changes: 54 additions & 0 deletions tools/generate-secp256k1-tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from secp256k1 import PublicKey, PrivateKey
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 = 850000
sigs = []

args = ""
for sk in sks:
pk = sk.pubkey
msg = randbytes(randint(3,40))
sig = sk.ecdsa_sign(msg)
sha = sha256()
sha.update(msg)
f.write(f"secp256k1_verify 0x{bytes(filter_pk(pk.serialize())).hex()} 0x{filter_msg(sha.digest()).hex()} 0x{bytes(filter_sig(sk.ecdsa_serialize_compact(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(PrivateKey())


with open("../op-tests/test-secp256k1.txt", "w+") as f:
f.write("; This file was generated by tools/generate-secp256k1-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 d12fa00

Please sign in to comment.