Skip to content

the peg-in tweak form (#23): a BIP 341 peg output committing to the chain's hash and the destination - #31

Merged
melvincarvalho merged 1 commit into
gh-pagesfrom
peg-tweak-form
Oct 1, 2026
Merged

melvincarvalho merged 1 commit into
gh-pagesfrom
peg-tweak-form

Conversation

@melvincarvalho

Copy link
Copy Markdown
Contributor

Towards #23 (the output and its verification; the reveal event, the claim's carriage and the wallet's descriptor import are the rest of that issue). Builds on #27/#28 (keys.mjs) and #24/#29 (the chain's hash).

siding/lib/pegtweak.mjs (pure):

  • Internal key: the peg holders' point, used as its even-y lift as BIP 341 does (a level-2 chain passes the NUMS key and its multi_a leaf, as the challenge does today).
  • Refund leaf: and_v(v:pk(refund), older(refundBlocks)) — <refund> CHECKSIGVERIFY <n> CSV, n pushed minimally.
  • Commitment leaf: pk(C) with C = NUMS + t·G, t = tagged("sidestr/peg-in", chainHash32 || sha256(script)): unspendable, commits to the chain (its event id, SPEC 3) and the sidechain output script, and is a plain pk() so a wallet policy or a hardware device accepts the tree (a raw OP_RETURN leaf would not be miniscript).
  • Output: Q = P + TapTweak(x(P) || root)·G via keys.tapTweak/tweakPoint; the tree pairs leaves left to right with lexically ordered branches; each leaf gets its control block (0xc0 | parity, the internal key, the path).
  • pegOutput(deps, { internal, refundKey, refundBlocks, chainHash, script, extraLeaves, hrp }) returns the output key and parity, scriptPubKey, address, root, tweak, the leaves with control blocks, a tr(…) descriptor and the reveal (what a verifier needs). pegMatches(deps, reveal, scriptPubKey) rebuilds and compares. pegSpendSecret is the holders' key-path secret: normalize(d) + tweak.

Tests (test/pegtweak-test.mjs, 15): the output key equals the engine's tapOutputKey and checkTapTweak confirms the parity (BIP 341 exactly); the holder's own parity does not change the output (the lift); the refund leaf's bytes and minimal pushes; the commitment leaf's key and scalar; bad inputs refused in words; two chains give two addresses from one key, two scripts likewise, and the same reveal the same address; every leaf's control block walks to the root, in two- and three-leaf trees; the key-path secret signs for the output for an even- and an odd-y holder; the descriptor's form.

Not in this PR: a script-path spend run through the kernel interpreter (a refund), the reveal event and the claim's carriage of it, and the producer's descriptor import.

…whose tree holds the refund leaf and a commitment leaf pk(C), C = NUMS + tagged('sidestr/peg-in', chainHash || sha256(script))·G; the output key from the internal key's even-y lift and the tree root (equal to the engine's tapOutputKey); control blocks per leaf; the reveal rebuilds and matches the output; the holders' key-path secret; a descriptor for wallets and devices; 15 tests

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

It introduces security-critical taproot/BIP 341 peg-in commitment cryptography whose subtle parity and commitment correctness warrants final human review despite thorough self-verifying tests.

Review effort: Balanced
Findings: None

What changed in this PR

This PR adds a pure ES module siding/lib/pegtweak.mjs implementing the "peg-in tweak form" from issue #23: a BIP 341 taproot peg output that commits to the chain's hash and the sidechain destination script inside the taproot tree (via an unspendable pk(C) commitment leaf) rather than alongside it in an OP_RETURN. It builds on the recently added keys.mjs (additive tweaks on full points) and federation.mjs (NUMS key, multi_a leaf), and fits the SPEC 6 peg flow as a level‑2 alternative to the marker form. It is accompanied by a 15‑check test file that verifies the output against the engine's own BIP 341 primitives. The module is not yet wired into any producer/validator code (only its test imports it).

Changes:

  • New pure module pegtweak.mjs: builds the refund leaf (and_v(v:pk(refund),older(n))), the commitment leaf (pk(NUMS + t·G)), the tap tree with per‑leaf control blocks, and pegOutput/pegMatches/pegSpendSecret plus a tr(…) descriptor and a verifier "reveal".
  • New test pegtweak-test.mjs: checks output key/parity against tapOutputKey/checkTapTweak, minimal script-number pushes, per‑leaf control-block paths (2- and 3-leaf trees), distinct outputs per chain/script, and key-path signing for even- and odd-y holders.
File Description
siding/​lib/​pegtweak.mjs New pure module deriving the BIP 341 peg output that commits to chain hash + destination script in the taproot tree.
siding/​test/​pegtweak-test.mjs New test suite verifying the output, leaves, control blocks, descriptor, and key-path spend against the engine's BIP 341 primitives.

I reviewed the cryptographic construction closely: the lift-to-even-y internal key, tapTweak/tweakPoint output derivation, the minimal CScriptNum encoding in pushNum (verified against boundary values), the tapTree pairing and control-block path assignment (traced for 2- and 3-leaf trees), the commitment scalar, and the key-path spend secret all appear correct and consistent with keys.mjs/federation.mjs/address.mjs. I did not find concrete defects to comment on. One intentional, documented placeholder: pegOutput emits a literal … in the descriptor field when extraLeaves are supplied (level‑2 descriptor import is explicitly out of scope for this PR), so I did not flag it.


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@melvincarvalho
melvincarvalho merged commit e8deb63 into gh-pages Oct 1, 2026
2 checks passed
@melvincarvalho
melvincarvalho deleted the peg-tweak-form branch October 1, 2026 14:28
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants