From 10d78bd3f72453c23070a6c992de9f602033849e Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Thu, 28 Nov 2024 11:46:41 +0900 Subject: [PATCH 1/3] add secp256r1 bench --- Cargo.lock | 1 + sdk/Cargo.toml | 1 + sdk/benches/secp256r1_instructions.rs | 96 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 sdk/benches/secp256r1_instructions.rs diff --git a/Cargo.lock b/Cargo.lock index 7bf671a49745f4..3be706ecac309d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8461,6 +8461,7 @@ dependencies = [ "num-derive", "num-traits", "num_enum", + "openssl", "qualifier_attr", "rand 0.7.3", "rand 0.8.5", diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 63c3632d26e1e5..91a828853f51fe 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -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"] } diff --git a/sdk/benches/secp256r1_instructions.rs b/sdk/benches/secp256r1_instructions.rs new file mode 100644 index 00000000000000..d49c2c1e7f645f --- /dev/null +++ b/sdk/benches/secp256r1_instructions.rs @@ -0,0 +1,96 @@ +#![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 { + (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 = (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(); + }); +} From e9b3694974825a525cc0947c4b6e0b97628b7b52 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Thu, 28 Nov 2024 13:03:05 +0900 Subject: [PATCH 2/3] cargo fmt --- sdk/benches/secp256r1_instructions.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/benches/secp256r1_instructions.rs b/sdk/benches/secp256r1_instructions.rs index d49c2c1e7f645f..9af312688504ef 100644 --- a/sdk/benches/secp256r1_instructions.rs +++ b/sdk/benches/secp256r1_instructions.rs @@ -2,7 +2,10 @@ extern crate test; use { - openssl::{ec::{EcGroup, EcKey}, nid::Nid}, + openssl::{ + ec::{EcGroup, EcKey}, + nid::Nid, + }, rand0_7::{thread_rng, Rng}, solana_feature_set::FeatureSet, solana_sdk::{ From 2577ed62fc2124b2b39e34f086531a0a7830c052 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 3 Dec 2024 07:55:36 +0900 Subject: [PATCH 3/3] apply feature `openssl-vendored` for bench scripts --- .github/workflows/benchmark.yml | 2 +- ci/bench/part2.sh | 2 +- ci/test-bench.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index d78f22288bd632..e73fafad73289a 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -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", diff --git a/ci/bench/part2.sh b/ci/bench/part2.sh index 76f1dfb50abffc..5a8f0550cb72a7 100755 --- a/ci/bench/part2.sh +++ b/ci/bench/part2.sh @@ -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} \ diff --git a/ci/test-bench.sh b/ci/test-bench.sh index c145512d7b1cc6..a6bb3f62847c26 100755 --- a/ci/test-bench.sh +++ b/ci/test-bench.sh @@ -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} \