From bef12c53dd82df2ceebc2a7c18eea8c19fbc38b3 Mon Sep 17 00:00:00 2001 From: Yehonatan Cohen Scaly Date: Sun, 19 Mar 2023 17:13:02 +0200 Subject: [PATCH] Added sk_to_pk() for Kem trait and implemented for dhkem (#40) --- src/kem.rs | 3 +++ src/kem/dhkem.rs | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/kem.rs b/src/kem.rs index ffcef3a..3f594d9 100644 --- a/src/kem.rs +++ b/src/kem.rs @@ -48,6 +48,9 @@ pub trait Kem: Sized { #[cfg(not(feature = "serde_impls"))] type PrivateKey: Clone + PartialEq + Eq + Serializable + Deserializable; + /// Computes the public key of a given private key + fn sk_to_pk(sk: &Self::PrivateKey) -> Self::PublicKey; + /// The encapsulated key for this KEM. This is used by the recipient to derive the shared /// secret. #[cfg(feature = "serde_impls")] diff --git a/src/kem/dhkem.rs b/src/kem/dhkem.rs index 1661d78..0ff960e 100644 --- a/src/kem/dhkem.rs +++ b/src/kem/dhkem.rs @@ -114,7 +114,7 @@ macro_rules! impl_dhkem { // The encapped key is the ephemeral pubkey let encapped_key = { - let pk_eph = <$dhkex as DhKeyExchange>::sk_to_pk(&sk_eph); + let pk_eph = <$kem_name as KemTrait>::sk_to_pk(&sk_eph); EncappedKey(pk_eph) }; @@ -212,6 +212,11 @@ macro_rules! impl_dhkem { <$dhkex as DhKeyExchange>::derive_keypair::<$kdf>(&suite_id, ikm) } + /// Computes the public key of a given private key + fn sk_to_pk(sk: &PrivateKey) -> PublicKey { + <$dhkex as DhKeyExchange>::sk_to_pk(sk) + } + // Runs encap_with_eph using a random ephemeral key fn encap( pk_recip: &Self::PublicKey,