Skip to content

feat(u4): add checked MSB projection - #24

Merged
RobinLinus merged 4 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-msb
Sep 28, 2026
Merged

RobinLinus merged 4 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-msb

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a checked u4 batch primitive that projects each canonical nibble to its most-significant bit using a 16-item lookup table.

The fragment:

  • rejects values outside 0..=15 before OP_PICK
  • preserves documented input/output ordering
  • rejects empty and stack-overlarge batches
  • records 440 script bytes, 65 witness bytes across 32 data items, 50 strict stack items, and 328 static non-push opcodes

Validation

  • python3 tools/kb.py validate
  • cargo test --locked arithmetic::u4::msb
  • cargo test --locked --test primitive_metrics u4_msb_metrics_are_current
  • git diff --check

Full-repository tests were intentionally not run.

Copy link
Copy Markdown
Contributor

Review recommendation: close or replace this version.

The lookup-table implementation is dominated by a direct threshold check in the measured 32-nibble configuration. With the same numeric range checks and output order, this fragment takes 440 policy-compiled bytes and combined stack peak 50; directly computing x >= 8, staging results on altstack and restoring them takes 382 bytes and peak 34.

The deterministic input is i % 16 for i=0..31; the direct variant also rejects -1 and 16. Fragment sizes exclude input/terminal code; peaks include the identical output checks. There are 32 simultaneous entry data items, zero hint items, and 63 serialized data-witness bytes for this fixture (65 maximum for 32 canonical nibbles), excluding script/control block. Evidence: locally-reproduced; isolated strict-local probes remain unclassified.

Reimplement with the simpler comparison if this API is wanted, and retain the table comparison as a negative result. This comparison does not assert dominance for unmeasured batch sizes.

September 11 local validation (unchanged PR head): python3 tools/kb.py validate passed; 2 selected library tests passed. These are targeted local checks, not a full-suite or complete-spend validation. Results using a stack-limit-disabled execution helper remain research-unlimited.

Reviewed commit: 29db12e57423b293d9ab179395a8229ae35a2766.

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Reworked this PR around the review result: the 16-entry lookup table is replaced with direct range checks plus nibble >= 8.

Measured at 32 nibbles: 446 locking-script bytes, 34-item peak, 350 static non-push opcodes, and 65 witness bytes. The focused tests cover asymmetric values, malformed negative/oversized inputs at each position, the 998-item standalone frontier, preserved main/alt-stack state, and the 997-item rejection.

Also added the implementation README metric fixture, knowledge page/catalog entry, comparison row, and scoped negative result. Focused MSB tests, the exact metric fixture, python3 tools/kb.py validate, formatting, and git diff --check pass; the full repository suite was not run.

Copy link
Copy Markdown
Contributor

Review of b7ce89336bbd98a9eb067cf3214dc479ba86b2e6:

The direct-threshold implementation addresses the earlier dominated-table concern and includes useful ordering/frontier tests. Resolve the module declaration conflict while retaining all current exports, then rerun the focused MSB and metric checks plus KB validation.

Current integration conflicts: src/arithmetic/u4/mod.rs. These must be resolved and the integrated result validated before merging.

Validation scope: source/diff and existing CI review; no new full-repository or field-arithmetic test run was requested.

brenorb and others added 2 commits September 27, 2026 21:15
Resolve the only textual conflict, src/arithmetic/u4/mod.rs, by keeping
every module declaration from origin/main (37) plus this PR's `msb`
module (38 total). Neither side declares `pub use` re-exports. The
catalog, metric fixtures, READMEs, and negative-results index merged
without textual conflicts; all 101 main catalog records are unchanged
and the PR's arithmetic/u4-msb record is added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The PR's "u4 MSB lookup table is dominated by a direct threshold at
n=32" entry used NR-064, which origin/main already assigns to
"Data-only signature budgets falsely reject complete spends" (linked
from knowledge/tapscript-budget-validation.md). Git merged both headings
without a textual conflict, leaving two NR-064 entries. Renumber the
u4 MSB entry to its centrally assigned ID, NR-071. No other file
references the renamed entry; main's NR-064 and its link are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RobinLinus
RobinLinus merged commit 80c0dcb into solving-bitcoin:main Sep 28, 2026
3 checks passed
@brenorb

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in e24036d7e99f9f5a8a1436c635151ed6674194da, which merges current main (e9d5a66b6391eccee64f72cc5af590bc14ed550c) into the PR (merge commit 21fbca652cc3ffce2790cca6e26308938a7f818e). A separate follow-up commit fixes a duplicate negative-result ID. The direct-threshold implementation and its tests are unchanged.

  • Module declaration conflict. The only textual conflict was src/arithmetic/u4/mod.rs, which matches the list in the review. I kept all 37 pub mod declarations from main and added this PR's msb module, for 38 in total. The sorted declaration sets confirm that main's set and the PR's set are both subsets of the merged file. Neither side declares any pub use re-exports, so there were none to keep.

  • Other shared files merged without textual conflicts.

    • knowledge/catalog.json: all 101 main records are unchanged, compared field by field. The only added record is arithmetic/u4-msb, identical to the PR's version. That gives 102 records and 257 configurations, with no duplicate IDs.
    • tests/primitive_metrics.rs: keeps all 112 of main's #[test] functions and adds u4_msb_metrics_are_current, for 113. Every metric key from main and from the PR is present. The only repeated keys are ones that main already repeats.
    • The src/ciphers/aes/mod.rs and src/arithmetic/u4/sum.rs formatting hunks from the PR were already on main. The PR's net diff against main is now 9 files, all additions.
  • Duplicate negative-result ID. Git merged knowledge/negative-results/index.md without a conflict, but the result had two NR-064 headings:

    • line 6: ## NR-064: u4 MSB lookup table is dominated by a direct threshold at n=32 (this PR)
    • line 1644: ## NR-064: Data-only signature budgets falsely reject complete spends (main, linked from knowledge/tapscript-budget-validation.md:171)

    python3 tools/kb.py validate still passed on that tree, so the validator does not catch duplicate NR IDs. Follow-up commit e24036d renames this PR's entry to NR-071. Nothing else references it (grep -rn "NR-064\|NR-071" over knowledge, src, docs, research, examples, tests and tools). Main's NR-064 and its link are unchanged, and all 66 NR headings are now unique.

  • Ordering and preserved-state frontier tests still catch defects. I checked each with a temporary mutant, then reverted it; git status was clean before committing.

    • Reversed output order (for i in 1..n { i OP_ROLL } appended after the restore loop): projects_asymmetric_boundary_values_in_order fails with Error: EqualVerify on the first bit check. The top item is 0 (from nibble 7) where 1 (from f) was expected.
    • Batch bound one too large (U4_MSB_MAX_BATCH = 999): respects_direct_threshold_stack_frontier fails with maximum MSB batch failed … Error: StackSize, peak max_nb_stack_items: 1001.
    • Schedule using one extra stack item (OP_DUP OP_DUP 8 OP_GREATERTHANOREQUAL OP_NIP): the same test fails at the maximum case (StackSize, 1001 items). With the maximum assertion temporarily disabled, the preserved-state case (996 nibbles plus one main-stack item and one alt-stack item) fails on its own with preserved state failed … Error: StackSize, 1001 items.
    • In every run the unrelated MSB tests still passed. The unmutated controls pass: the maximum case at 998 peaks at exactly 1,000 items, the preserved-state case at 996 succeeds, and 997 with preserved state is rejected with StackSize.
  • Metric. The u4_msb_metrics_are_current fixture passes without a refresh. It still reports 446 policy-produced locking-script bytes, 65 serialized witness bytes, a combined stack peak of 34 items, and 350 static non-push opcodes for 32 checked nibbles. The primitive uses no hint items.

Validation on the committed head with a clean tree (CARGO_PROFILE_{DEV,TEST}_OPT_LEVEL=1):

  • cargo test --locked --lib arithmetic::u4::msb: 4 passed
  • cargo test --locked --test primitive_metrics u4_msb: 1 passed
  • cargo test --locked --test primitive_metrics u4_: 45 passed
  • cargo test --locked --no-run: the whole crate and all test targets compile
  • python3 tools/kb.py validate: 102 records, 257 configurations
  • python3 -m unittest discover -s tools -p 'test_*.py': 45 passed
  • cargo fmt --all -- --check, git diff --check, and a search for conflict markers: all clean

No field-arithmetic tests were run. All evidence is local, using the strict tapscript-context executor with the stack limit enforced. Deployment remains unclassified, and no Bitcoin Core or relay-policy validation is claimed.

This head was subsequently integrated into main by #244.

@brenorb
brenorb deleted the feat/u4-msb branch October 28, 2026 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants