Skip to content

feat(sha1): add midstate continuation - #71

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

RobinLinus merged 2 commits into
solving-bitcoin:mainfrom
brenorb:feat/sha1-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 SHA-1 midstate continuation fragment
  • validate the independent compression oracle, standard digest, and exact suffix boundary
  • record the 209,619-byte metric in the README, comparison, and catalog

Validation

  • cargo test --locked hashes::sha1::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/#73/#82 into a consistent midstate-continuation family. Share padding/byte-order contracts and reference vectors, with active focused metrics for each supported configuration.

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: 174563e755f0c6a2da0b65023c63347320893d19.

@brenorb
brenorb force-pushed the feat/sha1-midstate-boundary branch from 174563e to 94c819e Compare September 18, 2026 22:44
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated and rebased on current main as commit 94c819e.\n\n- Keeps #71 as the SHA-1 member of the continuation family; it is not folded into the SHA-256 PRs.\n- Documents the exact contract: H0..H4 after one unpadded 64-byte prefix block, exactly 16 canonical byte-valued suffix items, 20 digest bytes returned, and caller-supplied prefix authentication.\n- Replaces the hand-rolled test compressor state source with the production bitcoin SHA-1 engine for the named 64-byte-0x42 fixture.\n- Adds an asymmetric prefix/suffix vector containing 0x00, 0x7f, 0x80, and 0xff, compares all 20 digest bytes, and adds strict short-input plus excess-input rejection.\n- Corrects the padding wording: SHA-1 uses a 64-bit big-endian length field containing 640 for the 80-byte message.\n- Re-measures the real-state final fragment: 205,489 locking-script bytes, 33-byte fixture witness / 49-byte canonical maximum, 632-item strict composition peak, zero hints. The fragment is unoptimized and deployment remains unclassified.\n\nValidation: affected SHA-1 tests passed; cargo test --locked --test primitive_metrics passed (106 passed, 1 ignored); python3 tools/kb.py validate passed; cargo fmt --all -- --check passed; git diff --cached --check passed.

Copy link
Copy Markdown
Contributor

Review of 94c819ead7f02883955fe37616318525f8c09adb:

The asymmetric reference vector and exact suffix boundary tests address the continuation-contract concerns. Preserve this PR's active metric test alongside current main's new hash metrics when resolving the shared test-file conflict; rerun the SHA1 midstate/padding checks and named metric test.

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.

…icts

Integrates origin/main (3d1001f) into feat/sha1-midstate-boundary
(94c819e) to address Robin Linus's 2026-09-25 review. Robin's conflict
list named only tests/primitive_metrics.rs; the actual trial merge also
conflicted on src/arithmetic/u4/mod.rs and src/hashes/sha1/mod.rs.

- src/arithmetic/u4/mod.rs: both sides' `pub mod` lists were already a
  subset of main's, so main's file is taken as-is (verified: every PR
  mod line is present in main's list).
- src/hashes/sha1/mod.rs: kept the PR's new sha1_80bytes_from_midstate
  continuation function, but adapted it to main's shared
  u8_reverse_toaltstack() helper (arithmetic::u32::stack) instead of
  restoring the PR's now-superseded local push_reverse_bytes_to_alt,
  per the repo convention of keeping main's refactor.
- tests/primitive_metrics.rs: kept every #[test] fn and metric key from
  both sides (134 tests = 133 from main + 1 PR-only
  sha1_midstate_metrics_are_current; 934 metric keys = union of main's
  931 and the PR's 802, no duplicates). All of main's hash metrics
  (including the new SHA-256 midstate/tagged-hash metrics) remain.

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

brenorb commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 709cbca7d3e9247566c0e7c4842d134483b466f3, merging current main (3d1001fc617eacd7920200b5c87ed7be25a2269c) into this branch.

  • Conflict list differs from the one quoted above. Robin's comment named only tests/primitive_metrics.rs. A trial merge on the reviewed head (git merge --no-commit --no-ff origin/main at 94c819e) actually conflicted on three files: src/arithmetic/u4/mod.rs, src/hashes/sha1/mod.rs, and tests/primitive_metrics.rs. Robin's target file is included and resolved below; the other two are reported for completeness since main moved further (27 PRs, Integrate 27 reviewed PRs with corrected validation and Taproot preflight #244) between the review and this integration.

  • src/arithmetic/u4/mod.rs: comm -23 <(git show HEAD:...|sort) <(git show origin/main:...|sort) is empty — every pub mod line this PR's base had is already present in main's file (main added cyclic_equality, equality, msb, mul, odd_inverse, threshold, transition_count, trichotomy, clamp independently). Resolved by taking main's file as-is; nothing from this PR was dropped because nothing was unique to it.

  • src/hashes/sha1/mod.rs: main refactored the existing push_reverse_bytes_to_alt local helper into a shared arithmetic::u32::stack::u8_reverse_toaltstack, with identical body, and removed the local copy. Kept this PR's new sha1_80bytes_from_midstate continuation function but adapted its one call site to u8_reverse_toaltstack(16) instead of restoring the now-superseded local helper, per the repo's "keep main's refactor, adapt the PR" rule. No other part of the file's diff overlapped.

  • tests/primitive_metrics.rs: kept every #[test] fn and metric key from both sides. Proof: main has 133 #[test] fns / 931 metric keys, this PR's base has 106 fns / 802 keys, and the merged file has exactly 134 fns / 934 keys — the set-union of both sides in each case, with main ⊆ merged and PR ⊆ merged verified by direct set comparison (no missing elements either direction) and no duplicates. The sole fn present only in the PR side is sha1_midstate_metrics_are_current; every one of main's hash metrics (including its new SHA-256 midstate/tagged-hash metrics, e.g. sha256_midstate_u4_witness, sha256_tagged_hash_witness, bip340_challenge_tag_hash) is preserved unchanged. rg -n '^(<<<<<<<|=======|>>>>>>>)' . is empty across the whole tree after resolution.

  • Midstate/padding contract mutant (reintroduces the encoded-length defect): changed { u32_push(640) } to { u32_push(128) } in sha1_80bytes_from_midstate (encoding only the 16-byte suffix's bit-length instead of the full 80-byte prefix+suffix message). Under the mutant, both hashes::sha1::tests::continues_from_midstate and continues_from_asymmetric_midstate_vector fail with Error: EqualVerify on the digest-byte comparison (thread '...continues_from_midstate' panicked ... Error: EqualVerify). Reverted; git diff src/hashes/sha1/mod.rs empty afterward, confirmed before the merge commit.

  • Suffix boundary mutant (reintroduces an off-by-one acceptance width): changed { u8_reverse_toaltstack(16) } to { u8_reverse_toaltstack(17) } (accepts one extra suffix byte). Under the mutant, hashes::sha1::tests::rejects_extra_suffix_bytes fails: thread '...rejects_extra_suffix_bytes' panicked at src/hashes/sha1/mod.rs:539:9: assertion failed: !result.success — the 17-input witness that should be rejected now succeeds. The control test rejects_short_suffix_bytes (unaffected by this mutant, since it exercises the short-input side) still passes, so the rejection is not vacuous. Reverted; git diff empty afterward.

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

    • cargo test --locked --lib hashes::sha1 -- --skip fields:: → 10 passed, 0 failed (round_functions_match_reference, message_schedule_matches_reference, single_round_matches_reference, hashes_standard_vectors, continues_from_midstate, continues_from_asymmetric_midstate_vector, rejects_extra_suffix_bytes, rejects_short_suffix_bytes, rejects_unsupported_message_length, prints_default_script_size).
    • cargo test --locked --test primitive_metrics sha1_midstate_metrics_are_current -- --exact → 1 passed, 0 failed (no UPDATE_PRIMITIVE_METRICS refresh needed — the merge only changed the call site, not the compiled script, so the README-quoted metrics are unchanged: 205,489 policy-produced fragment bytes, 33-byte fixture witness / 49-byte canonical maximum witness, 632-item strict combined main+alt-stack peak, zero hint items).
    • python3 tools/kb.py validate → knowledge base valid: 117 records, 284 configurations.
    • cargo fmt --all -- --check → clean (no output).
    • cargo test --locked --no-run → whole crate + all test binaries compile (bitcoin_lab, execution_limits, knowledge_base, primitive_metrics, prince_checked, prince_differential, taproot_commitment, tapscript_budget, tapscript_profiles).
  • All environment variables from CI were exported for every cargo command (CARGO_PROFILE_DEV_OPT_LEVEL=1, CARGO_PROFILE_TEST_OPT_LEVEL=1, CARGO_BUILD_JOBS=3). No field-arithmetic tests were run.

  • Evidence language: results above are locally-reproduced (deterministic local tests reproducing the claimed behavior, including both mutant failures). No Bitcoin Core or relay-policy validation is claimed. The continuation fragment's deployment class remains unclassified; execution uses the tapscript helper with the stack limit disabled (execute_script_without_stack_limit / execute_script_with_inputs_strict), i.e. research-unlimited where the stack-limit-disabled helper is used, per AGENTS.md.

  • Pushed to brenorb/bitcoin-scripts feat/sha1-midstate-boundary as a fast-forward (94c819e..709cbca). gh pr view 71 --repo solving-bitcoin/bitcoin-scripts --json headRefOid,mergeable reports headRefOid: 709cbca7d3e9247566c0e7c4842d134483b466f3, mergeable: MERGEABLE.

Nothing from Robin's comment was left undone.

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