Skip to content

Commit 9998171

Browse files
committed
add function to compute ed25519 pubkey
1 parent 0d38d80 commit 9998171

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

bolos-impl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ edition = "2018"
1111
bolos-common = { version = "0.1", path = "../bolos-common" }
1212
bolos-sys = { version = "0.1", path = "../bolos-sys" }
1313
zemu-sys = { version = "0.1", path = "../zemu" }
14-
14+
ed25519-dalek = { version = "2.1.1", default-features = false }
1515
cfg-if = "1.0.0"
1616
no-std-compat = { version = "0.4" }
1717

bolos-impl/src/crypto/ecfp256.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,25 @@ impl AsRef<[u8]> for PublicKey {
143143
}
144144
}
145145

146+
pub fn public_from_bytes_ed25519(
147+
bytes: &[u8; 32],
148+
out: &mut MaybeUninit<PublicKey>,
149+
) -> Result<(), Error> {
150+
use ed25519_dalek::{SigningKey, VerifyingKey};
151+
152+
// Initialize the PublicKey struct with the appropriate data
153+
unsafe {
154+
let out_ptr = out.as_mut_ptr();
155+
(*out_ptr).0.W[0] = 0x02; // Add prefix for compressed format
156+
(*out_ptr).0.W[1..33]
157+
.copy_from_slice(&SigningKey::from_bytes(bytes).verifying_key().to_bytes());
158+
(*out_ptr).0.W_len = 33; // Length includes the prefix byte
159+
(*out_ptr).0.curve = Curve::Ed25519 as u32;
160+
}
161+
162+
Ok(())
163+
}
164+
146165
pub struct SecretKey<const B: usize> {
147166
mode: Mode,
148167
curve: Curve,

bolos-mock/src/crypto/ecfp256.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ impl<const B: usize> SecretKey<B> {
118118
_: Mode,
119119
curve: Curve,
120120
path: BIP32Path<B>,
121-
ed25519_secret_key_bytes: Option<[u8; 32]>,
122121
) -> Self {
123122
let bytes = match curve {
124123
Curve::Secp256K1 => {
@@ -132,17 +131,11 @@ impl<const B: usize> SecretKey<B> {
132131
*secret.to_bytes().as_ref()
133132
}
134133
Curve::Ed25519 => {
135-
if let Some(bytes) = ed25519_secret_key_bytes {
136-
let secret = ed25519_dalek::SigningKey::from_bytes(&bytes);
137-
secret.to_bytes()
138-
} else {
139-
// Generate random bytes using the path if no bytes provided
140-
let mut bytes = [0u8; 32];
141-
let mut rng = Self::rng8(path);
142-
use rand_chacha8::rand_core::RngCore;
143-
rng.fill_bytes(&mut bytes);
144-
bytes
145-
}
134+
let mut bytes = [0u8; 32];
135+
let mut rng = Self::rng8(path);
136+
use rand_chacha8::rand_core::RngCore;
137+
rng.fill_bytes(&mut bytes);
138+
bytes
146139
}
147140
Curve::Stark256 => {
148141
panic!("invalid curve passed to ecfp256 new")

0 commit comments

Comments
 (0)