Skip to content

Commit

Permalink
cyphergraphy: require EcPk::Compressed to be Eq, Ord, Hash, Debug, Di…
Browse files Browse the repository at this point in the history
…splay
  • Loading branch information
dr-orlovsky committed Jul 21, 2024
1 parent ed12f66 commit f423961
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions addr/src/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions cyphergraphy/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -38,7 +39,7 @@ impl MultiDisplay<Encoding> 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(
Expand All @@ -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<Self, EcPkInvalid> {
Ok(ec25519::PublicKey::new(pk))
Ok(ec25519::PublicKey::new(pk.to_byte_array()))
}

fn from_pk_compressed_slice(slice: &[u8]) -> Result<Self, EcPkInvalid> {
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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()) }

Expand Down
5 changes: 3 additions & 2 deletions cyphergraphy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -119,7 +120,7 @@ pub trait EcPk: Clone + Eq + Send + Debug + MultiDisplay<Encoding> {
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;

Expand Down
11 changes: 6 additions & 5 deletions cyphergraphy/src/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use std::cmp::Ordering;
use std::ops::Deref;
use amplify::Bytes32;

use crate::*;

Expand All @@ -37,14 +38,14 @@ impl MultiDisplay<Encoding> 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<Self, EcPkInvalid> {
Ok(ec25519::x25519::PublicKey::new(pk))
Ok(ec25519::x25519::PublicKey::new(pk.to_byte_array()))
}

fn from_pk_compressed_slice(slice: &[u8]) -> Result<Self, EcPkInvalid> {
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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()) }

Expand Down

0 comments on commit f423961

Please sign in to comment.