Skip to content

Switch SHA-256 implementation from libsodium to Rust. - #5364

Merged
graydon merged 3 commits into
stellar:masterfrom
dmkozh:rust_sha256
Jul 24, 2026
Merged

Switch SHA-256 implementation from libsodium to Rust.#5364
graydon merged 3 commits into
stellar:masterfrom
dmkozh:rust_sha256

Conversation

@dmkozh

@dmkozh dmkozh commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

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):

libsodium
SHA256 bytes bench total duration: 26.6911 s, average duration per entry: 2.66911 ms
XDR SHA256 bench total duration: 42.8356 s, average duration per entry: 4.28356 ms

Rust
SHA256 bytes bench total duration: 4.57691 s, average duration per entry: 0.457691 ms
XDR SHA256 bench total duration: 15.4851 s, average duration per entry: 1.54851 ms

Checklist

  • Reviewed the contributing document
  • Rebased on top of master (no merge commits)
  • Ran clang-format v8.0.0 (via make format or the Visual Studio extension)
  • Compiles
  • Ran all tests
  • If change impacts performance, include supporting evidence per the performance document

Copilot AI review requested due to automatic review settings July 23, 2026 18:26
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.

Copilot AI left a comment

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.

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 sha2 dependency.

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.

Comment thread src/crypto/SHA.cpp
@graydon
graydon added this pull request to the merge queue Jul 24, 2026
Merged via the queue into stellar:master with commit e15b643 Jul 24, 2026
54 checks passed
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.

3 participants