1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//! Implements the static ecdh algorithm required by discv5 in terms of the `k256` library.
use super::k256::{
    self,
    ecdsa::{SigningKey, VerifyingKey},
    elliptic_curve::sec1::ToEncodedPoint,
};

pub fn ecdh(public_key: &VerifyingKey, secret_key: &SigningKey) -> Vec<u8> {
    k256::PublicKey::from_affine(
        (k256::PublicKey::from_sec1_bytes(public_key.to_sec1_bytes().as_ref())
            .unwrap()
            .to_projective()
            * k256::SecretKey::from_slice(&secret_key.to_bytes())
                .unwrap()
                .to_nonzero_scalar()
                .as_ref())
        .to_affine(),
    )
    .unwrap()
    .to_encoded_point(true)
    .as_bytes()
    .to_vec()
}