use crate::{PublicKey, SecretKey};
use zeroize::{Zeroize, Zeroizing};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
InvalidPeerKey,
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidPeerKey => f.write_str("peer key is not on the curve"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;
pub fn shared_x(secret: &SecretKey, peer: &PublicKey) -> Result<Zeroizing<[u8; 32]>> {
secret.with_secret_bytes(|bytes| {
let Ok(mut sec) = secp256k1::SecretKey::from_secret_bytes(*bytes) else {
return Err(Error::InvalidPeerKey);
};
let Ok(peer_x) = secp256k1::XOnlyPublicKey::from_byte_array(*peer.as_bytes()) else {
sec.non_secure_erase();
return Err(Error::InvalidPeerKey);
};
let peer_point =
secp256k1::PublicKey::from_x_only_public_key(peer_x, secp256k1::Parity::Even);
let mut point = secp256k1::ecdh::shared_secret_point(&peer_point, &sec);
sec.non_secure_erase();
let mut shared_x = Zeroizing::new([0u8; 32]);
shared_x.copy_from_slice(point.split_at(32).0);
point.zeroize();
Ok(shared_x)
})
}