From 6a42c4bd22b5f4c987652b1e8ff7b182881fbb4c Mon Sep 17 00:00:00 2001 From: Carlos Medeiros Date: Fri, 28 Feb 2025 09:58:17 +0000 Subject: [PATCH] add function to compute ed25519 pubkey --- bolos-impl/Cargo.toml | 2 +- bolos-impl/src/crypto/ecfp256.rs | 19 +++++++++++++++++++ bolos-mock/src/crypto/ecfp256.rs | 23 ++++++----------------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/bolos-impl/Cargo.toml b/bolos-impl/Cargo.toml index cc6b3a4..ee4641a 100644 --- a/bolos-impl/Cargo.toml +++ b/bolos-impl/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" bolos-common = { version = "0.1", path = "../bolos-common" } bolos-sys = { version = "0.1", path = "../bolos-sys" } zemu-sys = { version = "0.1", path = "../zemu" } - +ed25519-dalek = { version = "2.1.1", default-features = false } cfg-if = "1.0.0" no-std-compat = { version = "0.4" } diff --git a/bolos-impl/src/crypto/ecfp256.rs b/bolos-impl/src/crypto/ecfp256.rs index 04acc39..338e0eb 100644 --- a/bolos-impl/src/crypto/ecfp256.rs +++ b/bolos-impl/src/crypto/ecfp256.rs @@ -143,6 +143,25 @@ impl AsRef<[u8]> for PublicKey { } } +pub fn public_from_bytes_ed25519( + bytes: &[u8; 32], + out: &mut MaybeUninit, +) -> Result<(), Error> { + use ed25519_dalek::{SigningKey, VerifyingKey}; + + // Initialize the PublicKey struct with the appropriate data + unsafe { + let out_ptr = out.as_mut_ptr(); + (*out_ptr).0.W[0] = 0x02; // Add prefix for compressed format + (*out_ptr).0.W[1..33] + .copy_from_slice(&SigningKey::from_bytes(bytes).verifying_key().to_bytes()); + (*out_ptr).0.W_len = 33; // Length includes the prefix byte + (*out_ptr).0.curve = Curve::Ed25519 as u32; + } + + Ok(()) +} + pub struct SecretKey { mode: Mode, curve: Curve, diff --git a/bolos-mock/src/crypto/ecfp256.rs b/bolos-mock/src/crypto/ecfp256.rs index 4327f3d..f68b006 100644 --- a/bolos-mock/src/crypto/ecfp256.rs +++ b/bolos-mock/src/crypto/ecfp256.rs @@ -114,12 +114,7 @@ impl SecretKey { rand_chacha7::ChaCha8Rng::from_seed(seed) } - pub fn new( - _: Mode, - curve: Curve, - path: BIP32Path, - ed25519_secret_key_bytes: Option<[u8; 32]>, - ) -> Self { + pub fn new(_: Mode, curve: Curve, path: BIP32Path) -> Self { let bytes = match curve { Curve::Secp256K1 => { let secret = k256::ecdsa::SigningKey::random(&mut Self::rng8(path)); @@ -132,17 +127,11 @@ impl SecretKey { *secret.to_bytes().as_ref() } Curve::Ed25519 => { - if let Some(bytes) = ed25519_secret_key_bytes { - let secret = ed25519_dalek::SigningKey::from_bytes(&bytes); - secret.to_bytes() - } else { - // Generate random bytes using the path if no bytes provided - let mut bytes = [0u8; 32]; - let mut rng = Self::rng8(path); - use rand_chacha8::rand_core::RngCore; - rng.fill_bytes(&mut bytes); - bytes - } + let mut bytes = [0u8; 32]; + let mut rng = Self::rng8(path); + use rand_chacha8::rand_core::RngCore; + rng.fill_bytes(&mut bytes); + bytes } Curve::Stark256 => { panic!("invalid curve passed to ecfp256 new")