struct for shared secret without clone or copy

This commit is contained in:
Neemek 2025-11-07 09:08:34 +01:00
parent 7debf5e213
commit c81cb03ba7
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E

View file

@ -48,11 +48,11 @@ impl Secret {
Secret::from(U256::random(&mut OsRng)) Secret::from(U256::random(&mut OsRng))
} }
pub fn diffie_hellman(&self, public: &Public) -> Secret { pub fn diffie_hellman(&self, public: &Public) -> SharedSecret {
let point = CURVE_25519.point(public.0); let point = CURVE_25519.point(public.0);
let p = point * self.0; let p = point * self.0;
Secret(p.get_x()) SharedSecret(p.get_x().to_le_bytes())
} }
pub fn to_bytes(&self) -> [u8; 32] { pub fn to_bytes(&self) -> [u8; 32] {
@ -72,6 +72,15 @@ impl From<U256> for Secret {
} }
} }
#[derive(Debug, PartialEq)]
pub struct SharedSecret([u8; 32]);
impl SharedSecret {
pub fn derive_key<T: Fn([u8; 32]) -> [u32; 8]>(self, deriver: T) -> [u32; 8] {
deriver(self.0)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -103,7 +112,7 @@ mod tests {
let x_k = let x_k =
U256::from_be_hex("0933dc6ed7122bdf2a5f1b0516e218b743868769778787cfa1ac0ae2dfa89891"); U256::from_be_hex("0933dc6ed7122bdf2a5f1b0516e218b743868769778787cfa1ac0ae2dfa89891");
assert_eq!(x.0, x_k); assert_eq!(x.0, x_k.to_le_bytes());
} }
#[test] #[test]