Skip to content

feat(u4): add embedded threshold mask - #137

Merged
RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-threshold-mask
Sep 28, 2026
Merged

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-threshold-mask

Conversation

@brenorb

@brenorb brenorb commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Summary\n- add a range-checked u4 batch primitive that returns one Boolean for each nibble below an embedded public threshold\n- use bottom-to-top OP_ROLL scheduling so altstack restoration returns masks in the original order\n- document the canonical-byte boundary, limitations, catalog record, comparison, and exact metrics\n\n## Evidence\n- focused unit tests: cargo test --locked arithmetic::u4::threshold::tests --lib\n- exact metric fixture: cargo test --locked --test primitive_metrics u4_lt_mask_metrics_are_current -- --exact\n- knowledge validation: python3 tools/kb.py validate\n- formatting, JSON, and diff checks pass\n- representative 16-nibble fragment: 218 script bytes, 33 witness bytes, 18 strict peak stack items, 157 static non-push opcodes\n- execution class remains unclassified; no Bitcoin Core consensus or relay-policy validation is claimed\n\nThe full repository test suite was skipped intentionally.

Copy link
Copy Markdown
Contributor

Review: changes required before merge.

The vector result reverses the documented input order. The loop consumes the bottom input first using OP_ROLL, then restores staged results in reverse order. On input [1,2,3] bottom-to-top, the threshold(2), equality(1), trichotomy(2), and clamp(2) cases fail their documented ordered output checks and succeed with the reversed expected vectors. Preserve the documented order and add asymmetric input/output vectors, including preserved main/alt-stack state.

Evidence: locally-reproduced against the proposed implementation; local fragment execution only, deployment unclassified. This is not Bitcoin Core or complete-spend validation. Run the affected module tests and focused metric check after the fix.

Reviewed commit: 4050076a9cd2ba81d16cd8b8b6a930806dcb079d.

@brenorb
brenorb force-pushed the feat/u4-threshold-mask branch from 4050076 to 2583e72 Compare September 19, 2026 00:47
@brenorb

brenorb commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review comment in commit 2583e72.

  • Process the input batch top-to-bottom and restore staged masks through altstack, preserving the documented input/output order.
  • Corrected asymmetric threshold vectors and preserved-state checks; [4,5,15] at threshold 5 now verifies output order [1,0,0] bottom-to-top and both sentinels.
  • Added strict exact-boundary coverage: 998 standalone items reaches a measured 1,000-item peak; 997 items compose with one preserved main or alt-stack item; 998 plus a preserved item fails with StackSize.
  • Added malformed-input checks at every position and explicit range-check errors.
  • Updated metrics from 218/157 to 190 bytes/142 static non-push opcodes; stack peak remains 18 items.
  • Documented the batch + 2 + preserved_items <= 1000 composition bound and added witness-item metric coverage.

Tests passed: cargo test --locked --lib arithmetic::u4::threshold::tests, cargo test --locked --test primitive_metrics, cargo fmt --all -- --check, python3 tools/kb.py validate, and git diff --cached --check.

Copy link
Copy Markdown
Contributor

Review of 2583e72239651b9e5ec26ffccbb95607bb32cf0a:

The revised top-input schedule addresses the earlier reversed output order. Preserve asymmetric threshold vectors and exact stack-frontier coverage; rebase and rerun the threshold library/metric checks and KB validation.

Current integration conflicts: knowledge/catalog.json, knowledge/comparisons/arithmetic.md, knowledge/primitives/index.md, src/arithmetic/u4/README.md, src/arithmetic/u4/mod.rs, tests/primitive_metrics.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:13
Resolve the six integration conflicts Robin listed by keeping every entry
from both sides: catalog records (main 101 + arithmetic/u4-threshold = 102),
the metric fixture beside u4_parity_metrics_are_current, the u4 module
declaration, the primitives index, the arithmetic comparison row, and the
u4 README API bullet, metric row, and witness sentence. The threshold
README bullet adopts main's note that the range check does not establish
canonical ScriptNum encoding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add an asymmetric positional test over every threshold 0..=15 using
threshold-1, threshold, threshold+1, 0, and 15 between preserved main and
alt-stack sentinels, comparing the full bottom-to-top final stack. Add an
exact-frontier test deriving the maximum preserved state from
U4_LT_MASK_MAX_BATCH for batches 1, 16, 500, and 998, with main-only and
split main/alt preservation: the maximum reaches a 1,000-item peak and one
more item fails with StackSize. Register both tests in the catalog and
describe the coverage on the knowledge page.

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 4b75f894a32e9f4cff8cd94ec0478f06ddb3ced3 (merge 8dec733a50edc10936a8ecf14e163cdde85b294f of current origin/main e9d5a66b6391eccee64f72cc5af590bc14ed550c, plus one test follow-up). The top-input production schedule reviewed at 2583e72 is unchanged.

  • Integration conflicts. A trial git merge --no-ff origin/main conflicted on exactly the six files you listed: knowledge/catalog.json, knowledge/comparisons/arithmetic.md, knowledge/primitives/index.md, src/arithmetic/u4/README.md, src/arithmetic/u4/mod.rs, and tests/primitive_metrics.rs. Every entry from both sides was kept:
    • catalog.json: main's text is unchanged except that the PR's arithmetic/u4-threshold record goes back in its PR position, after arithmetic/u4-parity. Records: main 101 and PR 56 give 102 merged, with no duplicates. Configurations: main 256 and PR 162 give 257 merged. Both main ⊆ merged and PR ⊆ merged hold. All 101 main records are byte-for-byte equal as JSON, and the threshold record matches the PR's.
    • primitive_metrics.rs: u4_lt_mask_metrics_are_current is added back after u4_parity_metrics_are_current. Test functions: main 113 plus 1 gives 114 merged. Metric keys: main 840 ⊆ merged 845, and PR ⊆ merged.
    • mod.rs: pub mod threshold; sits in sorted position among main's new modules.
    • README.md, index.md and the comparison table: rows and bullets from both sides are kept. No line from main is dropped. The only PR-added line that changed is the threshold bullet, which now carries main's note that the range check does not establish canonical ScriptNum encoding.
    • No conflict markers remain.
  • Asymmetric threshold vectors are preserved. The existing projects_all_threshold_boundaries_in_order, handles_threshold_endpoints, and preserves_surrounding_main_and_alt_stack_items ([4,5,15] → [1,0,0] with main/alt sentinels) are unchanged. The new asymmetric_boundary_vectors_keep_positions_for_every_threshold covers every threshold 0..=15:
    • Inputs, bottom to top: threshold-1, threshold, threshold+1, 0, 15, where in range.
    • A main sentinel 99 and an alt sentinel 77 surround the batch.
    • It compares the complete bottom-to-top final stack under the strict executor.
    • It asserts that each expected mask vector for threshold >= 1 is not a palindrome.
  • Exact stack frontier is preserved. composes_with_preserved_state_at_the_stack_boundary and the 998-item peak check in rejects_invalid_nibbles_and_generation_bounds are unchanged. The new exact_stack_frontier_admits_max_preserved_and_rejects_one_more works as follows:
    • It derives max_preserved = U4_LT_MASK_MAX_BATCH - n for n ∈ {1, 16, 500, 998}.
    • It runs each case with main-only and split main+alt preservation.
    • At the maximum it asserts success, a measured 1,000-item combined peak, and the exact final stack length.
    • At maximum+1 it asserts ExecError::StackSize.
  • Mutants. These were temporary and reverted; the committed tree has none.
    • (a) The original reversed {index} OP_ROLL schedule from 4050076: asymmetric_boundary_vectors_..., projects_all_threshold_boundaries_in_order, and preserves_surrounding_... fail. The first failure is threshold 1, inputs [0, 1, 2, 0, 15] with left [[99], [], [1], [], [], [1], [77]] and right [[99], [1], [], [], [1], [], [77]].
    • (b) OP_LESSTHAN → OP_LESSTHANOREQUAL: the asymmetric, in-order, endpoint, and preservation tests fail. The first failure is threshold 0, inputs [0, 1, 0, 15] with left [[99], [1], [], [1], [], [77]] and right [[99], [], [], [], [], [77]].
    • (c) U4_LT_MASK_MAX_BATCH = 1_000 - 2 + 1: exact_stack_frontier_... fails at batch 1, main 998, alt 0, preserved 998 with Error: StackSize, and rejects_invalid_nibbles_and_generation_bounds fails its 998→999-item run.
    • Controls pass on the unmutated tree.
  • The two new tests are registered in the catalog record. The knowledge page describes the coverage. Metrics are unchanged: 190 policy-produced fragment bytes, 33 serialized witness bytes, 16 data items, zero hints, an 18-item strict peak, and 142 static non-push opcodes.

Validation on the committed HEAD with a clean tree:

  • cargo test --locked --lib arithmetic::u4::threshold: 7 passed
  • cargo test --locked --test primitive_metrics u4_lt_mask_metrics_are_current -- --exact: 1 passed
  • cargo test --locked --test primitive_metrics: 113 passed, 1 ignored
  • cargo test --locked --no-run: compiles
  • python3 tools/kb.py validate: 102 records / 257 configurations
  • python3 -m unittest discover -s tools -p 'test_*.py': 45 OK
  • cargo fmt --all -- --check: passed

No field-arithmetic or full-repository run was performed. Evidence remains locally-reproduced, and execution is unclassified (strict tapscript-context executor with the combined 1,000-item limit). No Bitcoin Core consensus or relay-policy validation is claimed.

This head was subsequently integrated into main by #244.

@brenorb
brenorb deleted the feat/u4-threshold-mask 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