-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Crypto op code sanity checks (#1430)
#1386 Results: ``` block target estimation/crypto/eck1 opcode time: [4.8418 ms 4.8650 ms 4.8882 ms] block target estimation/crypto/ecr1 opcode time: [18.634 ms 18.791 ms 18.992 ms] block target estimation/crypto/ed19 opcode time: [2.6451 ms 2.6698 ms 2.6963 ms] ``` s256: ``` block target estimation/crypto/s256 opcode 1 time: [3.1244 ms 3.1409 ms 3.1575 ms] Found 6 outliers among 100 measurements (6.00%) 3 (3.00%) low mild 3 (3.00%) high mild block target estimation/crypto/s256 opcode 10 time: [3.1082 ms 3.1299 ms 3.1515 ms] Found 2 outliers among 100 measurements (2.00%) 1 (1.00%) low mild 1 (1.00%) high severe block target estimation/crypto/s256 opcode 100 time: [4.1553 ms 4.1781 ms 4.2026 ms] block target estimation/crypto/s256 opcode 1000 time: [8.3133 ms 8.3554 ms 8.3990 ms] block target estimation/crypto/s256 opcode 10000 time: [10.562 ms 10.611 ms 10.661 ms] block target estimation/crypto/s256 opcode 19753 time: [10.794 ms 10.847 ms 10.902 ms] block target estimation/crypto/s256 opcode 29629 time: [10.694 ms 10.773 ms 10.853 ms] block target estimation/crypto/s256 opcode 44444 time: [10.724 ms 10.795 ms 10.870 ms] block target estimation/crypto/s256 opcode 66666 time: [10.745 ms 10.810 ms 10.879 ms] block target estimation/crypto/s256 opcode 100000 time: [10.551 ms 10.608 ms 10.665 ms] ``` k256: ``` block target estimation/crypto/k256 opcode 1 time: [4.0653 ms 4.0881 ms 4.1123 ms] block target estimation/crypto/k256 opcode 10 time: [4.0803 ms 4.1121 ms 4.1500 ms] block target estimation/crypto/k256 opcode 100 time: [3.9216 ms 3.9472 ms 3.9730 ms] block target estimation/crypto/k256 opcode 1000 time: [19.240 ms 19.610 ms 20.102 ms] block target estimation/crypto/k256 opcode 10000 time: [46.020 ms 46.250 ms 46.491 ms] Benchmarking block target estimation/crypto/k256 opcode 19753: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.0s, or reduce sample count to 90. block target estimation/crypto/k256 opcode 19753 time: [51.484 ms 51.795 ms 52.121 ms] Benchmarking block target estimation/crypto/k256 opcode 29629: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.1s, or reduce sample count to 90. block target estimation/crypto/k256 opcode 29629 time: [53.408 ms 54.647 ms 56.676 ms] Benchmarking block target estimation/crypto/k256 opcode 44444: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.2s, or reduce sample count to 90. block target estimation/crypto/k256 opcode 44444 time: [54.142 ms 54.426 ms 54.716 ms] Benchmarking block target estimation/crypto/k256 opcode 66666: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.3s, or reduce sample count to 90. block target estimation/crypto/k256 opcode 66666 time: [55.453 ms 55.730 ms 56.009 ms] Benchmarking block target estimation/crypto/k256 opcode 100000: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.4s, or reduce sample count to 90. block target estimation/crypto/k256 opcode 100000 time: [54.617 ms 55.698 ms 57.215 ms] ``` --------- Co-authored-by: Green Baneling <[email protected]>
- Loading branch information
1 parent
77bb5bf
commit fb22c66
Showing
8 changed files
with
196 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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![], | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod alu; | ||
|
||
pub mod crypto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters