Skip to content

Commit

Permalink
[refactor]: Reduce key size with Box
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Polyakov <arjentix@gmail.com>
  • Loading branch information
Arjentix authored and mversic committed Feb 6, 2024
1 parent 4fc4c01 commit 87608fc
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 185 deletions.
4 changes: 2 additions & 2 deletions config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum ParsedConfiguration {
/// The peer is responsible for submitting the genesis block
Full {
/// Genesis account key pair
key_pair: Box<KeyPair>,
key_pair: KeyPair,
/// Raw genesis block
raw_block: RawGenesisBlock,
},
Expand All @@ -69,7 +69,7 @@ impl Configuration {
.map_err(|report| ParseError::File { path, report })?;

Ok(ParsedConfiguration::Full {
key_pair: Box::new(KeyPair::new(self.public_key, private_key)?),
key_pair: KeyPair::new(self.public_key, private_key)?,
raw_block,
})
}
Expand Down
Binary file modified configs/peer/executor.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions core/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ mod tests {

#[test]
async fn custom_expired_transaction_is_rejected() {
const TTL_MS: u64 = 100;

let chain_id = ChainId::new("0");

let max_txs_in_block = 2;
Expand All @@ -769,10 +771,8 @@ mod tests {
AccountId::from_str("alice@wonderland").expect("Valid"),
)
.with_instructions(instructions);
tx.set_ttl(Duration::from_millis(10));
let now = std::time::Instant::now();
tx.set_ttl(Duration::from_millis(TTL_MS));
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,
Expand All @@ -784,7 +784,7 @@ mod tests {
.expect("Failed to push tx into queue");
let mut txs = Vec::new();
let mut expired_txs = Vec::new();
thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(TTL_MS));
queue.get_transactions_for_block(&wsv, max_txs_in_block, &mut txs, &mut expired_txs);
assert!(txs.is_empty());
assert_eq!(expired_txs.len(), 1);
Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub mod isi {
)));
}
if !account.remove_signatory(&public_key) {
return Err(FindError::PublicKey(Box::new(public_key)).into());
return Err(FindError::PublicKey(public_key).into());
}
Ok(())
})?;
Expand Down
19 changes: 11 additions & 8 deletions crypto/src/kex/x25519.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned as _;
use core::borrow::Borrow;
use alloc::{borrow::ToOwned as _, boxed::Box};
use core::borrow::Borrow as _;

use arrayref::array_ref;
use iroha_primitives::const_vec::ConstVec;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl KeyExchangeScheme for X25519Sha256 {
(pk, sk)
}
KeyGenOption::FromPrivateKey(ref s) => {
let crate::PrivateKey::Ed25519(s) = s.borrow() else {
let crate::PrivateKeyInner::Ed25519(s) = s.0.borrow() else {
panic!("Wrong private key type, expected `Ed25519`, got {s:?}")
};
let sk = StaticSecret::from(*array_ref!(s.as_bytes(), 0, 32));
Expand All @@ -66,12 +66,14 @@ impl KeyExchangeScheme for X25519Sha256 {
let edwards_compressed = edwards.compress();

(
PublicKey::Ed25519(
PublicKey(Box::new(crate::PublicKeyInner::Ed25519(
crate::ed25519::PublicKey::from_bytes(edwards_compressed.as_bytes()).expect(
"Ed25519 public key should be possible to create from X25519 public key",
),
),
PrivateKey::Ed25519(crate::ed25519::PrivateKey::from_bytes(sk.as_bytes())),
))),
PrivateKey(Box::new(crate::PrivateKeyInner::Ed25519(
crate::ed25519::PrivateKey::from_bytes(sk.as_bytes()),
))),
)
}

Expand All @@ -80,10 +82,11 @@ impl KeyExchangeScheme for X25519Sha256 {
local_private_key: &PrivateKey,
remote_public_key: &PublicKey,
) -> Result<SessionKey, Error> {
let crate::PrivateKey::Ed25519(local_private_key) = local_private_key else {
let crate::PrivateKeyInner::Ed25519(local_private_key) = local_private_key.0.borrow()
else {
panic!("Wrong private key type, expected `Ed25519`, got {local_private_key:?}")
};
let crate::PublicKey::Ed25519(remote_public_key) = remote_public_key else {
let crate::PublicKeyInner::Ed25519(remote_public_key) = remote_public_key.0.borrow() else {
panic!("Wrong public key type, expected `Ed25519`, got {remote_public_key:?}")
};

Expand Down
Loading

0 comments on commit 87608fc

Please sign in to comment.