github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/prime256v1/privkey.go (about) 1 package prime256v1 2 3 import ( 4 "crypto/ecdsa" 5 "crypto/elliptic" 6 "io" 7 "math/big" 8 ) 9 10 const ( 11 // PrivKeyBytesLen defines the length in bytes of a serialized private key. 12 PrivKeyBytesLen = 32 13 ) 14 15 // PrivateKey wraps an ecdsa.PrivateKey as a convenience mainly for signing 16 // things with the the private key without having to directly import the ecdsa 17 // package. 18 type PrivateKey ecdsa.PrivateKey 19 20 func GenerateECDSAKeyWithRand(curve elliptic.Curve, rand io.Reader) (*ecdsa.PrivateKey, error) { 21 return ecdsa.GenerateKey(curve, rand) 22 } 23 24 // GeneratePrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey 25 // instead of the normal ecdsa.PrivateKey. 26 func GeneratePrivateKey(curve elliptic.Curve) (*PrivateKey, error) { 27 key, err := GenerateKey(curve) 28 if err != nil { 29 return nil, err 30 } 31 return (*PrivateKey)(key), nil 32 } 33 34 // GetECDSAKeyValues get ecdsa privateKey values 35 func GetECDSAKeyValues(key *ecdsa.PrivateKey) (priv []byte, x, y *big.Int, err error) { 36 priv = key.D.Bytes() 37 x = key.PublicKey.X 38 y = key.PublicKey.Y 39 return 40 } 41 42 // NewPrivateKey instantiates a new private key from a scalar encoded as a 43 // big integer. 44 func NewPrivateKey(curve elliptic.Curve, d *big.Int) *PrivateKey { 45 b := make([]byte, 0, PrivKeyBytesLen) 46 dB := paddedAppend(PrivKeyBytesLen, b, d.Bytes()) 47 priv, _ := PrivKeyFromBytes(curve, dB) 48 return priv 49 } 50 51 // PrivKeyFromBytes returns a private and public key for `curve' based on the 52 // private key passed as an argument as a byte slice. 53 func PrivKeyFromBytes(curve elliptic.Curve, pk []byte) (*PrivateKey, *PublicKey) { 54 x, y := curve.ScalarBaseMult(pk) 55 56 priv := &ecdsa.PrivateKey{ 57 PublicKey: ecdsa.PublicKey{ 58 Curve: curve, 59 X: x, 60 Y: y, 61 }, 62 D: new(big.Int).SetBytes(pk), 63 } 64 65 return (*PrivateKey)(priv), (*PublicKey)(&priv.PublicKey) 66 } 67 68 // Public returns the PublicKey corresponding to this private key. 69 func (p PrivateKey) Public() (*big.Int, *big.Int) { 70 return p.PublicKey.X, p.PublicKey.Y 71 } 72 73 func (p PrivateKey) PublicBytes() (*big.Int, *big.Int) { 74 return p.PublicKey.X, p.PublicKey.Y 75 } 76 77 // PubKey returns the PublicKey corresponding to this private key. 78 func (p *PrivateKey) PubKey() *PublicKey { 79 return (*PublicKey)(&p.PublicKey) 80 } 81 82 // ToECDSA returns the private key as a *ecdsa.PrivateKey. 83 func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey { 84 return (*ecdsa.PrivateKey)(p) 85 } 86 87 // Sign generates an ECDSA signature for the provided hash (which should be the 88 // result of hashing a larger message) using the private key. Produced signature 89 // is deterministic (same message and same key yield the same signature) and 90 // canonical in accordance with RFC6979 and BIP0062. 91 func (p *PrivateKey) Sign(hash []byte) (*Signature, error) { 92 return signRFC6979((*ecdsa.PrivateKey)(p), hash) 93 } 94 95 // Serialize returns the private key number d as a big-endian binary-encoded 96 // number, padded to a length of 32 bytes. 97 func (p PrivateKey) Serialize() []byte { 98 b := make([]byte, 0, PrivKeyBytesLen) 99 return paddedAppend(PrivKeyBytesLen, b, p.ToECDSA().D.Bytes()) 100 } 101 102 // GetD satisfies the chainec PrivateKey interface. 103 func (p PrivateKey) GetD() *big.Int { 104 return p.D 105 }