1 // Copyright (c) 2015 The btcsuite developers
2 // Copyright (c) 2015-2023 The Decred developers
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
5 6 package secp256k1
7 8 // GenerateSharedSecret generates a shared secret based on a secret key and a
9 // public key using Diffie-Hellman key exchange (ECDH) (RFC 5903).
10 // RFC5903 Section 9 states we should only return x.
11 //
12 // It is recommended to securely hash the result before using as a cryptographic
13 // key.
14 func GenerateSharedSecret(seckey *SecretKey, pubkey *PublicKey) []byte {
15 var point, result JacobianPoint
16 pubkey.AsJacobian(&point)
17 ScalarMultNonConst(&seckey.Key, &point, &result)
18 result.ToAffine()
19 xBytes := result.X.Bytes()
20 return xBytes[:]
21 }
22