diff --git a/bip-musig2/reference.py b/bip-musig2/reference.py index f8dc446cd2..0c3769aa09 100644 --- a/bip-musig2/reference.py +++ b/bip-musig2/reference.py @@ -480,7 +480,7 @@ def test_nonce_agg_vectors(): neg_G = point_mul(G, n - 1) assert nonce_agg([pnonce[0][0:33] + cbytes(G), pnonce[1][0:33] + cbytes(neg_G)]) == expected[0][0:33] + cbytes(G) -def test_sign_vectors(): +def test_sign_verify_vectors(): X = fromhex_all([ 'F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9', 'DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659', @@ -490,6 +490,16 @@ def test_sign_vectors(): '508B81A611F100A6B2B6B29656590898AF488BCF2E1F55CF22E5CFB84421FE61' + 'FA27FD49B1D50085B481285E1CA205D55C82CC1B31FF5CD54A489829355901F7') + # The public nonce corresponding to secnonce is at index 0 + pnonce = fromhex_all([ + '0337C87821AFD50A8644D820A8F3E02E499C931865C2360FB43D0A0D20DAFE07EA' + + '0287BF891D2A6DEAEBADC909352AA9405D1428C15F4B75F04DAE642A95C2548480', + '0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798' + + '0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', + '032DE2662628C90B03F5E720284EB52FF7D71F4284F627B68A853D78C78E1FFE93' + + '03E4C5524E83FFE1493B9077CF1CA6BEB2090C93D930321071AD40B2F44E599046' + ]) + aggnonce = bytes.fromhex( '028465FCF0BBDBCF443AABCCE533D42B4B5A10966AC09A49655E8C42DAAB8FCD61' + '037496A3CC86926D452CAFCFD55D25972CA1675D549310DE296BFF42F72EEEA8C9') @@ -525,7 +535,7 @@ def test_sign_vectors(): session_ctx = SessionContext(aggnonce, [X[0], pk, invalid_pk], [], [], msg) assertRaises(InvalidContributionError, lambda: sign(secnonce, sk, session_ctx), - lambda e: e.signer == 2) + lambda e: e.signer == 2 and e.contrib == "pubkey") # Vector 5: Aggregate nonce is invalid due wrong tag, 0x04, in the first # half @@ -535,7 +545,7 @@ def test_sign_vectors(): session_ctx = SessionContext(invalid_aggnonce, [X[0], X[1], pk], [], [], msg) assertRaises(InvalidContributionError, lambda: sign(secnonce, sk, session_ctx), - lambda e: e.signer == None) + lambda e: e.signer == None and e.contrib == "aggnonce") # Vector 6: Aggregate nonce is invalid because the second half does not # correspond to an X coordinate invalid_aggnonce = bytes.fromhex( @@ -544,7 +554,7 @@ def test_sign_vectors(): session_ctx = SessionContext(invalid_aggnonce, [X[0], X[1], pk], [], [], msg) assertRaises(InvalidContributionError, lambda: sign(secnonce, sk, session_ctx), - lambda e: e.signer == None) + lambda e: e.signer == None and e.contrib == "aggnonce") # Vector 7: Aggregate nonce is invalid because second half exceeds field # size invalid_aggnonce = bytes.fromhex( @@ -553,7 +563,33 @@ def test_sign_vectors(): session_ctx = SessionContext(invalid_aggnonce, [X[0], X[1], pk], [], [], msg) assertRaises(InvalidContributionError, lambda: sign(secnonce, sk, session_ctx), - lambda e: e.signer == None) + lambda e: e.signer == None and e.contrib == "aggnonce") + + # Verification test vectors + # Vector 8 + assert partial_sig_verify(expected[0], pnonce, [pk, X[0], X[1]], [], [], msg, 0) + # Vector 9 + assert partial_sig_verify(expected[1], [pnonce[1], pnonce[0], pnonce[2]], [X[0], pk, X[1]], [], [], msg, 1) + # Vector 10 + assert partial_sig_verify(expected[2], [pnonce[1], pnonce[2], pnonce[0]], [X[0], X[1], pk], [], [], msg, 2) + + # Vector 11: Wrong signature (which is equal to the negation of valid signature expected[0]) + wrong_sig = bytes.fromhex('97AC833ADCB1AFA42EBF9E0725616F3C9A0D5B614F6FE283CEAAA37A8FFAF406') + assert not partial_sig_verify(wrong_sig, pnonce, [pk, X[0], X[1]], [], [], msg, 0) + # Vector 12: Wrong signer + assert not partial_sig_verify(expected[0], pnonce, [pk, X[0], X[1]], [], [], msg, 1) + # Vector 13: Signature exceeds group size + wrong_sig = bytes.fromhex('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141') + assert not partial_sig_verify(wrong_sig, pnonce, [pk, X[0], X[1]], [], [], msg, 0) + # Vector 14: Invalid pubnonce + invalid_pubnonce = bytes.fromhex('020000000000000000000000000000000000000000000000000000000000000009') + assertRaises(InvalidContributionError, + lambda: partial_sig_verify(expected[0], [invalid_pubnonce, pnonce[1], pnonce[2]], [pk, X[0], X[1]], [], [], msg, 0), + lambda e: e.signer == 0 and e.contrib == "pubnonce") + # Vector 15: Invalid public key + assertRaises(InvalidContributionError, + lambda: partial_sig_verify(expected[0], pnonce, [invalid_pk, X[0], X[1]], [], [], msg, 0), + lambda e: e.signer == 0 and e.contrib == "pubkey") def test_tweak_vectors(): X = fromhex_all([ @@ -663,6 +699,6 @@ def test_sign_and_verify_random(iters): test_key_agg_vectors() test_nonce_gen_vectors() test_nonce_agg_vectors() - test_sign_vectors() + test_sign_verify_vectors() test_tweak_vectors() test_sign_and_verify_random(4)