Skip to content

feat(u32): add embedded constant AND - #145

Merged
RobinLinus merged 5 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-and-constant
Sep 28, 2026
Merged

RobinLinus merged 5 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-and-constant

Conversation

@brenorb

@brenorb brenorb commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add u32_and_constant(value) for checked byte-oriented u32 masking with a public embedded operand
  • reuse the existing 256-entry Boolean table and remove it before return
  • document the witness/item tradeoff, table composition boundary, and conservative evidence class

Evidence boundary

  • 628 locking-script bytes
  • 13 serialized witness bytes across 4 data items
  • 0 hint items; 272-item strict combined main-plus-alt-stack peak
  • 460 static non-push opcodes (dynamic count unavailable)
  • evidence: inspected pending executable CI reproduction; execution: unclassified

Verification

  • cargo fmt --all -- --check
  • python3 tools/kb.py validate
  • python3 -m json.tool knowledge/catalog.json
  • git diff --check
  • focused Rust and metric tests are included; local compilation was blocked after the host filesystem reached 100% during dependency archive creation
  • full repository test suite skipped by request,

Copy link
Copy Markdown
Contributor

Review: changes required before merge.

The checked byte boundary misses three original input limbs. With four witness items encoding numeric 1, replacing any bottom-based position 0, 1, or 2 by the nonminimal encoding [0x01, 0x00] still succeeds under the local Tapscript Consensus profile after consuming the four outputs and appending OP_TRUE. Position 3 is rejected. Validate each original byte before arithmetic normalizes it; move or stage the validated limb before repeating the check. Add malformed-encoding regressions at every position under a profile that permits nonminimal numeric encodings globally.

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: 1f86d25a186f09ac01c5dc99727538589cdcfe5b.

@brenorb
brenorb force-pushed the feat/u32-and-constant branch from 1f86d25 to 916732b Compare September 19, 2026 01:23
@brenorb

brenorb commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Updated in commit 916732b and pushed to feat/u32-and-constant.

  • Fixed the root cause by rotating each original limb through verify_canonical_byte() before table setup, so all four witness encodings are validated before any Boolean-table query.
  • Added permissive-profile regressions for non-minimal [1,0] at every position, malformed/range inputs, and canonical 128/255 controls at every position.
  • Refreshed evidence and metrics: 620 bytes, 448 static non-push opcodes, 272-item table peak; the 13-byte/four-item witness profile remains unchanged.

Checks passed: focused AND 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 916732bec63daa7f61e71c6747c8546801ced249:

The validator now rotates through all four original limbs, addressing the earlier top-limb-only check. Preserve malformed/nonminimal inputs at every position and the AND result/order regressions. Rebase and rerun the focused AND and named metric checks plus KB validation.

Current integration conflicts: knowledge/catalog.json, knowledge/comparisons/arithmetic.md, knowledge/primitives/index.md, 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:09
Resolve conflicts in knowledge/catalog.json, knowledge/comparisons/arithmetic.md,
knowledge/primitives/index.md, and tests/primitive_metrics.rs by keeping every
main entry and adding the u32-and-constant record, rows, and metric test.
All 101 main catalog records and 256 configurations are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run the hostile-input regressions on a complete leaf that consumes all four
outputs before OP_TRUE under the local tapscript Consensus profile. Every
position now checks -1/256 (Verify), negative zero and nonminimal one
(EqualVerify), and a five-byte number (ScriptIntNumericOverflow), with
canonical 128/255 controls that must succeed. A test-only top-limb-only
mutant must fail the all-position alias predicate while accepting aliases at
positions 0-2.

Result/order checks now supply runtime witnesses through the shared strict
harness for six boundary/pattern cases and 100 seeded cases.

Correct stale 612-byte/444-opcode prose to the measured 620 bytes and 448
static non-push opcodes, and align the knowledge page evidence wording with
the catalog record.

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 78c28a7e916646ab9aacd559afe02155cf95e067 (on merge 481221f154cce17a71ec05f5765e9b3bbada770e). This merges current main (e9d5a66b6391eccee64f72cc5af590bc14ed550c), keeps all existing main records and metric tests, and does not change the production AND fragment.

  • Validator. The rotating check (3 OP_ROLL + verify_canonical_byte() for each of the four original limbs, before table setup) is unchanged from 916732b. I inspected the source of the historical head 1f86d25a186f09ac01c5dc99727538589cdcfe5b (also d599394/2db098e): it ran verify_canonical_byte() four times on the same top limb.

  • Malformed and nonminimal inputs at every position. rejects_malformed_and_nonminimal_limbs now runs on a complete leaf that consumes all four outputs before OP_TRUE. It uses the local tapscript Consensus profile, where global numeric minimality is off and the stack limit is enforced. The canonical control must actually succeed. At every position (0–3) the test checks five inputs and the exact error each must produce:

    • -1 and 256 → Verify
    • negative zero [0x80] and nonminimal one [0x01, 0x00] → EqualVerify
    • five-byte [0,0,0,0,1] → ScriptIntNumericOverflow

    Canonical 128/255 controls succeed at every position. This matches the contract used for the OR sibling (feat(u32): add embedded constant OR #146).

  • Top-limb-only mutant. The test includes a test-only historical mutant (same leaf wrapper, profile and all-position alias predicate). Its valid control succeeds. It accepts [0x01, 0x00] at positions 0–2 and rejects position 3 with EqualVerify, so the predicate returns false as required. I also temporarily removed 3 OP_ROLL from the production fragment, then reverted it before committing:

    • The test as written failed at and_constant.rs:153: limb 0 with [81] produced unexpected outcome: … success: false, error: Some(InvalidStackOperation). The expected error there is Verify.
    • I then moved the alias predicate ahead of that loop. It failed with assertion failed: all_nonminimal_aliases_rejected(&leaf), after the valid control had succeeded.

    This mutant was executed under the current interpreter. The historical commit was only source-inspected, not re-run under its old lockfile.

  • AND result and order. masks_boundaries_and_patterns now feeds runtime witnesses through the shared strict harness (arithmetic::test_helpers::{run_with_witness, word_witness}). It covers six boundary/pattern cases and 100 cases from a fixed seed (0x7533_325f_616e_6463). An order-sensitive u32_equal compares all four result limbs against word & mask. preserves_surrounding_main_and_alt_stack_items is kept.

  • Helpers. complete_leaf, complete_result_leaf, executed_success and all_nonminimal_aliases_rejected are local copies of the feat(u32): add embedded constant OR #146 test helpers. No shared constant-Boolean test module exists on main, and feat(u32): add embedded constant OR #146's file is not on main.

  • Integration conflicts. Exactly Robin's four files: knowledge/catalog.json, knowledge/comparisons/arithmetic.md, knowledge/primitives/index.md, tests/primitive_metrics.rs.

    • Catalog: I inserted the PR's arithmetic/u32-and-constant record unchanged into main's file. Records: main 101, PR 56, merged 102. Configurations: main 256, PR 162, merged 257. Main ⊆ merged and PR ⊆ merged, with no duplicate ids. The diff against main has 0 deleted lines.
    • Metrics: 113 #[test] fns on main, 35 on the PR, 114 merged. Both sides are ⊆ merged, with no duplicates. Both .chain(winternitz20_mixed_sum_metrics()) and .chain(u32_and_constant_metrics()) are kept.
    • Index and comparison tables: rows from both sides are kept.
  • Doc corrections (in the follow-up commit):

    • src/arithmetic/u32/README.md said 612 bytes / 444 opcodes. It now says the measured 620 / 448.
    • The comparisons/arithmetic.md prose said 612. It now says 620.
    • The knowledge page said the evidence was inspected. It now says locally-reproduced, which matches the catalog, and it describes the new tests.
  • Metric. Unchanged after the merge. The fixture was not refreshed. It measures 620 policy-produced fragment bytes, 13 serialized data-witness bytes, four data items, zero hint items, a 272-item combined main-plus-alt-stack peak, and 448 static non-push opcodes.

Validation on the committed head, clean tree:

  • cargo test --locked --lib arithmetic::u32::and_constant::tests: 3 passed
  • cargo test --locked --test primitive_metrics u32_and_constant_metrics_are_current -- --exact: 1 passed
  • 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 OK

No full-repository or field-arithmetic run.

Compiler 124b561ed75ac3ec4c6ad99207d8dcdd3bc67180, interpreter a09e87af444034698697f0a2267e755cf72f9aed. Evidence is local only and deployment is unclassified. No Bitcoin Core or relay-policy validation is claimed.

This head was subsequently integrated into main by #244.

@brenorb
brenorb deleted the feat/u32-and-constant 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