Skip to content

Commit e8664b2

Browse files
authored
Merge pull request bitcoin#1620 from theStack/bip352-mention-input_pubkey_sum-infinity-case
BIP-352: handle invalid privkey / pubkey sums for sending / scanning, add changelog
2 parents 70a7143 + 496e429 commit e8664b2

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

bip-0352.mediawiki

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ After the inputs have been selected, the sender can create one or more outputs f
302302
* Collect the private keys for each input from the ''[[#inputs-for-shared-secret-derivation|Inputs For Shared Secret Derivation]]'' list
303303
* For each private key ''a<sub>i</sub>'' corresponding to a [https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki BIP341] taproot output, check that the private key produces a point with an even Y coordinate and negate the private key if not<ref name="why_negate_taproot_private_keys">'''Why do taproot private keys need to be checked?''' Recall from [https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki BIP340] that each X-only public key has two corresponding private keys, ''d'' and ''n - d''. To maintain parity between sender and receiver, it is necessary to use the private key corresponding to the even Y coordinate when performing the ECDH step since the receiver will assume the even Y coordinate when summing the taproot X-only public keys.</ref>
304304
* Let ''a = a<sub>1</sub> + a<sub>2</sub> + ... + a<sub>n</sub>'', where each ''a<sub>i</sub>'' has been negated if necessary
305+
** If ''a = 0'', fail
305306
* Generate the ''input_hash'' with the smallest outpoint lexicographically and ''A = a·G'', using the method described above
306307
* Group receiver silent payment addresses by ''B<sub>scan</sub>'' (e.g. each group consists of one ''B<sub>scan</sub>'' and one or more ''B<sub>m</sub>'')
307308
* For each group:
@@ -334,6 +335,7 @@ A scan and spend key pair using BIP32 derivation are defined (taking inspiration
334335
If each of the checks in ''[[#scanning-silent-payment-eligible-transactions|Scanning silent payment eligible transactions]]'' passes, the receiving wallet must:
335336

336337
* Let ''A = A<sub>1</sub> + A<sub>2</sub> + ... + A<sub>n</sub>'', where each ''A<sub>i</sub>'' is the public key of an input from the ''[[#inputs-for-shared-secret-derivation|Inputs For Shared Secret Derivation]]'' list
338+
** If ''A'' is the point at infinity, skip the transaction
337339
* Generate the ''input_hash'' with the smallest outpoint lexicographically and ''A'', using the method described above
338340
* Let ''ecdh_shared_secret = input_hash·b<sub>scan</sub>·A''
339341
* Check for outputs:
@@ -481,6 +483,17 @@ A malicious notification could potentially cause the following issues:
481483
482484
Wallet designers can choose which tradeoffs they find appropriate. For example, a wallet could check the block filter to at least probabilistically confirm the likely existence of the UTXO, thus efficiently cutting down on spam. The payment could then be marked as unconfirmed until a scan is performed and the existence of the UTXO in accordance to the silent payment specification is verified.
483485

486+
== Change Log ==
487+
488+
To help implementers understand updates to this document, we attach a version number that resembles ''semantic versioning'' (<code>MAJOR.MINOR.PATCH</code>).
489+
The <code>MAJOR</code> version is incremented if changes to the BIP are introduced that are incompatible with prior versions.
490+
The <code>MINOR</code> version is incremented whenever the inputs or the output of an algorithm changes in a backward-compatible way or new backward-compatible functionality is added.
491+
The <code>PATCH</code> version is incremented for other changes that are noteworthy (bug fixes, test vectors, important clarifications, etc.).
492+
493+
* '''1.0.1''' (2024-06-22):
494+
** Add steps to fail if private key sum is zero (for sender) or public key sum is point at infinity (for receiver), add corresponding test vectors.
495+
* '''1.0.0''' (2024-05-08):
496+
** Initial version, merged as BIP-352.
484497
485498
== Acknowledgements ==
486499

bip-0352/reference.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ def create_outputs(input_priv_keys: List[Tuple[ECKey, bool]], outpoints: List[CO
127127
negated_keys.append(k)
128128

129129
a_sum = sum(negated_keys)
130+
if not a_sum.valid:
131+
# Input privkeys sum is zero -> fail
132+
return []
130133
input_hash = get_input_hash(outpoints, a_sum * G)
131134
silent_payment_groups: Dict[ECPubKey, List[ECPubKey]] = {}
132135
for recipient in recipients:
@@ -297,6 +300,10 @@ def scanning(b_scan: ECKey, B_spend: ECPubKey, A_sum: ECPubKey, input_hash: byte
297300
add_to_wallet = []
298301
if (len(input_pub_keys) > 0):
299302
A_sum = reduce(lambda x, y: x + y, input_pub_keys)
303+
if A_sum.get_bytes() is None:
304+
# Input pubkeys sum is point at infinity -> skip tx
305+
assert expected["outputs"] == []
306+
continue
300307
input_hash = get_input_hash([vin.outpoint for vin in vins], A_sum)
301308
pre_computed_labels = {
302309
(generate_label(b_scan, label) * G).get_bytes(False).hex(): generate_label(b_scan, label).hex()

bip-0352/send_and_receive_test_vectors.json

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2669,5 +2669,92 @@
26692669
}
26702670
}
26712671
]
2672+
},
2673+
{
2674+
"comment": "Input keys sum up to zero / point at infinity: sending fails, receiver skips tx",
2675+
"sending": [
2676+
{
2677+
"given": {
2678+
"vin": [
2679+
{
2680+
"txid": "3a286147b25e16ae80aff406f2673c6e565418c40f45c071245cdebc8a94174e",
2681+
"vout": 0,
2682+
"scriptSig": "",
2683+
"txinwitness": "024730440220085003179ce1a3a88ce0069aa6ea045e140761ab88c22a26ae2a8cfe983a6e4602204a8a39940f0735c8a4424270ac8da65240c261ab3fda9272f6d6efbf9cfea366012102557ef3e55b0a52489b4454c1169e06bdea43687a69c1f190eb50781644ab6975",
2684+
"prevout": {
2685+
"scriptPubKey": {
2686+
"hex": "00149d9e24f9fab4e35bf1a6df4b46cb533296ac0792"
2687+
}
2688+
},
2689+
"private_key": "a6df6a0bb448992a301df4258e06a89fe7cf7146f59ac3bd5ff26083acb22ceb"
2690+
},
2691+
{
2692+
"txid": "3a286147b25e16ae80aff406f2673c6e565418c40f45c071245cdebc8a94174e",
2693+
"vout": 1,
2694+
"scriptSig": "",
2695+
"txinwitness": "0247304402204586a68e1d97dd3c6928e3622799859f8c3b20c3c670cf654cc905c9be29fdb7022043fbcde1689f3f4045e8816caf6163624bd19e62e4565bc99f95c533e599782c012103557ef3e55b0a52489b4454c1169e06bdea43687a69c1f190eb50781644ab6975",
2696+
"prevout": {
2697+
"scriptPubKey": {
2698+
"hex": "00149860538b5575962776ed0814ae222c7d60c72d7b"
2699+
}
2700+
},
2701+
"private_key": "592095f44bb766d5cfe20bda71f9575ed2df6b9fb9addc7e5fdffe0923841456"
2702+
}
2703+
],
2704+
"recipients": [
2705+
"sp1qqtrqglu5g8kh6mfsg4qxa9wq0nv9cauwfwxw70984wkqnw2uwz0w2qnehen8a7wuhwk9tgrzjh8gwzc8q2dlekedec5djk0js9d3d7qhnq6lqj3s"
2706+
]
2707+
},
2708+
"expected": {
2709+
"outputs": [
2710+
[]
2711+
]
2712+
}
2713+
}
2714+
],
2715+
"receiving": [
2716+
{
2717+
"given": {
2718+
"vin": [
2719+
{
2720+
"txid": "3a286147b25e16ae80aff406f2673c6e565418c40f45c071245cdebc8a94174e",
2721+
"vout": 0,
2722+
"scriptSig": "",
2723+
"txinwitness": "024730440220085003179ce1a3a88ce0069aa6ea045e140761ab88c22a26ae2a8cfe983a6e4602204a8a39940f0735c8a4424270ac8da65240c261ab3fda9272f6d6efbf9cfea366012102557ef3e55b0a52489b4454c1169e06bdea43687a69c1f190eb50781644ab6975",
2724+
"prevout": {
2725+
"scriptPubKey": {
2726+
"hex": "00149d9e24f9fab4e35bf1a6df4b46cb533296ac0792"
2727+
}
2728+
}
2729+
},
2730+
{
2731+
"txid": "3a286147b25e16ae80aff406f2673c6e565418c40f45c071245cdebc8a94174e",
2732+
"vout": 1,
2733+
"scriptSig": "",
2734+
"txinwitness": "0247304402204586a68e1d97dd3c6928e3622799859f8c3b20c3c670cf654cc905c9be29fdb7022043fbcde1689f3f4045e8816caf6163624bd19e62e4565bc99f95c533e599782c012103557ef3e55b0a52489b4454c1169e06bdea43687a69c1f190eb50781644ab6975",
2735+
"prevout": {
2736+
"scriptPubKey": {
2737+
"hex": "00149860538b5575962776ed0814ae222c7d60c72d7b"
2738+
}
2739+
}
2740+
}
2741+
],
2742+
"outputs": [
2743+
"0000000000000000000000000000000000000000000000000000000000000000"
2744+
],
2745+
"key_material": {
2746+
"spend_priv_key": "0000000000000000000000000000000000000000000000000000000000000001",
2747+
"scan_priv_key": "0000000000000000000000000000000000000000000000000000000000000002"
2748+
},
2749+
"labels": []
2750+
},
2751+
"expected": {
2752+
"addresses": [
2753+
"sp1qqtrqglu5g8kh6mfsg4qxa9wq0nv9cauwfwxw70984wkqnw2uwz0w2qnehen8a7wuhwk9tgrzjh8gwzc8q2dlekedec5djk0js9d3d7qhnq6lqj3s"
2754+
],
2755+
"outputs": []
2756+
}
2757+
}
2758+
]
26722759
}
2673-
]
2760+
]

0 commit comments

Comments
 (0)