diff --git a/addr/src/tor.rs b/addr/src/tor.rs index 58f877a..fbc77a5 100644 --- a/addr/src/tor.rs +++ b/addr/src/tor.rs @@ -87,7 +87,7 @@ impl OnionAddrV3 { pk.copy_from_slice(&bytes[..32]); checksum.copy_from_slice(&bytes[32..34]); - let pk = PublicKey::from_pk_compressed(pk)?; + let pk = PublicKey::from_pk_compressed(pk.into())?; let checksum = u16::from_le_bytes(checksum); let addr = OnionAddrV3::from(pk); @@ -154,7 +154,7 @@ impl FromStr for OnionAddrV3 { } let mut key = [0u8; 32]; key.copy_from_slice(&data[..32]); - let pk = OnionAddrV3::from(PublicKey::from_pk_compressed(key)?); + let pk = OnionAddrV3::from(PublicKey::from_pk_compressed(key.into())?); let checksum = u16::from_le_bytes([data[32], data[33]]); if pk.checksum != checksum { return Err(OnionAddrParseError::InvalidChecksum { diff --git a/cyphergraphy/src/ed25519.rs b/cyphergraphy/src/ed25519.rs index c597100..ef08406 100644 --- a/cyphergraphy/src/ed25519.rs +++ b/cyphergraphy/src/ed25519.rs @@ -23,6 +23,7 @@ use std::cmp::Ordering; use std::ops::Deref; +use amplify::Bytes32; use crate::display::{Encoding, MultiDisplay}; use crate::{EcPk, EcPkInvalid, EcSig, EcSigInvalid, EcSign, EcSk, EcSkInvalid, EcVerifyError}; @@ -38,7 +39,7 @@ impl MultiDisplay for ec25519::PublicKey { impl EcPk for ec25519::PublicKey { const COMPRESSED_LEN: usize = 32; const CURVE_NAME: &'static str = "Edwards25519"; - type Compressed = [u8; 32]; + type Compressed = Bytes32; fn base_point() -> Self { ec25519::PublicKey::from_slice( @@ -51,10 +52,10 @@ impl EcPk for ec25519::PublicKey { .expect("hardcoded basepoint value") } - fn to_pk_compressed(&self) -> Self::Compressed { *self.deref() } + fn to_pk_compressed(&self) -> Self::Compressed { Bytes32::from_byte_array(*self.deref()) } fn from_pk_compressed(pk: Self::Compressed) -> Result { - Ok(ec25519::PublicKey::new(pk)) + Ok(ec25519::PublicKey::new(pk.to_byte_array())) } fn from_pk_compressed_slice(slice: &[u8]) -> Result { @@ -63,7 +64,7 @@ impl EcPk for ec25519::PublicKey { } let mut buf = [0u8; 32]; buf.copy_from_slice(slice); - Self::from_pk_compressed(buf) + Self::from_pk_compressed(buf.into()) } } @@ -104,7 +105,7 @@ impl Ord for PublicKey { impl EcPk for PublicKey { const COMPRESSED_LEN: usize = 32; const CURVE_NAME: &'static str = "Edwards25519"; - type Compressed = [u8; 32]; + type Compressed = Bytes32; fn base_point() -> Self { Self(ec25519::PublicKey::base_point()) } diff --git a/cyphergraphy/src/lib.rs b/cyphergraphy/src/lib.rs index 98cadb0..c144fab 100644 --- a/cyphergraphy/src/lib.rs +++ b/cyphergraphy/src/lib.rs @@ -35,7 +35,8 @@ pub mod ed25519; pub mod display; -use std::fmt::Debug; +use std::fmt::{Debug, Display}; +use std::hash::Hash; pub use digest::*; @@ -119,7 +120,7 @@ pub trait EcPk: Clone + Eq + Send + Debug + MultiDisplay { const CURVE_NAME: &'static str; // TODO: When generic_const_exprs arrive switch to Self::COMPRESSED_LEN arrays - type Compressed: Copy + Sized + Send + AsRef<[u8]>; + type Compressed: Copy + Sized + Eq + Ord + Hash + AsRef<[u8]> + Send + Debug + Display; fn base_point() -> Self; diff --git a/cyphergraphy/src/x25519.rs b/cyphergraphy/src/x25519.rs index 4edd63e..7e760b0 100644 --- a/cyphergraphy/src/x25519.rs +++ b/cyphergraphy/src/x25519.rs @@ -23,6 +23,7 @@ use std::cmp::Ordering; use std::ops::Deref; +use amplify::Bytes32; use crate::*; @@ -37,14 +38,14 @@ impl MultiDisplay for ec25519::x25519::PublicKey { impl EcPk for ec25519::x25519::PublicKey { const COMPRESSED_LEN: usize = 32; const CURVE_NAME: &'static str = "Curve25519"; - type Compressed = [u8; 32]; + type Compressed = Bytes32; fn base_point() -> Self { ec25519::x25519::PublicKey::base_point() } - fn to_pk_compressed(&self) -> Self::Compressed { *self.deref() } + fn to_pk_compressed(&self) -> Self::Compressed { Bytes32::from_byte_array(*self.deref()) } fn from_pk_compressed(pk: Self::Compressed) -> Result { - Ok(ec25519::x25519::PublicKey::new(pk)) + Ok(ec25519::x25519::PublicKey::new(pk.to_byte_array())) } fn from_pk_compressed_slice(slice: &[u8]) -> Result { @@ -53,7 +54,7 @@ impl EcPk for ec25519::x25519::PublicKey { } let mut buf = [0u8; 32]; buf.copy_from_slice(slice); - Self::from_pk_compressed(buf) + Self::from_pk_compressed(buf.into()) } } @@ -111,7 +112,7 @@ impl Ord for PublicKey { impl EcPk for PublicKey { const COMPRESSED_LEN: usize = 32; const CURVE_NAME: &'static str = "Curve25519"; - type Compressed = [u8; 32]; + type Compressed = Bytes32; fn base_point() -> Self { Self(ec25519::x25519::PublicKey::base_point()) }