Bitcoin Core  25.99.0
P2P Digital Currency
tests_wycheproof_generate.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Copyright (c) 2023 Random "Randy" Lattice and Sean Andersen
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or https://www.opensource.org/licenses/mit-license.php.
5 '''
6 Generate a C file with ECDSA testvectors from the Wycheproof project.
7 '''
8 
9 import json
10 import hashlib
11 import urllib.request
12 import sys
13 
14 filename_input = sys.argv[1]
15 
16 with open(filename_input) as f:
17  doc = json.load(f)
18 
19 num_groups = len(doc['testGroups'])
20 
21 def to_c_array(x):
22  if x == "": return ""
23  s = ',0x'.join(a+b for a,b in zip(x[::2], x[1::2]))
24  return "0x" + s
25 
26 
27 num_vectors = 0
28 offset_msg_running, offset_pk_running, offset_sig = 0, 0, 0
29 out = ""
30 messages = ""
31 signatures = ""
32 public_keys = ""
33 cache_msgs = {}
34 cache_public_keys = {}
35 
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]
42  # // 2 to convert hex to byte length
43  sig_size = len(test_vector['sig']) // 2
44  msg_size = len(test_vector['msg']) // 2
45 
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")
49 
50  if num_vectors != 0 and sig_size != 0: signatures += ",\n "
51 
52  new_msg = False
53  msg = to_c_array(test_vector['msg'])
54  msg_offset = offset_msg_running
55  # check for repeated msg
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
59  messages += msg
60  new_msg = True
61  else:
62  msg_offset = cache_msgs[msg]
63 
64  new_pk = False
65  pk = to_c_array(public_key['uncompressed'])
66  pk_offset = offset_pk_running
67  # check for repeated pk
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
71  public_keys += pk
72  new_pk = True
73  else:
74  pk_offset = cache_public_keys[pk]
75 
76  signatures += to_c_array(test_vector['sig'])
77 
78  out += " /" + "* tcId: " + str(test_vector['tcId']) + ". " + test_vector['comment'] + " *" + "/\n"
79  out += " {" + "{0}, {1}, {2}, {3}, {4}, {5}".format(
80  pk_offset,
81  msg_offset,
82  msg_size,
83  offset_sig,
84  sig_size,
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
89  num_vectors += 1
90 
91 struct_definition = """
92 typedef struct {
93  size_t pk_offset;
94  size_t msg_offset;
95  size_t msg_len;
96  size_t sig_offset;
97  size_t sig_len;
98  int expected_verify;
99 } wycheproof_ecdsa_testvector;
100 """
101 
102 
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))
105 
106 print(struct_definition)
107 
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")
111 
112 print("static const wycheproof_ecdsa_testvector testvectors[SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS] = {")
113 print(out)
114 print("};")
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1060