Skip to content

feat(u4): add embedded equality mask - #138

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

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u4-equality-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 equal to an embedded public target\n- use bottom-to-top OP_ROLL scheduling so altstack restoration returns masks in the original order\n- document the canonical-byte boundary, comparison with aggregate counting and lookup projections, catalog record, and exact metrics\n\n## Evidence\n- focused unit tests: cargo test --locked arithmetic::u4::equality::tests --lib\n- exact metric fixture: cargo test --locked --test primitive_metrics u4_eq_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: 16fdc6d1b73d7a60905c0de64c46b7cd9fd02007.

@brenorb
brenorb force-pushed the feat/u4-equality-mask branch from 16fdc6d to a0488ed Compare September 19, 2026 00:54
@brenorb

brenorb commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Updated in commit a0488ed and pushed to feat/u4-equality-mask.

  • Fixed the output ordering to preserve the documented input order, with asymmetric vectors and preserved main/alt-stack state coverage.
  • Replaced byte equality with numeric equality after range validation; permissive-profile tests cover non-minimal aliases at every position.
  • Added malformed-input rejection at every position and exact 998-item / preserved-state stack-boundary tests.
  • Refreshed metrics and documentation: 190 bytes, 142 static non-push opcodes, 18-item peak, 16 canonical witness items.

Checks passed: focused equality tests, all 34 non-ignored primitive_metrics tests, cargo fmt --all -- --check, python3 tools/kb.py validate, and git diff --cached --check.

Copy link
Copy Markdown
Contributor

Review of a0488edf271dbf0f55db1b4c6d0b0636b2ae59d7:

The revised schedule and numeric equality address the earlier output-order and byte-alias concerns. Preserve asymmetric equality masks and numeric-alias cases; rebase and rerun the equality-mask 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:20
Resolve integration conflicts in 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 by keeping every entry from both sides.

- catalog.json: all 101 main records unchanged plus the PR's
  arithmetic/u4-equality record (102 records, 257 configurations,
  no duplicate ids).
- primitive_metrics.rs: all 113 main #[test] fns plus
  u4_eq_mask_metrics_are_current (114, no duplicates).
- mod.rs, README, index and comparison tables: main's rows plus the
  PR's equality rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Address the review request to preserve asymmetric equality masks and
numeric-alias cases after integrating main.

- Run raw-witness alias tests under the explicit local
  TapscriptProfile::Consensus profile (require_minimal: false, stack
  limit enforced) with a clean-stack OP_TRUE assertion, matching the
  alias contract used by the adjacent-equality mask.
- Add a non-palindromic mixed-alias vector (target 1, mask
  [1,0,1,0,1,0]) with a canonical control and a Policy-profile
  MinimalData rejection control.
- Add test-only mutants of the historical 16fdc6d implementation: the
  OP_EQUAL comparator fails with Verify and the reversed OP_ROLL output
  schedule fails with NumEqualVerify on the same complete leaf.
- Correct the catalog record: script_bytes 218 -> 190 (stale value from
  16fdc6d; per_use_script_bytes and the README metric already read 190)
  and witness_bytes_max 33 -> null, because permitted numeric aliases can
  exceed the 33-byte canonical fixture.

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in a72a0139ff0873f2d1ab24a64af886dfc0e0b339. That commit sits on top of the merge 6a7231dbc81401edf0878ff9fca5a8ff52999e03, which brings current main (e9d5a66b6391eccee64f72cc5af590bc14ed550c) into the branch. Main's catalog records, module exports, README rows and metric tests are all kept.

  • Integration conflicts. A trial merge conflicted in the same 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. I kept both sides in each one:

    • Catalog: all 101 main records are byte-identical to main, and the PR's arithmetic/u4-equality record is inserted after arithmetic/u4-parity. That gives 102 unique ids and 257 unique configuration ids; both the main and PR id sets are subsets of the result.
    • Metrics: all 113 main #[test] functions plus u4_eq_mask_metrics_are_current, so 114 with no duplicates. Every metric key from both sides is present.
    • mod.rs: pub mod equality; sits in sorted order beside main's 37 modules.
    • README, index and comparison tables: main's rows plus the PR's equality rows.
    • After the merge commit, git diff origin/main shows only the PR's 8 files (+398 lines). No conflict markers remain.
  • Asymmetric equality masks are preserved and pinned. The existing tests are kept:

    • projects_one_hot_equality_mask_in_order: target 5 over 0..=f puts the single 1 at position 5, which a reversed schedule would move to position 10.
    • preserves_surrounding_main_and_alt_stack_items: mask [0,1,1] with preserved 99 on the main stack and 77 on the alt stack.
    • The per-position zero-alias vectors.

    New numeric_aliases_follow_an_asymmetric_mask_in_input_order checks the non-palindromic mask [1,0,1,0,1,0] from the raw witness [01 00], [80], [01], [02 00], [01 00 00 00], [0f] (target 1). Every output is checked in input order, followed by a clean-stack OP_TRUE. A canonical-encoding control with the same leaf also succeeds.

  • Numeric-alias cases and their profile. All raw-witness alias tests now run through execute_tapscript(.., TapscriptProfile::Consensus). That profile sets require_minimal: false, verify_minimal_if: true, CLTV/CSV on, enforce_stack_limit: true and OP_CAT off. Each test uses a shared assertion that requires executed success, no error, stack-limit enforcement and exactly one true terminal item. This is the same contract as the adjacent-equality PR (feat(u4): add adjacent equality mask #201).

    • The existing aliases are kept at every position of a 3-item batch: [01 00] for one, and [80] / [00 00] for zero.
    • Under the local Policy profile the same asymmetric alias witness is rejected with MinimalData. The alias result is therefore a Consensus-profile contract only. Callers that need canonical bytes still compose verify_canonical_nibble().
  • Mutants that bring back the original defects. I checked the historical code at 16fdc6d1b73d7a60905c0de64c46b7cd9fd02007 by reading its source; I did not re-run it under its old toolchain. I ran two temporary production mutants on the integrated tree and reverted both before committing:

    • (a) OP_NUMEQUAL → OP_EQUAL in u4_nibbles_to_eq_mask. Three tests failed: compares_nonminimal_numeric_encodings_and_preserves_boundary_state, numeric_aliases_follow_an_asymmetric_mask_in_input_order and the mutant test. Each failed at the clean-truthy assertion with "unexpected execution error: ... Error: Verify", leaving the byte-equality mask [],[],01,[],[] on the stack. Before I changed any tests, the pre-existing alias test also caught this mutant ("nonminimal one alias failed").
    • (b) The historical index OP_ROLL reversed output schedule. Five tests failed, including the asymmetric alias test with NumEqualVerify, the one-hot test and the preserved-state test with EqualVerify. Before I changed any tests, three of the original tests already failed under this mutant.

    Both mutants are also kept as test-only regressions in historical_byte_equality_and_reversed_schedule_mutants_fail, which uses the same complete leaf and witness as the fixed control:

    • the byte-comparator mutant must fail with Verify;
    • the reversed-schedule mutant (numeric comparator kept, so only the order changes) must fail with NumEqualVerify;
    • in both cases the clean-truthy contract must panic.

    Mutant construction and execution happen outside the panic-catching closure.

  • Catalog correction found while validating the integrated tree. script_bytes still read 218, a leftover from 16fdc6d. per_use_script_bytes, the README and the metric fixture all read 190, so it is now 190. witness_bytes_max changed from 33 to null, because permitted numeric aliases can be longer than the 33-byte canonical fixture. The knowledge page now says this and names the exact profile.

Metrics were not refreshed. u4_eq_mask_metrics_are_current passes unchanged:

  • 190 policy-produced fragment bytes;
  • 142 static non-push opcodes;
  • 18-item combined-stack peak;
  • 33 serialized data-witness bytes for 16 canonical data items;
  • zero hints.

These runs use compiler 124b561ed75ac3ec4c6ad99207d8dcdd3bc67180 and interpreter a09e87af444034698697f0a2267e755cf72f9aed. Evidence stays locally-reproduced with execution unclassified. No Bitcoin Core consensus or relay-policy validation is claimed.

Validation on the committed head with a clean tree:

  • cargo test --locked --lib arithmetic::u4::equality::tests: 7 passed
  • cargo test --locked --test primitive_metrics u4_eq_mask_metrics_are_current -- --exact: 1 passed
  • cargo test --locked --test primitive_metrics -- --skip fields::: 113 passed, 1 ignored
  • 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 cargo test --locked --no-run all passed

No field-arithmetic or full-repository test run was performed.

This head was subsequently integrated into main by #244.

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