Skip to content

feat(ripemd160): add midstate continuation - #73

Merged
RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/ripemd160-midstate-boundary
Sep 28, 2026
Merged

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
brenorb:feat/ripemd160-midstate-boundary

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add a fixed 64-byte-prefix / 16-byte-suffix RIPEMD-160 midstate continuation fragment
  • validate an independent dual-branch compression oracle, standard digest, and exact suffix boundary
  • record the 243,956-byte metric in the README, comparison, and catalog

Validation

  • cargo test --locked hashes::ripemd160::tests -- --nocapture
  • python3 tools/kb.py validate
  • cargo test --locked --test primitive_metrics
  • cargo fmt --check
  • git diff --check

The full repository test suite was not run.

Copy link
Copy Markdown
Contributor

Review: hold pending the changes below.

Combine this with #67/#71/#82 into a consistent midstate-continuation family. Retain the reference vectors and add active focused checks for the newly reported metrics.

Historical local validation (September 11, at this unchanged PR head): python3 tools/kb.py validate passed; 7 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.

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: deb096bcd06c19c101fb7c4ad22ccc8922183706.

@brenorb
brenorb force-pushed the feat/ripemd160-midstate-boundary branch from deb096b to 545afc6 Compare September 18, 2026 22:55
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated in place in commit 545afc6.

  • Rebased on current main.
  • Replaced the handwritten RIPEMD-160 midstate compressor in the test with the pinned bitcoin_hashes HashEngine, using RIPEMD little-endian state words.
  • Added an asymmetric prefix/suffix reference vector containing 0x00, 0x7f, 0x80, and 0xff.
  • Added strict 15-item and complete-wrapper 17-item rejection checks, plus 55/56/63/64/65-byte padding-boundary regressions.
  • Added active continuation metrics: 240,152 script bytes, 33-byte representative witness (49-byte canonical maximum), and 406-item strict combined main/alt-stack peak; zero auxiliary hints.

Validation: focused RIPEMD tests passed; cargo fmt --all -- --check; python3 tools/kb.py validate; git diff --cached --check; cargo test --locked --test primitive_metrics (106 passed, 1 ignored).

Copy link
Copy Markdown
Contributor

Review of 545afc61f8a2766d2ddd14017efd49f8a7898093:

The asymmetric reference vector and explicit little-endian RIPEMD length handling make the midstate contract clearer. Preserve both this PR's metric test and current main's newly added hash metrics when resolving the shared test-file conflict; rerun the midstate/padding regressions and named metric check.

Current integration conflicts: 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.

Integrates origin/main (3d1001f, PR solving-bitcoin#244's 27-PR batch) into the
ripemd160 midstate-continuation branch. Robin Linus's review on
2026-09-25 named tests/primitive_metrics.rs as the integration
conflict; the actual merge also produced conflicts in
src/hashes/ripemd160/mod.rs and src/arithmetic/u4/mod.rs.

- tests/primitive_metrics.rs: kept every #[test] fn and metric `key`
  from both sides (main's 134 test fns plus the PR's
  ripemd160_midstate_metrics_are_current, merged set has 135, no
  duplicates; sha256_midstate_metrics_are_current and
  sha256_tagged_hash_metrics_are_current preserved as required).
- src/hashes/ripemd160/mod.rs: main independently extracted the
  byte-reversal helper into the shared
  arithmetic::u32::stack::u8_reverse_toaltstack; adapted
  ripemd160_80bytes_from_midstate to call that shared helper instead
  of restoring the PR's now-redundant private
  push_reverse_bytes_to_alt.
- src/arithmetic/u4/mod.rs: main's mod list is a strict superset of
  the PR's (PR only reordered existing entries); took main's version
  (47 pub mod lines) verbatim.
- knowledge/catalog.json: verified main's 117 records and the PR's
  101 records (including the ripemd160-u32 record's
  message-80-midstate configuration) are both subsets of the merged
  117-record file, with no duplicate ids.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brenorb

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in f8e7c15a0b4b6be9ada3e3c220a030a86cf3048b, merging current origin/main (3d1001fc617eacd7920200b5c87ed7be25a2269c, which had moved to integrate PR #244's 27-PR batch since your review) into feat/ripemd160-midstate-boundary.

  • Conflicts: your list named tests/primitive_metrics.rs. The actual git merge --no-commit --no-ff origin/main also conflicted on src/hashes/ripemd160/mod.rs and src/arithmetic/u4/mod.rs, both because main made unrelated changes in the same regions:

    • src/arithmetic/u4/mod.rs: main independently sorted/extended the pub mod list (added cyclic_equality, equality, msb, mul, odd_inverse, threshold, transition_count, trichotomy, adjacent_eq, clamp); the PR side only reordered its own 37 entries and added none. Confirmed main's 47-entry list is a strict superset of the PR's 37, so I took main's file verbatim.
    • src/hashes/ripemd160/mod.rs: main extracted the byte-reversal loop this PR also needed into the shared arithmetic::u32::stack::u8_reverse_toaltstack (identical body to the PR's private push_reverse_bytes_to_alt) and updated the existing ripemd160_with_table call site. I adapted ripemd160_80bytes_from_midstate to call the shared u8_reverse_toaltstack(16) instead of restoring the PR's now-redundant private helper, per the "keep main's refactor, adapt the PR" rule.
    • tests/primitive_metrics.rs: merged the use bitcoin::hashes::{...} import lines, both new helper blocks (ripemd160_midstate_42x64/ripemd160_midstate_witness and the SHA256 midstate/tagged-hash helpers + SHA256_MIDSTATE_42X64), and both new tail tests (ripemd160_midstate_metrics_are_current ahead of u4_cyclic_equality_metrics_are_current/u32_or_constant_metrics_are_current).
  • Preservation proof (name-set comparisons): extracted every #[test] fn name and every key: "..." from git show origin/main:tests/primitive_metrics.rs, git show HEAD:tests/primitive_metrics.rs (pre-merge, 545afc6), and the merged file.

    • Test fns: main = 134, PR = 107 (106 shared with main + 1 unique: ripemd160_midstate_metrics_are_current), merged = 135. comm -23 in both directions is empty: main ⊆ merged and PR ⊆ merged, no duplicate fn names in the merged file. sha256_midstate_metrics_are_current and sha256_tagged_hash_metrics_are_current are present and pass.
    • Metric keys: main = 931, PR = 802, merged = 934, both sides fully subset of merged with no unexpected duplicates introduced by the merge (the ~30 duplicate keys present in the merged file, e.g. ripemd160_u32_32, ripemd160_u32_80_midstate, sha2_u32_80_midstate*, all pre-existed independently on both sides as an intentional cross-check pattern between the big metrics()/readme_metrics_are_current collector and the focused per-primitive tests).
    • knowledge/catalog.json (auto-merged, no conflict, but re-verified per the shared-file rule anyway): main has 117 records, the PR side 101 (all 101 already ⊆ main's 117), merged has 117, no duplicate ids. The hash/ripemd160-u32 record's configuration ids are {message-32} on main and {message-32, message-80-midstate} on the PR side; merged keeps both, matching PR ⊆ merged and main ⊆ merged.
  • Mutant 1 — little-endian → big-endian length encoding (the continuation padding shared by ripemd160_80bytes_from_midstate and ripemd160()): at src/hashes/ripemd160/mod.rs, temporarily changed { u32_push(((total_message_bytes as u32) * 8).swap_bytes()) } to { u32_push((total_message_bytes as u32) * 8) } (dropping .swap_bytes()). cargo test --locked --lib hashes::ripemd160 then failed 4 of 9: continues_from_midstate, continues_from_asymmetric_midstate_vector, hashes_standard_vectors, and padding_boundary_lengths, each panicking with Error: EqualVerify (assertion at src/hashes/ripemd160/mod.rs:508/538 for the two midstate tests as originally numbered). Reverted; git diff empty; re-ran the same command — 9/9 pass.

  • Mutant 2 — corrupt the asymmetric reference vector's expected digest by one byte: in continues_from_asymmetric_midstate_vector, temporarily changed let expected = reference_ripemd160::Hash::hash(&message).to_byte_array(); to compute expected then flip expected[0] ^= 0x01. cargo test --locked --lib hashes::ripemd160::tests::continues_from_asymmetric_midstate_vector failed with Error: EqualVerify at the assert!(result.success, ...) line (539). Reverted; git diff empty; re-ran — passes.

  • Validation on the committed HEAD (f8e7c15a) with a clean working tree (git status --porcelain empty):

    • cargo test --locked --lib hashes::ripemd160 — 9 passed.
    • cargo test --locked --test primitive_metrics -- ripemd160_midstate_metrics_are_current sha256_midstate_metrics_are_current sha256_tagged_hash_metrics_are_current consuming_bitwise_and_hash_metrics_are_current — 4 passed.
    • python3 tools/kb.py validate — "knowledge base valid: 117 records, 284 configurations".
    • python3 -m unittest discover -s tools -p 'test_*.py' — 49 passed.
    • cargo fmt --all -- --check — clean.
    • cargo test --locked --no-run — whole crate and every test binary compiled (bitcoin-lab lib, primitive_metrics, execution_limits, knowledge_base, taproot_commitment, tapscript_budget, tapscript_profiles, prince_checked, prince_differential).
    • All commands exported CARGO_PROFILE_DEV_OPT_LEVEL=1 CARGO_PROFILE_TEST_OPT_LEVEL=1 CARGO_BUILD_JOBS=3; no field-arithmetic tests (fields::) were run.
  • No metric value changed (the ripemd160_midstate_metrics_are_current fixture is untouched by the merge; its script-length/witness-size/stack-peak values are unchanged from 545afc6). No new negative-result IDs were needed or added (none were assigned for this PR).

  • Pushed as a fast-forward (545afc6..f8e7c15) to brenorb/bitcoin-scripts:feat/ripemd160-midstate-boundary. gh pr view 73 --repo solving-bitcoin/bitcoin-scripts --json headRefOid,mergeable reports headRefOid: f8e7c15a0b4b6be9ada3e3c220a030a86cf3048b, mergeable: MERGEABLE (checked 5 times).

Evidence throughout is locally-reproduced against this repository's tapscript test harness only (research-unlimited, stack limit disabled per execute_script_without_stack_limit/execute_raw_script_with_inputs_strict, as applicable); no Bitcoin Core or relay-policy validation is claimed.

@RobinLinus
RobinLinus merged commit 937d7e8 into solving-bitcoin:main Sep 28, 2026
3 checks passed
@brenorb
brenorb deleted the feat/ripemd160-midstate-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