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