Skip to content

perf(noise_sv2): use rust-bitcoin's chacha20-poly1305 for the AEAD - #2506

Open
rustaceanrob wants to merge 1 commit into
stratum-mining:mainfrom
rustaceanrob:nov-15-chacha
Open

rustaceanrob wants to merge 1 commit into
stratum-mining:mainfrom
rustaceanrob:nov-15-chacha

Conversation

@rustaceanrob

Copy link
Copy Markdown

Our chacha20-poly1305 implementation in rust-bitcoin has recently added multi-block SIMD support for both ChaCha20 and Poly1305. Performance is now better for all the roundtrip benchmarks due to a setup cost somewhere within RustCrypto's implementation (I am not as familiar with their implementation and cannot allude to why).

This adds a wrapper class for ChaCha20Poly1305 that uses the existing zeroize crate to clear memory on drop. Some additional message parsing is done when decrypting to extract the tag. Otherwise it is a simple replacement. A sanity check test that the wrapper is working is added as well.

The aead crate is preserved to avoid a breaking change, but this can also be removed in a 3.0.0 with a simple new type. Considering Stratum already depends on rust-bitcoin, this allows for a consolidation of organizations Stratum depends on for maintenance.

The tag check is done in constant time, same behavior as RustCrypto.

Ryzen 7 7700, median of 3 runs:

Benchmark RustCrypto rust-bitcoin Change
transport/roundtrip/64B 4.70 us 781 ns -83%
transport/roundtrip/256B 4.83 us 1.17 us -76%
transport/roundtrip/1024B 6.12 us 2.07 us -66%
transport/roundtrip/4096B 11.89 us 7.03 us -41%
noise/encode_only 1.79 us 407 ns -77%
encoder/noise/transport 1.80 us 396 ns -78%
handshake/handshake 215.9 us 211.9 us -2%

Constant time equality check: https://github.com/rust-bitcoin/rust-bitcoin/blob/master/chacha20_poly1305/src/lib.rs#L147

Our `chacha20-poly1305` implementation in `rust-bitcoin` has recently
added multi-block SIMD support for both ChaCha20 and Poly1305.
Performance is now better for all the roundtrip benchmarks due to a
setup cost somewhere within RustCrypto's implementation (I am not as
familiar with their implementation and cannot allude to why).

This adds a wrapper class for `ChaCha20Poly1305` that uses the existing
zeroize crate to clear memory on drop. Some additional message parsing
is done when decrypting to extract the tag. Otherwise it is a simple
replacement. A sanity check test that the wrapper is working is added as
well.

The `aead` crate is preserved to avoid a breaking change, but this can
also be removed in a `3.0.0` with a simple new type. Considering Stratum
already depends on `rust-bitcoin`, this allows for a consolidation of
organizations Stratum depends on for maintenance.

The tag check is done in constant time, same behavior as RustCrypto.

Ryzen 7 7700, median of 3 runs:

| Benchmark                         | RustCrypto | rust-bitcoin | Change |
|-----------------------------------|------------|--------------|--------|
| transport/roundtrip/64B           |    4.70 us |       781 ns |   -83% |
| transport/roundtrip/256B          |    4.83 us |      1.17 us |   -76% |
| transport/roundtrip/1024B         |    6.12 us |      2.07 us |   -66% |
| transport/roundtrip/4096B         |   11.89 us |      7.03 us |   -41% |
| noise/encode_only                 |    1.79 us |       407 ns |   -77% |
| encoder/noise/transport           |    1.80 us |       396 ns |   -78% |
| handshake/handshake               |   215.9 us |     211.9 us |    -2% |

Constant time equality check: https://github.com/rust-bitcoin/rust-bitcoin/blob/master/chacha20_poly1305/src/lib.rs#L147
@GitGab19

Copy link
Copy Markdown
Member

cc @bit-aloo

@bit-aloo

Copy link
Copy Markdown
Member

Damn!! rust-bitcoin's implementation is faster than rustcrypto 😆 I will have a look at this PR soon!

@bit-aloo
bit-aloo self-requested a review November 16, 2026 17:29
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