Skip to content

feat(u32): add reusable reverse byte adapter - #40

Merged
RobinLinus merged 2 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-reverse-byte-adapter
Sep 28, 2026
Merged

RobinLinus merged 2 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-reverse-byte-adapter

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add the reusable u8_reverse_toaltstack(num_bytes) byte-order adapter to the u32 stack module
  • replace four identical hash-local implementations while preserving the SHA-256 helper path
  • add order/state, zero-length, and short-stack tests plus catalog and README metrics

Research framing

Question: can the repeated hash input-routing fragment be made a single measured u32 primitive without changing its stack contract?

Result: yes. The adapter moves raw byte items from the main stack to the alt stack and restores top-first order. The representative four-byte fragment is 8 script bytes, 9 serialized witness bytes, and a strict five-item combined peak.

The execution evidence is locally reproduced and unclassified. Inputs remain raw Script numbers; callers must enforce byte range and canonical encoding when the trust boundary requires it. The fragment excludes input pushes and output checks.

Validation

  • cargo fmt --all -- --check
  • cargo test --locked arithmetic::u32::stack::tests::reverse_byte_batch_to_altstack_preserves_order_and_state
  • cargo test --locked --test primitive_metrics u32_reverse_byte_adapter_metrics_are_current
  • python3 tools/kb.py validate
  • git diff --check

The full repository test suite was intentionally not run; field-arithmetic tests remain skipped by default.

Copy link
Copy Markdown
Contributor

Review recommendation: fix before merging.

The shared reverse-byte adapter is useful, but the compatibility re-export pub use ... as push_reverse_bytes_to_alt conflicts with the repository instruction against legacy-path compatibility re-exports. Update consumers to import the domain-oriented arithmetic helper directly, and remove the old alias before merging.

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

@brenorb
brenorb force-pushed the feat/u32-reverse-byte-adapter branch from abb7767 to 62e0a03 Compare September 18, 2026 19:39
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated in place and rebased onto current main. Removed the legacy push_reverse_bytes_to_alt compatibility/helper exports and switched SHA-1, SHA-256, RIPEMD-160, SHAKE256, and HASH160 consumers to the domain-oriented u8_reverse_toaltstack helper. Strengthened the helper test for unrelated alt-stack state, raw/noncanonical items, zero length, and an explicit short-stack error. Corrected the representative/max witness metadata (9/13 bytes).

Checks: focused U32 and hash tests passed; active adapter, HASH160, SHAKE256, and bitwise/hash metric tests passed; cargo test --locked --test primitive_metrics (106 passed, 1 ignored); cargo fmt --all -- --check; python3 tools/kb.py validate (101 records, 245 configurations); git diff --cached --check.

Copy link
Copy Markdown
Contributor

Review of 62e0a03100bee51d9f3d31714263efd62139ed2e:

The shared adapter and removal of the legacy alias address the earlier feedback. However, this diff also removes OP_DROP from the unrelated canonical_compress_rejects_malformed_limbs test. If malformed input were accepted, the compressed result plus OP_TRUE would now fail clean-stack anyway, hiding the regression. Restore the output cleanup and assert the intended validation error. Keep this refactor limited to the adapter and its consumers.

Current integration conflicts: knowledge/catalog.json, knowledge/primitives/u32.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.

Integrate PR solving-bitcoin#40 with current main at e9d5a66, preserving the main additions and resolving the shared u32 catalog, docs, and metric rows.
@brenorb

brenorb commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in f1c3ba7, integrating main at e9d5a66 while retaining the shared adapter and its consumers.

Reproduced the test-harness defect before the fix: even a valid compression input left the compressed result plus OP_TRUE and reported failure without an interpreter error. Restored OP_DROP OP_TRUE. The malformed-limb test now uses strict local runtime witnesses, requires MinimalData for the tested nonminimal aliases and Verify for the tested out-of-range values at each position, and includes a valid-input control with identical cleanup. A deliberately unchecked compressor succeeds on an out-of-range limb, and the same shared rejection assertion demonstrably catches that bypass.

Resolved the catalog, knowledge-page, and metric conflicts while preserving main's byte-plane additions. Updated the remaining integrated SHA-256 suffix call site to the shared adapter; no legacy alias was restored. The production compression implementation is unchanged.

Validation on the integrated tree: all 24 u32 stack tests and 38 affected hash-consumer tests passed (SHA-256/u32, SHA-1, RIPEMD-160, HASH160, SHAKE256), as did the adapter and canonical-compression named metrics, KB validation, formatting, and whitespace checks. The corrected malformed-limb regression enforces the combined stack limit in local tapscript; other retained non-strict test paths remain research-unlimited. No new Bitcoin Core consensus or relay-policy claim is made. No broad repository or field-arithmetic suite was run.

@RobinLinus
RobinLinus merged commit b6470b3 into solving-bitcoin:main Sep 28, 2026
3 checks passed
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-reverse-byte-adapter 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