Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add secp256k1 and keccak256 host functions
- Loading branch information
Showing
45 changed files
with
1,499 additions
and
257 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
26 changes: 26 additions & 0 deletions
26
soroban-env-host/benches/common/cost_types/compute_ecdsa_secp256k1_pubkey.rs
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,26 @@ | ||
use crate::common::HostCostMeasurement; | ||
use k256::{PublicKey, SecretKey}; | ||
use rand::rngs::StdRng; | ||
use soroban_env_host::{cost_runner::ComputeEcdsaSecp256k1PubKeyRun, Host}; | ||
|
||
// This measures the costs to turn one byte buffer into an EcdsaSecp256k1 | ||
// pubkey, which should be constant time. The input value is ignored. | ||
pub(crate) struct ComputeEcdsaSecp256k1PubKeyMeasure { | ||
key: Vec<u8>, | ||
} | ||
|
||
impl HostCostMeasurement for ComputeEcdsaSecp256k1PubKeyMeasure { | ||
type Runner = ComputeEcdsaSecp256k1PubKeyRun; | ||
|
||
fn new_random_case(_host: &Host, _rng: &mut StdRng, _input: u64) -> Vec<u8> { | ||
// Very awkward: the 'rand' crate has two copies linked in due to | ||
// divergence between the requirements of k256 and ed25519. The StdRng | ||
// we're getting here is not the one k256 wants. So we use an OsRng | ||
// here, from the package k256 wants (and re-exports). | ||
let mut rng = k256::elliptic_curve::rand_core::OsRng; | ||
|
||
let secret = SecretKey::random(&mut rng); | ||
let public: PublicKey = secret.public_key(); | ||
public.to_sec1_bytes().into_vec() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
soroban-env-host/benches/common/cost_types/compute_ecdsa_secp256k1_sig.rs
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,32 @@ | ||
use crate::common::HostCostMeasurement; | ||
use k256::{ | ||
ecdsa::{signature::Signer, Signature, SigningKey}, | ||
SecretKey, | ||
}; | ||
use rand::rngs::StdRng; | ||
use soroban_env_host::{cost_runner::ComputeEcdsaSecp256k1SigRun, Host}; | ||
|
||
// This measures the costs to turn one byte buffer into an EcdsaSecp256k1 | ||
// signature, which should be constant time. The input value is ignored. | ||
pub(crate) struct ComputeEcdsaSecp256k1SigMeasure { | ||
sig: Vec<u8>, | ||
} | ||
|
||
impl HostCostMeasurement for ComputeEcdsaSecp256k1SigMeasure { | ||
type Runner = ComputeEcdsaSecp256k1SigRun; | ||
|
||
fn new_random_case(_host: &Host, _rng: &mut StdRng, input: u64) -> Vec<u8> { | ||
let size = 1 + input * Self::STEP_SIZE; | ||
|
||
// Very awkward: the 'rand' crate has two copies linked in due to | ||
// divergence between the requirements of k256 and ed25519. The StdRng | ||
// we're getting here is not the one k256 wants. So we use an OsRng | ||
// here, from the package k256 wants (and re-exports). | ||
let mut rng = k256::elliptic_curve::rand_core::OsRng; | ||
|
||
let sec: SecretKey = SecretKey::random(&mut rng); | ||
let msg: Vec<u8> = (0..size).map(|x| x as u8).collect(); | ||
let sig: Signature = SigningKey::from(sec).try_sign(msg.as_slice()).unwrap(); | ||
sig.to_bytes().to_vec() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
soroban-env-host/benches/common/cost_types/compute_keccak256_hash.rs
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,19 @@ | ||
use crate::common::HostCostMeasurement; | ||
use rand::rngs::StdRng; | ||
use soroban_env_host::{cost_runner::ComputeKeccak256HashRun, Host}; | ||
|
||
// This measures the costs of performing a keccak256 hash on a variable-sized | ||
// byte buffer. The input value is the size of the buffer. It should be | ||
// linear time. | ||
pub(crate) struct ComputeKeccak256HashMeasure; | ||
|
||
impl HostCostMeasurement for ComputeKeccak256HashMeasure { | ||
type Runner = ComputeKeccak256HashRun; | ||
|
||
const STEP_SIZE: u64 = 100; | ||
|
||
fn new_random_case(_host: &Host, _rng: &mut StdRng, input: u64) -> Vec<u8> { | ||
let size = 1 + input * Self::STEP_SIZE; | ||
(0..size).map(|n| n as u8).collect() | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
soroban-env-host/benches/common/cost_types/recover_ecdsa_secp256k1_key.rs
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,41 @@ | ||
use crate::common::HostCostMeasurement; | ||
use k256::{ecdsa::SigningKey, SecretKey}; | ||
use rand::rngs::StdRng; | ||
use sha3::{Digest, Keccak256}; | ||
use soroban_env_host::{ | ||
cost_runner::{RecoverEcdsaSecp256k1KeyRun, RecoverEcdsaSecp256k1KeySample}, | ||
xdr::Hash, | ||
Host, | ||
}; | ||
|
||
pub(crate) struct RecoverEcdsaSecp256k1KeyMeasure; | ||
|
||
// This measures the cost of verifying an EcdsaSecp256k1 signature of varying-length | ||
// messages. The input value is the length of the signed message. It should cost | ||
// linear CPU (for hashing) and zero heap memory. | ||
impl HostCostMeasurement for RecoverEcdsaSecp256k1KeyMeasure { | ||
type Runner = RecoverEcdsaSecp256k1KeyRun; | ||
|
||
const STEP_SIZE: u64 = 1000; | ||
|
||
fn new_random_case( | ||
_host: &Host, | ||
_rng: &mut StdRng, | ||
input: u64, | ||
) -> RecoverEcdsaSecp256k1KeySample { | ||
// Very awkward: the 'rand' crate has two copies linked in due to | ||
// divergence between the requirements of k256 and ed25519. The StdRng | ||
// we're getting here is not the one k256 wants. So we use an OsRng | ||
// here, from the package k256 wants (and re-exports). | ||
let mut rng = k256::elliptic_curve::rand_core::OsRng; | ||
|
||
let size = 1 + input * Self::STEP_SIZE; | ||
let sec: SecretKey = SecretKey::random(&mut rng); | ||
let msg: Vec<u8> = (0..size).map(|x| x as u8).collect(); | ||
let hash: Hash = Hash(Keccak256::digest(msg).into()); | ||
let (sig, rid) = SigningKey::from(sec) | ||
.sign_prehash_recoverable(hash.as_slice()) | ||
.unwrap(); | ||
RecoverEcdsaSecp256k1KeySample { hash, sig, rid } | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
soroban-env-host/benches/common/cost_types/verify_ecdsa_secp256k1_sig.rs
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,41 @@ | ||
use crate::common::HostCostMeasurement; | ||
use k256::{ | ||
ecdsa::{signature::Signer, Signature, SigningKey}, | ||
PublicKey, SecretKey, | ||
}; | ||
use rand::rngs::StdRng; | ||
use soroban_env_host::{ | ||
cost_runner::{VerifyEcdsaSecp256k1SigRun, VerifyEcdsaSecp256k1SigSample}, | ||
Host, | ||
}; | ||
|
||
pub(crate) struct VerifyEcdsaSecp256k1SigMeasure; | ||
|
||
// This measures the cost of verifying an EcdsaSecp256k1 signature of varying-length | ||
// messages. The input value is the length of the signed message. It should cost | ||
// linear CPU (for hashing) and zero heap memory. | ||
impl HostCostMeasurement for VerifyEcdsaSecp256k1SigMeasure { | ||
type Runner = VerifyEcdsaSecp256k1SigRun; | ||
|
||
const STEP_SIZE: u64 = 1000; | ||
|
||
fn new_random_case( | ||
_host: &Host, | ||
_rng: &mut StdRng, | ||
input: u64, | ||
) -> VerifyEcdsaSecp256k1SigSample { | ||
let size = 1 + input * Self::STEP_SIZE; | ||
|
||
// Very awkward: the 'rand' crate has two copies linked in due to | ||
// divergence between the requirements of k256 and ed25519. The StdRng | ||
// we're getting here is not the one k256 wants. So we use an OsRng | ||
// here, from the package k256 wants (and re-exports). | ||
let mut rng = k256::elliptic_curve::rand_core::OsRng; | ||
|
||
let sec: SecretKey = SecretKey::random(&mut rng); | ||
let key: PublicKey = sec.public_key(); | ||
let msg: Vec<u8> = (0..size).map(|x| x as u8).collect(); | ||
let sig: Signature = SigningKey::from(sec).try_sign(msg.as_slice()).unwrap(); | ||
VerifyEcdsaSecp256k1SigSample { key, msg, sig } | ||
} | ||
} |
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
Oops, something went wrong.