diff --git a/bip-0327/gen_vectors_helper.py b/bip-0327/gen_vectors_helper.py index d2f6ad4f..32b3e35f 100644 --- a/bip-0327/gen_vectors_helper.py +++ b/bip-0327/gen_vectors_helper.py @@ -15,6 +15,38 @@ def gen_key_agg_vectors(): print(" pubkey:", pk.hex().upper()) print(" tweak: ", tweak.hex().upper()) +def gen_sign_verify_vectors(): + print("sign_verify_vectors.json: Signature exceeds group size but is valid modulo n") + with open(os.path.join(sys.path[0], 'vectors', 'sign_verify_vectors.json')) as f: + test_data = json.load(f) + X = fromhex_all(test_data["pubkeys"]) + pnonce = fromhex_all(test_data["pnonces"]) + aggnonce = bytes.fromhex(test_data["aggnonces"][0]) + msg = bytes.fromhex(test_data["msgs"][0]) + pubkeys = X[0:3] + + session_ctx = SessionContext(aggnonce, pubkeys, [], [], msg) + (Q, gacc, _, b, R, e) = get_session_values(session_ctx) + P = cpoint(pubkeys[0]) + a = get_session_key_agg_coeff(session_ctx, P) + g = 1 if has_even_y(Q) else n - 1 + # Choose the first half of the pubnonce of signer 0 such that the effective + # nonce of signer 0 is -e*a*g*gacc*P. Then the partial signature 0 is + # valid. Since n*G = 0*G, the partial signature n satisfies the + # verification equation as well, so only the range check rejects it. + T = point_mul(P, e * a * g * gacc % n) + Re_s_ = point_negate(T) if has_even_y(R) else T + R_s2 = cpoint(pnonce[0][33:66]) + R_s1 = point_add(Re_s_, point_negate(point_mul(R_s2, b))) + # Change the first half of the pubnonce of signer 2 such that the + # aggregate nonce, and thus b, R and e, stay the same. + R_s1_2 = point_add(cpoint(aggnonce[0:33]), point_negate(point_add(R_s1, cpoint(pnonce[1][0:33])))) + pubnonces = [cbytes(R_s1) + pnonce[0][33:66], pnonce[1], cbytes(R_s1_2) + pnonce[2][33:66]] + assert nonce_agg(pubnonces) == aggnonce + assert partial_sig_verify(bytes_from_int(0), pubnonces, pubkeys, [], [], msg, 0) + print(" pnonce of signer 0:", pubnonces[0].hex().upper()) + print(" pnonce of signer 2:", pubnonces[2].hex().upper()) + def check_sign_verify_vectors(): with open(os.path.join(sys.path[0], 'vectors', 'sign_verify_vectors.json')) as f: test_data = json.load(f) @@ -187,6 +219,7 @@ def sig_agg_vectors(): }, indent=4)) gen_key_agg_vectors() +gen_sign_verify_vectors() check_sign_verify_vectors() check_tweak_vectors() print() diff --git a/bip-0327/vectors/sign_verify_vectors.json b/bip-0327/vectors/sign_verify_vectors.json index c4a74bd4..66b6402e 100644 --- a/bip-0327/vectors/sign_verify_vectors.json +++ b/bip-0327/vectors/sign_verify_vectors.json @@ -20,7 +20,9 @@ "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", "032DE2662628C90B03F5E720284EB52FF7D71F4284F627B68A853D78C78E1FFE9303E4C5524E83FFE1493B9077CF1CA6BEB2090C93D930321071AD40B2F44E599046", "0237C87821AFD50A8644D820A8F3E02E499C931865C2360FB43D0A0D20DAFE07EA0387BF891D2A6DEAEBADC909352AA9405D1428C15F4B75F04DAE642A95C2548480", - "0200000000000000000000000000000000000000000000000000000000000000090287BF891D2A6DEAEBADC909352AA9405D1428C15F4B75F04DAE642A95C2548480" + "0200000000000000000000000000000000000000000000000000000000000000090287BF891D2A6DEAEBADC909352AA9405D1428C15F4B75F04DAE642A95C2548480", + "0394D770EB9681D29BDCF7B4CD6A7B25CF5E1688BBA5E8601C9A84B40BF7BFBBB90287BF891D2A6DEAEBADC909352AA9405D1428C15F4B75F04DAE642A95C2548480", + "034EB831A6C253C123F87AF3B9C56757306A0FAD45816D0A58406BB16CA16A56CC03E4C5524E83FFE1493B9077CF1CA6BEB2090C93D930321071AD40B2F44E599046" ], "aggnonces": [ "028465FCF0BBDBCF443AABCCE533D42B4B5A10966AC09A49655E8C42DAAB8FCD61037496A3CC86926D452CAFCFD55D25972CA1675D549310DE296BFF42F72EEEA8C9", @@ -245,10 +247,10 @@ { "sig": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", "key_indices": [0, 1, 2], - "nonce_indices": [0, 1, 2], + "nonce_indices": [5, 1, 6], "msg_index": 0, "signer_index": 0, - "comment": "Signature exceeds group size" + "comment": "Signature exceeds group size but is valid modulo n. Detects implementations that omit the range check of the partial signature or check s <= n instead of s < n." } ], "verify_error_test_cases": [