Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: lint wycheproof's python script #1279

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions tools/tests_wycheproof_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
'''

import json
import hashlib
import urllib.request
import sys

filename_input = sys.argv[1]
Expand All @@ -19,7 +17,8 @@
num_groups = len(doc['testGroups'])

def to_c_array(x):
if x == "": return ""
if x == "":
return ""
s = ',0x'.join(a+b for a,b in zip(x[::2], x[1::2]))
return "0x" + s

Expand All @@ -43,18 +42,23 @@ def to_c_array(x):
sig_size = len(test_vector['sig']) // 2
msg_size = len(test_vector['msg']) // 2

if test_vector['result'] == "invalid": expected_verify = 0
elif test_vector['result'] == "valid": expected_verify = 1
else: raise ValueError("invalid result field")
if test_vector['result'] == "invalid":
expected_verify = 0
elif test_vector['result'] == "valid":
expected_verify = 1
else:
raise ValueError("invalid result field")

if num_vectors != 0 and sig_size != 0: signatures += ",\n "
if num_vectors != 0 and sig_size != 0:
signatures += ",\n "

new_msg = False
msg = to_c_array(test_vector['msg'])
msg_offset = offset_msg_running
# check for repeated msg
if msg not in cache_msgs.keys():
if num_vectors != 0 and msg_size != 0: messages += ",\n "
if msg not in cache_msgs:
if num_vectors != 0 and msg_size != 0:
messages += ",\n "
cache_msgs[msg] = offset_msg_running
messages += msg
new_msg = True
Expand All @@ -65,8 +69,9 @@ def to_c_array(x):
pk = to_c_array(public_key['uncompressed'])
pk_offset = offset_pk_running
# check for repeated pk
if pk not in cache_public_keys.keys():
if num_vectors != 0: public_keys += ",\n "
if pk not in cache_public_keys:
if num_vectors != 0:
public_keys += ",\n "
cache_public_keys[pk] = offset_pk_running
public_keys += pk
new_pk = True
Expand All @@ -76,15 +81,11 @@ def to_c_array(x):
signatures += to_c_array(test_vector['sig'])

out += " /" + "* tcId: " + str(test_vector['tcId']) + ". " + test_vector['comment'] + " *" + "/\n"
out += " {" + "{0}, {1}, {2}, {3}, {4}, {5}".format(
pk_offset,
msg_offset,
msg_size,
offset_sig,
sig_size,
expected_verify) + " },\n"
if new_msg: offset_msg_running += msg_size
if new_pk: offset_pk_running += 65
out += f" {{{pk_offset}, {msg_offset}, {msg_size}, {offset_sig}, {sig_size}, {expected_verify} }},\n"
if new_msg:
offset_msg_running += msg_size
if new_pk:
offset_pk_running += 65
offset_sig += sig_size
num_vectors += 1

Expand All @@ -101,7 +102,7 @@ def to_c_array(x):


print("/* Note: this file was autogenerated using tests_wycheproof_generate.py. Do not edit. */")
print("#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({})".format(num_vectors))
print(f"#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")

print(struct_definition)

Expand Down