diff --git a/config/src/genesis.rs b/config/src/genesis.rs index b6881ac4d65..b137b2298c4 100644 --- a/config/src/genesis.rs +++ b/config/src/genesis.rs @@ -48,7 +48,7 @@ pub enum ParsedConfiguration { /// The peer is responsible for submitting the genesis block Full { /// Genesis account key pair - key_pair: KeyPair, + key_pair: Box, /// Raw genesis block raw_block: RawGenesisBlock, }, @@ -69,7 +69,7 @@ impl Configuration { .map_err(|report| ParseError::File { path, report })?; Ok(ParsedConfiguration::Full { - key_pair: KeyPair::new(self.public_key, private_key)?, + key_pair: Box::new(KeyPair::new(self.public_key, private_key)?), raw_block, }) } diff --git a/core/src/queue.rs b/core/src/queue.rs index 8da1435caa4..d55bd2eb94d 100644 --- a/core/src/queue.rs +++ b/core/src/queue.rs @@ -770,7 +770,9 @@ mod tests { ) .with_instructions(instructions); tx.set_ttl(Duration::from_millis(10)); + let now = std::time::Instant::now(); let tx = tx.sign(alice_key); + println!("Signing time: {}ms", now.elapsed().as_millis()); let limits = TransactionLimits { max_instruction_number: 4096, max_wasm_size_bytes: 0, diff --git a/crypto/src/kex/x25519.rs b/crypto/src/kex/x25519.rs index 415ead3c393..071c4a3e5a7 100644 --- a/crypto/src/kex/x25519.rs +++ b/crypto/src/kex/x25519.rs @@ -1,5 +1,6 @@ #[cfg(not(feature = "std"))] -use alloc::{borrow::ToOwned as _, boxed::Box}; +use alloc::borrow::ToOwned as _; +use core::borrow::Borrow; use arrayref::array_ref; use iroha_primitives::const_vec::ConstVec; @@ -47,7 +48,7 @@ impl KeyExchangeScheme for X25519Sha256 { (pk, sk) } KeyGenOption::FromPrivateKey(ref s) => { - let crate::PrivateKey::Ed25519(s) = s else { + let crate::PrivateKey::Ed25519(s) = s.borrow() else { panic!("Wrong private key type, expected `Ed25519`, got {s:?}") }; let sk = StaticSecret::from(*array_ref!(s.as_bytes(), 0, 32)); @@ -70,9 +71,7 @@ impl KeyExchangeScheme for X25519Sha256 { "Ed25519 public key should be possible to create from X25519 public key", ), ), - PrivateKey::Ed25519(Box::new(crate::ed25519::PrivateKey::from_bytes( - sk.as_bytes(), - ))), + PrivateKey::Ed25519(crate::ed25519::PrivateKey::from_bytes(sk.as_bytes())), ) } @@ -128,7 +127,8 @@ mod tests { .unwrap(); assert_eq!(shared_secret1.payload(), shared_secret2.payload()); - let (public_key2, _secret_key1) = scheme.keypair(KeyGenOption::FromPrivateKey(secret_key1)); + let (public_key2, _secret_key1) = + scheme.keypair(KeyGenOption::FromPrivateKey(Box::new(secret_key1))); assert_eq!(public_key2, public_key1); } } diff --git a/crypto/src/lib.rs b/crypto/src/lib.rs index 58c1595a615..8f265d58ad8 100755 --- a/crypto/src/lib.rs +++ b/crypto/src/lib.rs @@ -120,7 +120,7 @@ pub enum KeyGenOption { /// Use seed UseSeed(Vec), /// Derive from private key - FromPrivateKey(PrivateKey), + FromPrivateKey(Box), } ffi::ffi_item! { @@ -158,9 +158,9 @@ impl KeyGenConfiguration { /// Construct using private key with [`Ed25519`](Algorithm::Ed25519) algorithm #[must_use] - pub fn from_private_key(private_key: PrivateKey) -> Self { + pub fn from_private_key(private_key: impl Into>) -> Self { Self { - key_gen_option: KeyGenOption::FromPrivateKey(private_key), + key_gen_option: KeyGenOption::FromPrivateKey(private_key.into()), algorithm: Algorithm::default(), } } @@ -253,7 +253,7 @@ impl From<(ed25519::PublicKey, ed25519::PrivateKey)> for KeyPair { fn from((public_key, private_key): (ed25519::PublicKey, ed25519::PrivateKey)) -> Self { Self { public_key: PublicKey::Ed25519(public_key), - private_key: PrivateKey::Ed25519(Box::new(private_key)), + private_key: PrivateKey::Ed25519(private_key), } } } @@ -516,7 +516,7 @@ impl PublicKey { impl From for PublicKey { fn from(private_key: PrivateKey) -> Self { let digest_function = private_key.algorithm(); - let key_gen_option = KeyGenOption::FromPrivateKey(private_key); + let key_gen_option = KeyGenOption::FromPrivateKey(Box::new(private_key)); match digest_function { Algorithm::Ed25519 => { @@ -537,9 +537,9 @@ ffi::ffi_item! { /// Private Key used in signatures. #[derive(Clone)] #[cfg_attr(all(feature = "ffi_export", not(feature = "ffi_import")), ffi_type(opaque))] - #[allow(missing_docs)] + #[allow(missing_docs, variant_size_differences)] pub enum PrivateKey { - Ed25519(Box), + Ed25519(ed25519::PrivateKey), Secp256k1(secp256k1::PrivateKey), BlsNormal(bls::BlsNormalPrivateKey), BlsSmall(bls::BlsSmallPrivateKey), @@ -568,9 +568,9 @@ impl PrivateKey { /// - If the given payload is not a valid private key for the given digest function pub fn from_raw(algorithm: Algorithm, payload: &[u8]) -> Result { match algorithm { - Algorithm::Ed25519 => ed25519::Ed25519Sha512::parse_private_key(payload) - .map(Box::new) - .map(Self::Ed25519), + Algorithm::Ed25519 => { + ed25519::Ed25519Sha512::parse_private_key(payload).map(Self::Ed25519) + } Algorithm::Secp256k1 => { secp256k1::EcdsaSecp256k1Sha256::parse_private_key(payload).map(Self::Secp256k1) } diff --git a/crypto/src/signature/ed25519.rs b/crypto/src/signature/ed25519.rs index a4e5be6c970..4906eb4e600 100644 --- a/crypto/src/signature/ed25519.rs +++ b/crypto/src/signature/ed25519.rs @@ -1,4 +1,4 @@ -use core::convert::TryFrom; +use core::{borrow::Borrow as _, convert::TryFrom}; use arrayref::array_ref; use ed25519_dalek::Signature; @@ -33,7 +33,7 @@ impl Ed25519Sha512 { PrivateKey::generate(&mut rng) } KeyGenOption::FromPrivateKey(ref s) => { - let crate::PrivateKey::Ed25519(s) = s else { + let crate::PrivateKey::Ed25519(s) = s.borrow() else { panic!("Wrong private key type, expected `Ed25519`, got {s:?}") }; PrivateKey::clone(s) @@ -93,10 +93,10 @@ mod test { #[test] fn ed25519_load_keys() { let secret = PrivateKey::from_hex(Algorithm::Ed25519, PRIVATE_KEY).unwrap(); - let (p1, s1) = Ed25519Sha512::keypair(KeyGenOption::FromPrivateKey(secret)); + let (p1, s1) = Ed25519Sha512::keypair(KeyGenOption::FromPrivateKey(Box::new(secret))); assert_eq!( - PrivateKey::Ed25519(Box::new(s1)), + PrivateKey::Ed25519(s1), PrivateKey::from_hex(Algorithm::Ed25519, PRIVATE_KEY).unwrap() ); assert_eq!( @@ -108,7 +108,7 @@ mod test { #[test] fn ed25519_verify() { let secret = PrivateKey::from_hex(Algorithm::Ed25519, PRIVATE_KEY).unwrap(); - let (p, _) = Ed25519Sha512::keypair(KeyGenOption::FromPrivateKey(secret)); + let (p, _) = Ed25519Sha512::keypair(KeyGenOption::FromPrivateKey(Box::new(secret))); Ed25519Sha512::verify(MESSAGE_1, hex::decode(SIGNATURE_1).unwrap().as_slice(), &p).unwrap(); @@ -129,7 +129,7 @@ mod test { #[test] fn ed25519_sign() { let secret = PrivateKey::from_hex(Algorithm::Ed25519, PRIVATE_KEY).unwrap(); - let (p, s) = Ed25519Sha512::keypair(KeyGenOption::FromPrivateKey(secret)); + let (p, s) = Ed25519Sha512::keypair(KeyGenOption::FromPrivateKey(Box::new(secret))); let sig = Ed25519Sha512::sign(MESSAGE_1, &s); Ed25519Sha512::verify(MESSAGE_1, &sig, &p).unwrap(); diff --git a/crypto/src/signature/mod.rs b/crypto/src/signature/mod.rs index 9b46a7cd1f6..d5e4c27db00 100644 --- a/crypto/src/signature/mod.rs +++ b/crypto/src/signature/mod.rs @@ -196,6 +196,7 @@ impl SignatureOf { /// /// # Errors /// Fails if signing fails + #[inline] fn from_hash(key_pair: KeyPair, hash: HashOf) -> Self { Self(Signature::new(key_pair, hash.as_ref()), PhantomData) } @@ -217,6 +218,7 @@ impl SignatureOf { /// /// # Errors /// Fails if signing fails + #[inline] pub fn new(key_pair: KeyPair, value: &T) -> Self { Self::from_hash(key_pair, HashOf::new(value)) } @@ -469,6 +471,7 @@ impl SignaturesOf { /// /// # Errors /// Forwards [`SignatureOf::new`] errors + #[inline] pub fn new(key_pair: KeyPair, value: &T) -> Self { SignatureOf::new(key_pair, value).into() } diff --git a/crypto/src/signature/secp256k1.rs b/crypto/src/signature/secp256k1.rs index ac407ade678..f73868db01b 100644 --- a/crypto/src/signature/secp256k1.rs +++ b/crypto/src/signature/secp256k1.rs @@ -36,6 +36,7 @@ impl EcdsaSecp256k1Sha256 { mod ecdsa_secp256k1 { #[cfg(not(feature = "std"))] use alloc::{format, string::ToString as _, vec::Vec}; + use core::borrow::Borrow; use arrayref::array_ref; use digest::Digest as _; @@ -68,7 +69,7 @@ mod ecdsa_secp256k1 { .expect("Creating private key from seed should always succeed") } KeyGenOption::FromPrivateKey(ref s) => { - let crate::PrivateKey::Secp256k1(s) = s else { + let crate::PrivateKey::Secp256k1(s) = s.borrow() else { panic!("Wrong private key type, expected `Secp256k1`, got {s:?}") }; s.clone() @@ -153,9 +154,9 @@ mod test { #[test] fn secp256k1_compatibility() { let secret = private_key(); - let (p, s) = EcdsaSecp256k1Sha256::keypair(KeyGenOption::FromPrivateKey( + let (p, s) = EcdsaSecp256k1Sha256::keypair(KeyGenOption::FromPrivateKey(Box::new( crate::PrivateKey::Secp256k1(secret), - )); + ))); let _sk = secp256k1::SecretKey::from_slice(&s.to_bytes()).unwrap(); let _pk = secp256k1::PublicKey::from_slice(&p.to_sec1_bytes()).unwrap(); @@ -205,9 +206,9 @@ mod test { #[test] fn secp256k1_sign() { let secret = private_key(); - let (pk, sk) = EcdsaSecp256k1Sha256::keypair(KeyGenOption::FromPrivateKey( + let (pk, sk) = EcdsaSecp256k1Sha256::keypair(KeyGenOption::FromPrivateKey(Box::new( crate::PrivateKey::Secp256k1(secret), - )); + ))); let sig = EcdsaSecp256k1Sha256::sign(MESSAGE_1, &sk); EcdsaSecp256k1Sha256::verify(MESSAGE_1, &sig, &pk).unwrap();