Skip to content

Commit 29dd603

Browse files
authored
Merge pull request #354 from vincenthz/coxide4
bump version of cryptoxide and ed25519-bip32
2 parents ef940f0 + e8f84d3 commit 29dd603

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

rust/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ exclude = [
1515
crate-type = ["cdylib", "rlib"]
1616

1717
[dependencies]
18-
cryptoxide = "0.3.2"
18+
cryptoxide = "0.4.2"
1919
cbor_event = "2.1.3"
20-
ed25519-bip32 = "0.3.2"
20+
ed25519-bip32 = "0.4.1"
2121
sha2 = "^0.9"
2222
digest = "^0.9"
2323
bech32 = "0.7.2"

rust/src/chain_crypto/algorithms/ed25519.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::chain_crypto::key::{
22
AsymmetricKey, AsymmetricPublicKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic,
33
};
4-
use crate::chain_crypto::sign::{SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm};
4+
use crate::chain_crypto::sign::{
5+
SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm,
6+
};
57
use cryptoxide::ed25519;
68
use rand_os::rand_core::{CryptoRng, RngCore};
79

@@ -11,7 +13,7 @@ use ed25519_bip32::XPub;
1113
pub struct Ed25519;
1214

1315
#[derive(Clone)]
14-
pub struct Priv([u8; ed25519::SEED_LENGTH]);
16+
pub struct Priv([u8; ed25519::PRIVATE_KEY_LENGTH]);
1517

1618
#[derive(Clone, PartialEq, Eq, Hash)]
1719
pub struct Pub(pub(crate) [u8; ed25519::PUBLIC_KEY_LENGTH]);
@@ -68,7 +70,7 @@ impl AsymmetricKey for Ed25519 {
6870
const SECRET_BECH32_HRP: &'static str = "ed25519_sk";
6971

7072
fn generate<T: RngCore + CryptoRng>(mut rng: T) -> Self::Secret {
71-
let mut priv_bytes = [0u8; ed25519::SEED_LENGTH];
73+
let mut priv_bytes = [0u8; ed25519::PRIVATE_KEY_LENGTH];
7274
rng.fill_bytes(&mut priv_bytes);
7375
Priv(priv_bytes)
7476
}
@@ -79,17 +81,17 @@ impl AsymmetricKey for Ed25519 {
7981
}
8082

8183
fn secret_from_binary(data: &[u8]) -> Result<Self::Secret, SecretKeyError> {
82-
if data.len() != ed25519::SEED_LENGTH {
84+
if data.len() != ed25519::PRIVATE_KEY_LENGTH {
8385
return Err(SecretKeyError::SizeInvalid);
8486
}
85-
let mut buf = [0; ed25519::SEED_LENGTH];
86-
buf[0..ed25519::SEED_LENGTH].clone_from_slice(data);
87+
let mut buf = [0; ed25519::PRIVATE_KEY_LENGTH];
88+
buf[0..ed25519::PRIVATE_KEY_LENGTH].clone_from_slice(data);
8789
Ok(Priv(buf))
8890
}
8991
}
9092

9193
impl SecretKeySizeStatic for Ed25519 {
92-
const SECRET_KEY_SIZE: usize = ed25519::SEED_LENGTH;
94+
const SECRET_KEY_SIZE: usize = ed25519::PRIVATE_KEY_LENGTH;
9395
}
9496

9597
impl VerificationAlgorithm for Ed25519 {
@@ -115,7 +117,7 @@ impl VerificationAlgorithm for Ed25519 {
115117
signature: &Self::Signature,
116118
msg: &[u8],
117119
) -> Verification {
118-
ed25519::verify(msg, &pubkey.0, signature.as_ref()).into()
120+
ed25519::verify(msg, &pubkey.0, &signature.0).into()
119121
}
120122
}
121123

rust/src/chain_crypto/algorithms/ed25519_extended.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::chain_crypto::key::{AsymmetricKey, AsymmetricPublicKey, SecretKeyError, SecretKeySizeStatic};
1+
use crate::chain_crypto::key::{
2+
AsymmetricKey, AsymmetricPublicKey, SecretKeyError, SecretKeySizeStatic,
3+
};
24
use crate::chain_crypto::sign::SigningAlgorithm;
35

46
use super::ed25519 as ei;
@@ -12,7 +14,7 @@ use ed25519_bip32::{XPrv, XPRV_SIZE};
1214
pub struct Ed25519Extended;
1315

1416
#[derive(Clone)]
15-
pub struct ExtendedPriv([u8; ed25519::PRIVATE_KEY_LENGTH]);
17+
pub struct ExtendedPriv([u8; ed25519::EXTENDED_KEY_LENGTH]);
1618

1719
impl AsRef<[u8]> for ExtendedPriv {
1820
fn as_ref(&self) -> &[u8] {
@@ -22,7 +24,7 @@ impl AsRef<[u8]> for ExtendedPriv {
2224

2325
impl ExtendedPriv {
2426
pub fn from_xprv(xprv: &XPrv) -> Self {
25-
let mut buf = [0; ed25519::PRIVATE_KEY_LENGTH];
27+
let mut buf = [0; ed25519::EXTENDED_KEY_LENGTH];
2628
xprv.get_extended_mut(&mut buf);
2729
ExtendedPriv(buf)
2830
}
@@ -39,22 +41,22 @@ impl AsymmetricKey for Ed25519Extended {
3941
rng.fill_bytes(&mut priv_bytes);
4042
let xprv = XPrv::normalize_bytes_force3rd(priv_bytes);
4143

42-
let mut out = [0u8; ed25519::PRIVATE_KEY_LENGTH];
44+
let mut out = [0u8; ed25519::EXTENDED_KEY_LENGTH];
4345
xprv.get_extended_mut(&mut out);
4446
ExtendedPriv(out)
4547
}
4648

4749
fn compute_public(key: &Self::Secret) -> <Self::PubAlg as AsymmetricPublicKey>::Public {
48-
let pk = ed25519::to_public(&key.0);
50+
let pk = ed25519::extended_to_public(&key.0);
4951
ei::Pub(pk)
5052
}
5153

5254
fn secret_from_binary(data: &[u8]) -> Result<Self::Secret, SecretKeyError> {
53-
if data.len() != ed25519::PRIVATE_KEY_LENGTH {
55+
if data.len() != ed25519::EXTENDED_KEY_LENGTH {
5456
return Err(SecretKeyError::SizeInvalid);
5557
}
56-
let mut buf = [0; ed25519::PRIVATE_KEY_LENGTH];
57-
buf[0..ed25519::PRIVATE_KEY_LENGTH].clone_from_slice(data);
58+
let mut buf = [0; ed25519::EXTENDED_KEY_LENGTH];
59+
buf.clone_from_slice(data);
5860
// TODO structure check
5961
Ok(ExtendedPriv(buf))
6062
}

rust/src/chain_crypto/algorithms/legacy_daedalus.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::chain_crypto::key::{
22
AsymmetricKey, AsymmetricPublicKey, PublicKeyError, SecretKeyError, SecretKeySizeStatic,
33
};
4-
use crate::chain_crypto::sign::{SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm};
4+
use crate::chain_crypto::sign::{
5+
SignatureError, SigningAlgorithm, Verification, VerificationAlgorithm,
6+
};
57

68
use cryptoxide::digest::Digest;
79
use cryptoxide::hmac::Hmac;
@@ -35,17 +37,15 @@ impl LegacyPriv {
3537
LegacyPriv(buf)
3638
}
3739

38-
pub fn inner_key(&self) -> [u8; ed25519::PRIVATE_KEY_LENGTH] {
39-
let mut buf = [0; ed25519::PRIVATE_KEY_LENGTH];
40-
buf[0..ed25519::PRIVATE_KEY_LENGTH]
41-
.clone_from_slice(&self.0.as_ref()[0..ed25519::PRIVATE_KEY_LENGTH]);
40+
pub fn inner_key(&self) -> [u8; ed25519::EXTENDED_KEY_LENGTH] {
41+
let mut buf = [0; ed25519::EXTENDED_KEY_LENGTH];
42+
buf.clone_from_slice(&self.0.as_ref()[0..ed25519::EXTENDED_KEY_LENGTH]);
4243
buf
4344
}
4445

4546
pub fn chaincode(&self) -> [u8; CHAIN_CODE_SIZE] {
4647
let mut buf = [0; CHAIN_CODE_SIZE];
47-
buf[0..CHAIN_CODE_SIZE]
48-
.clone_from_slice(&self.0.as_ref()[ed25519::PRIVATE_KEY_LENGTH..XPRV_SIZE]);
48+
buf.clone_from_slice(&self.0.as_ref()[ed25519::EXTENDED_KEY_LENGTH..XPRV_SIZE]);
4949
buf
5050
}
5151
}
@@ -94,7 +94,7 @@ impl AsymmetricKey for LegacyDaedalus {
9494

9595
fn compute_public(key: &Self::Secret) -> <Self as AsymmetricPublicKey>::Public {
9696
let ed25519e = key.inner_key();
97-
let pubkey = ed25519::to_public(&ed25519e);
97+
let pubkey = ed25519::extended_to_public(&ed25519e);
9898
let chaincode = key.chaincode();
9999

100100
let mut buf = [0; XPUB_SIZE];

0 commit comments

Comments
 (0)