From 0cc7d0f9b2e0c8eba446063bb6f846bcf816da9e Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Tue, 12 Aug 2025 23:39:45 +0000 Subject: [PATCH 1/2] Expose conversions to and from integer representations to allow usage in downstream crates and network protocols. --- src/private_key.rs | 7 ++++--- src/public_key.rs | 5 +++++ src/shared_secret.rs | 25 ++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/private_key.rs b/src/private_key.rs index 32621e1..fe928da 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..274bc55 100644 --- a/src/public_key.rs +++ b/src/public_key.rs @@ -54,6 +54,11 @@ where } } + ///Creates a foeign 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(); + } } From 7b5fc9216fe0a968bfe8e7b97d7a153044c87311 Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Tue, 12 Aug 2025 23:41:02 +0000 Subject: [PATCH 2/2] fix typos --- src/private_key.rs | 2 +- src/public_key.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/private_key.rs b/src/private_key.rs index fe928da..659187b 100644 --- a/src/private_key.rs +++ b/src/private_key.rs @@ -25,7 +25,7 @@ impl> PrivateKe Self { params, key } } - /// Obtains the parameters used by this`PrivateKey` + /// Obtains the parameters used by this `PrivateKey` pub const fn params(&self) -> CsidhParams { self.params } diff --git a/src/public_key.rs b/src/public_key.rs index 274bc55..b15baf5 100644 --- a/src/public_key.rs +++ b/src/public_key.rs @@ -54,7 +54,7 @@ where } } - ///Creates a foeign public key from this `PublicKey` + ///Creates a foreign public key from this `PublicKey` pub fn to_repr(&self) -> Uint { return self.key().to_montgomery(); }