Skip to content

feat(u32): add embedded constant subtraction - #144

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

RobinLinus merged 6 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-sub-constant

Conversation

@brenorb

@brenorb brenorb commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add u32_sub_constant(value) for checked four-byte u32 subtraction with a public embedded subtrahend
  • reuse the existing borrow chain while reducing the witness from eight byte limbs to four
  • document the measured boundary, hostile-witness checks, and witness-width tradeoff

Evidence

  • 137 locking-script bytes
  • 9 representative witness bytes across 4 data items; 13-byte maximum canonical fixture
  • 0 hint items; 10-item strict combined main-plus-alt-stack peak
  • 81 static non-push opcodes (dynamic count unavailable)
  • evidence: locally-reproduced; execution: unclassified

Verification

  • cargo fmt --all -- --check
  • cargo test --locked arithmetic::u32::sub_constant::tests --lib (3 passed)
  • python3 tools/kb.py validate
  • python3 -m json.tool knowledge/catalog.json
  • git diff --check
  • metric snapshot test included; local rerun was blocked by the host disk reaching 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: 1dcf119c014a6853df1be2d476f90f77f394fe82.

@brenorb
brenorb force-pushed the feat/u32-sub-constant branch from 1dcf119 to d377c74 Compare September 19, 2026 01:20
@brenorb

brenorb commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Updated in commit d377c74 and pushed to feat/u32-sub-constant.

  • Fixed the root cause by rotating each original limb through verify_canonical_byte() before the borrow chain, so all four witness encodings are validated before arithmetic rearrangement.
  • Added permissive-profile regressions for non-minimal [1,0] at every position, malformed/range inputs, and canonical 128/255 controls at every position.
  • Refreshed metrics and docs: 149 bytes, 83 static non-push opcodes, 9-item peak; the 9-byte representative and 13-byte maximum witness profiles remain unchanged.

Checks passed: focused subtraction 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 d377c74cfcd05257b28a30a340d4c5822eb9f7f3:

The validator now rotates through all four limbs. Preserve per-position canonicality checks and subtraction wrap/borrow vectors while rebasing; rerun the focused subtraction and named metric checks plus KB validation.

Current integration conflicts: knowledge/catalog.json, knowledge/comparisons/arithmetic.md, knowledge/primitives/index.md, src/arithmetic/u32/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:21
Resolve conflicts with current main while keeping every record, module,
index entry, and metric from both sides:

- knowledge/catalog.json: main's 101 records unchanged plus
  arithmetic/u32-sub-constant (102 records, 257 configurations).
- knowledge/comparisons/arithmetic.md: keep main's new rows and the
  leading-zero-byte paragraph alongside the constant-subtraction row and
  paragraph.
- knowledge/primitives/index.md: keep main's new u4/u32 entries and the
  constant-subtraction entry.
- src/arithmetic/u32/mod.rs: keep main's xnor and new modules plus
  sub_constant.
- tests/primitive_metrics.rs: keep winternitz20_mixed_sum_metrics and
  u32_sub_constant_metrics in the aggregate chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Run the raw-input regression on a complete leaf (outputs consumed, then
  OP_TRUE) under the local Tapscript Consensus profile. At every one of the
  four positions check -1/256 (Verify), negative zero and nonminimal one
  (EqualVerify), and five-byte overflow (ScriptIntNumericOverflow), with a
  succeeding canonical control and canonical 0/127/128/255 limbs at every
  position.
- Add a test-only no-rotation mutant: its control succeeds, it accepts the
  nonminimal alias at positions 0-2, and it rejects position 3.
- Supply the word as runtime witness items through the shared strict
  harness and add explicit 0 - c wrap, single-boundary borrow (listed
  first), multi-boundary borrow, top-limb borrow, and 0xffffffff vectors
  plus 100 seeded random cases.
- Check stack preservation for borrowing and non-borrowing cases.
- Correct stale docs: comparison paragraph 141 -> 149 bytes, README peak
  10 -> 9 items (now metric-marked), and the operand order of the reused
  u32_sub_drop(1, 0).

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in c3eb6ec801c2cb189bd9ab6937ee355923f11522, on top of merge commit 2c9ab129808821202959d67ce577ae507e8b536a (current main e9d5a66b6391eccee64f72cc5af590bc14ed550c merged into the branch, no rebase or force-push). The production validator that rotates through all four original limbs (3 OP_ROLL before each verify_canonical_byte()) is unchanged.

  • Integration conflicts. The trial merge conflicted in exactly the five files you listed. Each was resolved by keeping both sides:

    • knowledge/catalog.json: main's 101 records are byte-identical, and arithmetic/u32-sub-constant is inserted after arithmetic/u32. The result has 102 records and 257 configurations, with no duplicates. Main ⊆ merged and PR ⊆ merged hold for both record ids and configuration ids.
    • tests/primitive_metrics.rs: merged has 114 #[test] fns (113 from main plus u32_sub_constant_metrics_are_current), 35 aggregate chains (main's 34 plus u32_sub_constant_metrics) and 847 metric keys (main's 840 plus the PR's 7). Nothing is missing from either side.
    • src/arithmetic/u32/mod.rs: keeps all of main's modules, including xnor, shift, zero and the others, plus sub_constant.
    • knowledge/primitives/index.md and knowledge/comparisons/arithmetic.md: keep main's new rows and entries, the constant-subtraction row, and both explanatory paragraphs.
  • Per-position canonicality. rejects_malformed_and_nonminimal_limbs now runs a complete leaf: fragment, u32_drop, OP_TRUE, under the local Tapscript Consensus profile. That profile does not enforce global numeric minimality and does enforce the stack limit. The canonical control must succeed. Each of the four positions then gets five inputs, each rejected with an exact error:

    • -1 and 256: Verify
    • negative zero [0x80] and nonminimal one [1,0]: EqualVerify
    • five-byte overflow: ScriptIntNumericOverflow

    Canonical 0/127/128/255 limbs at every position must succeed.

  • Faithful test-only mutant. It repeats the check four times without the roll, the same shape as the historical top-limb-only validator. It uses the same wrapper, profile and all-position predicate. Its control succeeds, it accepts [1,0] at positions 0–2, and it rejects position 3 with EqualVerify. This mutant ships as an assertion in the test.

  • Production mutant, temporary and reverted. I removed 3 OP_ROLL from u32_sub_constant. rejects_malformed_and_nonminimal_limbs then fails with limb 0 with [81] produced unexpected outcome: ... success: true. The other two tests still pass. After reverting, git diff is clean.

  • Wrap/borrow vectors. subtracts_boundaries_and_wraps now passes the hostile word as runtime witness items through the shared strict harness (run_with_witness, final stack exactly [1], stack limit enforced). The leaf compares all four output limbs with a precomputed expected word. It has 27 explicit (word, constant, expected) vectors plus 100 cases from seed 0x7533_325f_7375_6263. Each explicit vector is also checked against wrapping_sub. The vectors cover:

    • 0 - c for constants in every limb, and 0 - 0xffffffff
    • one borrow across each of the three limb boundaries. These come first so a failure names its boundary.
    • borrows through two and three boundaries
    • a borrow out of the top limb, with and without lower borrows
    • the 0xffffffff identities, 0xfffffffe - 0xffffffff, and the earlier sign/midpoint/mixed cases
  • Borrow mutants, temporary and reverted. In u32_sub_zipped I inserted OP_DROP 0 after a borrow is produced, which zeroes that borrow. For the all-boundary mutant and for each single-boundary mutant (low, middle, high), subtracts_boundaries_and_wraps fails at that boundary's own single-borrow vector:

    • all boundaries and the low boundary: witness [0, 0, 1, 0]
    • middle boundary: [0, 1, 0, 0]
    • high boundary: [1, 0, 0, 0]

    preserves_surrounding_main_and_alt_stack_items also fails under every borrow mutant. After reverting, git diff is clean.

  • Stack preservation. Main-stack and alt-stack sentinels are now checked for 0x10203040 - 0x55667788, 0 - 1 and 0 - 0xffffffff.

  • Stale docs corrected.

    • The comparison paragraph said 141 locking bytes; it now says 149, matching the metric.
    • The README said a 10-item peak; it now says 9 and uses the u32_sub_constant_stack metric marker.
    • The knowledge page said the fragment reuses u32_sub_drop(0, 1); the code uses u32_sub_drop(1, 0). The page now names that call and describes the new test coverage.

Metrics are unchanged and were not refreshed. The policy-produced fragment is 149 bytes, with a 9-byte representative / 13-byte maximum witness, 4 data items, 0 hint items, a 9-item combined main-plus-alt-stack peak and 83 static non-push opcodes. The generic two-word baseline is 77 bytes, a 21-byte witness over 8 items, and a 9-item peak.

Validation on the committed HEAD with a clean tree:

  • cargo test --locked --lib arithmetic::u32::sub_constant: 3 passed
  • cargo test --locked --test primitive_metrics u32_sub_constant_metrics_are_current -- --exact: 1 passed. All u32 metric tests (... primitive_metrics u32): 40 passed.
  • python3 tools/kb.py validate: valid, 102 records / 257 configurations
  • python3 -m unittest discover -s tools -p 'test_*.py': 45 passed
  • cargo fmt --all -- --check, git diff --cached --check, cargo test --locked --no-run: pass

Runs used compiler 124b561ed75ac3ec4c6ad99207d8dcdd3bc67180 and interpreter a09e87af444034698697f0a2267e755cf72f9aed. Field-arithmetic tests and the full repository suite were not run. Evidence stays locally-reproduced and deployment stays 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-sub-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