Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2023 Random "Randy" Lattice and Sean Andersen3# Distributed under the MIT software license, see the accompanying4# file COPYING or https://www.opensource.org/licenses/mit-license.php.5'''6Generate a C file with ECDSA testvectors from the Wycheproof project.7'''9import json10import sys12from wycheproof_utils import to_c_array14filename_input = sys.argv[1]16with open(filename_input) as f:17doc = json.load(f)19num_groups = len(doc['testGroups'])22num_vectors = 023offset_msg_running, offset_pk_running, offset_sig = 0, 0, 024out = ""25messages = ""26signatures = ""27public_keys = ""28cache_msgs = {}29cache_public_keys = {}31for i in range(num_groups):32group = doc['testGroups'][i]33num_tests = len(group['tests'])34public_key = group['publicKey']35for j in range(num_tests):36test_vector = group['tests'][j]37# // 2 to convert hex to byte length38sig_size = len(test_vector['sig']) // 239msg_size = len(test_vector['msg']) // 241if test_vector['result'] == "invalid":42expected_verify = 043elif test_vector['result'] == "valid":44expected_verify = 145else:46raise ValueError("invalid result field")48if num_vectors != 0 and sig_size != 0:49signatures += ",\n "51new_msg = False52msg = to_c_array(test_vector['msg'])53msg_offset = offset_msg_running54# check for repeated msg55if msg not in cache_msgs:56if num_vectors != 0 and msg_size != 0:57messages += ",\n "58cache_msgs[msg] = offset_msg_running59messages += msg60new_msg = True61else:62msg_offset = cache_msgs[msg]64new_pk = False65pk = to_c_array(public_key['uncompressed'])66pk_offset = offset_pk_running67# check for repeated pk68if pk not in cache_public_keys:69if num_vectors != 0:70public_keys += ",\n "71cache_public_keys[pk] = offset_pk_running72public_keys += pk73new_pk = True74else:75pk_offset = cache_public_keys[pk]77signatures += to_c_array(test_vector['sig'])79out += " /" + "* tcId: " + str(test_vector['tcId']) + ". " + test_vector['comment'] + " *" + "/\n"80out += f" {{{pk_offset}, {msg_offset}, {msg_size}, {offset_sig}, {sig_size}, {expected_verify} }},\n"81if new_msg:82offset_msg_running += msg_size83if new_pk:84offset_pk_running += 6585offset_sig += sig_size86num_vectors += 188struct_definition = """89typedef struct {90size_t pk_offset;91size_t msg_offset;92size_t msg_len;93size_t sig_offset;94size_t sig_len;95int expected_verify;96} wycheproof_ecdsa_testvector;97"""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] = {")110print(out)111print("};")
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.