Skip to content

Commit 1c6ac0c

Browse files
committed
bip327: minor fixes
- An error test vector doesn’t specify the InvalidContributionError type - In *DeterministicSign*, use GetXonlyPubkey instead of GetPubkey - The key_agg_and_tweak fn doesn’t specify the return type - In partial_sig_verify_internal, the pubkey arg should be PlainPk - Remove unused enumerate() fn calls - In test_sign_verify, add an additional assert statement
1 parent af8f9e4 commit 1c6ac0c

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

bip-0327.mediawiki

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ Algorithm ''DeterministicSign(sk, aggothernonce, pk<sub>1..u</sub>, tweak<sub>1.
619619
* Let ''keyagg_ctx<sub>0</sub> = KeyAgg(pk<sub>1..u</sub>)''; fail if that fails
620620
* For ''i = 1 .. v'':
621621
** Let ''keyagg_ctx<sub>i</sub> = ApplyTweak(keyagg_ctx<sub>i-1</sub>, tweak<sub>i</sub>, is_xonly_t<sub>i</sub>)''; fail if that fails
622-
* Let ''aggpk = GetPubkey(keyagg_ctx<sub>v</sub>)''
622+
* Let ''aggpk = GetXonlyPubkey(keyagg_ctx<sub>v</sub>)''
623623
* Let ''k<sub>i</sub> = int(hash<sub>MuSig/deterministic/nonce</sub>(sk' || aggothernonce || aggpk || bytes(8, len(m)) || m || bytes(1, i - 1))) mod n'' for ''i = 1,2''
624624
* Fail if ''k<sub>1</sub> = 0'' or ''k<sub>2</sub> = 0''
625625
* Let ''R<sub>⁎,1</sub> = k<sub>1</sub>⋅G, R<sub>⁎,2</sub> = k<sub>2</sub>⋅G''

bip-0327/gen_vectors_helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ def sig_agg_vectors():
153153
"psig_indices": [7, 8],
154154
"error": {
155155
"type": "invalid_contribution",
156-
"signer": 1
156+
"signer": 1,
157+
"contrib": "psig",
157158
},
158159
"comment": "Partial signature is invalid because it exceeds group size"
159160
}

bip-0327/reference.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def nonce_agg(pubnonces: List[bytes]) -> bytes:
317317
('is_xonly', List[bool]),
318318
('msg', bytes)])
319319

320-
def key_agg_and_tweak(pubkeys: List[PlainPk], tweaks: List[bytes], is_xonly: List[bool]):
320+
def key_agg_and_tweak(pubkeys: List[PlainPk], tweaks: List[bytes], is_xonly: List[bool]) -> KeyAggContext:
321321
if len(tweaks) != len(is_xonly):
322322
raise ValueError('The `tweaks` and `is_xonly` arrays must have the same length.')
323323
keyagg_ctx = key_agg(pubkeys)
@@ -367,7 +367,7 @@ def sign(secnonce: bytearray, sk: bytes, session_ctx: SessionContext) -> bytes:
367367
raise ValueError('secret key value is out of range.')
368368
P = point_mul(G, d_)
369369
assert P is not None
370-
pk = cbytes(P)
370+
pk = PlainPk(cbytes(P))
371371
if not pk == secnonce[64:97]:
372372
raise ValueError('Public key does not match nonce_gen argument')
373373
a = get_session_key_agg_coeff(session_ctx, P)
@@ -430,7 +430,7 @@ def partial_sig_verify(psig: bytes, pubnonces: List[bytes], pubkeys: List[PlainP
430430
session_ctx = SessionContext(aggnonce, pubkeys, tweaks, is_xonly, msg)
431431
return partial_sig_verify_internal(psig, pubnonces[i], pubkeys[i], session_ctx)
432432

433-
def partial_sig_verify_internal(psig: bytes, pubnonce: bytes, pk: bytes, session_ctx: SessionContext) -> bool:
433+
def partial_sig_verify_internal(psig: bytes, pubnonce: bytes, pk: PlainPk, session_ctx: SessionContext) -> bool:
434434
(Q, gacc, _, b, R, e) = get_session_values(session_ctx)
435435
s = int_from_bytes(psig)
436436
if s >= n:
@@ -523,7 +523,7 @@ def test_key_agg_vectors() -> None:
523523

524524
assert get_xonly_pk(key_agg(pubkeys)) == expected
525525

526-
for i, test_case in enumerate(error_test_cases):
526+
for test_case in error_test_cases:
527527
exception, except_fn = get_error_details(test_case)
528528

529529
pubkeys = [X[i] for i in test_case["key_indices"]]
@@ -572,7 +572,7 @@ def test_nonce_agg_vectors() -> None:
572572
expected = bytes.fromhex(test_case["expected"])
573573
assert nonce_agg(pubnonces) == expected
574574

575-
for i, test_case in enumerate(error_test_cases):
575+
for test_case in error_test_cases:
576576
exception, except_fn = get_error_details(test_case)
577577
pubnonces = [pnonce[i] for i in test_case["pnonce_indices"]]
578578
assert_raises(exception, lambda: nonce_agg(pubnonces), except_fn)
@@ -598,7 +598,10 @@ def test_sign_verify_vectors() -> None:
598598

599599
aggnonces = fromhex_all(test_data["aggnonces"])
600600
# The aggregate of the first three elements of pnonce is at index 0
601-
assert(aggnonces[0] == nonce_agg([pnonce[0], pnonce[1], pnonce[2]]))
601+
assert (aggnonces[0] == nonce_agg([pnonce[0], pnonce[1], pnonce[2]]))
602+
# The aggregate of the first and fourth elements of pnonce is at index 1,
603+
# which is the infinity point encoded as a zeroed 33-byte array
604+
assert (aggnonces[1] == nonce_agg([pnonce[0], pnonce[3]]))
602605

603606
msgs = fromhex_all(test_data["msgs"])
604607

@@ -626,7 +629,7 @@ def test_sign_verify_vectors() -> None:
626629
assert sign(secnonce_tmp, sk, session_ctx) == expected
627630
assert partial_sig_verify(expected, pubnonces, pubkeys, [], [], msg, signer_index)
628631

629-
for i, test_case in enumerate(sign_error_test_cases):
632+
for test_case in sign_error_test_cases:
630633
exception, except_fn = get_error_details(test_case)
631634

632635
pubkeys = [X[i] for i in test_case["key_indices"]]
@@ -646,7 +649,7 @@ def test_sign_verify_vectors() -> None:
646649

647650
assert not partial_sig_verify(sig, pubnonces, pubkeys, [], [], msg, signer_index)
648651

649-
for i, test_case in enumerate(verify_error_test_cases):
652+
for test_case in verify_error_test_cases:
650653
exception, except_fn = get_error_details(test_case)
651654

652655
sig = bytes.fromhex(test_case["sig"])
@@ -702,7 +705,7 @@ def test_tweak_vectors() -> None:
702705
assert sign(secnonce_tmp, sk, session_ctx) == expected
703706
assert partial_sig_verify(expected, pubnonces, pubkeys, tweaks, is_xonly, msg, signer_index)
704707

705-
for i, test_case in enumerate(error_test_cases):
708+
for test_case in error_test_cases:
706709
exception, except_fn = get_error_details(test_case)
707710

708711
pubkeys = [X[i] for i in test_case["key_indices"]]
@@ -747,7 +750,7 @@ def test_det_sign_vectors() -> None:
747750
session_ctx = SessionContext(aggnonce, pubkeys, tweaks, is_xonly, msg)
748751
assert partial_sig_verify_internal(psig, pubnonce, pubkeys[signer_index], session_ctx)
749752

750-
for i, test_case in enumerate(error_test_cases):
753+
for test_case in error_test_cases:
751754
exception, except_fn = get_error_details(test_case)
752755

753756
pubkeys = [X[i] for i in test_case["key_indices"]]
@@ -796,7 +799,7 @@ def test_sig_agg_vectors() -> None:
796799
aggpk = get_xonly_pk(key_agg_and_tweak(pubkeys, tweaks, is_xonly))
797800
assert schnorr_verify(msg, aggpk, sig)
798801

799-
for i, test_case in enumerate(error_test_cases):
802+
for test_case in error_test_cases:
800803
exception, except_fn = get_error_details(test_case)
801804

802805
pubnonces = [pnonce[i] for i in test_case["nonce_indices"]]

bip-0327/vectors/sig_agg_vectors.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
],
144144
"error": {
145145
"type": "invalid_contribution",
146-
"signer": 1
146+
"signer": 1,
147+
"contrib": "psig"
147148
},
148149
"comment": "Partial signature is invalid because it exceeds group size"
149150
}

0 commit comments

Comments
 (0)