Repository navigation
the peg-in tweak form (#23): a BIP 341 peg output committing to the chain's hash and the destination - #31
Conversation
…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
There was a problem hiding this comment.
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, andpegOutput/pegMatches/pegSpendSecretplus atr(…)descriptor and a verifier "reveal". - New test
pegtweak-test.mjs: checks output key/parity againsttapOutputKey/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.
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):and_v(v:pk(refund), older(refundBlocks))—<refund> CHECKSIGVERIFY <n> CSV,npushed minimally.pk(C)withC = 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 plainpk()so a wallet policy or a hardware device accepts the tree (a raw OP_RETURN leaf would not be miniscript).Q = P + TapTweak(x(P) || root)·Gviakeys.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, atr(…)descriptor and the reveal (what a verifier needs).pegMatches(deps, reveal, scriptPubKey)rebuilds and compares.pegSpendSecretis the holders' key-path secret:normalize(d) + tweak.Tests (
test/pegtweak-test.mjs, 15): the output key equals the engine'stapOutputKeyandcheckTapTweakconfirms 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.