use crate::curve25519::{curve25519, curve25519_base};
macro_rules! bytes_impl {
($t:ident, $n:literal) => {
impl From<[u8; $n]> for $t {
fn from(v: [u8; $n]) -> Self {
$t(v)
}
}
impl Into<[u8; $n]> for $t {
fn into(self) -> [u8; $n] {
self.0
}
}
impl core::convert::TryFrom<&[u8]> for $t {
type Error = ();
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() == $n {
Ok($t(<[u8; $n]>::try_from(value).unwrap()))
} else {
Err(())
}
}
}
impl AsRef<[u8]> for $t {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
};
}
pub struct SecretKey([u8; 32]);
bytes_impl!(SecretKey, 32);
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PublicKey([u8; 32]);
bytes_impl!(PublicKey, 32);
pub struct SharedSecret([u8; 32]);
bytes_impl!(SharedSecret, 32);
pub fn dh(n: &SecretKey, p: &PublicKey) -> SharedSecret {
SharedSecret(curve25519(&n.0, &p.0))
}
pub fn base(x: &SecretKey) -> PublicKey {
PublicKey(curve25519_base(&x.0))
}