Skip to content

Shrink short BLAKE3 from 59,534 to 43,930 bytes - #264

Merged
RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
benthecarman:blake3-short-pick
Oct 17, 2026
Merged

RobinLinus merged 3 commits into
solving-bitcoin:mainfrom
benthecarman:blake3-short-pick

Conversation

@benthecarman

Copy link
Copy Markdown
Contributor

Summary

The short direct-u4 BLAKE3 profile now comes from a new generator, src/hashes/blake3/short.rs. It models the main and alt stacks exactly and emits opcodes directly. The 32-byte unkeyed fragment shrinks from 59,534 to 43,930 bytes (−26%), and its strict peak falls from 527 to 499 items. Every length from 1 to 32 bytes and every mode and output width improves by a similar margin.

Public APIs and the witness layout are unchanged. The limb-profile scripts and every Ed25519 challenge script are byte-identical, checked by hashing an export of main.

Measurements

Hint items are 0 in every row. All 64 inputs are present at entry, and the peak includes the output verifier.

Configuration Script bytes Static non-push opcodes Strict peak
32 B unkeyed 59,534 → 43,930 41,135 → 28,854 527 → 499
32 B, low 128 bits 59,105 → 43,466 40,816 → 28,614 527 → 499
32 B keyed 59,532 → 43,915 41,135 → 28,855 527 → 499
32 B derive-key 59,521 → 43,928 41,131 → 28,856 527 → 499
32 B, 64-byte XOF 60,131 → 44,500 41,442 → 29,134 527 → 499
32 B keyed, 64-byte XOF 60,133 → 44,483 41,443 → 29,135 527 → 499
1 B unkeyed 56,124 → 39,294 – 435
Push + compute + verify, 32 B 59,726 → 44,122 – –

Table setup is 362 B and cleanup is 183 B (199 B for the 128-bit prefix). The scripts are over 32 KiB, so the compile policy applies no optimizer passes. The BLAKE3 peephole pass leaves every configuration unchanged.

How it works

Values are stored with offsets so that most lookups need no index arithmetic:

  • Addition results. A nibble n is stored as n + MU, which is directly the OP_PICK depth of a small table pointing at XOR row n. A full XOR is PICK ADD PICK.
  • Carries. These are stored as q + KAPPA, so a nibble sum is directly the depth of its modulo entry. DUP PICK TUCK SUB PICK yields the result nibble and the next carry.
  • Message words. The low nibble of each word gets the carry offset once, at entry.
  • Exact offsets elsewhere. Outside the steady state (round zero, constant operands, the output phase), the generator adds the exact residual offset before each lookup. Constant state, key and padding words are folded into those offsets and never pushed.
  • Self-check. While generating, every lookup and word operation is checked against host arithmetic on a sample message, and the final output against a host compression.
  • Input checks. An exact OP_DEPTH count check runs first. Each witness nibble is then range-checked with DUP 0 16 WITHIN VERIFY the first time it is fetched.

Main's packing of the 16 XOR rows into a 171-item shortest common superstring is kept. The rows go in as three pieces, fitted around the shift, lut, carry and modulo tables. Those tables keep their positions, so every shift adjustment still pushes as one data byte. Table memory falls from 384 to 302 items.

The column/diagonal G-call order is fixed per round from a coordinate search. It is at least 168 B shorter in all 288 length/mode/output configurations. Each XOR also tries more nibble orders.

The construction comes from an agentic Claude Opus 5.5 run against an older revision (5860061, where this script was 62,647 B; that run reached 44,401 B). Here it is generalized to lengths 1–32, all three modes and 16/32/64-byte output, and combined with main's table packing.

What did not pay

Recorded as NR-086, with the full table in knowledge/negative-results/blake3-short-generator.md:

  • Fully packed 295-item table: +341 B. Moving the shift table pushes every shift adjustment past 127, costing an extra push byte each.
  • Initial state as literals: +149 B.
  • Unbiased message low nibbles: +146 B.
  • Second shift table for the last round: −16 B, but at a cost of 16 more peak items.
  • One-step lookahead in the XOR order choice: −2 B, at about 3× generation time.
  • Natural (unpermuted) witness layout: −3 B. Not adopted, so existing witnesses stay valid.

About 45% of the remaining bytes are operand fetches (3-byte rolls of deep state nibbles, 4-byte message picks). OP-013 now targets 43,930 B.

Validation

Evidence is differentially-validated against the blake3 crate. The catalog's execution class moves from research-unlimited to unclassified: every test now runs under the strict stack limit, and no in-repo Bitcoin Core run was added.

Independently of this repository's tests, the 32-byte unkeyed script was graded with a separate harness. It runs on the pinned rust-bitcoin-scriptexec fork and compares against the blake3 crate:

  • correct on 503 messages;
  • sound on 581 malformed cases (out-of-range items at every position, wrong item counts, wrong claimed digests);
  • 43,930 B, 28,854 non-push opcodes, peak 499.

Every one of those 1,084 cases was then spent through Bitcoin Core's consensus library, as a single-leaf taproot script path with taproot flags. Core agrees on all of them: the 503 correct messages spend, and the 581 malformed cases fail.

Not done

  • Ed25519 challenge. Its scripts use the separate 64-byte-block compression with preserved caller prefixes, so they don't benefit yet. Porting the generator there is separate work.
  • Ignored readme_metrics_are_current suite. Not run, because it executes bn254 field scripts. The new blake3_short_metrics_are_current checks the short-profile rows.
  • Non-minimal encodings. Without the minimal-number policy rule, which tapscript consensus does not require, an encoding like [n, 0x00] is accepted as nibble n. A test confirms it yields the same digest, so it cannot select a different message. This behaviour is unchanged from main.

Test plan

  • cargo test --locked --lib hashes::blake3, including the ignored utils tests
  • cargo test --locked --test primitive_metrics (164 passed)
  • cargo test --locked -- --skip fields:: (all groups pass)
  • python3 tools/kb.py validate, cargo fmt --check
  • Seeded random and all-0xff messages for every length, mode and output width (297 scripts, strict stack)
  • Malformed items at every witness position, with and without minimal-number enforcement; missing and extra items; wrong digest
  • Modelled peak equals executed peak
  • Metrics updated intentionally with UPDATE_PRIMITIVE_METRICS=1 (single-threaded); every changed number is a short-profile row
  • External grader plus Bitcoin Core replay of 1,084 cases (above)

🤖 Generated with Claude Code

benthecarman and others added 3 commits October 16, 2026 20:31
Replace the tracked-stack backend of the short direct-u4 BLAKE3 profile
with a generator that models the main and alt stacks exactly and emits
opcodes directly. Addition outputs are stored biased so that each nibble
is directly the OP_PICK depth of its lookup entry, carries are biased so
that a nibble sum is directly the depth of its modulo entry, and
constant state words are folded into lookup addresses. While
generating, every lookup and word operation is checked against host
arithmetic on a sample message.

The construction comes from an agentic Claude Opus 5.5 run against an
older upstream revision. It is generalized here to every message length
from 1 to 32 bytes, all three modes and 16-, 32- and 64-byte output,
keeping the public API and the witness layout. The 32-byte unkeyed
fragment shrinks from 59,534 to 44,305 bytes and from 41,135 to 28,893
static non-push opcodes. Its strict peak including the verifier rises
from 527 to 581 items.

Remove the digit backend, late tables and keyed-root entry point that
only the old short path used. The limb profile and every Ed25519
challenge script stay byte-identical.

Add tests for seeded random messages in every mode and output width,
malformed witness items at every position, the exact stack model and
the table lifecycle, and a focused metric test for the short-profile
rows of the ignored repository-wide suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Store the 16 XOR rows of the short-profile tables as three pieces of
their 171-item shortest common superstring instead of 256 separate
entries, fitted around the shift, lut, carry and modulo tables. Those
tables keep their positions, so every steady-state shift adjustment
still pushes as one data byte. The table shrinks from 384 to 302 items
and is now built by a greedy encoder that copies earlier entries where
that is shorter than a literal.

Use a fixed column and diagonal G-call order per round, found by a
coordinate search over the 24 orders of each phase; it is shorter for
every length, mode and output width. Try cyclic nibble orders for each
XOR, and drop the materialized-state and unbiased-message variants,
which never produced the shortest script.

The 32-byte unkeyed fragment shrinks from 44,305 to 43,930 bytes and
28,854 static non-push opcodes, and its strict peak including the
verifier falls from 581 to 499 items, below the earlier 527.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rewrite the short-profile sections of the BLAKE3 README and the
blake3-short-u4 knowledge page for the new generator: its biased value
representations, packed table layout, generation-time choices,
lifecycle byte counts, stack contract, and the tests behind the
differential and malformed-witness evidence. Credit the agentic Claude
Opus 5.5 run the construction comes from.

Record the measured steps and the variants that did not pay as NR-086
with a dedicated page, move the OP-013 frontier to 43,930 bytes and 499
items, and add dated remeasurements to the closed keyed, derive-key and
XOF records instead of rewriting their history. Update the hash
comparison rows, the catalog record's description and deployment class,
and add depth-biased values to the lookup-table techniques.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RobinLinus
RobinLinus merged commit adda14f into solving-bitcoin:main Oct 17, 2026
3 checks passed
@benthecarman
benthecarman deleted the blake3-short-pick branch October 17, 2026 04:13
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