From ddc39ffbdddf48281cff341706578d1c4a30721e Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Thu, 27 Jul 2023 15:37:37 +0300 Subject: [PATCH 1/7] Add ed19 and ecr1 benchmarks --- Cargo.lock | 3 + benches/Cargo.toml | 3 + benches/benches/set/crypto.rs | 112 ++++++++++++++++++++++++++++++---- 3 files changed, 106 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86c82338b0f..7751de71b52 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2661,10 +2661,13 @@ dependencies = [ "clap 4.3.15", "criterion", "ctrlc", + "ed25519-dalek", "ethnum", "fuel-core", "fuel-core-storage", "fuel-core-types", + "p256 0.13.2", + "rand 0.7.3", "rand 0.8.5", "serde", "serde_json", diff --git a/benches/Cargo.toml b/benches/Cargo.toml index f783ee896a9..9d59cf1048c 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -9,10 +9,13 @@ version = "0.0.0" clap = { workspace = true, features = ["derive"] } criterion = { version = "0.4", features = ["html_reports"] } ctrlc = "3.2.3" +ed25519-dalek = "1.0" # TODO: upgrade to 2.0 when it's released, and remove rand below +ed25519-dalek_old_rand = { package = "rand", version = "0.7.3" } ethnum = "1.3" fuel-core = { path = "../crates/fuel-core", default-features = false, features = ["metrics", "rocksdb-production"] } fuel-core-storage = { path = "./../crates/storage" } fuel-core-types = { path = "./../crates/types", features = ["test-helpers"] } +p256 = { version = "0.13", default-features = false, features = ["digest", "ecdsa"] } rand = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } diff --git a/benches/benches/set/crypto.rs b/benches/benches/set/crypto.rs index 05fa7612bbb..4679480bd6c 100644 --- a/benches/benches/set/crypto.rs +++ b/benches/benches/set/crypto.rs @@ -1,6 +1,7 @@ use super::run_group_ref; use criterion::Criterion; +use ed25519_dalek::Signer; use fuel_core_benches::*; use fuel_core_types::{ fuel_asm::*, @@ -15,9 +16,10 @@ use rand::{ pub fn run(c: &mut Criterion) { let rng = &mut StdRng::seed_from_u64(2322u64); - let secret = SecretKey::random(rng); let message = Message::new(b"foo"); - let signature = Signature::sign(&secret, &message); + + let eck1_secret = SecretKey::random(rng); + let eck1_signature = Signature::sign(&eck1_secret, &message); run_group_ref( &mut c.benchmark_group("eck1"), @@ -25,38 +27,124 @@ pub fn run(c: &mut Criterion) { VmBench::new(op::eck1(0x11, 0x20, 0x21)) .with_prepare_script(vec![ op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), - op::addi(0x21, 0x20, signature.as_ref().len().try_into().unwrap()), + op::addi( + 0x21, + 0x20, + eck1_signature.as_ref().len().try_into().unwrap(), + ), op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), op::movi(0x10, PublicKey::LEN.try_into().unwrap()), op::aloc(0x10), op::move_(0x11, RegId::HP), ]) - .with_data(signature.iter().chain(message.iter()).copied().collect()), + .with_data( + eck1_signature + .iter() + .chain(message.iter()) + .copied() + .collect(), + ), ); run_group_ref( - &mut c.benchmark_group("s256"), - "s256", - VmBench::new(op::s256(0x10, 0x00, 0x11)) + &mut c.benchmark_group("k256"), + "k256", + VmBench::new(op::k256(0x10, 0x00, 0x11)) .with_prepare_script(vec![ op::movi(0x10, Bytes32::LEN.try_into().unwrap()), op::aloc(0x10), op::move_(0x10, RegId::HP), op::movi(0x11, 32), ]) - .with_data(signature.iter().chain(message.iter()).copied().collect()), + .with_data( + eck1_signature + .iter() + .chain(message.iter()) + .copied() + .collect(), + ), ); run_group_ref( - &mut c.benchmark_group("k256"), - "k256", - VmBench::new(op::k256(0x10, 0x00, 0x11)) + &mut c.benchmark_group("s256"), + "s256", + VmBench::new(op::s256(0x10, 0x00, 0x11)) .with_prepare_script(vec![ op::movi(0x10, Bytes32::LEN.try_into().unwrap()), op::aloc(0x10), op::move_(0x10, RegId::HP), op::movi(0x11, 32), ]) - .with_data(signature.iter().chain(message.iter()).copied().collect()), + .with_data( + eck1_signature + .iter() + .chain(message.iter()) + .copied() + .collect(), + ), + ); + + let ecr1_secret = p256::ecdsa::SigningKey::random(rng); + let ecr1_signature = secp256r1::sign_prehashed(&ecr1_secret, &message) + .expect("Failed to sign with secp256r1"); + + run_group_ref( + &mut c.benchmark_group("ecr1"), + "ecr1", + VmBench::new(op::ecr1(0x11, 0x20, 0x21)) + .with_prepare_script(vec![ + op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), + op::addi( + 0x21, + 0x20, + ecr1_signature.as_ref().len().try_into().unwrap(), + ), + op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), + op::movi(0x10, PublicKey::LEN.try_into().unwrap()), + op::aloc(0x10), + op::move_(0x11, RegId::HP), + ]) + .with_data( + ecr1_signature + .iter() + .chain(message.iter()) + .copied() + .collect(), + ), + ); + + let ed19_keypair = + ed25519_dalek::Keypair::generate(&mut ed25519_dalek_old_rand::rngs::OsRng {}); + let ed19_signature = ed19_keypair.sign(&*message); + + run_group_ref( + &mut c.benchmark_group("ed19"), + "ed19", + VmBench::new(op::ed19(0x20, 0x21, 0x22)) + .with_prepare_script(vec![ + op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), + op::addi( + 0x21, + 0x20, + ed19_keypair.public.as_ref().len().try_into().unwrap(), + ), + op::addi( + 0x22, + 0x21, + ed19_signature.as_ref().len().try_into().unwrap(), + ), + op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), + op::movi(0x10, PublicKey::LEN.try_into().unwrap()), + op::aloc(0x10), + op::move_(0x11, RegId::HP), + ]) + .with_data( + ed19_signature + .to_bytes() + .iter() + .chain(message.iter()) + .copied() + .collect(), + ), ); } From 2506a03843c6fbcff32553a4ca225270d98720e6 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 14 Aug 2023 15:43:08 +0300 Subject: [PATCH 2/7] Add block_target_gas benchmarks for ed19 and ecr1 --- benches/benches/block_target_gas.rs | 74 ++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/benches/benches/block_target_gas.rs b/benches/benches/block_target_gas.rs index acea6c06e44..a186af98c58 100644 --- a/benches/benches/block_target_gas.rs +++ b/benches/benches/block_target_gas.rs @@ -5,6 +5,7 @@ use criterion::{ BenchmarkGroup, Criterion, }; +use ed25519_dalek::Signer; use fuel_core::service::{ config::Trigger, Config, @@ -20,6 +21,10 @@ use fuel_core_types::{ Instruction, RegId, }, + fuel_crypto::{ + secp256r1, + *, + }, fuel_tx::UniqueIdentifier, fuel_types::AssetId, }; @@ -28,7 +33,12 @@ use fuel_core_types::{ #[global_allocator] static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; -fn run(id: &str, group: &mut BenchmarkGroup, script: Vec) { +fn run( + id: &str, + group: &mut BenchmarkGroup, + script: Vec, + script_data: Vec, +) { group.bench_function(id, |b| { let rt = tokio::runtime::Builder::new_current_thread() .enable_all() @@ -60,7 +70,7 @@ fn run(id: &str, group: &mut BenchmarkGroup, script: Vec) let tx = fuel_core_types::fuel_tx::TransactionBuilder::script( // Infinite loop script.clone().into_iter().collect(), - vec![], + script_data.clone(), ) .gas_limit(TARGET_BLOCK_GAS_LIMIT - BASE) .gas_price(1) @@ -110,6 +120,7 @@ fn block_target_gas(c: &mut Criterion) { "Script with noop opcode and infinite loop", &mut group, [op::noop(), op::jmpb(RegId::ZERO, 0)].to_vec(), + vec![], ); run( @@ -121,6 +132,7 @@ fn block_target_gas(c: &mut Criterion) { op::jmpb(RegId::ZERO, 0), ] .to_vec(), + vec![], ); run( @@ -132,6 +144,7 @@ fn block_target_gas(c: &mut Criterion) { op::jmpb(RegId::ZERO, 0), ] .to_vec(), + vec![], ); run( @@ -142,6 +155,63 @@ fn block_target_gas(c: &mut Criterion) { op::jmpb(RegId::ZERO, 0), ] .to_vec(), + vec![], + ); + + let message = fuel_core_types::fuel_crypto::Message::new(b"foo"); + let ecr1_secret = p256::ecdsa::SigningKey::random(&mut rand::thread_rng()); + let ecr1_signature = secp256r1::sign_prehashed(&ecr1_secret, &message) + .expect("Failed to sign with secp256r1"); + + run( + "Script with ecr1 opcode and infinite loop", + &mut group, + [ + op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), + op::addi( + 0x21, + 0x20, + ecr1_signature.as_ref().len().try_into().unwrap(), + ), + op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), + op::movi(0x10, PublicKey::LEN.try_into().unwrap()), + op::aloc(0x10), + op::move_(0x11, RegId::HP), + op::ecr1(0x11, 0x20, 0x21), + op::jmpb(RegId::ZERO, 0), + ] + .to_vec(), + vec![], + ); + + let ed19_keypair = + ed25519_dalek::Keypair::generate(&mut ed25519_dalek_old_rand::rngs::OsRng {}); + let ed19_signature = ed19_keypair.sign(&*message); + + run( + "Script with ed19 opcode and infinite loop", + &mut group, + [ + op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), + op::addi( + 0x21, + 0x20, + ed19_keypair.public.as_ref().len().try_into().unwrap(), + ), + op::addi( + 0x22, + 0x21, + ed19_signature.as_ref().len().try_into().unwrap(), + ), + op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), + op::movi(0x10, ed25519_dalek::PUBLIC_KEY_LENGTH.try_into().unwrap()), + op::aloc(0x10), + op::move_(0x11, RegId::HP), + op::ed19(0x10, 0x11, 0x12), + op::jmpb(RegId::ZERO, 0), + ] + .to_vec(), + vec![], ); // The test is supper long because we don't use `DependentCost` for k256 opcode From fe1f58f72afcfa0bdbf42a0b371f9a0d4ad14423 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 14 Aug 2023 15:45:26 +0300 Subject: [PATCH 3/7] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c5db62779e..75f8082d544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Description of the upcoming release here. - Something new here 1 - Something new here 2 +- [#1263](https://github.com/FuelLabs/fuel-core/pull/1263): Add gas benchmarks for `ED19` and `ECR1` instructions ### Changed From d445c6f4a094a64f7e683b079bb5f081c27d7eef Mon Sep 17 00:00:00 2001 From: Green Baneling Date: Mon, 14 Aug 2023 13:46:43 +0100 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75f8082d544..d56767a44d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,7 @@ Description of the upcoming release here. ### Added -- Something new here 1 -- Something new here 2 -- [#1263](https://github.com/FuelLabs/fuel-core/pull/1263): Add gas benchmarks for `ED19` and `ECR1` instructions +- [#1263](https://github.com/FuelLabs/fuel-core/pull/1263): Add gas benchmarks for `ED19` and `ECR1` instructions. ### Changed From 07f6d107aa83391ee1f5dbf44484a356bd49498d Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 14 Aug 2023 16:17:44 +0300 Subject: [PATCH 5/7] Cleanup --- benches/benches/set/crypto.rs | 62 +++++++++++++++-------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/benches/benches/set/crypto.rs b/benches/benches/set/crypto.rs index 4679480bd6c..2a8d6826d02 100644 --- a/benches/benches/set/crypto.rs +++ b/benches/benches/set/crypto.rs @@ -24,7 +24,7 @@ pub fn run(c: &mut Criterion) { run_group_ref( &mut c.benchmark_group("eck1"), "eck1", - VmBench::new(op::eck1(0x11, 0x20, 0x21)) + VmBench::new(op::eck1(RegId::HP, 0x20, 0x21)) .with_prepare_script(vec![ op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), op::addi( @@ -32,10 +32,8 @@ pub fn run(c: &mut Criterion) { 0x20, eck1_signature.as_ref().len().try_into().unwrap(), ), - op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), op::movi(0x10, PublicKey::LEN.try_into().unwrap()), op::aloc(0x10), - op::move_(0x11, RegId::HP), ]) .with_data( eck1_signature @@ -46,18 +44,28 @@ pub fn run(c: &mut Criterion) { ), ); + let ecr1_secret = p256::ecdsa::SigningKey::random(rng); + let ecr1_signature = secp256r1::sign_prehashed(&ecr1_secret, &message) + .expect("Failed to sign with secp256r1"); + run_group_ref( - &mut c.benchmark_group("k256"), - "k256", - VmBench::new(op::k256(0x10, 0x00, 0x11)) + &mut c.benchmark_group("ecr1"), + "ecr1", + VmBench::new(op::ecr1(0x11, 0x20, 0x21)) .with_prepare_script(vec![ - op::movi(0x10, Bytes32::LEN.try_into().unwrap()), + op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), + op::addi( + 0x21, + 0x20, + ecr1_signature.as_ref().len().try_into().unwrap(), + ), + op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), + op::movi(0x10, PublicKey::LEN.try_into().unwrap()), op::aloc(0x10), - op::move_(0x10, RegId::HP), - op::movi(0x11, 32), + op::move_(0x11, RegId::HP), ]) .with_data( - eck1_signature + ecr1_signature .iter() .chain(message.iter()) .copied() @@ -66,13 +74,12 @@ pub fn run(c: &mut Criterion) { ); run_group_ref( - &mut c.benchmark_group("s256"), - "s256", - VmBench::new(op::s256(0x10, 0x00, 0x11)) + &mut c.benchmark_group("k256"), + "k256", + VmBench::new(op::k256(RegId::HP, RegId::ZERO, 0x11)) .with_prepare_script(vec![ op::movi(0x10, Bytes32::LEN.try_into().unwrap()), op::aloc(0x10), - op::move_(0x10, RegId::HP), op::movi(0x11, 32), ]) .with_data( @@ -84,28 +91,17 @@ pub fn run(c: &mut Criterion) { ), ); - let ecr1_secret = p256::ecdsa::SigningKey::random(rng); - let ecr1_signature = secp256r1::sign_prehashed(&ecr1_secret, &message) - .expect("Failed to sign with secp256r1"); - run_group_ref( - &mut c.benchmark_group("ecr1"), - "ecr1", - VmBench::new(op::ecr1(0x11, 0x20, 0x21)) + &mut c.benchmark_group("s256"), + "s256", + VmBench::new(op::s256(RegId::HP, RegId::ZERO, 0x11)) .with_prepare_script(vec![ - op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), - op::addi( - 0x21, - 0x20, - ecr1_signature.as_ref().len().try_into().unwrap(), - ), - op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), - op::movi(0x10, PublicKey::LEN.try_into().unwrap()), + op::movi(0x10, Bytes32::LEN.try_into().unwrap()), op::aloc(0x10), - op::move_(0x11, RegId::HP), + op::movi(0x11, 32), ]) .with_data( - ecr1_signature + eck1_signature .iter() .chain(message.iter()) .copied() @@ -133,10 +129,6 @@ pub fn run(c: &mut Criterion) { 0x21, ed19_signature.as_ref().len().try_into().unwrap(), ), - op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), - op::movi(0x10, PublicKey::LEN.try_into().unwrap()), - op::aloc(0x10), - op::move_(0x11, RegId::HP), ]) .with_data( ed19_signature From d3336c4c2eae17416a36afe5c22449925de0e53d Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 14 Aug 2023 16:35:03 +0300 Subject: [PATCH 6/7] Add missing public key to ed19 gas benches --- benches/benches/set/crypto.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benches/benches/set/crypto.rs b/benches/benches/set/crypto.rs index 2a8d6826d02..fcdf972dbfa 100644 --- a/benches/benches/set/crypto.rs +++ b/benches/benches/set/crypto.rs @@ -131,9 +131,11 @@ pub fn run(c: &mut Criterion) { ), ]) .with_data( - ed19_signature + ed19_keypair + .public .to_bytes() .iter() + .chain(ed19_signature.to_bytes().iter()) .chain(message.iter()) .copied() .collect(), From 1937849f754c993272231963ad7d1ccb032cc792 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 14 Aug 2023 15:45:47 +0100 Subject: [PATCH 7/7] Update benches/benches/set/crypto.rs Co-authored-by: Green Baneling --- benches/benches/set/crypto.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/benches/benches/set/crypto.rs b/benches/benches/set/crypto.rs index fcdf972dbfa..49cc6bdc2bb 100644 --- a/benches/benches/set/crypto.rs +++ b/benches/benches/set/crypto.rs @@ -51,7 +51,7 @@ pub fn run(c: &mut Criterion) { run_group_ref( &mut c.benchmark_group("ecr1"), "ecr1", - VmBench::new(op::ecr1(0x11, 0x20, 0x21)) + VmBench::new(op::ecr1(RegId::HP, 0x20, 0x21)) .with_prepare_script(vec![ op::gtf_args(0x20, 0x00, GTFArgs::ScriptData), op::addi( @@ -59,10 +59,8 @@ pub fn run(c: &mut Criterion) { 0x20, ecr1_signature.as_ref().len().try_into().unwrap(), ), - op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()), op::movi(0x10, PublicKey::LEN.try_into().unwrap()), op::aloc(0x10), - op::move_(0x11, RegId::HP), ]) .with_data( ecr1_signature