ecdh.go 758 B

123456789101112131415161718192021
  1. // Copyright (c) 2015 The btcsuite developers
  2. // Copyright (c) 2015-2016 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. package secp256k1
  6. // GenerateSharedSecret generates a shared secret based on a private key and a
  7. // public key using Diffie-Hellman key exchange (ECDH) (RFC 5903).
  8. // RFC5903 Section 9 states we should only return x.
  9. //
  10. // It is recommended to securily hash the result before using as a cryptographic
  11. // key.
  12. func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte {
  13. var point, result JacobianPoint
  14. pubkey.AsJacobian(&point)
  15. ScalarMultNonConst(&privkey.Key, &point, &result)
  16. result.ToAffine()
  17. xBytes := result.X.Bytes()
  18. return xBytes[:]
  19. }