secp256k1
This commit is contained in:
74
secp256k1/tools/check-abi.sh
Executable file
74
secp256k1/tools/check-abi.sh
Executable file
@@ -0,0 +1,74 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
default_base_version="$(git describe --match "v*.*.*" --abbrev=0)"
|
||||
default_new_version="HEAD"
|
||||
|
||||
display_help_and_exit() {
|
||||
echo "Usage: $0 [<base_ver> [<new_ver>]]"
|
||||
echo ""
|
||||
echo "Description: This script uses the ABI Compliance Checker tool to determine if the ABI"
|
||||
echo " of a new version of libsecp256k1 has changed in a backward-incompatible way."
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " base_ver Specify the base version as a git commit-ish"
|
||||
echo " (default: most recent reachable tag matching \"v.*.*\", currently \"$default_base_version\")"
|
||||
echo " new_ver Specify the new version as a git commit-ish"
|
||||
echo " (default: $default_new_version)"
|
||||
echo " -h, --help Display this help message"
|
||||
exit 0
|
||||
}
|
||||
|
||||
if [ "$#" -eq 0 ]; then
|
||||
base_version="$default_base_version"
|
||||
new_version="$default_new_version"
|
||||
elif [ "$#" -eq 1 ] && { [ "$1" = "-h" ] || [ "$1" = "--help" ]; }; then
|
||||
display_help_and_exit
|
||||
elif [ "$#" -eq 1 ] || [ "$#" -eq 2 ]; then
|
||||
base_version="$1"
|
||||
if [ "$#" -eq 2 ]; then
|
||||
new_version="$2"
|
||||
fi
|
||||
else
|
||||
echo "Invalid usage. See help:"
|
||||
echo ""
|
||||
display_help_and_exit
|
||||
fi
|
||||
|
||||
checkout_and_build() {
|
||||
_orig_dir="$(pwd)"
|
||||
git worktree add --detach "$1" "$2"
|
||||
cd "$1"
|
||||
mkdir build && cd build
|
||||
cmake -S .. --preset dev-mode \
|
||||
-DCMAKE_C_COMPILER=gcc -DCMAKE_BUILD_TYPE=None -DCMAKE_C_FLAGS="-g -Og -gdwarf-4" \
|
||||
-DSECP256K1_BUILD_BENCHMARK=OFF \
|
||||
-DSECP256K1_BUILD_TESTS=OFF \
|
||||
-DSECP256K1_BUILD_EXHAUSTIVE_TESTS=OFF \
|
||||
-DSECP256K1_BUILD_CTIME_TESTS=OFF \
|
||||
-DSECP256K1_BUILD_EXAMPLES=OFF
|
||||
cmake --build . -j "$(nproc)"
|
||||
# FIXME: Just set LIBPATH to lib/libsecp256k1.so once version 0.6.0 is
|
||||
# released.
|
||||
if [ -f "src/libsecp256k1.so" ]; then
|
||||
LIBPATH="src/libsecp256k1.so"
|
||||
else
|
||||
LIBPATH="lib/libsecp256k1.so"
|
||||
fi
|
||||
abi-dumper $LIBPATH -o ABI.dump -lver "$2" -public-headers ../include/
|
||||
cd "$_orig_dir"
|
||||
}
|
||||
|
||||
echo "Comparing $base_version (base version) to $new_version (new version)"
|
||||
echo
|
||||
|
||||
base_source_dir="$(mktemp -d)"
|
||||
checkout_and_build "$base_source_dir" "$base_version"
|
||||
|
||||
new_source_dir="$(mktemp -d)"
|
||||
checkout_and_build "$new_source_dir" "$new_version"
|
||||
|
||||
abi-compliance-checker -lib libsecp256k1 -old "${base_source_dir}/build/ABI.dump" -new "${new_source_dir}/build/ABI.dump"
|
||||
git worktree remove "$base_source_dir"
|
||||
git worktree remove "$new_source_dir"
|
||||
72
secp256k1/tools/symbol-check.py
Executable file
72
secp256k1/tools/symbol-check.py
Executable file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check that a libsecp256k1 shared library exports only expected symbols.
|
||||
|
||||
Usage examples:
|
||||
- When building with Autotools:
|
||||
./tools/symbol-check.py .libs/libsecp256k1.so
|
||||
./tools/symbol-check.py .libs/libsecp256k1-<V>.dll
|
||||
./tools/symbol-check.py .libs/libsecp256k1.dylib
|
||||
|
||||
- When building with CMake:
|
||||
./tools/symbol-check.py build/lib/libsecp256k1.so
|
||||
./tools/symbol-check.py build/bin/libsecp256k1-<V>.dll
|
||||
./tools/symbol-check.py build/lib/libsecp256k1.dylib"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
import lief
|
||||
|
||||
|
||||
class UnexpectedExport(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def get_exported_exports(library) -> list[str]:
|
||||
"""Adapter function to get exported symbols based on the library format."""
|
||||
if library.format == lief.Binary.FORMATS.ELF:
|
||||
return [symbol.name for symbol in library.exported_symbols]
|
||||
elif library.format == lief.Binary.FORMATS.PE:
|
||||
return [entry.name for entry in library.get_export().entries]
|
||||
elif library.format == lief.Binary.FORMATS.MACHO:
|
||||
return [symbol.name[1:] for symbol in library.exported_symbols]
|
||||
raise NotImplementedError(f"Unsupported format: {library.format}")
|
||||
|
||||
|
||||
def grep_expected_symbols() -> list[str]:
|
||||
"""Guess the list of expected exported symbols from the source code."""
|
||||
grep_output = subprocess.check_output(
|
||||
["git", "grep", r"^\s*SECP256K1_API", "--", "include"],
|
||||
universal_newlines=True,
|
||||
encoding="utf-8"
|
||||
)
|
||||
lines = grep_output.split("\n")
|
||||
pattern = re.compile(r'\bsecp256k1_\w+')
|
||||
exported: list[str] = [pattern.findall(line)[-1] for line in lines if line.strip()]
|
||||
return exported
|
||||
|
||||
|
||||
def check_symbols(library, expected_exports) -> None:
|
||||
"""Check that the library exports only the expected symbols."""
|
||||
actual_exports = get_exported_exports(library)
|
||||
unexpected_exports = set(actual_exports) - set(expected_exports)
|
||||
if unexpected_exports != set():
|
||||
raise UnexpectedExport(f"Unexpected exported symbols: {unexpected_exports}")
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print(__doc__)
|
||||
return 1
|
||||
library = lief.parse(sys.argv[1])
|
||||
expected_exports = grep_expected_symbols()
|
||||
try:
|
||||
check_symbols(library, expected_exports)
|
||||
except UnexpectedExport as e:
|
||||
print(f"{sys.argv[0]}: In {sys.argv[1]}: {e}")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
656
secp256k1/tools/test_vectors_musig2_generate.py
Executable file
656
secp256k1/tools/test_vectors_musig2_generate.py
Executable file
@@ -0,0 +1,656 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import json
|
||||
import textwrap
|
||||
|
||||
max_pubkeys = 0
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print(
|
||||
"This script converts BIP MuSig2 test vectors in a given directory to a C file that can be used in the test framework."
|
||||
)
|
||||
print("Usage: %s <dir>" % sys.argv[0])
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def hexstr_to_intarray(str):
|
||||
return ", ".join([f"0x{b:02X}" for b in bytes.fromhex(str)])
|
||||
|
||||
|
||||
def create_init(name):
|
||||
return """
|
||||
static const struct musig_%s_vector musig_%s_vector = {
|
||||
""" % (
|
||||
name,
|
||||
name,
|
||||
)
|
||||
|
||||
|
||||
def init_array(key):
|
||||
return textwrap.indent("{ %s },\n" % hexstr_to_intarray(data[key]), 4 * " ")
|
||||
|
||||
|
||||
def init_arrays(key):
|
||||
s = textwrap.indent("{\n", 4 * " ")
|
||||
s += textwrap.indent(
|
||||
",\n".join(["{ %s }" % hexstr_to_intarray(x) for x in data[key]]), 8 * " "
|
||||
)
|
||||
s += textwrap.indent("\n},\n", 4 * " ")
|
||||
return s
|
||||
|
||||
|
||||
def init_indices(array):
|
||||
return " %d, { %s }" % (
|
||||
len(array),
|
||||
", ".join(map(str, array) if len(array) > 0 else "0"),
|
||||
)
|
||||
|
||||
|
||||
def init_is_xonly(case):
|
||||
if len(case["tweak_indices"]) > 0:
|
||||
return ", ".join(map(lambda x: "1" if x else "0", case["is_xonly"]))
|
||||
return "0"
|
||||
|
||||
|
||||
def init_optional_expected(case):
|
||||
return hexstr_to_intarray(case["expected"]) if "expected" in case else 0
|
||||
|
||||
|
||||
def init_cases(cases, f):
|
||||
s = textwrap.indent("{\n", 4 * " ")
|
||||
for (i, case) in enumerate(cases):
|
||||
s += textwrap.indent("%s\n" % f(case), 8 * " ")
|
||||
s += textwrap.indent("},\n", 4 * " ")
|
||||
return s
|
||||
|
||||
|
||||
def finish_init():
|
||||
return "};\n"
|
||||
|
||||
|
||||
s = (
|
||||
"""/**
|
||||
* Automatically generated by %s.
|
||||
*
|
||||
* The test vectors for the KeySort function are included in this file. They can
|
||||
* be found in src/modules/extrakeys/tests_impl.h. */
|
||||
"""
|
||||
% sys.argv[0]
|
||||
)
|
||||
|
||||
|
||||
s += """
|
||||
enum MUSIG_ERROR {
|
||||
MUSIG_PUBKEY,
|
||||
MUSIG_TWEAK,
|
||||
MUSIG_PUBNONCE,
|
||||
MUSIG_AGGNONCE,
|
||||
MUSIG_SECNONCE,
|
||||
MUSIG_SIG,
|
||||
MUSIG_SIG_VERIFY,
|
||||
MUSIG_OTHER
|
||||
};
|
||||
"""
|
||||
|
||||
# key agg vectors
|
||||
with open(sys.argv[1] + "/key_agg_vectors.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
max_key_indices = max(
|
||||
len(test_case["key_indices"]) for test_case in data["valid_test_cases"]
|
||||
)
|
||||
max_tweak_indices = max(
|
||||
len(test_case["tweak_indices"]) for test_case in data["error_test_cases"]
|
||||
)
|
||||
num_pubkeys = len(data["pubkeys"])
|
||||
max_pubkeys = max(num_pubkeys, max_pubkeys)
|
||||
num_tweaks = len(data["tweaks"])
|
||||
num_valid_cases = len(data["valid_test_cases"])
|
||||
num_error_cases = len(data["error_test_cases"])
|
||||
|
||||
# Add structures for valid and error cases
|
||||
s += (
|
||||
"""
|
||||
struct musig_key_agg_valid_test_case {
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
unsigned char expected[32];
|
||||
};
|
||||
"""
|
||||
% max_key_indices
|
||||
)
|
||||
s += """
|
||||
struct musig_key_agg_error_test_case {
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
size_t tweak_indices_len;
|
||||
size_t tweak_indices[%d];
|
||||
int is_xonly[%d];
|
||||
enum MUSIG_ERROR error;
|
||||
};
|
||||
""" % (
|
||||
max_key_indices,
|
||||
max_tweak_indices,
|
||||
max_tweak_indices,
|
||||
)
|
||||
|
||||
# Add structure for entire vector
|
||||
s += """
|
||||
struct musig_key_agg_vector {
|
||||
unsigned char pubkeys[%d][33];
|
||||
unsigned char tweaks[%d][32];
|
||||
struct musig_key_agg_valid_test_case valid_case[%d];
|
||||
struct musig_key_agg_error_test_case error_case[%d];
|
||||
};
|
||||
""" % (
|
||||
num_pubkeys,
|
||||
num_tweaks,
|
||||
num_valid_cases,
|
||||
num_error_cases,
|
||||
)
|
||||
|
||||
s += create_init("key_agg")
|
||||
# Add pubkeys and tweaks to the vector
|
||||
s += init_arrays("pubkeys")
|
||||
s += init_arrays("tweaks")
|
||||
|
||||
# Add valid cases to the vector
|
||||
s += init_cases(
|
||||
data["valid_test_cases"],
|
||||
lambda case: "{ %s, { %s }},"
|
||||
% (init_indices(case["key_indices"]), hexstr_to_intarray(case["expected"])),
|
||||
)
|
||||
|
||||
def comment_to_error(case):
|
||||
comment = case["comment"]
|
||||
if "public key" in comment.lower():
|
||||
return "MUSIG_PUBKEY"
|
||||
elif "tweak" in comment.lower():
|
||||
return "MUSIG_TWEAK"
|
||||
else:
|
||||
sys.exit("Unknown error")
|
||||
|
||||
# Add error cases to the vector
|
||||
s += init_cases(
|
||||
data["error_test_cases"],
|
||||
lambda case: "{ %s, %s, { %s }, %s },"
|
||||
% (
|
||||
init_indices(case["key_indices"]),
|
||||
init_indices(case["tweak_indices"]),
|
||||
init_is_xonly(case),
|
||||
comment_to_error(case),
|
||||
),
|
||||
)
|
||||
|
||||
s += finish_init()
|
||||
|
||||
# nonce gen vectors
|
||||
with open(sys.argv[1] + "/nonce_gen_vectors.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
# The MuSig2 implementation only allows messages of length 32
|
||||
data["test_cases"] = list(
|
||||
filter(lambda c: c["msg"] is None or len(c["msg"]) == 64, data["test_cases"])
|
||||
)
|
||||
|
||||
num_tests = len(data["test_cases"])
|
||||
|
||||
s += """
|
||||
struct musig_nonce_gen_test_case {
|
||||
unsigned char rand_[32];
|
||||
int has_sk;
|
||||
unsigned char sk[32];
|
||||
unsigned char pk[33];
|
||||
int has_aggpk;
|
||||
unsigned char aggpk[32];
|
||||
int has_msg;
|
||||
unsigned char msg[32];
|
||||
int has_extra_in;
|
||||
unsigned char extra_in[32];
|
||||
unsigned char expected_secnonce[97];
|
||||
unsigned char expected_pubnonce[66];
|
||||
};
|
||||
"""
|
||||
|
||||
s += (
|
||||
"""
|
||||
struct musig_nonce_gen_vector {
|
||||
struct musig_nonce_gen_test_case test_case[%d];
|
||||
};
|
||||
"""
|
||||
% num_tests
|
||||
)
|
||||
|
||||
s += create_init("nonce_gen")
|
||||
|
||||
def init_array_maybe(array):
|
||||
return "%d , { %s }" % (
|
||||
0 if array is None else 1,
|
||||
hexstr_to_intarray(array) if array is not None else 0,
|
||||
)
|
||||
|
||||
s += init_cases(
|
||||
data["test_cases"],
|
||||
lambda case: "{ { %s }, %s, { %s }, %s, %s, %s, { %s }, { %s } },"
|
||||
% (
|
||||
hexstr_to_intarray(case["rand_"]),
|
||||
init_array_maybe(case["sk"]),
|
||||
hexstr_to_intarray(case["pk"]),
|
||||
init_array_maybe(case["aggpk"]),
|
||||
init_array_maybe(case["msg"]),
|
||||
init_array_maybe(case["extra_in"]),
|
||||
hexstr_to_intarray(case["expected_secnonce"]),
|
||||
hexstr_to_intarray(case["expected_pubnonce"]),
|
||||
),
|
||||
)
|
||||
|
||||
s += finish_init()
|
||||
|
||||
# nonce agg vectors
|
||||
with open(sys.argv[1] + "/nonce_agg_vectors.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
num_pnonces = len(data["pnonces"])
|
||||
num_valid_cases = len(data["valid_test_cases"])
|
||||
num_error_cases = len(data["error_test_cases"])
|
||||
|
||||
pnonce_indices_len = 2
|
||||
for case in data["valid_test_cases"] + data["error_test_cases"]:
|
||||
assert len(case["pnonce_indices"]) == pnonce_indices_len
|
||||
|
||||
# Add structures for valid and error cases
|
||||
s += """
|
||||
struct musig_nonce_agg_test_case {
|
||||
size_t pnonce_indices[2];
|
||||
/* if valid case */
|
||||
unsigned char expected[66];
|
||||
/* if error case */
|
||||
int invalid_nonce_idx;
|
||||
};
|
||||
"""
|
||||
# Add structure for entire vector
|
||||
s += """
|
||||
struct musig_nonce_agg_vector {
|
||||
unsigned char pnonces[%d][66];
|
||||
struct musig_nonce_agg_test_case valid_case[%d];
|
||||
struct musig_nonce_agg_test_case error_case[%d];
|
||||
};
|
||||
""" % (
|
||||
num_pnonces,
|
||||
num_valid_cases,
|
||||
num_error_cases,
|
||||
)
|
||||
|
||||
s += create_init("nonce_agg")
|
||||
s += init_arrays("pnonces")
|
||||
|
||||
for cases in (data["valid_test_cases"], data["error_test_cases"]):
|
||||
s += init_cases(
|
||||
cases,
|
||||
lambda case: "{ { %s }, { %s }, %d },"
|
||||
% (
|
||||
", ".join(map(str, case["pnonce_indices"])),
|
||||
init_optional_expected(case),
|
||||
case["error"]["signer"] if "error" in case else 0,
|
||||
),
|
||||
)
|
||||
s += finish_init()
|
||||
|
||||
# sign/verify vectors
|
||||
with open(sys.argv[1] + "/sign_verify_vectors.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
# The MuSig2 implementation only allows messages of length 32
|
||||
assert list(filter(lambda x: len(x) == 64, data["msgs"]))[0] == data["msgs"][0]
|
||||
data["msgs"] = [data["msgs"][0]]
|
||||
|
||||
def filter_msg32(k):
|
||||
return list(filter(lambda x: x["msg_index"] == 0, data[k]))
|
||||
|
||||
data["valid_test_cases"] = filter_msg32("valid_test_cases")
|
||||
data["sign_error_test_cases"] = filter_msg32("sign_error_test_cases")
|
||||
data["verify_error_test_cases"] = filter_msg32("verify_error_test_cases")
|
||||
data["verify_fail_test_cases"] = filter_msg32("verify_fail_test_cases")
|
||||
|
||||
num_pubkeys = len(data["pubkeys"])
|
||||
max_pubkeys = max(num_pubkeys, max_pubkeys)
|
||||
num_secnonces = len(data["secnonces"])
|
||||
num_pubnonces = len(data["pnonces"])
|
||||
num_aggnonces = len(data["aggnonces"])
|
||||
num_msgs = len(data["msgs"])
|
||||
num_valid_cases = len(data["valid_test_cases"])
|
||||
num_sign_error_cases = len(data["sign_error_test_cases"])
|
||||
num_verify_fail_cases = len(data["verify_fail_test_cases"])
|
||||
num_verify_error_cases = len(data["verify_error_test_cases"])
|
||||
|
||||
all_cases = (
|
||||
data["valid_test_cases"]
|
||||
+ data["sign_error_test_cases"]
|
||||
+ data["verify_error_test_cases"]
|
||||
+ data["verify_fail_test_cases"]
|
||||
)
|
||||
max_key_indices = max(len(test_case["key_indices"]) for test_case in all_cases)
|
||||
max_nonce_indices = max(
|
||||
len(test_case["nonce_indices"]) if "nonce_indices" in test_case else 0
|
||||
for test_case in all_cases
|
||||
)
|
||||
# Add structures for valid and error cases
|
||||
s += (
|
||||
"""
|
||||
/* Omit pubnonces in the test vectors because our partial signature verification
|
||||
* implementation is able to accept the aggnonce directly. */
|
||||
struct musig_valid_case {
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
size_t aggnonce_index;
|
||||
size_t msg_index;
|
||||
size_t signer_index;
|
||||
unsigned char expected[32];
|
||||
};
|
||||
"""
|
||||
% max_key_indices
|
||||
)
|
||||
|
||||
s += (
|
||||
"""
|
||||
struct musig_sign_error_case {
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
size_t aggnonce_index;
|
||||
size_t msg_index;
|
||||
size_t secnonce_index;
|
||||
enum MUSIG_ERROR error;
|
||||
};
|
||||
"""
|
||||
% max_key_indices
|
||||
)
|
||||
|
||||
s += """
|
||||
struct musig_verify_fail_error_case {
|
||||
unsigned char sig[32];
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
size_t nonce_indices_len;
|
||||
size_t nonce_indices[%d];
|
||||
size_t msg_index;
|
||||
size_t signer_index;
|
||||
enum MUSIG_ERROR error;
|
||||
};
|
||||
""" % (
|
||||
max_key_indices,
|
||||
max_nonce_indices,
|
||||
)
|
||||
|
||||
# Add structure for entire vector
|
||||
s += """
|
||||
struct musig_sign_verify_vector {
|
||||
unsigned char sk[32];
|
||||
unsigned char pubkeys[%d][33];
|
||||
unsigned char secnonces[%d][194];
|
||||
unsigned char pubnonces[%d][194];
|
||||
unsigned char aggnonces[%d][66];
|
||||
unsigned char msgs[%d][32];
|
||||
struct musig_valid_case valid_case[%d];
|
||||
struct musig_sign_error_case sign_error_case[%d];
|
||||
struct musig_verify_fail_error_case verify_fail_case[%d];
|
||||
struct musig_verify_fail_error_case verify_error_case[%d];
|
||||
};
|
||||
""" % (
|
||||
num_pubkeys,
|
||||
num_secnonces,
|
||||
num_pubnonces,
|
||||
num_aggnonces,
|
||||
num_msgs,
|
||||
num_valid_cases,
|
||||
num_sign_error_cases,
|
||||
num_verify_fail_cases,
|
||||
num_verify_error_cases,
|
||||
)
|
||||
|
||||
s += create_init("sign_verify")
|
||||
s += init_array("sk")
|
||||
s += init_arrays("pubkeys")
|
||||
s += init_arrays("secnonces")
|
||||
s += init_arrays("pnonces")
|
||||
s += init_arrays("aggnonces")
|
||||
s += init_arrays("msgs")
|
||||
|
||||
s += init_cases(
|
||||
data["valid_test_cases"],
|
||||
lambda case: "{ %s, %d, %d, %d, { %s }},"
|
||||
% (
|
||||
init_indices(case["key_indices"]),
|
||||
case["aggnonce_index"],
|
||||
case["msg_index"],
|
||||
case["signer_index"],
|
||||
init_optional_expected(case),
|
||||
),
|
||||
)
|
||||
|
||||
def sign_error(case):
|
||||
comment = case["comment"]
|
||||
if "pubkey" in comment or "public key" in comment:
|
||||
return "MUSIG_PUBKEY"
|
||||
elif "Aggregate nonce" in comment:
|
||||
return "MUSIG_AGGNONCE"
|
||||
elif "Secnonce" in comment:
|
||||
return "MUSIG_SECNONCE"
|
||||
else:
|
||||
sys.exit("Unknown sign error")
|
||||
|
||||
s += init_cases(
|
||||
data["sign_error_test_cases"],
|
||||
lambda case: "{ %s, %d, %d, %d, %s },"
|
||||
% (
|
||||
init_indices(case["key_indices"]),
|
||||
case["aggnonce_index"],
|
||||
case["msg_index"],
|
||||
case["secnonce_index"],
|
||||
sign_error(case),
|
||||
),
|
||||
)
|
||||
|
||||
def verify_error(case):
|
||||
comment = case["comment"]
|
||||
if "exceeds" in comment:
|
||||
return "MUSIG_SIG"
|
||||
elif "Wrong signer" in comment or "Wrong signature" in comment:
|
||||
return "MUSIG_SIG_VERIFY"
|
||||
elif "pubnonce" in comment:
|
||||
return "MUSIG_PUBNONCE"
|
||||
elif "pubkey" in comment:
|
||||
return "MUSIG_PUBKEY"
|
||||
else:
|
||||
sys.exit("Unknown verify error")
|
||||
|
||||
for cases in ("verify_fail_test_cases", "verify_error_test_cases"):
|
||||
s += init_cases(
|
||||
data[cases],
|
||||
lambda case: "{ { %s }, %s, %s, %d, %d, %s },"
|
||||
% (
|
||||
hexstr_to_intarray(case["sig"]),
|
||||
init_indices(case["key_indices"]),
|
||||
init_indices(case["nonce_indices"]),
|
||||
case["msg_index"],
|
||||
case["signer_index"],
|
||||
verify_error(case),
|
||||
),
|
||||
)
|
||||
|
||||
s += finish_init()
|
||||
|
||||
# tweak vectors
|
||||
with open(sys.argv[1] + "/tweak_vectors.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
num_pubkeys = len(data["pubkeys"])
|
||||
max_pubkeys = max(num_pubkeys, max_pubkeys)
|
||||
num_pubnonces = len(data["pnonces"])
|
||||
num_tweaks = len(data["tweaks"])
|
||||
num_valid_cases = len(data["valid_test_cases"])
|
||||
num_error_cases = len(data["error_test_cases"])
|
||||
|
||||
all_cases = data["valid_test_cases"] + data["error_test_cases"]
|
||||
max_key_indices = max(len(test_case["key_indices"]) for test_case in all_cases)
|
||||
max_tweak_indices = max(len(test_case["tweak_indices"]) for test_case in all_cases)
|
||||
max_nonce_indices = max(len(test_case["nonce_indices"]) for test_case in all_cases)
|
||||
# Add structures for valid and error cases
|
||||
s += """
|
||||
struct musig_tweak_case {
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
size_t nonce_indices_len;
|
||||
size_t nonce_indices[%d];
|
||||
size_t tweak_indices_len;
|
||||
size_t tweak_indices[%d];
|
||||
int is_xonly[%d];
|
||||
size_t signer_index;
|
||||
unsigned char expected[32];
|
||||
};
|
||||
""" % (
|
||||
max_key_indices,
|
||||
max_nonce_indices,
|
||||
max_tweak_indices,
|
||||
max_tweak_indices,
|
||||
)
|
||||
|
||||
# Add structure for entire vector
|
||||
s += """
|
||||
struct musig_tweak_vector {
|
||||
unsigned char sk[32];
|
||||
unsigned char secnonce[97];
|
||||
unsigned char aggnonce[66];
|
||||
unsigned char msg[32];
|
||||
unsigned char pubkeys[%d][33];
|
||||
unsigned char pubnonces[%d][194];
|
||||
unsigned char tweaks[%d][32];
|
||||
struct musig_tweak_case valid_case[%d];
|
||||
struct musig_tweak_case error_case[%d];
|
||||
};
|
||||
""" % (
|
||||
num_pubkeys,
|
||||
num_pubnonces,
|
||||
num_tweaks,
|
||||
num_valid_cases,
|
||||
num_error_cases,
|
||||
)
|
||||
s += create_init("tweak")
|
||||
s += init_array("sk")
|
||||
s += init_array("secnonce")
|
||||
s += init_array("aggnonce")
|
||||
s += init_array("msg")
|
||||
s += init_arrays("pubkeys")
|
||||
s += init_arrays("pnonces")
|
||||
s += init_arrays("tweaks")
|
||||
|
||||
s += init_cases(
|
||||
data["valid_test_cases"],
|
||||
lambda case: "{ %s, %s, %s, { %s }, %d, { %s }},"
|
||||
% (
|
||||
init_indices(case["key_indices"]),
|
||||
init_indices(case["nonce_indices"]),
|
||||
init_indices(case["tweak_indices"]),
|
||||
init_is_xonly(case),
|
||||
case["signer_index"],
|
||||
init_optional_expected(case),
|
||||
),
|
||||
)
|
||||
|
||||
s += init_cases(
|
||||
data["error_test_cases"],
|
||||
lambda case: "{ %s, %s, %s, { %s }, %d, { %s }},"
|
||||
% (
|
||||
init_indices(case["key_indices"]),
|
||||
init_indices(case["nonce_indices"]),
|
||||
init_indices(case["tweak_indices"]),
|
||||
init_is_xonly(case),
|
||||
case["signer_index"],
|
||||
init_optional_expected(case),
|
||||
),
|
||||
)
|
||||
|
||||
s += finish_init()
|
||||
|
||||
# sigagg vectors
|
||||
with open(sys.argv[1] + "/sig_agg_vectors.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
num_pubkeys = len(data["pubkeys"])
|
||||
max_pubkeys = max(num_pubkeys, max_pubkeys)
|
||||
num_tweaks = len(data["tweaks"])
|
||||
num_psigs = len(data["psigs"])
|
||||
num_valid_cases = len(data["valid_test_cases"])
|
||||
num_error_cases = len(data["error_test_cases"])
|
||||
|
||||
all_cases = data["valid_test_cases"] + data["error_test_cases"]
|
||||
max_key_indices = max(len(test_case["key_indices"]) for test_case in all_cases)
|
||||
max_tweak_indices = max(len(test_case["tweak_indices"]) for test_case in all_cases)
|
||||
max_psig_indices = max(len(test_case["psig_indices"]) for test_case in all_cases)
|
||||
|
||||
# Add structures for valid and error cases
|
||||
s += """
|
||||
/* Omit pubnonces in the test vectors because they're only needed for
|
||||
* implementations that do not directly accept an aggnonce. */
|
||||
struct musig_sig_agg_case {
|
||||
size_t key_indices_len;
|
||||
size_t key_indices[%d];
|
||||
size_t tweak_indices_len;
|
||||
size_t tweak_indices[%d];
|
||||
int is_xonly[%d];
|
||||
unsigned char aggnonce[66];
|
||||
size_t psig_indices_len;
|
||||
size_t psig_indices[%d];
|
||||
/* if valid case */
|
||||
unsigned char expected[64];
|
||||
/* if error case */
|
||||
int invalid_sig_idx;
|
||||
};
|
||||
""" % (
|
||||
max_key_indices,
|
||||
max_tweak_indices,
|
||||
max_tweak_indices,
|
||||
max_psig_indices,
|
||||
)
|
||||
|
||||
# Add structure for entire vector
|
||||
s += """
|
||||
struct musig_sig_agg_vector {
|
||||
unsigned char pubkeys[%d][33];
|
||||
unsigned char tweaks[%d][32];
|
||||
unsigned char psigs[%d][32];
|
||||
unsigned char msg[32];
|
||||
struct musig_sig_agg_case valid_case[%d];
|
||||
struct musig_sig_agg_case error_case[%d];
|
||||
};
|
||||
""" % (
|
||||
num_pubkeys,
|
||||
num_tweaks,
|
||||
num_psigs,
|
||||
num_valid_cases,
|
||||
num_error_cases,
|
||||
)
|
||||
|
||||
s += create_init("sig_agg")
|
||||
s += init_arrays("pubkeys")
|
||||
s += init_arrays("tweaks")
|
||||
s += init_arrays("psigs")
|
||||
s += init_array("msg")
|
||||
|
||||
for cases in (data["valid_test_cases"], data["error_test_cases"]):
|
||||
s += init_cases(
|
||||
cases,
|
||||
lambda case: "{ %s, %s, { %s }, { %s }, %s, { %s }, %d },"
|
||||
% (
|
||||
init_indices(case["key_indices"]),
|
||||
init_indices(case["tweak_indices"]),
|
||||
init_is_xonly(case),
|
||||
hexstr_to_intarray(case["aggnonce"]),
|
||||
init_indices(case["psig_indices"]),
|
||||
init_optional_expected(case),
|
||||
case["error"]["signer"] if "error" in case else 0,
|
||||
),
|
||||
)
|
||||
s += finish_init()
|
||||
s += "enum { MUSIG_VECTORS_MAX_PUBKEYS = %d };" % max_pubkeys
|
||||
print(s)
|
||||
166
secp256k1/tools/tests_wycheproof_generate_ecdh.py
Executable file
166
secp256k1/tools/tests_wycheproof_generate_ecdh.py
Executable file
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2024 Random "Randy" Lattice and Sean Andersen
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Generate a C file with ECDH testvectors from the Wycheproof project.
|
||||
'''
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
from binascii import hexlify, unhexlify
|
||||
from wycheproof_utils import to_c_array
|
||||
|
||||
def should_skip_flags(test_vector_flags):
|
||||
# skip these vectors because they are for ASN.1 encoding issues and other curves.
|
||||
# for more details, see https://github.com/bitcoin-core/secp256k1/pull/1492#discussion_r1572491546
|
||||
flags_to_skip = {"InvalidAsn", "WrongCurve"}
|
||||
return any(flag in test_vector_flags for flag in flags_to_skip)
|
||||
|
||||
def should_skip_tcid(test_vector_tcid):
|
||||
# We skip some test case IDs that have a public key whose custom ASN.1 representation explicitly
|
||||
# encodes some curve parameters that are invalid. libsecp256k1 never parses this part so we do
|
||||
# not care testing those. See https://github.com/bitcoin-core/secp256k1/pull/1492#discussion_r1572491546
|
||||
tcids_to_skip = [496, 497, 502, 503, 504, 505, 507]
|
||||
return test_vector_tcid in tcids_to_skip
|
||||
|
||||
# Rudimentary ASN.1 DER public key parser.
|
||||
# This should not be used for anything other than parsing Wycheproof test vectors.
|
||||
def parse_der_pk(s):
|
||||
tag = s[0]
|
||||
L = int(s[1])
|
||||
offset = 0
|
||||
if L & 0x80:
|
||||
if L == 0x81:
|
||||
L = int(s[2])
|
||||
offset = 1
|
||||
elif L == 0x82:
|
||||
L = 256 * int(s[2]) + int(s[3])
|
||||
offset = 2
|
||||
else:
|
||||
raise ValueError("invalid L")
|
||||
value = s[(offset + 2):(L + 2 + offset)]
|
||||
rest = s[(L + 2 + offset):]
|
||||
|
||||
if len(rest) > 0 or tag == 0x06: # OBJECT IDENTIFIER
|
||||
return parse_der_pk(rest)
|
||||
if tag == 0x03: # BIT STRING
|
||||
return value
|
||||
if tag == 0x30: # SEQUENCE
|
||||
return parse_der_pk(value)
|
||||
raise ValueError("unknown tag")
|
||||
|
||||
def parse_public_key(pk):
|
||||
der_pub_key = parse_der_pk(unhexlify(pk)) # Convert back to str and strip off the `0x`
|
||||
return hexlify(der_pub_key).decode()[2:]
|
||||
|
||||
def normalize_private_key(sk):
|
||||
# Ensure the private key is at most 64 characters long, retaining the last 64 if longer.
|
||||
# In the wycheproof test vectors, some private keys have leading zeroes
|
||||
normalized = sk[-64:].zfill(64)
|
||||
if len(normalized) != 64:
|
||||
raise ValueError("private key must be exactly 64 characters long.")
|
||||
return normalized
|
||||
|
||||
def normalize_expected_result(er):
|
||||
result_mapping = {"invalid": 0, "valid": 1, "acceptable": 1}
|
||||
return result_mapping[er]
|
||||
|
||||
filename_input = sys.argv[1]
|
||||
|
||||
with open(filename_input) as f:
|
||||
doc = json.load(f)
|
||||
|
||||
num_vectors = 0
|
||||
offset_sk_running, offset_pk_running, offset_shared = 0, 0, 0
|
||||
test_vectors_out = ""
|
||||
private_keys = ""
|
||||
shared_secrets = ""
|
||||
public_keys = ""
|
||||
cache_sks = {}
|
||||
cache_public_keys = {}
|
||||
|
||||
for group in doc['testGroups']:
|
||||
assert group["type"] == "EcdhTest"
|
||||
assert group["curve"] == "secp256k1"
|
||||
for test_vector in group['tests']:
|
||||
if should_skip_flags(test_vector['flags']) or should_skip_tcid(test_vector['tcId']):
|
||||
continue
|
||||
|
||||
public_key = parse_public_key(test_vector['public'])
|
||||
private_key = normalize_private_key(test_vector['private'])
|
||||
expected_result = normalize_expected_result(test_vector['result'])
|
||||
|
||||
# // 2 to convert hex to byte length
|
||||
shared_size = len(test_vector['shared']) // 2
|
||||
sk_size = len(private_key) // 2
|
||||
pk_size = len(public_key) // 2
|
||||
|
||||
new_sk = False
|
||||
sk = to_c_array(private_key)
|
||||
sk_offset = offset_sk_running
|
||||
|
||||
# check for repeated sk
|
||||
if sk not in cache_sks:
|
||||
if num_vectors != 0 and sk_size != 0:
|
||||
private_keys += ",\n "
|
||||
cache_sks[sk] = offset_sk_running
|
||||
private_keys += sk
|
||||
new_sk = True
|
||||
else:
|
||||
sk_offset = cache_sks[sk]
|
||||
|
||||
new_pk = False
|
||||
pk = to_c_array(public_key) if public_key != '0x' else ''
|
||||
|
||||
pk_offset = offset_pk_running
|
||||
# check for repeated pk
|
||||
if pk not in cache_public_keys:
|
||||
if num_vectors != 0 and len(pk) != 0:
|
||||
public_keys += ",\n "
|
||||
cache_public_keys[pk] = offset_pk_running
|
||||
public_keys += pk
|
||||
new_pk = True
|
||||
else:
|
||||
pk_offset = cache_public_keys[pk]
|
||||
|
||||
|
||||
shared_secrets += ",\n " if num_vectors and shared_size else ""
|
||||
shared_secrets += to_c_array(test_vector['shared'])
|
||||
wycheproof_tcid = test_vector['tcId']
|
||||
|
||||
test_vectors_out += " /" + "* tcId: " + str(test_vector['tcId']) + ". " + test_vector['comment'] + " *" + "/\n"
|
||||
test_vectors_out += f" {{{pk_offset}, {pk_size}, {sk_offset}, {sk_size}, {offset_shared}, {shared_size}, {expected_result}, {wycheproof_tcid} }},\n"
|
||||
if new_sk:
|
||||
offset_sk_running += sk_size
|
||||
if new_pk:
|
||||
offset_pk_running += pk_size
|
||||
offset_shared += shared_size
|
||||
num_vectors += 1
|
||||
|
||||
struct_definition = """
|
||||
typedef struct {
|
||||
size_t pk_offset;
|
||||
size_t pk_len;
|
||||
size_t sk_offset;
|
||||
size_t sk_len;
|
||||
size_t shared_offset;
|
||||
size_t shared_len;
|
||||
int expected_result;
|
||||
int wycheproof_tcid;
|
||||
} wycheproof_ecdh_testvector;
|
||||
"""
|
||||
|
||||
print("/* Note: this file was autogenerated using tests_wycheproof_ecdh.py. Do not edit. */")
|
||||
print(f"#define SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")
|
||||
|
||||
print(struct_definition)
|
||||
|
||||
print("static const unsigned char wycheproof_ecdh_private_keys[] = { " + private_keys + "};\n")
|
||||
print("static const unsigned char wycheproof_ecdh_public_keys[] = { " + public_keys + "};\n")
|
||||
print("static const unsigned char wycheproof_ecdh_shared_secrets[] = { " + shared_secrets + "};\n")
|
||||
|
||||
print("static const wycheproof_ecdh_testvector testvectors[SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS] = {")
|
||||
print(test_vectors_out)
|
||||
print("};")
|
||||
111
secp256k1/tools/tests_wycheproof_generate_ecdsa.py
Executable file
111
secp256k1/tools/tests_wycheproof_generate_ecdsa.py
Executable file
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2023 Random "Randy" Lattice and Sean Andersen
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Generate a C file with ECDSA testvectors from the Wycheproof project.
|
||||
'''
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
from wycheproof_utils import to_c_array
|
||||
|
||||
filename_input = sys.argv[1]
|
||||
|
||||
with open(filename_input) as f:
|
||||
doc = json.load(f)
|
||||
|
||||
num_groups = len(doc['testGroups'])
|
||||
|
||||
|
||||
num_vectors = 0
|
||||
offset_msg_running, offset_pk_running, offset_sig = 0, 0, 0
|
||||
out = ""
|
||||
messages = ""
|
||||
signatures = ""
|
||||
public_keys = ""
|
||||
cache_msgs = {}
|
||||
cache_public_keys = {}
|
||||
|
||||
for i in range(num_groups):
|
||||
group = doc['testGroups'][i]
|
||||
num_tests = len(group['tests'])
|
||||
public_key = group['publicKey']
|
||||
for j in range(num_tests):
|
||||
test_vector = group['tests'][j]
|
||||
# // 2 to convert hex to byte length
|
||||
sig_size = len(test_vector['sig']) // 2
|
||||
msg_size = len(test_vector['msg']) // 2
|
||||
|
||||
if test_vector['result'] == "invalid":
|
||||
expected_verify = 0
|
||||
elif test_vector['result'] == "valid":
|
||||
expected_verify = 1
|
||||
else:
|
||||
raise ValueError("invalid result field")
|
||||
|
||||
if num_vectors != 0 and sig_size != 0:
|
||||
signatures += ",\n "
|
||||
|
||||
new_msg = False
|
||||
msg = to_c_array(test_vector['msg'])
|
||||
msg_offset = offset_msg_running
|
||||
# check for repeated msg
|
||||
if msg not in cache_msgs:
|
||||
if num_vectors != 0 and msg_size != 0:
|
||||
messages += ",\n "
|
||||
cache_msgs[msg] = offset_msg_running
|
||||
messages += msg
|
||||
new_msg = True
|
||||
else:
|
||||
msg_offset = cache_msgs[msg]
|
||||
|
||||
new_pk = False
|
||||
pk = to_c_array(public_key['uncompressed'])
|
||||
pk_offset = offset_pk_running
|
||||
# check for repeated pk
|
||||
if pk not in cache_public_keys:
|
||||
if num_vectors != 0:
|
||||
public_keys += ",\n "
|
||||
cache_public_keys[pk] = offset_pk_running
|
||||
public_keys += pk
|
||||
new_pk = True
|
||||
else:
|
||||
pk_offset = cache_public_keys[pk]
|
||||
|
||||
signatures += to_c_array(test_vector['sig'])
|
||||
|
||||
out += " /" + "* tcId: " + str(test_vector['tcId']) + ". " + test_vector['comment'] + " *" + "/\n"
|
||||
out += f" {{{pk_offset}, {msg_offset}, {msg_size}, {offset_sig}, {sig_size}, {expected_verify} }},\n"
|
||||
if new_msg:
|
||||
offset_msg_running += msg_size
|
||||
if new_pk:
|
||||
offset_pk_running += 65
|
||||
offset_sig += sig_size
|
||||
num_vectors += 1
|
||||
|
||||
struct_definition = """
|
||||
typedef struct {
|
||||
size_t pk_offset;
|
||||
size_t msg_offset;
|
||||
size_t msg_len;
|
||||
size_t sig_offset;
|
||||
size_t sig_len;
|
||||
int expected_verify;
|
||||
} wycheproof_ecdsa_testvector;
|
||||
"""
|
||||
|
||||
|
||||
print("/* Note: this file was autogenerated using tests_wycheproof_generate_ecdsa.py. Do not edit. */")
|
||||
print(f"#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")
|
||||
|
||||
print(struct_definition)
|
||||
|
||||
print("static const unsigned char wycheproof_ecdsa_messages[] = { " + messages + "};\n")
|
||||
print("static const unsigned char wycheproof_ecdsa_public_keys[] = { " + public_keys + "};\n")
|
||||
print("static const unsigned char wycheproof_ecdsa_signatures[] = { " + signatures + "};\n")
|
||||
|
||||
print("static const wycheproof_ecdsa_testvector testvectors[SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS] = {")
|
||||
print(out)
|
||||
print("};")
|
||||
12
secp256k1/tools/wycheproof_utils.py
Normal file
12
secp256k1/tools/wycheproof_utils.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2024 Random "Randy" Lattice and Sean Andersen
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Utility functions for generating C files for testvectors from the Wycheproof project.
|
||||
'''
|
||||
|
||||
def to_c_array(x):
|
||||
if x == "":
|
||||
return ""
|
||||
s = ',0x'.join(a + b for a, b in zip(x[::2], x[1::2]))
|
||||
return "0x" + s
|
||||
Reference in New Issue
Block a user