Skip to content
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

"flow" op code sanity benchmarks #1433

Merged
merged 20 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Description of the upcoming release here.

### Added

- [#1433](https://github.com/FuelLabs/fuel-core/pull/1433): Add "sanity" benchmarks for flow opcodes
- [#1430](https://github.com/FuelLabs/fuel-core/pull/1430): Add "sanity" benchmarks for crypto opcodes
- [#1419](https://github.com/FuelLabs/fuel-core/pull/1419): Add additional "sanity" benchmarks for arithmetic op code instructions.
- [#1411](https://github.com/FuelLabs/fuel-core/pull/1411): Added WASM and `no_std` compatibility
- [#1371](https://github.com/FuelLabs/fuel-core/pull/1371): Add new client function for querying the `MessageStatus` for a specific message (by `Nonce`)
Expand Down
90 changes: 10 additions & 80 deletions benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use block_target_gas_set::alu::run_alu;
use block_target_gas_set::{
alu::run_alu,
contract::run_contract,
crypto::run_crypto,
flow::run_flow,
};
use criterion::{
criterion_group,
criterion_main,
Expand Down Expand Up @@ -170,88 +175,13 @@ fn block_target_gas(c: &mut Criterion) {
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(),
ecr1_signature
.as_ref()
.iter()
.chain(message.as_ref())
.copied()
.collect(),
);

let ed19_keypair =
ed25519_dalek::Keypair::generate(&mut ed25519_dalek_old_rand::rngs::OsRng {});
let ed19_signature = ed19_keypair.sign(&*message);
run_alu(&mut group);

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(0x20, 0x21, 0x22),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
ed19_keypair
.public
.as_ref()
.iter()
.chain(ed19_signature.as_ref())
.chain(message.as_ref())
.copied()
.collect(),
);
run_contract(&mut group);

// The test is supper long because we don't use `DependentCost` for k256 opcode
// run(
// "Script with k256 opcode and infinite loop",
// &mut group,
// [
// op::movi(0x10, 1 << 18 - 1),
// op::aloc(0x10),
// op::k256(RegId::HP, RegId::ZERO, 0x10),
// op::jmpb(RegId::ZERO, 0),
// ]
// .to_vec(),
// );
run_crypto(&mut group);

run_alu(&mut group);
run_flow(&mut group);

group.finish();
}
Expand Down
20 changes: 20 additions & 0 deletions benches/benches/block_target_gas_set/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably just keep it since we're gonna add it soon anyway.

// use crate::utils::generate_linear_costs;

pub fn run_contract(_group: &mut BenchmarkGroup<WallTime>) {
// This breaks the benchmarking
// for i in generate_linear_costs() {
// let id = format!("flow/retd_contract opcode {:?}", i);
// run(
// &id,
// group,
// vec![
// op::movi(0x10, i),
// op::retd(RegId::ONE, 0x10),
// op::jmpb(RegId::ZERO, 0),
// ]
// .to_vec(),
// vec![],
// );
// }
}
171 changes: 171 additions & 0 deletions benches/benches/block_target_gas_set/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
use crate::{
utils::generate_linear_costs,
*,
};
use rand::{
rngs::StdRng,
SeedableRng,
};

// ECK1: Secp251k1 signature recovery
// ECR1: Secp256r1 signature recovery
// ED19: edDSA curve25519 verification
// K256: keccak-256
// S256: SHA-2-256
pub fn run_crypto(group: &mut BenchmarkGroup<WallTime>) {
let rng = &mut StdRng::seed_from_u64(2322u64);

let message = Message::new(b"foo");

let eck1_secret = SecretKey::random(rng);
let eck1_signature = Signature::sign(&eck1_secret, &message);
run(
"crypto/eck1 opcode valid",
group,
[
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData),
op::addi(
0x21,
0x20,
eck1_signature.as_ref().len().try_into().unwrap(),
),
op::movi(0x10, PublicKey::LEN.try_into().unwrap()),
op::aloc(0x10),
op::eck1(RegId::HP, 0x20, 0x21),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
eck1_signature
.iter()
.chain(message.iter())
.copied()
.collect(),
);

let wrong_message = Message::new(b"bar");

run(
"crypto/eck1 opcode invalid",
group,
[
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData),
op::addi(
0x21,
0x20,
eck1_signature.as_ref().len().try_into().unwrap(),
),
op::movi(0x10, PublicKey::LEN.try_into().unwrap()),
op::aloc(0x10),
op::eck1(RegId::HP, 0x20, 0x21),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
eck1_signature
.iter()
.chain(wrong_message.iter())
.copied()
.collect(),
);

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(
"crypto/ecr1 opcode",
group,
[
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_(0x11, RegId::HP),
op::ecr1(0x11, 0x20, 0x21),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
ecr1_signature
.as_ref()
.iter()
.chain(message.as_ref())
.copied()
.collect(),
);

let message = fuel_core_types::fuel_crypto::Message::new(b"foo");
let ed19_keypair =
ed25519_dalek::Keypair::generate(&mut ed25519_dalek_old_rand::rngs::OsRng {});
let ed19_signature = ed19_keypair.sign(&*message);

run(
"crypto/ed19 opcode",
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(0x20, 0x21, 0x22),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
ed19_keypair
.public
.as_ref()
.iter()
.chain(ed19_signature.as_ref())
.chain(message.as_ref())
.copied()
.collect(),
);

for i in generate_linear_costs() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you didn't create generate_linear_costs() but I'm curious to know why it's being used here. It looks like it generates ns that increase exponentially and then decrease exponentially, and for each n, we run sha256 on n bytes. Is there a reason that we use this specific generator function over something like a monotonic or linear generator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand the name--I've been trying to think of a new name. But it's used in a number of or benchmarks for dependent costs. The actual values used aren't particularly important as long as they show a good range between 0 -> a lot.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming arb_dependent_cost_values to communicate that they aren't particularly important values and what they are for.

let id = format!("crypto/s256 opcode {:?}", i);
run(
&id,
group,
[
op::movi(0x11, 32),
op::aloc(0x11),
op::movi(0x10, i),
op::s256(RegId::HP, RegId::ZERO, 0x10),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
vec![],
)
}

for i in generate_linear_costs() {
let id = format!("crypto/k256 opcode {:?}", i);
run(
&id,
group,
[
op::movi(0x11, 32),
op::aloc(0x11),
op::movi(0x10, i),
op::k256(RegId::HP, RegId::ZERO, 0x10),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
vec![],
)
}
}
72 changes: 72 additions & 0 deletions benches/benches/block_target_gas_set/flow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::*;

// JMP: Jump
// JI: Jump immediate
// JNE: Jump if not equal
// JNEI: Jump if not equal immediate
// JNZI: Jump if not zero immediate
// JMPB: Jump relative backwards
// JMPF: Jump relative forwards
// JNZB: Jump if not zero relative backwards
// JNZF: Jump if not zero relative forwards
// JNEB: Jump if not equal relative backwards
// JNEF: Jump if not equal relative forwards
// RET: Return from context
pub fn run_flow(group: &mut BenchmarkGroup<WallTime>) {
run(
"flow/jmp opcode",
group,
vec![op::movi(0x10, 0), op::jmp(0x10)],
vec![],
);

run(
"flow/ji opcode",
group,
vec![op::ji(0), op::jmpb(RegId::ZERO, 0)],
vec![],
);

run(
"flow/jne opcode",
group,
vec![
op::movi(0x10, 0),
op::jne(RegId::ZERO, RegId::ONE, 0x10),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);

run(
"flow/jnei opcode",
group,
vec![
op::jnei(RegId::ZERO, RegId::ONE, 0),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);

run(
"flow/jnzi opcode",
group,
vec![op::jnzi(RegId::ONE, 0), op::jmpb(RegId::ZERO, 0)],
vec![],
);

// Don't know how to test "returning" op codes
// run(
// "flow/ret_script opcode",
// group,
// vec![op::ret(RegId::ONE), op::jmpb(RegId::ZERO, 0)].to_vec(),
// vec![],
// );
//
// run(
// "flow/ret_contract opcode",
// group,
// vec![op::ret(RegId::ONE), op::jmpb(RegId::ZERO, 0)].to_vec(),
// vec![],
// );
Comment on lines +174 to +187
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ret and retd look tricky=D But its price is essential only in the scenario if someone calls another contract and returns from it(and repeats this move many times). Because otherwise, these opcodes return to finish the current execution.

You can skip it for now, but create an issue to write a benchmark later that: the scenario either calls another contract and returns with a lot of data or runs a lot of transactions with this opcode.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it as a subtask to the parent issue, like we did with gtf.

}
6 changes: 6 additions & 0 deletions benches/benches/block_target_gas_set/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
pub mod alu;

pub mod crypto;

pub mod flow;

pub mod contract;
Loading