2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-20 13:34:28 +00:00
|
|
|
"crypto/ecdsa"
|
2019-08-27 13:29:42 +00:00
|
|
|
"crypto/elliptic"
|
2019-02-20 13:34:28 +00:00
|
|
|
"crypto/x509"
|
2018-03-25 10:45:54 +00:00
|
|
|
"encoding/hex"
|
2020-03-18 14:11:19 +00:00
|
|
|
"encoding/json"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2019-09-04 21:12:39 +00:00
|
|
|
"fmt"
|
2018-03-21 16:11:04 +00:00
|
|
|
"math/big"
|
2019-01-28 12:03:02 +00:00
|
|
|
|
2020-07-13 09:59:41 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-04-14 14:24:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-03-21 16:11:04 +00:00
|
|
|
)
|
|
|
|
|
2020-06-10 16:14:13 +00:00
|
|
|
// coordLen is the number of bytes in serialized X or Y coordinate.
|
|
|
|
const coordLen = 32
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
// PublicKeys is a list of public keys.
|
|
|
|
type PublicKeys []*PublicKey
|
|
|
|
|
|
|
|
func (keys PublicKeys) Len() int { return len(keys) }
|
|
|
|
func (keys PublicKeys) Swap(i, j int) { keys[i], keys[j] = keys[j], keys[i] }
|
|
|
|
func (keys PublicKeys) Less(i, j int) bool {
|
2020-02-12 15:08:33 +00:00
|
|
|
return keys[i].Cmp(keys[j]) == -1
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 15:25:28 +00:00
|
|
|
// DecodeBytes decodes a PublicKeys from the given slice of bytes.
|
2020-02-12 11:55:19 +00:00
|
|
|
func (keys *PublicKeys) DecodeBytes(data []byte) error {
|
2019-11-11 15:25:28 +00:00
|
|
|
b := io.NewBinReaderFromBuf(data)
|
|
|
|
b.ReadArray(keys)
|
|
|
|
return b.Err
|
|
|
|
}
|
|
|
|
|
2020-04-26 10:42:05 +00:00
|
|
|
// Bytes encodes PublicKeys to the new slice of bytes.
|
|
|
|
func (keys *PublicKeys) Bytes() []byte {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
buf.WriteArray(*keys)
|
|
|
|
if buf.Err != nil {
|
|
|
|
panic(buf.Err)
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:25:28 +00:00
|
|
|
// Contains checks whether passed param contained in PublicKeys.
|
|
|
|
func (keys PublicKeys) Contains(pKey *PublicKey) bool {
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.Equal(pKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-29 07:44:31 +00:00
|
|
|
// Copy returns copy of keys.
|
|
|
|
func (keys PublicKeys) Copy() PublicKeys {
|
|
|
|
res := make(PublicKeys, len(keys))
|
|
|
|
copy(res, keys)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:25:28 +00:00
|
|
|
// Unique returns set of public keys.
|
|
|
|
func (keys PublicKeys) Unique() PublicKeys {
|
|
|
|
unique := PublicKeys{}
|
|
|
|
for _, publicKey := range keys {
|
|
|
|
if !unique.Contains(publicKey) {
|
|
|
|
unique = append(unique, publicKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unique
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
// PublicKey represents a public key and provides a high level
|
2020-07-14 05:40:39 +00:00
|
|
|
// API around ecdsa.PublicKey.
|
|
|
|
type PublicKey ecdsa.PublicKey
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2019-11-21 12:41:14 +00:00
|
|
|
// Equal returns true in case public keys are equal.
|
2019-11-11 15:25:28 +00:00
|
|
|
func (p *PublicKey) Equal(key *PublicKey) bool {
|
|
|
|
return p.X.Cmp(key.X) == 0 && p.Y.Cmp(key.Y) == 0
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:08:33 +00:00
|
|
|
// Cmp compares two keys.
|
|
|
|
func (p *PublicKey) Cmp(key *PublicKey) int {
|
|
|
|
xCmp := p.X.Cmp(key.X)
|
|
|
|
if xCmp != 0 {
|
|
|
|
return xCmp
|
|
|
|
}
|
|
|
|
return p.Y.Cmp(key.Y)
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// NewPublicKeyFromString returns a public key created from the
|
2018-03-25 10:45:54 +00:00
|
|
|
// given hex string.
|
|
|
|
func NewPublicKeyFromString(s string) (*PublicKey, error) {
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 09:59:41 +00:00
|
|
|
return NewPublicKeyFromBytes(b, elliptic.P256())
|
2020-04-07 07:55:56 +00:00
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2020-07-13 09:59:41 +00:00
|
|
|
// NewPublicKeyFromBytes returns public key created from b using given EC.
|
|
|
|
func NewPublicKeyFromBytes(b []byte, curve elliptic.Curve) (*PublicKey, error) {
|
2019-02-19 18:37:35 +00:00
|
|
|
pubKey := new(PublicKey)
|
2020-07-13 09:59:41 +00:00
|
|
|
pubKey.Curve = curve
|
2020-04-07 07:55:56 +00:00
|
|
|
if err := pubKey.DecodeBytes(b); err != nil {
|
|
|
|
return nil, err
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
return pubKey, nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:14:13 +00:00
|
|
|
// getBytes serializes X and Y using compressed or uncompressed format.
|
|
|
|
func (p *PublicKey) getBytes(compressed bool) []byte {
|
2019-10-10 15:40:16 +00:00
|
|
|
if p.IsInfinity() {
|
2018-03-25 10:45:54 +00:00
|
|
|
return []byte{0x00}
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:14:13 +00:00
|
|
|
var resLen = 1 + coordLen
|
|
|
|
if !compressed {
|
|
|
|
resLen += coordLen
|
|
|
|
}
|
|
|
|
var res = make([]byte, resLen)
|
|
|
|
var prefix byte
|
|
|
|
|
|
|
|
xBytes := p.X.Bytes()
|
|
|
|
copy(res[1+coordLen-len(xBytes):], xBytes)
|
|
|
|
if compressed {
|
|
|
|
if p.Y.Bit(0) == 0 {
|
|
|
|
prefix = 0x02
|
|
|
|
} else {
|
|
|
|
prefix = 0x03
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prefix = 0x04
|
|
|
|
yBytes := p.Y.Bytes()
|
|
|
|
copy(res[1+coordLen+coordLen-len(yBytes):], yBytes)
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
}
|
2020-06-10 16:14:13 +00:00
|
|
|
res[0] = prefix
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns byte array representation of the public key in compressed
|
|
|
|
// form (33 bytes with 0x02 or 0x03 prefix, except infinity which is always 0).
|
|
|
|
func (p *PublicKey) Bytes() []byte {
|
|
|
|
return p.getBytes(true)
|
|
|
|
}
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2020-06-10 16:14:13 +00:00
|
|
|
// UncompressedBytes returns byte array representation of the public key in
|
|
|
|
// uncompressed form (65 bytes with 0x04 prefix, except infinity which is
|
|
|
|
// always 0).
|
|
|
|
func (p *PublicKey) UncompressedBytes() []byte {
|
|
|
|
return p.getBytes(false)
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 13:45:30 +00:00
|
|
|
// NewPublicKeyFromASN1 returns a NEO PublicKey from the ASN.1 serialized key.
|
|
|
|
func NewPublicKeyFromASN1(data []byte) (*PublicKey, error) {
|
2019-02-20 13:34:28 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
pubkey interface{}
|
|
|
|
)
|
|
|
|
if pubkey, err = x509.ParsePKIXPublicKey(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pk, ok := pubkey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("given bytes aren't ECDSA public key")
|
|
|
|
}
|
2020-07-14 05:40:39 +00:00
|
|
|
result := PublicKey(*pk)
|
|
|
|
return &result, nil
|
2019-02-20 13:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// decodeCompressedY performs decompression of Y coordinate for given X and Y's least significant bit.
|
2020-07-13 09:59:41 +00:00
|
|
|
// We use here a short-form Weierstrass curve (https://www.hyperelliptic.org/EFD/g1p/auto-shortw.html)
|
|
|
|
// y² = x³ + ax + b. Two types of elliptic curves are supported:
|
|
|
|
// 1. Secp256k1 (Koblitz curve): y² = x³ + b,
|
|
|
|
// 2. Secp256r1 (Random curve): y² = x³ - 3x + b.
|
|
|
|
// To decode compressed curve point we perform the following operation: y = sqrt(x³ + ax + b mod p)
|
|
|
|
// where `p` denotes the order of the underlying curve field
|
2020-07-14 05:40:39 +00:00
|
|
|
func decodeCompressedY(x *big.Int, ylsb uint, curve elliptic.Curve) (*big.Int, error) {
|
2020-07-13 09:59:41 +00:00
|
|
|
var a *big.Int
|
|
|
|
switch curve.(type) {
|
|
|
|
case *btcec.KoblitzCurve:
|
|
|
|
a = big.NewInt(0)
|
|
|
|
default:
|
|
|
|
a = big.NewInt(3)
|
|
|
|
}
|
|
|
|
cp := curve.Params()
|
|
|
|
xCubed := new(big.Int).Exp(x, big.NewInt(3), cp.P)
|
|
|
|
aX := new(big.Int).Mul(x, a)
|
|
|
|
aX.Mod(aX, cp.P)
|
|
|
|
ySquared := new(big.Int).Sub(xCubed, aX)
|
2019-09-04 21:12:39 +00:00
|
|
|
ySquared.Add(ySquared, cp.B)
|
|
|
|
ySquared.Mod(ySquared, cp.P)
|
|
|
|
y := new(big.Int).ModSqrt(ySquared, cp.P)
|
|
|
|
if y == nil {
|
|
|
|
return nil, errors.New("error computing Y for compressed point")
|
|
|
|
}
|
|
|
|
if y.Bit(0) != ylsb {
|
|
|
|
y.Neg(y)
|
|
|
|
y.Mod(y, cp.P)
|
|
|
|
}
|
|
|
|
return y, nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 12:03:02 +00:00
|
|
|
// DecodeBytes decodes a PublicKey from the given slice of bytes.
|
|
|
|
func (p *PublicKey) DecodeBytes(data []byte) error {
|
2019-09-16 09:18:13 +00:00
|
|
|
b := io.NewBinReaderFromBuf(data)
|
2019-09-16 16:31:49 +00:00
|
|
|
p.DecodeBinary(b)
|
|
|
|
return b.Err
|
2019-01-28 12:03:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 05:40:39 +00:00
|
|
|
// DecodeBinary decodes a PublicKey from the given BinReader using information
|
|
|
|
// about the EC curve to decompress Y point. Secp256r1 is a default value for EC curve.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (p *PublicKey) DecodeBinary(r *io.BinReader) {
|
2019-09-05 08:58:34 +00:00
|
|
|
var prefix uint8
|
|
|
|
var x, y *big.Int
|
|
|
|
var err error
|
2019-01-28 12:03:02 +00:00
|
|
|
|
2019-12-12 17:17:50 +00:00
|
|
|
prefix = uint8(r.ReadB())
|
2019-09-16 09:18:13 +00:00
|
|
|
if r.Err != nil {
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 05:40:39 +00:00
|
|
|
if p.Curve == nil {
|
|
|
|
p.Curve = elliptic.P256()
|
|
|
|
}
|
|
|
|
curve := p.Curve
|
|
|
|
curveParams := p.Params()
|
2018-03-25 10:45:54 +00:00
|
|
|
// Infinity
|
2019-01-28 12:03:02 +00:00
|
|
|
switch prefix {
|
|
|
|
case 0x00:
|
2019-09-05 08:58:34 +00:00
|
|
|
// noop, initialized to nil
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2019-01-28 12:03:02 +00:00
|
|
|
case 0x02, 0x03:
|
2019-09-05 08:58:34 +00:00
|
|
|
// Compressed public keys
|
|
|
|
xbytes := make([]byte, 32)
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(xbytes)
|
2019-09-16 09:18:13 +00:00
|
|
|
if r.Err != nil {
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2019-09-05 08:58:34 +00:00
|
|
|
}
|
|
|
|
x = new(big.Int).SetBytes(xbytes)
|
2019-09-09 08:23:27 +00:00
|
|
|
ylsb := uint(prefix & 0x1)
|
2020-07-14 05:40:39 +00:00
|
|
|
y, err = decodeCompressedY(x, ylsb, curve)
|
2019-09-05 08:58:34 +00:00
|
|
|
if err != nil {
|
2019-12-25 14:44:30 +00:00
|
|
|
r.Err = err
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2019-09-05 08:58:34 +00:00
|
|
|
}
|
2019-01-28 12:03:02 +00:00
|
|
|
case 0x04:
|
2019-09-05 08:58:34 +00:00
|
|
|
xbytes := make([]byte, 32)
|
|
|
|
ybytes := make([]byte, 32)
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(xbytes)
|
|
|
|
r.ReadBytes(ybytes)
|
2019-09-16 09:18:13 +00:00
|
|
|
if r.Err != nil {
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2019-09-05 08:58:34 +00:00
|
|
|
}
|
|
|
|
x = new(big.Int).SetBytes(xbytes)
|
|
|
|
y = new(big.Int).SetBytes(ybytes)
|
2020-07-14 05:40:39 +00:00
|
|
|
if !curve.IsOnCurve(x, y) {
|
2019-12-25 15:00:25 +00:00
|
|
|
r.Err = errors.New("encoded point is not on the P256 curve")
|
|
|
|
return
|
|
|
|
}
|
2019-01-28 12:03:02 +00:00
|
|
|
default:
|
2020-08-06 14:44:08 +00:00
|
|
|
r.Err = fmt.Errorf("invalid prefix %d", prefix)
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2020-07-14 05:40:39 +00:00
|
|
|
if x.Cmp(curveParams.P) >= 0 || y.Cmp(curveParams.P) >= 0 {
|
2019-09-16 16:31:49 +00:00
|
|
|
r.Err = errors.New("enccoded point is not correct (X or Y is bigger than P")
|
|
|
|
return
|
2019-09-05 09:31:03 +00:00
|
|
|
}
|
2019-09-05 08:58:34 +00:00
|
|
|
p.X, p.Y = x, y
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
// EncodeBinary encodes a PublicKey to the given BinWriter.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (p *PublicKey) EncodeBinary(w *io.BinWriter) {
|
2019-11-22 10:34:06 +00:00
|
|
|
w.WriteBytes(p.Bytes())
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
2019-02-19 18:37:35 +00:00
|
|
|
|
2019-12-03 14:25:25 +00:00
|
|
|
// GetVerificationScript returns NEO VM bytecode with CHECKSIG command for the
|
|
|
|
// public key.
|
|
|
|
func (p *PublicKey) GetVerificationScript() []byte {
|
2019-02-19 18:37:35 +00:00
|
|
|
b := p.Bytes()
|
2020-04-14 14:24:21 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2020-06-26 08:07:27 +00:00
|
|
|
if address.Prefix == address.NEO2Prefix {
|
|
|
|
buf.WriteB(0x21) // PUSHBYTES33
|
|
|
|
buf.WriteBytes(p.Bytes())
|
|
|
|
buf.WriteB(0xAC) // CHECKSIG
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
2020-04-21 13:45:48 +00:00
|
|
|
emit.Bytes(buf.BinWriter, b)
|
2020-04-14 14:24:21 +00:00
|
|
|
emit.Opcode(buf.BinWriter, opcode.PUSHNULL)
|
2020-07-10 06:56:15 +00:00
|
|
|
emit.Syscall(buf.BinWriter, "Neo.Crypto.VerifyWithECDsaSecp256r1")
|
2019-12-03 14:25:25 +00:00
|
|
|
|
2020-04-14 14:24:21 +00:00
|
|
|
return buf.Bytes()
|
2019-12-03 14:25:25 +00:00
|
|
|
}
|
2019-02-19 18:37:35 +00:00
|
|
|
|
2020-02-19 09:10:36 +00:00
|
|
|
// GetScriptHash returns a Hash160 of verification script for the key.
|
|
|
|
func (p *PublicKey) GetScriptHash() util.Uint160 {
|
|
|
|
return hash.Hash160(p.GetVerificationScript())
|
2019-02-19 18:37:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Address returns a base58-encoded NEO-specific address based on the key hash.
|
2019-08-27 14:16:33 +00:00
|
|
|
func (p *PublicKey) Address() string {
|
2020-02-19 09:10:36 +00:00
|
|
|
return address.Uint160ToString(p.GetScriptHash())
|
2019-02-19 18:37:35 +00:00
|
|
|
}
|
2019-08-27 13:29:42 +00:00
|
|
|
|
|
|
|
// Verify returns true if the signature is valid and corresponds
|
2019-10-22 14:56:03 +00:00
|
|
|
// to the hash and public key.
|
2019-08-27 13:29:42 +00:00
|
|
|
func (p *PublicKey) Verify(signature []byte, hash []byte) bool {
|
|
|
|
if p.X == nil || p.Y == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
rBytes := new(big.Int).SetBytes(signature[0:32])
|
|
|
|
sBytes := new(big.Int).SetBytes(signature[32:64])
|
2020-07-14 05:40:39 +00:00
|
|
|
pk := ecdsa.PublicKey(*p)
|
|
|
|
return ecdsa.Verify(&pk, hash, rBytes, sBytes)
|
2019-08-27 13:29:42 +00:00
|
|
|
}
|
2019-09-04 21:12:39 +00:00
|
|
|
|
2019-10-10 15:40:16 +00:00
|
|
|
// IsInfinity checks if the key is infinite (null, basically).
|
|
|
|
func (p *PublicKey) IsInfinity() bool {
|
2019-09-04 21:12:39 +00:00
|
|
|
return p.X == nil && p.Y == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the Stringer interface.
|
|
|
|
func (p *PublicKey) String() string {
|
2019-10-10 15:40:16 +00:00
|
|
|
if p.IsInfinity() {
|
2019-09-04 21:12:39 +00:00
|
|
|
return "00"
|
|
|
|
}
|
|
|
|
bx := hex.EncodeToString(p.X.Bytes())
|
|
|
|
by := hex.EncodeToString(p.Y.Bytes())
|
|
|
|
return fmt.Sprintf("%s%s", bx, by)
|
|
|
|
}
|
2020-03-18 14:11:19 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (p PublicKey) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(hex.EncodeToString(p.Bytes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (p *PublicKey) UnmarshalJSON(data []byte) error {
|
|
|
|
l := len(data)
|
|
|
|
if l < 2 || data[0] != '"' || data[l-1] != '"' {
|
|
|
|
return errors.New("wrong format")
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes := make([]byte, l-2)
|
|
|
|
_, err := hex.Decode(bytes, data[1:l-1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.DecodeBytes(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|