Skip to content

Commit

Permalink
[secp256r1] Add bench logic (#3825)
Browse files Browse the repository at this point in the history
* add secp256r1 bench

* cargo fmt

* apply feature `openssl-vendored` for bench scripts
  • Loading branch information
samkim-crypto authored Dec 3, 2024
1 parent 6a867ad commit 9c899a7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
test:
- {
name: "solana-sdk",
commands: ["cargo +$rust_nightly bench -p solana-sdk"],
commands: ["cargo +$rust_nightly bench -p solana-sdk --features openssl-vendored"],
}
- {
name: "solana-runtime",
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ci/bench/part2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ source "$here"/common.sh

# Run sdk benches
_ cargo +"$rust_nightly" bench --manifest-path sdk/Cargo.toml ${V:+--verbose} \
-- -Z unstable-options --format=json | tee -a "$BENCH_FILE"
--features openssl-vendored -- -Z unstable-options --format=json | tee -a "$BENCH_FILE"

# Run runtime benches
_ cargo +"$rust_nightly" bench --manifest-path runtime/Cargo.toml ${V:+--verbose} \
Expand Down
2 changes: 1 addition & 1 deletion ci/test-bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ rm -f "$BENCH_FILE"

# Run sdk benches
_ $cargoNightly bench --manifest-path sdk/Cargo.toml ${V:+--verbose} \
-- -Z unstable-options --format=json | tee -a "$BENCH_FILE"
--features openssl-vendored -- -Z unstable-options --format=json | tee -a "$BENCH_FILE"

# Run runtime benches
_ $cargoNightly bench --manifest-path runtime/Cargo.toml ${V:+--verbose} \
Expand Down
1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ anyhow = { workspace = true }
assert_matches = { workspace = true }
curve25519-dalek = { workspace = true }
hex = { workspace = true }
openssl = { workspace = true }
solana-logger = { workspace = true }
solana-program = { workspace = true, features = ["dev-context-only-utils"] }
solana-sdk = { path = ".", features = ["dev-context-only-utils"] }
Expand Down
99 changes: 99 additions & 0 deletions sdk/benches/secp256r1_instructions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#![feature(test)]

extern crate test;
use {
openssl::{
ec::{EcGroup, EcKey},
nid::Nid,
},
rand0_7::{thread_rng, Rng},
solana_feature_set::FeatureSet,
solana_sdk::{
hash::Hash,
signature::{Keypair, Signer},
transaction::Transaction,
},
solana_secp256r1_program::new_secp256r1_instruction,
test::Bencher,
};

// 5k transactions should be enough for benching loop
const TX_COUNT: u16 = 5120;

// prepare a bunch of unique txs
fn create_test_transactions(message_length: u16) -> Vec<Transaction> {
(0..TX_COUNT)
.map(|_| {
let mut rng = thread_rng();
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let secp_privkey = EcKey::generate(&group).unwrap();
let message: Vec<u8> = (0..message_length).map(|_| rng.gen_range(0, 255)).collect();
let secp_instruction = new_secp256r1_instruction(&message, secp_privkey).unwrap();
let mint_keypair = Keypair::new();

Transaction::new_signed_with_payer(
&[secp_instruction.clone()],
Some(&mint_keypair.pubkey()),
&[&mint_keypair],
Hash::default(),
)
})
.collect()
}

#[bench]
fn bench_secp256r1_len_032(b: &mut Bencher) {
let feature_set = FeatureSet::all_enabled();
let txs = create_test_transactions(32);
let mut tx_iter = txs.iter().cycle();
b.iter(|| {
tx_iter
.next()
.unwrap()
.verify_precompiles(&feature_set)
.unwrap();
});
}

#[bench]
fn bench_secp256r1_len_256(b: &mut Bencher) {
let feature_set = FeatureSet::all_enabled();
let txs = create_test_transactions(256);
let mut tx_iter = txs.iter().cycle();
b.iter(|| {
tx_iter
.next()
.unwrap()
.verify_precompiles(&feature_set)
.unwrap();
});
}

#[bench]
fn bench_secp256r1_len_32k(b: &mut Bencher) {
let feature_set = FeatureSet::all_enabled();
let txs = create_test_transactions(32 * 1024);
let mut tx_iter = txs.iter().cycle();
b.iter(|| {
tx_iter
.next()
.unwrap()
.verify_precompiles(&feature_set)
.unwrap();
});
}

#[bench]
fn bench_secp256r1_len_max(b: &mut Bencher) {
let required_extra_space = 113_u16; // len for pubkey, sig, and offsets
let feature_set = FeatureSet::all_enabled();
let txs = create_test_transactions(u16::MAX - required_extra_space);
let mut tx_iter = txs.iter().cycle();
b.iter(|| {
tx_iter
.next()
.unwrap()
.verify_precompiles(&feature_set)
.unwrap();
});
}

0 comments on commit 9c899a7

Please sign in to comment.