|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import json |
| 5 | +import textwrap |
| 6 | + |
| 7 | +max_participants = 0 |
| 8 | + |
| 9 | +if len(sys.argv) < 2: |
| 10 | + print( |
| 11 | + "This script converts BIP FROST test vectors in a given directory to a C file that can be used in the test framework." |
| 12 | + ) |
| 13 | + print("Usage: %s <dir>" % sys.argv[0]) |
| 14 | + sys.exit(1) |
| 15 | + |
| 16 | + |
| 17 | +def hexstr_to_intarray(str): |
| 18 | + return ", ".join([f"0x{b:02X}" for b in bytes.fromhex(str)]) |
| 19 | + |
| 20 | + |
| 21 | +def create_init(name): |
| 22 | + return """ |
| 23 | +static const struct frost_%s_vector frost_%s_vector = { |
| 24 | +""" % ( |
| 25 | + name, |
| 26 | + name, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def init_array(key): |
| 31 | + return textwrap.indent("{ %s },\n" % hexstr_to_intarray(data[key]), 4 * " ") |
| 32 | + |
| 33 | + |
| 34 | +def init_arrays(key): |
| 35 | + s = textwrap.indent("{\n", 4 * " ") |
| 36 | + s += textwrap.indent( |
| 37 | + ",\n".join(["{ %s }" % hexstr_to_intarray(x) for x in data[key]]), 8 * " " |
| 38 | + ) |
| 39 | + s += textwrap.indent("\n},\n", 4 * " ") |
| 40 | + return s |
| 41 | + |
| 42 | + |
| 43 | +def init_nested_arrays(array): |
| 44 | + return "{ %s }" % ", ".join(["{ %s }" % hexstr_to_intarray(x) for x in array]) |
| 45 | + |
| 46 | + |
| 47 | +def init_indices(array): |
| 48 | + return " %d, { %s }" % ( |
| 49 | + len(array), |
| 50 | + ", ".join(map(str, array)) if len(array) > 0 else "0", |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def init_is_xonly(case): |
| 55 | + if len(case.get("tweak_indices", [])) > 0: |
| 56 | + return ", ".join("1" if x else "0" for x in case["is_xonly"]) |
| 57 | + return "0" |
| 58 | + |
| 59 | + |
| 60 | +def init_optional_expected(case): |
| 61 | + return hexstr_to_intarray(case["expected"]) if "expected" in case else "0" |
| 62 | + |
| 63 | + |
| 64 | +def init_cases(cases, f): |
| 65 | + s = textwrap.indent("{\n", 4 * " ") |
| 66 | + for (i, case) in enumerate(cases): |
| 67 | + s += textwrap.indent("%s\n" % f(case), 8 * " ") |
| 68 | + s += textwrap.indent("},\n", 4 * " ") |
| 69 | + return s |
| 70 | + |
| 71 | + |
| 72 | +def finish_init(): |
| 73 | + return "};\n" |
| 74 | + |
| 75 | + |
| 76 | +s = ( |
| 77 | + """/** |
| 78 | + * Automatically generated by %s. |
| 79 | + * |
| 80 | + * The test vectors for the FROST implementation. |
| 81 | + */ |
| 82 | +""" |
| 83 | + % sys.argv[0] |
| 84 | +) |
| 85 | + |
| 86 | +s += """ |
| 87 | +enum FROST_ERROR { |
| 88 | + FROST_PUBKEY, |
| 89 | + FROST_PUBSHARE, |
| 90 | + FROST_TWEAK, |
| 91 | + FROST_PUBNONCE, |
| 92 | + FROST_AGGNONCE, |
| 93 | + FROST_SECNONCE, |
| 94 | + FROST_SIG, |
| 95 | + FROST_SIG_VERIFY, |
| 96 | + FROST_OTHER |
| 97 | +}; |
| 98 | +""" |
| 99 | + |
| 100 | +# key gen vectors |
| 101 | +with open(sys.argv[1] + "/keygen_vectors.json") as f: |
| 102 | + data = json.load(f) |
| 103 | + |
| 104 | + num_valid_cases = len(data["valid_test_cases"]) |
| 105 | + num_pubshare_fail_cases = len(data["pubshare_correctness_fail_test_cases"]) |
| 106 | + num_group_pubkey_fail_cases = len(data["group_pubkey_correctness_fail_test_cases"]) |
| 107 | + |
| 108 | + all_cases = ( |
| 109 | + data["valid_test_cases"] |
| 110 | + + data["pubshare_correctness_fail_test_cases"] |
| 111 | + + data["group_pubkey_correctness_fail_test_cases"] |
| 112 | + ) |
| 113 | + max_participants = max( |
| 114 | + len(test_case["participant_identifiers"]) for test_case in all_cases |
| 115 | + ) |
| 116 | + |
| 117 | + # Add structures for valid and error cases |
| 118 | + s += """ |
| 119 | +struct frost_key_gen_valid_test_case { |
| 120 | + size_t max_participants; |
| 121 | + size_t min_participants; |
| 122 | + unsigned char group_public_key[33]; |
| 123 | + size_t participant_identifiers_len; |
| 124 | + size_t participant_identifiers[%d]; |
| 125 | + unsigned char participant_pubshares[%d][33]; |
| 126 | + unsigned char participant_secshares[%d][32]; |
| 127 | +}; |
| 128 | +""" % ( |
| 129 | + max_participants, |
| 130 | + max_participants, |
| 131 | + max_participants, |
| 132 | + ) |
| 133 | + s += """ |
| 134 | +struct frost_key_gen_pubshare_fail_test_case { |
| 135 | + size_t max_participants; |
| 136 | + size_t min_participants; |
| 137 | + unsigned char group_public_key[33]; |
| 138 | + size_t participant_identifiers_len; |
| 139 | + size_t participant_identifiers[%d]; |
| 140 | + unsigned char participant_pubshares[%d][33]; |
| 141 | + unsigned char participant_secshares[%d][32]; |
| 142 | + enum FROST_ERROR error; |
| 143 | +}; |
| 144 | +""" % ( |
| 145 | + max_participants, |
| 146 | + max_participants, |
| 147 | + max_participants, |
| 148 | + ) |
| 149 | + s += """ |
| 150 | +struct frost_key_gen_pubkey_fail_test_case { |
| 151 | + size_t max_participants; |
| 152 | + size_t min_participants; |
| 153 | + unsigned char group_public_key[33]; |
| 154 | + size_t participant_identifiers_len; |
| 155 | + size_t participant_identifiers[%d]; |
| 156 | + unsigned char participant_pubshares[%d][33]; |
| 157 | + unsigned char participant_secshares[%d][32]; |
| 158 | + enum FROST_ERROR error; |
| 159 | +}; |
| 160 | +""" % ( |
| 161 | + max_participants, |
| 162 | + max_participants, |
| 163 | + max_participants, |
| 164 | + ) |
| 165 | + |
| 166 | + # Add structure for entire vector |
| 167 | + |
| 168 | + s += """ |
| 169 | +struct frost_key_gen_vector { |
| 170 | + struct frost_key_gen_valid_test_case valid_cases[%d]; |
| 171 | + struct frost_key_gen_pubshare_fail_test_case pubshare_fail_cases[%d]; |
| 172 | + struct frost_key_gen_pubkey_fail_test_case pubkey_fail_cases[%d]; |
| 173 | +}; |
| 174 | +""" % ( |
| 175 | + num_valid_cases, |
| 176 | + num_pubshare_fail_cases, |
| 177 | + num_group_pubkey_fail_cases, |
| 178 | + ) |
| 179 | + |
| 180 | + s += create_init("key_gen") |
| 181 | + |
| 182 | + # Add valid cases to the vector |
| 183 | + s += init_cases( |
| 184 | + data["valid_test_cases"], |
| 185 | + lambda case: "{ %d, %d, { %s }, %s, %s, %s }," |
| 186 | + % ( |
| 187 | + case["max_participants"], |
| 188 | + case["min_participants"], |
| 189 | + hexstr_to_intarray(case["group_public_key"]), |
| 190 | + init_indices(case["participant_identifiers"]), |
| 191 | + init_nested_arrays(case["participant_pubshares"]), |
| 192 | + init_nested_arrays(case["participant_secshares"]), |
| 193 | + ), |
| 194 | + ) |
| 195 | + |
| 196 | + def comment_to_error(case): |
| 197 | + comment = case["comment"] |
| 198 | + if "public key" in comment.lower(): |
| 199 | + return "FROST_PUBKEY" |
| 200 | + elif "pubshare" in comment.lower(): |
| 201 | + return "FROST_PUBSHARE" |
| 202 | + else: |
| 203 | + sys.exit("Unknown error") |
| 204 | + |
| 205 | + for cases in ("pubshare_correctness_fail_test_cases", "group_pubkey_correctness_fail_test_cases"): |
| 206 | + s += init_cases( |
| 207 | + data[cases], |
| 208 | + lambda case: "{ %d, %d, { %s }, %s, %s, %s, %s }," |
| 209 | + % ( |
| 210 | + case["max_participants"], |
| 211 | + case["min_participants"], |
| 212 | + hexstr_to_intarray(case["group_public_key"]), |
| 213 | + init_indices(case["participant_identifiers"]), |
| 214 | + init_nested_arrays(case["participant_pubshares"]), |
| 215 | + init_nested_arrays(case["participant_secshares"]), |
| 216 | + comment_to_error(case), |
| 217 | + ), |
| 218 | + ) |
| 219 | + s += finish_init() |
| 220 | +s += "enum { FROST_VECTORS_MAX_PARTICIPANTS = %d };" % max_participants |
| 221 | +print(s) |
0 commit comments