From 9f2ce90f494a5b415bec0898e3185437126fc7d9 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 9 Sep 2026 22:44:24 -0700 Subject: Boot: price the Ark exit advisory by size only — no more 5s of secp256k1 on load. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The exit-depth advisory rebuilt every spendable coin's exit chain to learn its fee, and each hop's input taproot key ran a musig key aggregation (several secp256k1 multiplies on uncached points). With 27 coins of depth up to 86 (1685 hops) that was ~5.3s of solid main-thread work on every boot, laptop and phone alike — requestIdleCallback only delayed it, and the Android wrapper hits a full boot whenever the OS evicts the app, so "reopening the app" froze for seconds. A fee only needs vsizes, and a taproot output script is 34 bytes whatever key it holds: vtxoTransactions/transitionInputTxout gain a sizeOnly mode with stand-in keys, exit.js exposes exitTxVsizes (same chain checks, no EC, nothing broadcastable), and the advisory + coins page use it. Verified against the real signed chain on the live coin set: identical vsizes on all 27 coins, 9s → 26ms. Also: the sync-slot replay at boot merged the ark snapshot and rewrote ~1MB of state (plus re-adopting it into the manager) twice per boot even when the union brought nothing. Compare the serialized union to what's stored and skip the write/adopt when unchanged. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01Nw1Dijik1tLVKRM1WkU7xE --- src/ark/exit.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/ark/exit.js') diff --git a/src/ark/exit.js b/src/ark/exit.js index e2b253f..616c6a1 100644 --- a/src/ark/exit.js +++ b/src/ark/exit.js @@ -18,7 +18,7 @@ import { schnorr, secp256k1 } from '@noble/curves/secp256k1'; import { concatBytes } from './proto.js'; import { pubkeyPolicyTaproot, txid } from './send.js'; -import { vtxoTransactions, harkLeafTaproot, tapLeafHash, taprootScriptSighash } from './refresh.js'; +import { vtxoTransactions, harkLeafTaproot, harkUnlockWitnessShape, tapLeafHash, taprootScriptSighash } from './refresh.js'; const u16le = (n) => Uint8Array.of(n & 0xff, (n >> 8) & 0xff); const u32le = (n) => Uint8Array.of(n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >>> 24) & 0xff); @@ -57,8 +57,16 @@ export function serializeTxW(tx, witnesses) { // The vtxo's chain as broadcastable transactions, oldest first. // Throws if any transition lacks its signature (an unsigned chain can't exit). -export function signedExitTxs(vtxo, serverPub) { - const items = vtxoTransactions(vtxo, serverPub); +export const signedExitTxs = (vtxo, serverPub) => exitChain(vtxo, serverPub, false); + +// Just the vsize of each hop's transaction, with the same "can this chain +// exit at all" checks — but no elliptic-curve work: the output keys are +// stand-ins of the right length. For fee estimates only; nothing here is +// broadcastable. +export const exitTxVsizes = (vtxo, serverPub) => exitChain(vtxo, serverPub, true).map((t) => t.vsize); + +function exitChain(vtxo, serverPub, sizeOnly) { + const items = vtxoTransactions(vtxo, serverPub, sizeOnly); return items.map((it, i) => { const t = vtxo.genesis[i].transition; let witness; @@ -70,14 +78,20 @@ export function signedExitTxs(vtxo, serverPub) { throw new Error('vtxo chain is missing its unlock preimage — cannot exit'); } const preimage = hex.decode(t.unlock.preimage); - const tp = harkLeafTaproot(hex.decode(t.userPubkey), serverPub, vtxo.expiryHeight, sha256(preimage)); - const cb = concatBytes(Uint8Array.of(0xc0 | tp.outputParity), tp.internalXOnly, tp.expiryLeaf); - witness = [hex.decode(t.signature), preimage, tp.unlockScript, cb]; + if (sizeOnly) { + const shape = harkUnlockWitnessShape(sha256(preimage)); + witness = [hex.decode(t.signature), preimage, shape.unlockScript, shape.controlBlock]; + } else { + const tp = harkLeafTaproot(hex.decode(t.userPubkey), serverPub, vtxo.expiryHeight, sha256(preimage)); + const cb = concatBytes(Uint8Array.of(0xc0 | tp.outputParity), tp.internalXOnly, tp.expiryLeaf); + witness = [hex.decode(t.signature), preimage, tp.unlockScript, cb]; + } } else { throw new Error('unsupported genesis transition: ' + t.type); } - const idInternal = txid(it.tx); const { raw, vsize } = serializeTxW(it.tx, [witness]); + if (sizeOnly) return { vsize }; + const idInternal = txid(it.tx); return { txid: hex.encode(idInternal.slice().reverse()), txidInternal: idInternal, -- cgit v1.2.3