Skip to content

Benchmark Ripemd160 and optimize it#20

Open
timohanke wants to merge 17 commits intodfinity:mainfrom
research-ag:bench
Open

Benchmark Ripemd160 and optimize it#20
timohanke wants to merge 17 commits intodfinity:mainfrom
research-ag:bench

Conversation

@timohanke
Copy link
Copy Markdown
Contributor

Reduces instructions by 50%, GC pressure by 75%.

@timohanke timohanke requested a review from a team as a code owner April 23, 2026 12:47
Comment thread src/Ripemd160.mo Outdated
Comment on lines +352 to +355
// final block. Calling sum() a second time without an intervening
// reset() will hash additional padding bytes on top of the already-
// padded state and produce a different (incorrect) result. Call
// reset() before reusing the Digest for another message.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a high chance that developers miss this comment and (unknowingly) produce incorrect hashes.

How about:

  • A: adding a defensive guard: trap on misuse so callers fail loudly instead of silently producing a wrong hash
  • B: make sum() fully non-consuming (snapshot/restore): save the bits that padding mutates, the restore at the end.

Option A is clearly the easiest change, and it seems sufficient here to remove the footgun. If this is implemented, it would be good to mention this in the changelog.

Copy link
Copy Markdown
Contributor Author

@timohanke timohanke Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did something similar in the new sha2 version ( https://github.com/research-ag/sha2/tree/static, not yet published).

Ok, I did option A here now for Ripemd160 and for Hmac where the same problem existed. After sum(), a subsequent write... or second sum() call will trap.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comment. The doc-comment still says that calling sum() a second time will produce an incorrect result.

Copy link
Copy Markdown
Contributor Author

@timohanke timohanke Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it. And fixed a couple of other very small points brought up by CodeRabbit. Ready for final review now.

@timohanke timohanke requested a review from fspreiss April 28, 2026 09:45
fspreiss
fspreiss previously approved these changes Apr 28, 2026
@github-actions github-actions Bot dismissed fspreiss’s stale review April 28, 2026 12:13

Review dismissed by automation script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants