Skip to content

feat(sha256): add BIP340 tagged hash fragment - #68

Merged
RobinLinus merged 4 commits into
solving-bitcoin:mainfrom
brenorb:feat/sha256-tagged-hash
Sep 25, 2026
Merged

RobinLinus merged 4 commits into
solving-bitcoin:mainfrom
brenorb:feat/sha256-tagged-hash

Conversation

@brenorb

@brenorb brenorb commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add a reusable u32 SHA-256 fragment for SHA256(tag_hash || tag_hash || message) over a 32-byte message
  • preserve the prior chaining state while routing the second block above it
  • validate the BIP340 challenge tag vector and exact 32-item message contract
  • record script, witness, and stack metrics in the knowledge base

Validation

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

The full repository test suite was not run; this PR is scoped to the SHA-256 primitive and its metric/documentation surface.

Copy link
Copy Markdown
Contributor

Review: hold pending the changes below.

Integrate this with the SHA256 midstate work in #67/#82. The tag is known at generation time, so precompute the midstate after the constant 64-byte tag block instead of paying for that compression again in Script. Reproduce the tagged-hash vectors and measure the final whole fragment after this change.

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

@brenorb
brenorb force-pushed the feat/sha256-tagged-hash branch from df12eb7 to b579a2c Compare September 18, 2026 22:35
@brenorb

brenorb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated and rebased on #67 as commit b579a2c.\n\n- Reuses the midstate continuation machinery without changing the public 80-byte/640-bit contract.\n- Precomputes the chaining state after the 64-byte BIP340 tag block with the production bitcoin SHA-256 engine; the Script fragment now consumes only the 32-byte message and uses the correct 768-bit final length.\n- Adds independent tagged-hash vectors for multiple tags and messages containing 0x00, 0x7f, 0x80, and 0xff, plus strict wrong-message-count rejection. The API is BIP340-style tagged hashing of a payload, not the complete nonce-and-pubkey BIP340 challenge.\n- Re-measures the final whole fragment: 530,755 locking-script bytes, 65-byte fixture witness / 97-byte canonical maximum, 856-item strict composition peak, zero hints. The fragment is reported unoptimized and deployment remains unclassified.\n- Updates the README, knowledge page, comparison, catalog, and active metric coverage.\n\nValidation: 16 affected SHA-256 library tests passed; cargo test --locked --test primitive_metrics passed (107 passed, 1 ignored); python3 tools/kb.py validate passed; cargo fmt --all -- --check passed; git diff --cached --check passed.\n\nThis branch is stacked on #67 and should be reviewed after #67's consolidated midstate changes.

@RobinLinus
RobinLinus merged commit 9215db1 into solving-bitcoin:main Sep 25, 2026
3 checks passed
brenorb added a commit to brenorb/bitcoin-scripts that referenced this pull request Sep 28, 2026
…PIs, resolve NR-064 collision

Rebases the RIPEMD-160/SHA-1/SHA-256 output-prefix adapters over the SHA256
midstate-continuation and BIP340 tagged-hash APIs merged into main via solving-bitcoin#67/solving-bitcoin#68
(and the further 27-PR integration in solving-bitcoin#244). Resolves conflicts in
knowledge/catalog.json, knowledge/comparisons/hashes.md,
knowledge/negative-results/index.md, knowledge/primitives/sha256-u32.md,
src/arithmetic/u4/mod.rs, src/hashes/ripemd160/mod.rs, src/hashes/sha1/mod.rs,
src/hashes/sha256/README.md, src/hashes/sha256/sha2_u4.rs, and
tests/primitive_metrics.rs by keeping both API families: main's
sha256_80bytes_from_midstate/sha256_tagged_hash_32bytes plus this PR's
sha256_prefix/ripemd160_prefix/sha1_prefix adapters, with matching catalog
records, README rows, and primitive_metrics entries on both sides.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@brenorb
brenorb deleted the feat/sha256-tagged-hash 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