diff --git a/src/private_key.rs b/src/private_key.rs index 32621e1..659187b 100644 --- a/src/private_key.rs +++ b/src/private_key.rs @@ -25,11 +25,12 @@ impl> PrivateKe Self { params, key } } - pub(crate) const fn params(&self) -> CsidhParams { + /// Obtains the parameters used by this `PrivateKey` + pub const fn params(&self) -> CsidhParams { self.params } - - pub(crate) const fn key(&self) -> [u32; N] { + ///Obtains the key material in this `PrivateKey` + pub const fn key(&self) -> [u32; N] { self.key } } diff --git a/src/public_key.rs b/src/public_key.rs index 64d4da9..b15baf5 100644 --- a/src/public_key.rs +++ b/src/public_key.rs @@ -54,6 +54,11 @@ where } } + ///Creates a foreign public key from this `PublicKey` + pub fn to_repr(&self) -> Uint { + return self.key().to_montgomery(); + } + pub(crate) const fn key(&self) -> ConstMontyForm { self.key } diff --git a/src/shared_secret.rs b/src/shared_secret.rs index 715cb17..0d16fc2 100644 --- a/src/shared_secret.rs +++ b/src/shared_secret.rs @@ -4,7 +4,10 @@ use crypto_bigint::{ rand_core::CryptoRngCore, }; -use crate::{csidh::csidh, private_key::PrivateKey, public_key::PublicKey}; +use crate::{ + CsidhParams, csidh::csidh, montgomery_curve::MontgomeryCurve, private_key::PrivateKey, + public_key::PublicKey, +}; /// A shared secret created with the CSIDH key exchange. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -36,4 +39,24 @@ where ), } } + + /// Constructs a `SharedSecret` from the foreign shared secret, if the secret is valid. + #[must_use] + pub fn new( + params: CsidhParams, + shared_secret: Uint, + rng: &mut impl CryptoRngCore, + ) -> Option { + let shared_secret = ConstMontyForm::new(&shared_secret); + if MontgomeryCurve::new(params, shared_secret).is_supersingular(rng) { + Some(Self { shared_secret }) + } else { + None + } + } + + ///Creates a foreign shared secret from this `SharedSecret` + pub fn to_repr(&self) -> Uint { + return self.shared_secret.to_montgomery(); + } }