Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions bip-0089/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from typing import Dict, Mapping, Optional, Sequence, Tuple, NewType, NamedTuple, List, Callable, Any, cast
import hashlib
import json
import os
from pathlib import Path
import secrets
import sys

from bip32 import (
CURVE_N,
Expand Down Expand Up @@ -280,7 +279,7 @@ def build_session_ctx(obj):
return (pk, a, e, R, tweaks, is_xonly)

def test_blind_nonce_gen_vectors():
with open(os.path.join(sys.path[0], 'vectors', 'blind_nonce_gen_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'blind_nonce_gen_vectors.json') as f:
tv = json.load(f)

for tc in tv["test_cases"]:
Expand Down Expand Up @@ -311,7 +310,7 @@ def get_bytes_maybe(key) -> Optional[bytes]:
assert len(expected_blindpubnonce) == 33

def test_blind_challenge_gen_vectors():
with open(os.path.join(sys.path[0], 'vectors', 'blind_challenge_gen_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'blind_challenge_gen_vectors.json') as f:
tv = json.load(f)

# ---------- Valid cases ----------
Expand Down Expand Up @@ -388,7 +387,7 @@ def test_blind_challenge_gen_vectors():
assert raised, "Expected an exception but none was raised"

def test_blind_sign_and_verify_vectors():
with open(os.path.join(sys.path[0], 'vectors', 'blind_sign_and_verify_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'blind_sign_and_verify_vectors.json') as f:
tv = json.load(f)

# ------------------ Valid ------------------
Expand Down Expand Up @@ -479,7 +478,7 @@ def try_again():
assert_raises(exception, lambda: verify_blind_signature(pk, blindpubnonce, blindchallenge, blindsignature, pk_parity, nonce_parity), except_fn)

def test_unblind_signature_vectors():
with open(os.path.join(sys.path[0], 'vectors', 'unblind_signature_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'unblind_signature_vectors.json') as f:
tv = json.load(f)

# ---------- Valid ----------
Expand Down Expand Up @@ -619,7 +618,7 @@ def delegator_sign(
return signature

def test_compute_tweak_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'compute_bip32_tweak_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'compute_bip32_tweak_vectors.json') as f:
data = json.load(f)

default_xpub_data = data.get("xpub")
Expand Down Expand Up @@ -675,7 +674,7 @@ def test_compute_tweak_vectors() -> None:
raise AssertionError("expected failure but case succeeded")

def test_delegator_sign_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'delegator_sign_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'delegator_sign_vectors.json') as f:
data = json.load(f)

for case in data.get("test_cases", []):
Expand Down Expand Up @@ -706,7 +705,7 @@ def test_delegator_sign_vectors() -> None:


def test_input_verification_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'input_verification_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'input_verification_vectors.json') as f:
data = json.load(f)


Expand All @@ -729,7 +728,7 @@ def test_input_verification_vectors() -> None:
)

def test_change_output_verification_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'change_output_verification_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'change_output_verification_vectors.json') as f:
data = json.load(f)

for case in data.get("test_cases", []):
Expand Down
6 changes: 4 additions & 2 deletions bip-0327/gen_vectors_helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

from reference import *

def gen_key_agg_vectors():
Expand All @@ -16,7 +18,7 @@ def gen_key_agg_vectors():
print(" tweak: ", tweak.hex().upper())

def check_sign_verify_vectors():
with open(os.path.join(sys.path[0], 'vectors', 'sign_verify_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'sign_verify_vectors.json') as f:
test_data = json.load(f)
X = fromhex_all(test_data["pubkeys"])
pnonce = fromhex_all(test_data["pnonces"])
Expand Down Expand Up @@ -44,7 +46,7 @@ def check_sign_verify_vectors():
assert has_even_y(Q) and has_even_y(R)

def check_tweak_vectors():
with open(os.path.join(sys.path[0], 'vectors', 'tweak_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'tweak_vectors.json') as f:
test_data = json.load(f)

X = fromhex_all(test_data["pubkeys"])
Expand Down
19 changes: 9 additions & 10 deletions bip-0327/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ def partial_sig_agg(psigs: List[bytes], session_ctx: SessionContext) -> bytes:
#

import json
import os
import sys
from pathlib import Path

def fromhex_all(l):
return [bytes.fromhex(l_i) for l_i in l]
Expand Down Expand Up @@ -497,7 +496,7 @@ def get_error_details(test_case):
return exception, except_fn

def test_key_sort_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'key_sort_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'key_sort_vectors.json') as f:
test_data = json.load(f)

X = fromhex_all(test_data["pubkeys"])
Expand All @@ -506,7 +505,7 @@ def test_key_sort_vectors() -> None:
assert key_sort(X) == X_sorted

def test_key_agg_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'key_agg_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'key_agg_vectors.json') as f:
test_data = json.load(f)

X = fromhex_all(test_data["pubkeys"])
Expand All @@ -530,7 +529,7 @@ def test_key_agg_vectors() -> None:
assert_raises(exception, lambda: key_agg_and_tweak(pubkeys, tweaks, is_xonly), except_fn)

def test_nonce_gen_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'nonce_gen_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'nonce_gen_vectors.json') as f:
test_data = json.load(f)

for test_case in test_data["test_cases"]:
Expand All @@ -557,7 +556,7 @@ def get_value_maybe(key) -> Optional[bytes]:
assert nonce_gen_internal(rand_, sk, pk, aggpk, msg, extra_in) == (expected_secnonce, expected_pubnonce)

def test_nonce_agg_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'nonce_agg_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'nonce_agg_vectors.json') as f:
test_data = json.load(f)

pnonce = fromhex_all(test_data["pnonces"])
Expand All @@ -575,7 +574,7 @@ def test_nonce_agg_vectors() -> None:
assert_raises(exception, lambda: nonce_agg(pubnonces), except_fn)

def test_sign_verify_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'sign_verify_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'sign_verify_vectors.json') as f:
test_data = json.load(f)

sk = bytes.fromhex(test_data["sk"])
Expand Down Expand Up @@ -658,7 +657,7 @@ def test_sign_verify_vectors() -> None:
assert_raises(exception, lambda: partial_sig_verify(sig, pubnonces, pubkeys, [], [], msg, signer_index), except_fn)

def test_tweak_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'tweak_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'tweak_vectors.json') as f:
test_data = json.load(f)

sk = bytes.fromhex(test_data["sk"])
Expand Down Expand Up @@ -715,7 +714,7 @@ def test_tweak_vectors() -> None:
assert_raises(exception, lambda: sign(secnonce, sk, session_ctx), except_fn)

def test_det_sign_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'det_sign_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'det_sign_vectors.json') as f:
test_data = json.load(f)

sk = bytes.fromhex(test_data["sk"])
Expand Down Expand Up @@ -762,7 +761,7 @@ def test_det_sign_vectors() -> None:
assert_raises(exception, try_fn, except_fn)

def test_sig_agg_vectors() -> None:
with open(os.path.join(sys.path[0], 'vectors', 'sig_agg_vectors.json')) as f:
with open(Path(__file__).parent / 'vectors' / 'sig_agg_vectors.json') as f:
test_data = json.load(f)

X = fromhex_all(test_data["pubkeys"])
Expand Down
5 changes: 2 additions & 3 deletions bip-0328/reference.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#! /usr/bin/env python3

import json
import os
import sys
from pathlib import Path

from _base58 import xpub_to_pub_hex
from _bip327 import cbytes, key_agg
Expand All @@ -14,7 +13,7 @@ def aggregate_to_xpub(aggregate: bytes) -> ExtendedKey:
return ExtendedKey(ExtendedKey.MAINNET_PUBLIC, 0, b"\x00\x00\x00\x00", 0, CHAINCODE, None, aggregate)

def test_aggregate_to_xpub():
with open(os.path.join(sys.path[0], "vectors.json"), "r") as f:
with open(Path(__file__).parent / "vectors.json", "r") as f:
test_data = json.load(f)

for test_case in test_data:
Expand Down
5 changes: 2 additions & 3 deletions bip-0340/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ def schnorr_verify(msg: bytes, pubkey: bytes, sig: bytes) -> bool:
# The following code is only used to verify the test vectors.
#
import csv
import os
import sys
from pathlib import Path

def test_vectors() -> bool:
all_passed = True
with open(os.path.join(sys.path[0], 'test-vectors.csv'), newline='') as csvfile:
with open(Path(__file__).parent / 'test-vectors.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
reader.__next__()
for row in reader:
Expand Down