Skip to content

refactor(compile): extract unspendable key derivation - #368

Draft
vadim-anfv wants to merge 2 commits into
bitcoindevkit:masterfrom
vadim-anfv:feat/nums-verify
Draft

vadim-anfv wants to merge 2 commits into
bitcoindevkit:masterfrom
vadim-anfv:feat/nums-verify

Conversation

@vadim-anfv

@vadim-anfv vadim-anfv commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Blocked by #378, see #368 (comment).

Continues #218. compile --type tr returns the r it derived the unspendable internal key from (#225); this adds the check that the pair holds, and extracts the derivation so both halves share it.

The CLI surface is not part of this PR: the options are laid out in #218 (comment), and the logic is the same under all of them. Once we pick one, I'll open a follow-up PR wiring verification into the CLI, with integration tests.

Notes to the reviewers

  • compile behaves as before, the existing test_compile_* tests pass untouched
  • #[allow(dead_code)] is there because nothing calls the verification until the command exists, and goes away with it
  • I considered wrapping the key and its r in a type, and left it as plain functions until the command adds more callers

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.51%. Comparing base (d2317d2) to head (3339960).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #368      +/-   ##
==========================================
+ Coverage   59.08%   59.51%   +0.42%     
==========================================
  Files          25       26       +1     
  Lines        4646     4695      +49     
==========================================
+ Hits         2745     2794      +49     
  Misses       1901     1901              
Flag Coverage Δ
rust 59.51% <100.00%> (+0.42%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Move the H + rG derivation out of the compile handler into new module,
so that the verification that pairs with it can share it.
The derivation itself is unchanged.
Check that a tr() descriptor's internal key is the NUMS point tweaked
by a given r, so the r that compile returns can be checked against the
descriptor.

No caller yet: the CLI surface is undecided, hence the dead_code allow.
@vadim-anfv vadim-anfv changed the title feat(compile): verify the unspendable internal key refactor(compile): extract unspendable key derivation Nov 3, 2026

@bvotteler bvotteler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 3339960

Confirmed all tests pass at merge base as well as at HEAD, using just test.

compile behaves as before, the existing test_compile_* tests pass untouched

Confirmed test_compile behaves as before, also ran taproot tests and found expected behavior.

taproot and test_compile runs & results

At HEAD (commit: 3339960):

$ cargo test --features compiler -- taproot test_compile
[..]
# taproot summary:
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 16 filtered out; finished in 0.00s
[..]
# test_compile summary:
test result: ok. 11 passed; 0 failed; 0 ignored; 0 measured; 16 filtered out; finished in 0.05s

At merge base,git switch --detach HEAD~2 (commit 4dbc3e8):

$ cargo test --features compiler -- taproot test_compile
[..]
# taproot summary:
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 16 filtered out; finished in 0.00s
[..]
# test_compile summary:
test result: ok. 11 passed; 0 failed; 0 ignored; 0 measured; 16 filtered out; finished in 0.04s

Confirmed expectations:

  • Visually confirmed individual tests for test_compile are the same
  • 5 new taproot tests at HEAD
  • all tests passing

@Musab1258

Copy link
Copy Markdown
Contributor

Concept ACK

The direction of extracting the unspendable-key derivation logic and adding verification for the generated key and randomness pair makes sense. This builds naturally on PR #225, which introduced randomized key generation and exposed the randomness factor. I am currently reviewing the implementation and tests.

@Musab1258

Musab1258 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

ACK 3339960

I fetched the PR locally and built it with --no-default-features and
--all-features; both pass.

I compared the removed inline derivation in descriptor.rs with
derive_unspendable_internal_key in taproot.rs; it is the same H + rG derivation

I also ran just test and all tests passed.

Comment thread src/utils/taproot.rs
}

/// Extract the internal key of a `tr()` descriptor.
#[allow(dead_code)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You can remove this; the code compiles without it since this function is called in the verify_unspendable_internal_key function, which itself has a #[allow(dead_code)] flag.

Comment thread src/utils/taproot.rs
const NUMS_UNSPENDABLE_KEY_HEX: &str =
"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0";

pub fn generate_unspendable_internal_key() -> Result<(XOnlyPublicKey, SecretKey), Error> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since you added doc comments (///) for other functions, I am thinking you should do so for this function too.

@tvpeter

tvpeter commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

My comment on #218 applies for this PR.

Thank you.

Comment thread src/utils/taproot.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (non-blocking): verify_unspendable_internal_key is called by the four tests and taproot_internal_key by the first in mod tests. Both #[allow(dead_code)]s can go if the two functions move into the test module:

#[cfg(test)]
mod tests {
    use super::*;
    // other imports

    /// Check that the internal key of a `tr()` descriptor is the NUMS point tweaked by `r`.
    fn verify_unspendable_internal_key(descriptor: &str, r: &SecretKey) -> Result<(), Error> {
        if derive_unspendable_internal_key(r)? != taproot_internal_key(descriptor)? {
            return Err(Error::UnspendableKeyMismatch);
        }
        Ok(())
    }

    /// Extract the internal key of a `tr()` descriptor.
    fn taproot_internal_key(descriptor: &str) -> Result<XOnlyPublicKey, Error> {
        ...
    }

    // existing consts and tests
}

@tvpeter tvpeter moved this from Todo to In Progress in BDK-CLI Nov 18, 2026
@tvpeter tvpeter removed this from the CLI 4.1.0 milestone Nov 18, 2026
@vadim-anfv

Copy link
Copy Markdown
Collaborator Author

My comment on #218 applies for this PR.

Moving this to draft. As agreed in #218, it will also put compile under a top level policy command and add policy verify, so the diff will change a lot.

I'm letting #378 go first, so it doesn't have to be reworked for the rename. And since the restructure is breaking, it can only be merged after 4.1.0 is released.

@bvotteler @Musab1258 @emma31-dev thanks for the reviews, sorry for the churn.

@vadim-anfv
vadim-anfv marked this pull request as draft November 19, 2026 10:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

5 participants