Switch SHA-256 implementation from libsodium to Rust. - #5364
Merged
Conversation
Rust implementation is faster because it uses SHA-NI instructions for hardware acceleration. In a previous optimization iteration (never merged) we've used OpenSSL implementation in C++ (for the same reason), however it turned out to be problematic as some of its versions acquired lock for hashing, which made it non-parallelizable.
Contributor
There was a problem hiding this comment.
Pull request overview
Moves SHA-256 hashing from libsodium to RustCrypto’s hardware-accelerated sha2 implementation.
Changes:
- Adds one-shot and incremental Rust SHA-256 APIs through the CXX bridge.
- Refactors C++ SHA-256 wrappers to use the Rust implementation.
- Adds and locks the
sha2dependency.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/rust/src/sha256.rs |
Implements Rust SHA-256 hashing. |
src/rust/src/lib.rs |
Registers the SHA-256 module. |
src/rust/src/bridge.rs |
Exposes hashing APIs to C++. |
src/rust/Cargo.toml |
Adds the sha2 dependency. |
src/crypto/SHA.h |
Converts incremental state to PImpl. |
src/crypto/SHA.cpp |
Routes hashing through Rust. |
Cargo.lock |
Records the direct dependency. |
graydon
approved these changes
Jul 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Rust implementation is faster because it uses SHA-NI instructions for hardware acceleration.
In a previous optimization iteration (never merged) we've used OpenSSL implementation in C++ (for the same reason), however it turned out to be problematic as some of its versions acquired lock for hashing, which made it non-parallelizable. That's why this accidentally got included into 'parallelization improvements' optimization batch - but I don't really see much harm in merging this sooner rather than later.
This has a tangible impact on apply load benchmarks (in the order of 10s of ms) and looks really good on micro-benchmarks (at least on my laptop, but most relatively modern CPUs should support SHA-NI):
Checklist
clang-formatv8.0.0 (viamake formator the Visual Studio extension)