6 Generate a C file with ECDSA testvectors from the Wycheproof project.
14 filename_input = sys.argv[1]
16 with open(filename_input)
as f:
19 num_groups = len(doc[
'testGroups'])
23 s =
',0x'.join(a+b
for a,b
in zip(x[::2], x[1::2]))
28 offset_msg_running, offset_pk_running, offset_sig = 0, 0, 0
34 cache_public_keys = {}
36 for i
in range(num_groups):
37 group = doc[
'testGroups'][i]
38 num_tests = len(group[
'tests'])
39 public_key = group[
'publicKey']
40 for j
in range(num_tests):
41 test_vector = group[
'tests'][j]
43 sig_size = len(test_vector[
'sig']) // 2
44 msg_size = len(test_vector[
'msg']) // 2
46 if test_vector[
'result'] ==
"invalid": expected_verify = 0
47 elif test_vector[
'result'] ==
"valid": expected_verify = 1
48 else:
raise ValueError(
"invalid result field")
50 if num_vectors != 0
and sig_size != 0: signatures +=
",\n "
54 msg_offset = offset_msg_running
56 if msg
not in cache_msgs.keys():
57 if num_vectors != 0
and msg_size != 0: messages +=
",\n "
58 cache_msgs[msg] = offset_msg_running
62 msg_offset = cache_msgs[msg]
66 pk_offset = offset_pk_running
68 if pk
not in cache_public_keys.keys():
69 if num_vectors != 0: public_keys +=
",\n "
70 cache_public_keys[pk] = offset_pk_running
74 pk_offset = cache_public_keys[pk]
78 out +=
" /" +
"* tcId: " + str(test_vector[
'tcId']) +
". " + test_vector[
'comment'] +
" *" +
"/\n"
79 out +=
" {" +
"{0}, {1}, {2}, {3}, {4}, {5}".
format(
85 expected_verify) +
" },\n"
86 if new_msg: offset_msg_running += msg_size
87 if new_pk: offset_pk_running += 65
88 offset_sig += sig_size
91 struct_definition =
"""
99 } wycheproof_ecdsa_testvector;
103 print(
"/* Note: this file was autogenerated using tests_wycheproof_generate.py. Do not edit. */")
104 print(
"#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({})".
format(num_vectors))
106 print(struct_definition)
108 print(
"static const unsigned char wycheproof_ecdsa_messages[] = { " + messages +
"};\n")
109 print(
"static const unsigned char wycheproof_ecdsa_public_keys[] = { " + public_keys +
"};\n")
110 print(
"static const unsigned char wycheproof_ecdsa_signatures[] = { " + signatures +
"};\n")
112 print(
"static const wycheproof_ecdsa_testvector testvectors[SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS] = {")