Skip to content

Commit 4877fcd

Browse files
committed
Merge bitcoin/bitcoin#30048: crypto: add NUMS_H const
9408a04 tests, fuzz: use new NUMS_H const (josibake) b946f8a crypto: add NUMS_H const (josibake) Pull request description: Broken out from #28122 --- [BIP341](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#constructing-and-spending-taproot-outputs) defines a NUMS point `H` as *H = lift_x(0x50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0)* which is [constructed](https://github.com/ElementsProject/secp256k1-zkp/blob/11af7015de624b010424273be3d91f117f172c82/src/modules/rangeproof/main_impl.h#L16) by taking the hash of the standard uncompressed encoding of the [secp256k1](https://www.secg.org/sec2-v2.pdf) base point G as X coordinate." Add this as a constant so it can be used in our codebase. My primary motivation is BIP352 specifies a special case for when taproot spends use `H` as the internal key, but outside of BIP352 it seems generally useful to have `H` in the codebase, for testing or other use cases. ACKs for top commit: paplorinc: re-ACK 9408a04 achow101: ACK 9408a04 theStack: Code-review ACK 9408a04 Tree-SHA512: ad84492f5d635c0cb05bd82546079ded7e5138e95361f20d8285a9ad6e69c10ee2cc3fe46e16b46ef03c4253c8bee1051911c6b91264c90c3b1ad33a824bff4b
2 parents 2f53f22 + 9408a04 commit 4877fcd

File tree

8 files changed

+39
-10
lines changed

8 files changed

+39
-10
lines changed

src/pubkey.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <secp256k1_schnorrsig.h>
1414
#include <span.h>
1515
#include <uint256.h>
16+
#include <util/strencodings.h>
1617

1718
#include <algorithm>
1819
#include <cassert>
@@ -181,6 +182,17 @@ int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned
181182
return 1;
182183
}
183184

185+
/** Nothing Up My Sleeve (NUMS) point
186+
*
187+
* NUMS_H is a point with an unknown discrete logarithm, constructed by taking the sha256 of 'g'
188+
* (uncompressed encoding), which happens to be a point on the curve.
189+
*
190+
* For an example script for calculating H, refer to the unit tests in
191+
* ./test/functional/test_framework/crypto/secp256k1.py
192+
*/
193+
static const std::vector<unsigned char> NUMS_H_DATA{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
194+
const XOnlyPubKey XOnlyPubKey::NUMS_H{NUMS_H_DATA};
195+
184196
XOnlyPubKey::XOnlyPubKey(Span<const unsigned char> bytes)
185197
{
186198
assert(bytes.size() == 32);

src/pubkey.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ class XOnlyPubKey
233233
uint256 m_keydata;
234234

235235
public:
236+
/** Nothing Up My Sleeve point H
237+
* Used as an internal key for provably disabling the key path spend
238+
* see BIP341 for more details */
239+
static const XOnlyPubKey NUMS_H;
240+
236241
/** Construct an empty x-only pubkey. */
237242
XOnlyPubKey() = default;
238243

src/test/fuzz/miniscript.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,6 @@ const struct KeyComparator {
309309
// A dummy scriptsig to pass to VerifyScript (we always use Segwit v0).
310310
const CScript DUMMY_SCRIPTSIG;
311311

312-
//! Public key to be used as internal key for dummy Taproot spends.
313-
const std::vector<unsigned char> NUMS_PK{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
314-
315312
//! Construct a miniscript node as a shared_ptr.
316313
template<typename... Args> NodeRef MakeNodeRef(Args&&... args) {
317314
return miniscript::MakeNodeRef<CPubKey>(miniscript::internal::NoDupCheck{}, std::forward<Args>(args)...);
@@ -1018,7 +1015,7 @@ CScript ScriptPubKey(MsCtx ctx, const CScript& script, TaprootBuilder& builder)
10181015

10191016
// For Taproot outputs we always use a tree with a single script and a dummy internal key.
10201017
builder.Add(0, script, TAPROOT_LEAF_TAPSCRIPT);
1021-
builder.Finalize(XOnlyPubKey{NUMS_PK});
1018+
builder.Finalize(XOnlyPubKey::NUMS_H);
10221019
return GetScriptForDestination(builder.GetOutput());
10231020
}
10241021

src/test/key_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <common/system.h>
88
#include <key_io.h>
9+
#include <span.h>
910
#include <streams.h>
1011
#include <test/util/random.h>
1112
#include <test/util/setup_common.h>
@@ -364,4 +365,13 @@ BOOST_AUTO_TEST_CASE(key_ellswift)
364365
}
365366
}
366367

368+
BOOST_AUTO_TEST_CASE(bip341_test_h)
369+
{
370+
std::vector<unsigned char> G_uncompressed = ParseHex("0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8");
371+
HashWriter hw;
372+
hw.write(MakeByteSpan(G_uncompressed));
373+
XOnlyPubKey H{hw.GetSHA256()};
374+
BOOST_CHECK(XOnlyPubKey::NUMS_H == H);
375+
}
376+
367377
BOOST_AUTO_TEST_SUITE_END()

src/test/miniscript_tests.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ class TestSignatureChecker : public BaseSignatureChecker {
288288
}
289289
};
290290

291-
//! Public key to be used as internal key for dummy Taproot spends.
292-
const std::vector<unsigned char> NUMS_PK{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
293-
294291
using Fragment = miniscript::Fragment;
295292
using NodeRef = miniscript::NodeRef<CPubKey>;
296293
using miniscript::operator"" _mst;
@@ -330,7 +327,7 @@ CScript ScriptPubKey(miniscript::MiniscriptContext ctx, const CScript& script, T
330327

331328
// For Taproot outputs we always use a tree with a single script and a dummy internal key.
332329
builder.Add(0, script, TAPROOT_LEAF_TAPSCRIPT);
333-
builder.Finalize(XOnlyPubKey{NUMS_PK});
330+
builder.Finalize(XOnlyPubKey::NUMS_H);
334331
return GetScriptForDestination(builder.GetOutput());
335332
}
336333

src/test/script_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,7 @@ BOOST_AUTO_TEST_CASE(sign_invalid_miniscript)
12681268
const auto invalid_pubkey{ParseHex("173d36c8c9c9c9ffffffffffff0200000000021e1e37373721361818181818181e1e1e1e19000000000000000000b19292929292926b006c9b9b9292")};
12691269
TaprootBuilder builder;
12701270
builder.Add(0, {invalid_pubkey}, 0xc0);
1271-
XOnlyPubKey nums{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
1272-
builder.Finalize(nums);
1271+
builder.Finalize(XOnlyPubKey::NUMS_H);
12731272
prev.vout.emplace_back(0, GetScriptForDestination(builder.GetOutput()));
12741273
curr.vin.emplace_back(COutPoint{prev.GetHash(), 0});
12751274
sig_data.tr_spenddata = builder.GetSpendData();

test/functional/feature_framework_unit_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"crypto.muhash",
2626
"crypto.poly1305",
2727
"crypto.ripemd160",
28+
"crypto.secp256k1",
2829
"script",
2930
"segwit_addr",
3031
"wallet_util",

test/functional/test_framework/crypto/secp256k1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* G: the secp256k1 generator point
1616
"""
1717

18+
import unittest
19+
from hashlib import sha256
1820

1921
class FE:
2022
"""Objects of this class represent elements of the field GF(2**256 - 2**32 - 977).
@@ -344,3 +346,9 @@ def mul(self, a):
344346

345347
# Precomputed table with multiples of G for fast multiplication
346348
FAST_G = FastGEMul(G)
349+
350+
class TestFrameworkSecp256k1(unittest.TestCase):
351+
def test_H(self):
352+
H = sha256(G.to_bytes_uncompressed()).digest()
353+
assert GE.lift_x(FE.from_bytes(H)) is not None
354+
self.assertEqual(H.hex(), "50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")

0 commit comments

Comments
 (0)