privkey.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2013-2014 The btcsuite developers
  2. // Copyright (c) 2015-2020 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. import (
  7. "crypto/ecdsa"
  8. "crypto/rand"
  9. )
  10. // PrivateKey provides facilities for working with secp256k1 private keys within
  11. // this package and includes functionality such as serializing and parsing them
  12. // as well as computing their associated public key.
  13. type PrivateKey struct {
  14. Key ModNScalar
  15. }
  16. // NewPrivateKey instantiates a new private key from a scalar encoded as a
  17. // big integer.
  18. func NewPrivateKey(key *ModNScalar) *PrivateKey {
  19. return &PrivateKey{Key: *key}
  20. }
  21. // PrivKeyFromBytes returns a private based on the provided byte slice which is
  22. // interpreted as an unsigned 256-bit big-endian integer in the range [0, N-1],
  23. // where N is the order of the curve.
  24. //
  25. // Note that this means passing a slice with more than 32 bytes is truncated and
  26. // that truncated value is reduced modulo N. It is up to the caller to either
  27. // provide a value in the appropriate range or choose to accept the described
  28. // behavior.
  29. //
  30. // Typically callers should simply make use of GeneratePrivateKey when creating
  31. // private keys which properly handles generation of appropriate values.
  32. func PrivKeyFromBytes(privKeyBytes []byte) *PrivateKey {
  33. var privKey PrivateKey
  34. privKey.Key.SetByteSlice(privKeyBytes)
  35. return &privKey
  36. }
  37. // GeneratePrivateKey returns a private key that is suitable for use with
  38. // secp256k1.
  39. func GeneratePrivateKey() (*PrivateKey, error) {
  40. key, err := ecdsa.GenerateKey(S256(), rand.Reader)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return PrivKeyFromBytes(key.D.Bytes()), nil
  45. }
  46. // PubKey computes and returns the public key corresponding to this private key.
  47. func (p *PrivateKey) PubKey() *PublicKey {
  48. var result JacobianPoint
  49. ScalarBaseMultNonConst(&p.Key, &result)
  50. result.ToAffine()
  51. return NewPublicKey(&result.X, &result.Y)
  52. }
  53. // Zero manually clears the memory associated with the private key. This can be
  54. // used to explicitly clear key material from memory for enhanced security
  55. // against memory scraping.
  56. func (p *PrivateKey) Zero() {
  57. p.Key.Zero()
  58. }
  59. // PrivKeyBytesLen defines the length in bytes of a serialized private key.
  60. const PrivKeyBytesLen = 32
  61. // Serialize returns the private key as a 256-bit big-endian binary-encoded
  62. // number, padded to a length of 32 bytes.
  63. func (p PrivateKey) Serialize() []byte {
  64. var privKeyBytes [PrivKeyBytesLen]byte
  65. p.Key.PutBytes(&privKeyBytes)
  66. return privKeyBytes[:]
  67. }