Skip to content

feat(u32): add signed less-than comparison - #84

Merged
RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-signed-compare
Sep 28, 2026
Merged

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-signed-compare

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add signed two's-complement less-than over the existing four-byte u32 representation
  • branch on sign bytes and reuse unsigned ordering for equal signs
  • document the canonical-byte precondition and measured cost

Validation

  • cargo test --locked arithmetic::u32::cmp::tests -- --nocapture
  • cargo test --locked --test primitive_metrics
  • python3 tools/kb.py validate
  • git diff --check

The full repository test suite was intentionally not run.

Copy link
Copy Markdown
Contributor

Review recommendation: fix before merging.

The signed comparison passes its targeted tests, but the newly added test uses rand::thread_rng(). Replace it with a fixed seed and record the deterministic configuration, as required by the research protocol. Add an active focused metric check for the new reported row.

September 11 local validation (unchanged PR head): python3 tools/kb.py validate passed; 1 selected library test 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.

Integration: rebase on current main, resolve any catalog/NR/OP ID collisions, and run the affected correctness tests and focused metric checks.

Reviewed commit: ca6a35c6cdf08a81652b725c86d1059e2dba4dbb.

@brenorb
brenorb force-pushed the feat/u32-signed-compare branch from ca6a35c to 7c23dc6 Compare September 18, 2026 23:21
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review recommendation in commit 7c23dc6 (rebased onto current main).

  • Replaced the signed-comparison test's thread_rng() with deterministic ChaCha20Rng seed 0x5532434f4d500001, covering 36 boundary pairs plus 256 generated pairs with oracle (a as i32) < (b as i32).
  • Added active u32_signed_lessthan_metrics_are_current, including strict execution of both opposite-sign orderings and README metric synchronization.
  • Corrected the representative witness/stack accounting to 21 serialized bytes (25-byte maximum for all canonical byte limbs) and an 11-item strict peak; retained 49 static non-push opcodes.
  • Rebased the catalog, comparison, primitive documentation, and test references onto current main.

Validation:

  • cargo test --locked --lib arithmetic::u32::cmp::tests::test_u32_signed_less_than -- --exact
  • cargo test --locked --test primitive_metrics — 106 passed, 1 ignored
  • cargo fmt --all -- --check
  • python3 tools/kb.py validate
  • git diff --cached --check

Copy link
Copy Markdown
Contributor

Review of 7c23dc66ed173d4789548c0dbcf9e32b5c243e37:

The fixed ChaCha20 seed and active signed-comparison metric address the earlier reproducibility gaps. Preserve the existing catalog configurations when rebasing, then rerun the signed comparison and named metric check plus KB validation.

Current integration conflicts: knowledge/catalog.json. 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:11
Integrate origin/main (e9d5a66) into feat/u32-signed-compare. The only
conflict was knowledge/catalog.json in the arithmetic/u32 record: keep
main's byte-plane test entries, security, and stack-contract text and
append the PR's signed less-than test/metric references and sentences.
All 101 records and 256 configurations from main and all 245 PR
configurations are retained (257 merged, no duplicate ids).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The signed-lessthan configuration was attached to the
arithmetic/u32-compressed-equal record, whose knowledge page, tests,
and implementation do not describe it. Move it unchanged into the
arithmetic/u32 record, which already lists the signed comparison test,
metric check, security note, and stack contract. Configuration values
and the record/configuration counts (101/257) are unchanged.

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in eb1c02131ed17f1fef278da4a6f70b53550dbb4a, merging current main (e9d5a66b6391eccee64f72cc5af590bc14ed550c) into the PR in merge commit c3f6a0abe9943de8549acdae4d89fcb7fb85ed7b. The PR's signed-comparison code, tests, and metric values are unchanged.

  • Integration conflict: a trial merge of 7c23dc6 with main conflicted only in knowledge/catalog.json, which matches your list. Both hunks were in the arithmetic/u32 record. For tests, I kept main's six byte-plane entries and appended arithmetic::u32::cmp::tests::test_u32_signed_less_than and primitive_metrics::u32_signed_lessthan_metrics_are_current. For security and stack_contract, I kept main's byte-plane wording and added the PR's signed-comparison sentences. All other files merged automatically.

  • Catalog configurations preserved: I compared record and configuration IDs across main, the PR head and the merge:

    • Main has 101 records and 256 configurations. The PR head has 101 and 245. The merge has 101 and 257, with no duplicate IDs.
    • Every main record and every main (record, configuration) pair is in the merge. All 256 main configurations are byte-identical there.
    • At the merge commit, every PR pair is also in the merge. The only addition over main is signed-lessthan.
  • Follow-up catalog fix (separate commit eb1c021): on the PR branch, the signed-lessthan configuration sat inside the arithmetic/u32-compressed-equal record. That record's knowledge page, tests, and implementation don't describe it. I moved it unchanged into arithmetic/u32, which already lists the signed test, metric check, security note, and stack contract. Counts stay at 101/257, signed-lessthan is still unique, and kb.py show arithmetic/u32 now lists it.

  • Signed edge cases still present: test_u32_signed_less_than checks all 36 ordered pairs from {0, 1, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff} against the oracle (a as i32) < (b as i32). That covers:

    • the sign boundary 0x7fffffff/0x80000000 in both orders;
    • equal values (the six diagonal pairs);
    • -1 vs 0 and 0 vs -1;
    • i32::MIN vs -1.

    It then checks 256 pairs from ChaCha20Rng seed 0x5532434f4d500001. The active metric check runs both opposite-sign orderings (9abcdef0/12345678) under strict execution.

  • Mutants (temporary; each was reverted and git diff was empty before committing):

    • Unsigned mutant: u32_signed_lessthan() returns u32_lessthan(). The signed test fails with assertion failed: exec_result.success (src/support/execution.rs:296). The metric test fails with signed comparison failed for 9abcdef0 and 12345678.
    • Wrong sign-bit mutant: when the signs differ, return the right word's sign bit instead of the left word's (OP_NIP OP_NIP in place of OP_DROP OP_SWAP OP_DROP). Both tests fail with the same assertions.
    • Non-strict mutant: the same-sign branch uses u32_lessthanorequal(). The signed test fails on the equal-value boundary pairs with assertion failed: exec_result.success. The metric test also fails, but only on its README byte check: its correctness inputs are opposite-sign, so the boundary test is what catches this mutant.
    • The unmodified tree passes both tests.

Validation on the committed HEAD with a clean tree:

  • cargo test --locked --lib arithmetic::u32::cmp::tests::test_u32_signed_less_than -- --exact: 1 passed
  • cargo test --locked --lib arithmetic::u32::cmp::: 7 passed
  • cargo test --locked --test primitive_metrics u32_signed_lessthan_metrics_are_current -- --exact: 1 passed
  • cargo test --locked --test primitive_metrics: 113 passed, 1 ignored
  • python3 tools/kb.py validate: 101 records / 257 configurations
  • python3 -m unittest discover -s tools -p 'test_*.py': 45 passed
  • cargo fmt --all -- --check and cargo test --locked --no-run both passed

No metrics changed. The fixture still measures:

  • 63 policy-produced fragment bytes;
  • a 21-byte representative data witness (25-byte maximum for eight canonical byte limbs);
  • an 11-item strict peak;
  • 49 static non-push opcodes;
  • zero hint items.

These runs use compiler 124b561ed75ac3ec4c6ad99207d8dcdd3bc67180 and interpreter a09e87af444034698697f0a2267e755cf72f9aed, both pinned by main. The library sweep uses the tapscript-context execute_script helper. The metric check uses strict execution with the stack limit enforced. Evidence is still locally-reproduced and deployment is unclassified. No Bitcoin Core, relay-policy, full-repository, or field-arithmetic run is claimed.

This head was subsequently integrated into main by #244.

brenorb added a commit to brenorb/bitcoin-scripts that referenced this pull request Sep 28, 2026
Merges origin/main (3d1001f, which has since integrated solving-bitcoin#84 signed
less-than, solving-bitcoin#45 fixed rotations/equality, solving-bitcoin#40 reverse-byte adapter, and
27 other PRs) into feat/u32-compressed-boundary, addressing Robin
Linus's 2026-09-25 review.

Conflicts resolved:
- knowledge/catalog.json: kept the u32 record's "tests" entries from
  both sides (u32_compression_metrics_are_current and
  u32_reverse_byte_adapter_metrics_are_current), and merged the
  "security"/"stack_contract" prose so both sides' sentences survive.
  main ⊆ merged and PR ⊆ merged verified by record-id and
  (record, configuration)-id set comparison: 117 records / 285
  configurations after merge, no duplicates, no ids outside the union
  of both sides.
- knowledge/primitives/u32.md: concatenated both sides' bullets
  (signed compression boundary / unchecked decoder from the PR;
  reusable routing / byte-plane transpose / fixed-byte rotations /
  SHA-256 rotation from main). All 43 main bullets and 40 PR bullets
  verified present in the merged 45.
- src/arithmetic/u4/mod.rs: union of both sides' `pub mod` lines,
  alphabetically sorted (main added several u4 modules the PR
  predates; no semantic content lost).
- tests/primitive_metrics.rs: kept every #[test] fn from both sides
  (u32_compression_metrics_are_current from the PR;
  u32_reverse_byte_adapter_metrics_are_current, u32_equality_metrics_are_current,
  u32_fixed_rotation_metrics_are_current, u32_byte_planes_metrics_are_current
  from main). 207 fns after merge = union of main's 206 and the PR's
  169 (168 shared).

src/arithmetic/u32/README.md and src/arithmetic/u32/stack.rs merged
without conflict.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brenorb
brenorb deleted the feat/u32-signed-compare 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