Skip to content

ripemd: interleave the two RIPEMD-160 lines in compress - #944

Closed
DaniPopes wants to merge 2 commits into
RustCrypto:masterfrom
DaniPopes:dani/ripemd160-interleave-lines
Closed

DaniPopes wants to merge 2 commits into
RustCrypto:masterfrom
DaniPopes:dani/ripemd160-interleave-lines

Conversation

@DaniPopes

Copy link
Copy Markdown

Each of the 80 steps of a RIPEMD-160 line depends on the previous step of that line, but the left and right lines are independent of each other. The macro-generated compress ran all 80 left steps and then all 80 right steps, so the two dependency chains started about 900 instructions apart and the CPU could not overlap them. The function ran at about 2.5 instructions per cycle, limited by one serial chain at a time.

This rewrites compress for RIPEMD-160 so that both lines advance one step at a time. The message order, rotation and constant tables are now plain arrays, the 80 steps are fully unrolled by a macro, and the additions of each step are ordered so that only the boolean function, one addition, the rotation and the final addition lie on the chain. RIPEMD-128, -256 and -320 are not changed.

On x86-64 with default flags (no target-cpu), hashing 64 KiB repeatedly on an AMD EPYC 4245P pinned at 4 GHz:

cycles per block ns per block
master 706 176
this PR 462 115

The same change in an EVM that calls this crate for the RIPEMD-160 precompile (criterion, full transaction including the precompile call, median of 5 alternating runs on the same machine):

input before after
0 bytes 1414 ns 1354 ns 🟢 0.96x
64 bytes 1567 ns 1460 ns 🟢 0.93x
256 bytes 2076 ns 1797 ns 🟢 0.87x
1024 bytes 4157 ns 3144 ns 🟢 0.76x

The existing test vectors pass on 1.85 and stable, and the new compress matches the old one on 16800 random inputs of 0 to 2099 bytes. I only measured x86-64.

Each of the 80 steps of a RIPEMD-160 line depends on the previous step of
that line, but the left and right lines are independent. The macro-generated
`compress` ran all 80 left steps and then all 80 right steps, so the two
dependency chains were 80 steps (about 900 instructions) apart and the CPU
could not overlap them.

Advance both lines one step at a time, expressed through the standard
message-order, rotation and constant tables and a fully unrolled macro, so
the out-of-order core runs the two chains side by side. The additions of
each step are ordered so that only the boolean function, one addition, the
rotation and the final addition are on the chain.

On an x86-64 build with default flags this takes 708 cycles per block down to
462 cycles per block (hashing 64 KiB repeatedly, `perf stat` cycles:u).
Digests are unchanged: the existing test vectors pass, and 16800 random
inputs of 0 to 2099 bytes match ripemd 0.2.0.
@DaniPopes
DaniPopes marked this pull request as ready for review November 9, 2026 17:44
@DaniPopes DaniPopes closed this Nov 9, 2026
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.

1 participant