privkey.go raw

   1  // Copyright (c) 2013-2016 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package btcec
   6  
   7  import (
   8  	secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
   9  )
  10  
  11  // PrivateKey wraps an ecdsa.PrivateKey as a convenience mainly for signing
  12  // things with the private key without having to directly import the ecdsa
  13  // package.
  14  type PrivateKey = secp.PrivateKey
  15  
  16  // PrivKeyFromBytes returns a private and public key for `curve' based on the
  17  // private key passed as an argument as a byte slice.
  18  func PrivKeyFromBytes(pk []byte) (*PrivateKey, *PublicKey) {
  19  	privKey := secp.PrivKeyFromBytes(pk)
  20  
  21  	return privKey, privKey.PubKey()
  22  }
  23  
  24  // NewPrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey
  25  // instead of the normal ecdsa.PrivateKey.
  26  func NewPrivateKey() (*PrivateKey, error) {
  27  	return secp.GeneratePrivateKey()
  28  }
  29  
  30  // PrivKeyFromScalar instantiates a new private key from a scalar encoded as a
  31  // big integer.
  32  func PrivKeyFromScalar(key *ModNScalar) *PrivateKey {
  33  	return &PrivateKey{Key: *key}
  34  }
  35  
  36  // PrivKeyBytesLen defines the length in bytes of a serialized private key.
  37  const PrivKeyBytesLen = 32
  38