Skip to content

Commit

Permalink
ED19 and ECR1 benchmarks (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal authored Aug 14, 2023
1 parent 504b27c commit 6dbe2dd
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Description of the upcoming release here.

### Added

- [#1263](https://github.com/FuelLabs/fuel-core/pull/1263): Add gas benchmarks for `ED19` and `ECR1` instructions.
- [#1286](https://github.com/FuelLabs/fuel-core/pull/1286): Include readable names for test cases where missing.

### Changed
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ version = "0.0.0"
clap = { workspace = true, features = ["derive"] }
criterion = { version = "0.5", features = ["html_reports", "async", "async_tokio"] }
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 }
Expand Down
74 changes: 72 additions & 2 deletions benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use criterion::{
BenchmarkGroup,
Criterion,
};
use ed25519_dalek::Signer;
use fuel_core::service::{
config::Trigger,
Config,
Expand All @@ -20,6 +21,10 @@ use fuel_core_types::{
Instruction,
RegId,
},
fuel_crypto::{
secp256r1,
*,
},
fuel_tx::UniqueIdentifier,
fuel_types::AssetId,
};
Expand All @@ -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<WallTime>, script: Vec<Instruction>) {
fn run(
id: &str,
group: &mut BenchmarkGroup<WallTime>,
script: Vec<Instruction>,
script_data: Vec<u8>,
) {
group.bench_function(id, |b| {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand Down Expand Up @@ -60,7 +70,7 @@ fn run(id: &str, group: &mut BenchmarkGroup<WallTime>, script: Vec<Instruction>)
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)
Expand Down Expand Up @@ -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(
Expand All @@ -121,6 +132,7 @@ fn block_target_gas(c: &mut Criterion) {
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
vec![],
);

run(
Expand All @@ -132,6 +144,7 @@ fn block_target_gas(c: &mut Criterion) {
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
vec![],
);

run(
Expand All @@ -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
Expand Down
114 changes: 97 additions & 17 deletions benches/benches/set/crypto.rs
Original file line number Diff line number Diff line change
@@ -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::*,
Expand All @@ -15,48 +16,127 @@ 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"),
"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(0x21, 0x20, signature.as_ref().len().try_into().unwrap()),
op::addi(0x22, 0x21, message.as_ref().len().try_into().unwrap()),
op::addi(
0x21,
0x20,
eck1_signature.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(),
),
);

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("s256"),
"s256",
VmBench::new(op::s256(0x10, 0x00, 0x11))
&mut c.benchmark_group("ecr1"),
"ecr1",
VmBench::new(op::ecr1(RegId::HP, 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::movi(0x10, PublicKey::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(
ecr1_signature
.iter()
.chain(message.iter())
.copied()
.collect(),
),
);

run_group_ref(
&mut c.benchmark_group("k256"),
"k256",
VmBench::new(op::k256(0x10, 0x00, 0x11))
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(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(RegId::HP, RegId::ZERO, 0x11))
.with_prepare_script(vec![
op::movi(0x10, Bytes32::LEN.try_into().unwrap()),
op::aloc(0x10),
op::movi(0x11, 32),
])
.with_data(
eck1_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(),
),
])
.with_data(
ed19_keypair
.public
.to_bytes()
.iter()
.chain(ed19_signature.to_bytes().iter())
.chain(message.iter())
.copied()
.collect(),
),
);
}

0 comments on commit 6dbe2dd

Please sign in to comment.