6Generate a C file with ECDSA testvectors from the Wycheproof project.
12from wycheproof_utils
import to_c_array
14filename_input = sys.argv[1]
16with open(filename_input)
as f:
19num_groups = len(doc[
'testGroups'])
23offset_msg_running, offset_pk_running, offset_sig = 0, 0, 0
31for i
in range(num_groups):
32 group = doc[
'testGroups'][i]
33 num_tests = len(group[
'tests'])
34 public_key = group[
'publicKey']
35 for j
in range(num_tests):
36 test_vector = group[
'tests'][j]
38 sig_size = len(test_vector[
'sig']) // 2
39 msg_size = len(test_vector[
'msg']) // 2
41 if test_vector[
'result'] ==
"invalid":
43 elif test_vector[
'result'] ==
"valid":
46 raise ValueError(
"invalid result field")
48 if num_vectors != 0
and sig_size != 0:
53 msg_offset = offset_msg_running
55 if msg
not in cache_msgs:
56 if num_vectors != 0
and msg_size != 0:
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:
71 cache_public_keys[pk] = offset_pk_running
75 pk_offset = cache_public_keys[pk]
79 out +=
" /" +
"* tcId: " + str(test_vector[
'tcId']) +
". " + test_vector[
'comment'] +
" *" +
"/\n"
80 out += f
" {{{pk_offset}, {msg_offset}, {msg_size}, {offset_sig}, {sig_size}, {expected_verify} }},\n"
82 offset_msg_running += msg_size
84 offset_pk_running += 65
85 offset_sig += sig_size
88struct_definition =
"""
96} wycheproof_ecdsa_testvector;
100print("/* Note: this file was autogenerated using tests_wycheproof_generate_ecdsa.py. Do not edit. */")
101print(f"#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")
103print(struct_definition)
105print("static const unsigned char wycheproof_ecdsa_messages[] = { " + messages + "};\n
")
106print("static const unsigned char wycheproof_ecdsa_public_keys[] = { " + public_keys +
"};\n")
107print(
"static const unsigned char wycheproof_ecdsa_signatures[] = { " + signatures +
"};\n")
109print(
"static const wycheproof_ecdsa_testvector testvectors[SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS] = {")