From acbde8f77b3302e3a1c176adcd9889468b857c19 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 30 Jan 2026 19:58:00 -0700 Subject: [PATCH] elliptic-curve: add `SecretKey::diffie_hellman` I found myself wanting this working on `dhkem` --- elliptic-curve/src/secret_key.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index 0bbf20827..87d2513d6 100644 --- a/elliptic-curve/src/secret_key.rs +++ b/elliptic-curve/src/secret_key.rs @@ -19,6 +19,9 @@ use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing}; #[cfg(feature = "arithmetic")] use crate::{CurveArithmetic, NonZeroScalar, PublicKey}; +#[cfg(feature = "ecdh")] +use crate::ecdh; + #[cfg(feature = "pem")] use pem_rfc7468::{self as pem, PemLabel}; @@ -172,6 +175,17 @@ where self.inner.to_bytes() } + /// Perform Elliptic Curve Diffie-Hellman with the given public key, returning a shared secret. + /// + /// See the documentation in the [`ecdh`] module for more information. + #[cfg(feature = "ecdh")] + pub fn diffie_hellman(self, public_key: &PublicKey) -> ecdh::SharedSecret + where + C: CurveArithmetic, + { + ecdh::diffie_hellman(self.to_nonzero_scalar(), public_key.as_affine()) + } + /// Deserialize secret key encoded in the SEC1 ASN.1 DER `ECPrivateKey` format. #[cfg(feature = "sec1")] pub fn from_sec1_der(der_bytes: &[u8]) -> Result