Skip to content

feat(u32): document fixed rotations - #45

Merged
RobinLinus merged 5 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-fixed-rotation-boundary
Sep 28, 2026
Merged

RobinLinus merged 5 commits into
solving-bitcoin:mainfrom
brenorb:feat/u32-fixed-rotation-boundary

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add direct reference-vector tests for the 8-, 16-, and 24-bit specialized rotations
  • document and catalog each fixed-byte rotation boundary
  • record generated script, serialized witness, and strict stack metrics

Research framing

Question: what are the exact costs of the fixed-byte rotations used by the u32 rotation dispatcher?

Result: u32_rrot8() is 3 script bytes, u32_rrot16() is 1 byte, and u32_rrot(24) is 2 bytes. The four-byte witness serializes to 9 bytes; strict peaks are 5, 4, and 5 items respectively.

The evidence is locally reproduced and unclassified for deployment. Inputs are raw byte-valued stack items; callers remain responsible for range and canonical ScriptNum checks.

Validation

  • cargo fmt --all -- --check
  • cargo test --locked fixed_byte_rotations_match_reference
  • cargo test --locked --test primitive_metrics u32_fixed_rotation_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: hold pending the changes below.

Fold this into the rotation/stack documentation package. The 24-bit rotation test should invoke the public helper rather than only manually reconstructing an OP_ROLL sequence; otherwise it does not protect the documented API.

Historical local validation (September 11, at this 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.

Rebase on current main, address the specific gaps above, and run the affected correctness and active metric checks. Broad/full-repository runs are not requested.

Reviewed commit: 1b738930cc7b11792ea28bad20b407467286954f.

@brenorb
brenorb force-pushed the feat/u32-fixed-rotation-boundary branch from 1b73893 to 61f1867 Compare September 18, 2026 19:53
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated in place as the rotation/stack documentation package and rebased onto current main. The 24-bit regression now calls the public u32_rrot(24) dispatcher, and the focused test uses runtime witness data while preserving an unrelated alt-stack sentinel. Added active metrics for u32_rrot8(), u32_rrot16(), and u32_rrot(24) with 9-byte representative and 13-byte canonical maximum witnesses; catalog/docs mark these helpers unchecked and caller-validated.

Checks: cargo test --locked --lib arithmetic::u32::rotate::tests::fixed_byte_rotations_match_reference; cargo test --locked --lib arithmetic::u32::rotate::tests::test_rrot; active fixed-rotation metrics; cargo test --locked --test primitive_metrics (106 passed, 1 ignored); cargo fmt --all -- --check; python3 tools/kb.py validate (101 records, 247 configurations); git diff --cached --check.

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated #46 here as the shared u32 representation/validation package. Added byte-for-byte u32_equal()/u32_equalverify() contracts, active metrics for both (17-byte representative / 25-byte canonical maximum witness, 9-item peak), four-position mismatch coverage, raw ScriptNum alias behavior, exact main/alt-stack preservation, and explicit short-word execution errors. The equality assertion leaves no result on success; it is not a terminal predicate by itself.

The required full metrics run after these Rust metric changes passed: 107 passed, 1 ignored. python3 tools/kb.py validate passes with 101 records and 249 configurations; formatting and staged-whitespace checks pass.

@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated #47 into this PR. Added executable seven-bit rotation boundary, stack-preservation, and short-input error coverage; active metrics and knowledge/docs now record the 76-byte fragment, 9-byte representative witness, 13-byte maximum witness, and 8-item peak. Required checks passed: cargo fmt --all -- --check, python3 tools/kb.py validate, git diff --cached --check, and cargo test --locked --test primitive_metrics (107 passed, 1 ignored).

Copy link
Copy Markdown
Contributor

Review of 17f289261022a45a3da55c4dc924895a7db9443f:

The 24-bit test now calls the public helper and active metrics were added, addressing the earlier review. One small doc correction remains: u8_rrot7(i) rotates the selected byte, so its new description as rotating a u32 word is misleading; describe the actual byte helper. Resolve shared catalog/metric additions and rerun fixed-rotation/equality tests and named metrics.

Current integration conflicts: knowledge/catalog.json, knowledge/primitives/u32.md, src/arithmetic/u32/README.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
… planes

Resolve the shared catalog, u32 knowledge page, u32 README, and primitive
metrics conflicts by keeping both sides:

- knowledge/catalog.json: merge the u32 security and stack-contract text so it
  covers the PR's raw-byte equality and unchecked fixed rotations as well as
  main's checked/unchecked byte-plane transpose. All 101 records and every
  configuration from both sides are present (262 configurations, no
  duplicates).
- knowledge/primitives/u32.md and src/arithmetic/u32/README.md: keep main's
  byte-plane bullet/sentence and the PR's fixed-rotation bullets.
- tests/primitive_metrics.rs: keep the PR's u32_equality and
  u32_fixed_rotation metric tests and main's u32_byte_planes metric test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The PR had changed the u8_rrot7(i) doc comment to "Right rotation of a u32
word by the fixed seven-bit SHA-256 rotation", but the helper only touches
one byte: it moves the item at stack depth i to the top and replaces it with
(x & 0x7f) (x >> 7), high bit on top, growing the stack by one item. The
recombination into rotated bytes happens in u32_rrot7().

Document the exact stack effect and the unchecked out-of-range behavior, add
the same note to the u32 README, and add
u8_rrot7_splits_only_the_selected_byte, which checks depths 0..=4 and byte
boundaries 0, 1, 0x7f, 0x80, 0xa5, 0xfe, 0xff, that the other limbs keep
their order, the final depth, an alt-stack marker, and the 0x100 unchecked
case.

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 00c4d95a71cdaf66696df541e636eae92d167301, on top of merge commit 537391a2e2dc8b30a88da73b23a786730723231d, which brings current main (e9d5a66b6391eccee64f72cc5af590bc14ed550c) into this PR.

  • u8_rrot7(i) doc (follow-up commit 00c4d95). The PR had changed its doc comment from "Right rotation of the i-th u8 element by 7 bits" to "Right rotation of a u32 word by the fixed seven-bit SHA-256 rotation". That doc comment was the only place with the wrong wording. git grep u8_rrot7 finds the helper only in rotate.rs, not in the README, the knowledge page or the catalog. Reading the code shows the helper does less than either description. It moves the item at stack depth i to the top and replaces it with (x & 0x7f) (x >> 7), with the high bit on top, so the stack grows by one item. It does not recombine the byte. u32_rrot7() builds each rotated byte as 2 * low + high across neighbouring limbs. The doc comment now gives the exact stack effect x_i x_{i-1} .. x_0 -> x_{i-1} .. x_0 (x & 0x7f) (x >> 7), says the other items keep their values and order, and gives the unchecked result for items outside 0..=255: (x - 128, 1) when x >= 128, otherwise (x, 0). The u32 README bullet for u32_rrot7() has a matching note about the byte helper.

  • Executed check. The new test arithmetic::u32::rotate::tests::u8_rrot7_splits_only_the_selected_byte covers depths 0..=4, including the {i} OP_ROLL path used by u32_rrot7(). It uses bytes 0, 1, 0x7f, 0x80, 0xa5, 0xfe, 0xff, surrounded by distinct limbs. For each case it checks the high bit, the low seven bits, every other limb in its original order, OP_DEPTH == 0 after that (which proves the stack grew by exactly one), and an alt-stack marker. It also pins the unchecked 0x100 -> (0x80, 1) case. The test runs through the shared execute_script_with_inputs_strict harness: tapscript context, stack limit enforced, deployment unclassified. I ran three temporary mutants, and each one made the test fail:

    • Wrong item: for i = 1, OP_SWAP replaced by OP_ROT. Failure: u8_rrot7(1) did not split byte 0x00.
    • Wrong amount: split threshold changed from 128 to 64, which is a six-bit split. Failure: u8_rrot7(0) did not split byte 0x7f.
    • Outputs swapped: OP_SWAP added after OP_ENDIF. Failure: u8_rrot7(0) did not split byte 0x01.

    After each mutant I restored the file and confirmed it was byte-identical to the original (cmp). The unmutated test passes.

  • Integration (merge commit 537391a). git merge --no-ff origin/main conflicted in exactly the four files you listed: knowledge/catalog.json, knowledge/primitives/u32.md, src/arithmetic/u32/README.md and tests/primitive_metrics.rs.

    • Catalog: the u32 security and stack_contract fields now include both this PR's text (raw-byte equality, unchecked fixed rotations) and main's text (checked and unchecked byte-plane transpose). Records: main 101, PR 101, merged 101. Configurations: main 256, PR 250, merged 262. Nothing from either side is missing and there are no duplicates. The only PR-side list entries that differ are the two arithmetic/u4-lsb placeholder paths, which main had already replaced with concrete test names. This PR never changed them, so main's version stands.
    • Knowledge page and README: main's byte-plane sentence and bullet are kept alongside this PR's fixed-rotation bullets.
    • Metrics: all #[test] functions from both sides are kept. Main has 112, the PR has 107, and the merged file has 114, with nothing missing and no duplicates. That includes this PR's u32_equality_metrics_are_current and u32_fixed_rotation_metrics_are_current and main's u32_byte_planes_metrics_are_current.
  • Metrics. No fixture value changed and I did not refresh any metric.

Validation on the committed head with a clean tree:

  • cargo test --locked --lib arithmetic::u32::rotate: 19 passed. arithmetic::u32::stack (the equality tests): 25 passed. arithmetic::u32::byte_planes: 5 passed. All of arithmetic::u32::: 127 passed.
  • Named metrics u32_fixed_rotation, u32_equality, u32_rrot7_checked, u32_rrot8_checked, u32_rrot16_checked and u32_byte_planes: 6 passed. The full primitive_metrics file: 114 passed, 1 ignored.
  • python3 tools/kb.py validate: 101 records, 262 configurations. python3 -m unittest discover -s tools -p 'test_*.py': 45 passed.
  • cargo fmt --all -- --check and cargo test --locked --no-run both pass.

All of this evidence is local, and the deployment class is unclassified. No Bitcoin Core or relay-policy validation 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-fixed-rotation-boundary 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