-
Notifications
You must be signed in to change notification settings - Fork 180
Add noise bench #2037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GitGab19
merged 11 commits into
stratum-mining:main
from
Shourya742:2025-12-19-add-noise-bench
Jan 21, 2026
Merged
Add noise bench #2037
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f2c8be
add criterion as dev dependency
Shourya742 162fe73
add noise codec bench
Shourya742 732fa73
add noise handshake bench
Shourya742 047dd40
add some test for noise initiator
Shourya742 5900f09
add some test for noise responder
Shourya742 a76ef3a
pin rayon-core for noise-sv2 dev-dependency
Shourya742 578dc94
1. Add common module
Shourya742 810d533
move rng to constructor
Shourya742 fda904c
add comments on common benchmark methods in noise
Shourya742 34b6238
add benches section in readme.md
Shourya742 fc93767
add benches.md
Shourya742 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # Benchmarks | ||
|
|
||
| This document records the output of `cargo bench` for the `noise_sv2` crate. | ||
| The benchmarks measure the performance characteristics of the Noise protocol | ||
| implementation used by Stratum V2. | ||
|
|
||
| The results are intended to: | ||
| - Quantify the cost of the Noise handshake | ||
| - Measure post-handshake encryption and decryption overhead | ||
| - Serve as a reference point for future comparison and regression detection | ||
|
|
||
| All measurements are environment-dependent and should not be interpreted as | ||
| absolute performance guarantees. | ||
|
|
||
| ## Benchmark Output | ||
|
|
||
| The following section contains the **raw output** of `cargo bench` captured | ||
| during a single run. | ||
|
|
||
| ### Handshake Benchmarks | ||
|
|
||
| ```text | ||
| Benchmarking handshake/step_0_initiator | ||
| Benchmarking handshake/step_0_initiator: Warming up for 3.0000 s | ||
| Benchmarking handshake/step_0_initiator: Collecting 100 samples in estimated 5.3787 s (56k iterations) | ||
| Benchmarking handshake/step_0_initiator: Analyzing | ||
| handshake/step_0_initiator | ||
| time: [18.560 µs 18.737 µs 18.981 µs] | ||
| change: [-50.218% -49.023% -47.697%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 17 outliers among 100 measurements (17.00%) | ||
| 11 (11.00%) high mild | ||
| 6 (6.00%) high severe | ||
|
|
||
| Benchmarking handshake/step_1_responder | ||
| Benchmarking handshake/step_1_responder: Warming up for 3.0000 s | ||
| Benchmarking handshake/step_1_responder: Collecting 100 samples in estimated 7.4286 s (15k iterations) | ||
| Benchmarking handshake/step_1_responder: Analyzing | ||
| handshake/step_1_responder | ||
| time: [177.39 µs 178.07 µs 178.93 µs] | ||
| change: [-54.298% -53.548% -52.733%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 10 outliers among 100 measurements (10.00%) | ||
| 1 (1.00%) low mild | ||
| 2 (2.00%) high mild | ||
| 7 (7.00%) high severe | ||
|
|
||
| Benchmarking handshake/step_2_initiator | ||
| Benchmarking handshake/step_2_initiator: Warming up for 3.0000 s | ||
| Benchmarking handshake/step_2_initiator: Collecting 100 samples in estimated 6.1452 s (10k iterations) | ||
| Benchmarking handshake/step_2_initiator: Analyzing | ||
| handshake/step_2_initiator | ||
| time: [120.55 µs 120.91 µs 121.32 µs] | ||
| change: [-44.862% -43.680% -42.592%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 10 outliers among 100 measurements (10.00%) | ||
| 1 (1.00%) low mild | ||
| 6 (6.00%) high mild | ||
| 3 (3.00%) high severe | ||
|
|
||
| Benchmarking handshake/handshake | ||
| Benchmarking handshake/handshake: Warming up for 3.0000 s | ||
| Benchmarking handshake/handshake: Collecting 100 samples in estimated 6.1256 s (10k iterations) | ||
| Benchmarking handshake/handshake: Analyzing | ||
| handshake/handshake time: [316.58 µs 317.41 µs 318.34 µs] | ||
| change: [-43.167% -39.954% -36.598%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 7 outliers among 100 measurements (7.00%) | ||
| 1 (1.00%) low mild | ||
| 5 (5.00%) high mild | ||
| 1 (1.00%) high severe | ||
| ```` | ||
|
|
||
| --- | ||
|
|
||
| ### Transport Roundtrip Benchmarks | ||
|
|
||
| ```text | ||
| Gnuplot not found, using plotters backend | ||
|
|
||
| Benchmarking transport/roundtrip/64B | ||
| Benchmarking transport/roundtrip/64B: Warming up for 3.0000 s | ||
| Benchmarking transport/roundtrip/64B: Collecting 100 samples in estimated 5.0026 s (914k iterations) | ||
| Benchmarking transport/roundtrip/64B: Analyzing | ||
| transport/roundtrip/64B time: [5.1966 µs 5.2037 µs 5.2126 µs] | ||
| change: [-34.332% -31.654% -28.918%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 15 outliers among 100 measurements (15.00%) | ||
| 3 (3.00%) high mild | ||
| 12 (12.00%) high severe | ||
|
|
||
| Benchmarking transport/roundtrip/256B | ||
| Benchmarking transport/roundtrip/256B: Warming up for 3.0000 s | ||
| Benchmarking transport/roundtrip/256B: Collecting 100 samples in estimated 5.0128 s (858k iterations) | ||
| Benchmarking transport/roundtrip/256B: Analyzing | ||
| transport/roundtrip/256B | ||
| time: [5.4840 µs 5.5004 µs 5.5190 µs] | ||
| change: [-37.044% -34.403% -31.675%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 10 outliers among 100 measurements (10.00%) | ||
| 4 (4.00%) high mild | ||
| 6 (6.00%) high severe | ||
|
|
||
| Benchmarking transport/roundtrip/1024B | ||
| Benchmarking transport/roundtrip/1024B: Warming up for 3.0000 s | ||
| Benchmarking transport/roundtrip/1024B: Collecting 100 samples in estimated 5.0300 s (621k iterations) | ||
| Benchmarking transport/roundtrip/1024B: Analyzing | ||
| transport/roundtrip/1024B | ||
| time: [7.2587 µs 7.2827 µs 7.3112 µs] | ||
| change: [-32.654% -29.950% -27.189%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 18 outliers among 100 measurements (18.00%) | ||
| 7 (7.00%) high mild | ||
| 11 (11.00%) high severe | ||
|
|
||
| Benchmarking transport/roundtrip/4096B | ||
| Benchmarking transport/roundtrip/4096B: Warming up for 3.0000 s | ||
| Benchmarking transport/roundtrip/4096B: Collecting 100 samples in estimated 5.0042 s (268k iterations) | ||
| Benchmarking transport/roundtrip/4096B: Analyzing | ||
| transport/roundtrip/4096B | ||
| time: [16.213 µs 16.388 µs 16.600 µs] | ||
| change: [-27.110% -23.533% -19.897%] (p = 0.00 < 0.05) | ||
| Performance has improved. | ||
| Found 13 outliers among 100 measurements (13.00%) | ||
| 6 (6.00%) high mild | ||
| 7 (7.00%) high severe | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Interpretation Notes | ||
|
|
||
| * Handshake cost is dominated by responder-side processing and key exchange. | ||
| * Post-handshake transport costs scale predictably with payload size and remain | ||
| within single-digit microseconds for small messages. | ||
|
|
||
| --- | ||
|
|
||
| ## Reproducing | ||
|
|
||
| Run benchmarks locally with: | ||
|
|
||
| ```bash | ||
| cargo bench | ||
| ``` |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| use rand::{rngs::StdRng, SeedableRng}; | ||
| use secp256k1::{Keypair, Secp256k1}; | ||
|
|
||
| pub fn rng() -> StdRng { | ||
| // Fixed seed for deterministic benchmark runs. | ||
| StdRng::seed_from_u64(0xdead_beef) | ||
| } | ||
|
|
||
| // Generates BIP340 compliant keypair | ||
| pub fn generate_key_with_rng<R: rand::Rng + ?Sized>(rng: &mut R) -> Keypair { | ||
| let secp = Secp256k1::new(); | ||
| let (mut secret_key, public_key) = secp.generate_keypair(rng); | ||
| if public_key.x_only_public_key().1 == secp256k1::Parity::Odd { | ||
| secret_key = secret_key.negate(); | ||
| } | ||
| Keypair::from_secret_key(&secp, &secret_key) | ||
| } | ||
|
|
||
| #[allow(warnings)] | ||
| pub fn payload(len: usize) -> Vec<u8> { | ||
| vec![0u8; len] | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
| use noise_sv2::{Initiator, Responder}; | ||
|
|
||
| use crate::common::{generate_key_with_rng, rng}; | ||
| mod common; | ||
|
|
||
| fn bench_nx_handshake(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("handshake"); | ||
|
|
||
| group.bench_function("step_0_initiator", |b| { | ||
| b.iter_batched( | ||
| || Initiator::new(None), | ||
| |mut initiator| { | ||
| let _msg_0 = initiator.step_0().unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }); | ||
|
|
||
| group.bench_function("step_1_responder", |b| { | ||
| b.iter_batched( | ||
| || { | ||
| let mut rng = rng(); | ||
| let responder_key = generate_key_with_rng(&mut rng); | ||
| let mut initiator = Initiator::new(None); | ||
| let responder = Responder::new(responder_key, 60); | ||
|
|
||
| let msg_0 = initiator.step_0().unwrap(); | ||
| (responder, msg_0, rng) | ||
| }, | ||
| |(mut responder, msg_0, mut rng)| { | ||
| let _ = responder.step_1_with_now_rng(msg_0, 10, &mut rng).unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }); | ||
|
|
||
| group.bench_function("step_2_initiator", |b| { | ||
| b.iter_batched( | ||
| || { | ||
| let mut rng = rng(); | ||
| let responder_key = generate_key_with_rng(&mut rng); | ||
| let mut initiator = Initiator::new(None); | ||
| let mut responder = Responder::new(responder_key, 60); | ||
|
|
||
| let msg_0 = initiator.step_0().unwrap(); | ||
| let (msg_2, _) = responder.step_1_with_now_rng(msg_0, 10, &mut rng).unwrap(); | ||
|
|
||
| (initiator, msg_2) | ||
| }, | ||
| |(mut initiator, msg_2)| { | ||
| let _ = initiator.step_2_with_now(msg_2, 10).unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }); | ||
|
|
||
| group.bench_function("handshake", |b| { | ||
| b.iter_batched( | ||
| || { | ||
| let mut rng = rng(); | ||
| let responder_key = generate_key_with_rng(&mut rng); | ||
| let initiator = Initiator::new(None); | ||
| let responder = Responder::new(responder_key, 60); | ||
| (initiator, responder, rng) | ||
| }, | ||
| |(mut initiator, mut responder, mut rng)| { | ||
| let msg_0 = initiator.step_0().unwrap(); | ||
| let (msg_2, _) = responder.step_1_with_now_rng(msg_0, 10, &mut rng).unwrap(); | ||
| let _ = initiator.step_2_with_now(msg_2, 10).unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }); | ||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!(handshake, bench_nx_handshake); | ||
| criterion_main!(handshake); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
| use noise_sv2::{Initiator, Responder}; | ||
|
|
||
| use crate::common::{generate_key_with_rng, payload, rng}; | ||
| mod common; | ||
|
|
||
| fn bench_encryption_roundtrip(c: &mut Criterion) { | ||
| let responder_key = generate_key_with_rng(&mut rng()); | ||
| let mut initiator = Initiator::new(None); | ||
| let mut responder = Responder::new(responder_key, 10); | ||
|
|
||
| let msg_0 = initiator.step_0().unwrap(); | ||
| let (msg_2, mut responder_transport) = responder.step_1(msg_0).unwrap(); | ||
| let mut initiator_transport = initiator.step_2(msg_2).unwrap(); | ||
|
|
||
| for size in [64usize, 256, 1024, 4096] { | ||
| c.bench_function(&format!("transport/roundtrip/{size}B"), |b| { | ||
| b.iter_batched( | ||
| || payload(size), | ||
| |mut msg| { | ||
| initiator_transport.encrypt(&mut msg).unwrap(); | ||
| responder_transport.decrypt(&mut msg).unwrap(); | ||
| responder_transport.encrypt(&mut msg).unwrap(); | ||
| initiator_transport.decrypt(&mut msg).unwrap(); | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| criterion_group!(roundtrip, bench_encryption_roundtrip); | ||
| criterion_main!(roundtrip); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.