6Generate a C file with ECDSA testvectors from the Wycheproof project.
12filename_input = sys.argv[1]
14with open(filename_input)
as f:
17num_groups = len(doc[
'testGroups'])
22 s =
',0x'.join(a+b
for a,b
in zip(x[::2], x[1::2]))
27offset_msg_running, offset_pk_running, offset_sig = 0, 0, 0
35for i
in range(num_groups):
36 group = doc[
'testGroups'][i]
37 num_tests = len(group[
'tests'])
38 public_key = group[
'publicKey']
39 for j
in range(num_tests):
40 test_vector = group[
'tests'][j]
42 sig_size = len(test_vector[
'sig']) // 2
43 msg_size = len(test_vector[
'msg']) // 2
45 if test_vector[
'result'] ==
"invalid":
47 elif test_vector[
'result'] ==
"valid":
50 raise ValueError(
"invalid result field")
52 if num_vectors != 0
and sig_size != 0:
57 msg_offset = offset_msg_running
59 if msg
not in cache_msgs:
60 if num_vectors != 0
and msg_size != 0:
62 cache_msgs[msg] = offset_msg_running
66 msg_offset = cache_msgs[msg]
70 pk_offset = offset_pk_running
72 if pk
not in cache_public_keys:
75 cache_public_keys[pk] = offset_pk_running
79 pk_offset = cache_public_keys[pk]
83 out +=
" /" +
"* tcId: " + str(test_vector[
'tcId']) +
". " + test_vector[
'comment'] +
" *" +
"/\n"
84 out += f
" {{{pk_offset}, {msg_offset}, {msg_size}, {offset_sig}, {sig_size}, {expected_verify} }},\n"
86 offset_msg_running += msg_size
88 offset_pk_running += 65
89 offset_sig += sig_size
92struct_definition =
"""
100} wycheproof_ecdsa_testvector;
104print("/* Note: this file was autogenerated using tests_wycheproof_generate.py. Do not edit. */")
105print(f"#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")
107print(struct_definition)
109print("static const unsigned char wycheproof_ecdsa_messages[] = { " + messages + "};\n
")
110print("static const unsigned char wycheproof_ecdsa_public_keys[] = { " + public_keys +
"};\n")
111print(
"static const unsigned char wycheproof_ecdsa_signatures[] = { " + signatures +
"};\n")
113print(
"static const wycheproof_ecdsa_testvector testvectors[SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS] = {")