crypto

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package crypto implements asymmetric (public/private key) cryptographic signing and verification for Kwil.

This package is based on the go-libp2p crypto package. This uses the pure Go dcrec module for secp256k1 keys and ecdsa signing and verification, and the standard library's ed25519 package for Ed25519 keys and signing.

Index

Constants

View Source
const RecoveryIDOffset = 64

RecoveryIDOffset points to the byte offset within the signature that contains the recovery id.

View Source
const (
	// ReservedKeyIDs is the range of key types that are reserved for internal purposes.
	ReservedKeyIDs = 1 << 16 // 65536
)
View Source
const Secp256k1SignatureLength = 64 + 1 // 64 bytes ECDSA signature + 1 byte recovery id

SignatureLength indicates the byte length required to carry a signature with recovery id.

Variables

View Source
var (
	ErrInvalidSignature = errors.New("invalid signature")
)

Functions

func EthereumAddressFromPubKey added in v0.4.0

func EthereumAddressFromPubKey(pubKey *Secp256k1PublicKey) []byte

func GenerateEd25519Key

func GenerateEd25519Key(src io.Reader) (PrivateKey, PublicKey, error)

GenerateEd25519Key generates a new ed25519 private and public key pair. The returned keys may be cast to *Ed25519PrivateKey and *Ed25519PublicKey.

func GenerateSecp256k1Key

func GenerateSecp256k1Key(src io.Reader) (PrivateKey, PublicKey, error)

GenerateSecp256k1Key generates a new Secp256k1 private and public key pair. If the provided io.Reader is nil, crypto/rand.Reader is used. The returned keys may be cast to *Secp256k1PrivateKey and *Secp256k1PublicKey.

func KeyEquals added in v0.4.0

func KeyEquals(k1, k2 Key) bool

KeyEquals checks whether two Keys are equivalent (same type and bytes).

func RegisterKeyType added in v0.4.0

func RegisterKeyType(kd KeyDefinition) error

RegisterKeyType registers a new keyType. The KeyType and its string should be unique and the KeyType value must be greater than ReservedKeyTypes.

func WireEncodeKey added in v0.4.0

func WireEncodeKey(key Key) []byte

func WireEncodeKeyType added in v0.4.0

func WireEncodeKeyType(kt KeyType) []byte

Types

type Ed25519Definition added in v0.4.0

type Ed25519Definition struct{}

func (Ed25519Definition) EncodeFlag added in v0.4.0

func (Ed25519Definition) EncodeFlag() uint32

func (Ed25519Definition) Generate added in v0.4.0

func (Ed25519Definition) Generate() PrivateKey

func (Ed25519Definition) Type added in v0.4.0

func (Ed25519Definition) Type() KeyType

func (Ed25519Definition) UnmarshalPrivateKey added in v0.4.0

func (Ed25519Definition) UnmarshalPrivateKey(b []byte) (PrivateKey, error)

func (Ed25519Definition) UnmarshalPublicKey added in v0.4.0

func (Ed25519Definition) UnmarshalPublicKey(b []byte) (PublicKey, error)

type Ed25519PrivateKey

type Ed25519PrivateKey struct {
	// contains filtered or unexported fields
}

Ed25519PrivateKey is an ed25519 private key.

func UnmarshalEd25519PrivateKey added in v0.4.0

func UnmarshalEd25519PrivateKey(data []byte) (*Ed25519PrivateKey, error)

UnmarshalEd25519PrivateKey returns a private key from input bytes.

func (*Ed25519PrivateKey) Bytes

func (k *Ed25519PrivateKey) Bytes() []byte

Bytes private key bytes.

func (*Ed25519PrivateKey) Equals added in v0.4.0

func (k *Ed25519PrivateKey) Equals(o Key) bool

Equals compares two ed25519 private keys.

func (*Ed25519PrivateKey) Public added in v0.4.0

func (k *Ed25519PrivateKey) Public() PublicKey

Public returns an ed25519 public key from a private key.

func (*Ed25519PrivateKey) Sign

func (k *Ed25519PrivateKey) Sign(msg []byte) (res []byte, err error)

Sign returns a signature from an input message.

func (*Ed25519PrivateKey) Type added in v0.4.0

func (k *Ed25519PrivateKey) Type() KeyType

Type of the private key (Ed25519).

type Ed25519PublicKey

type Ed25519PublicKey struct {
	// contains filtered or unexported fields
}

Ed25519PublicKey is an ed25519 public key.

func UnmarshalEd25519PublicKey added in v0.4.0

func UnmarshalEd25519PublicKey(data []byte) (*Ed25519PublicKey, error)

UnmarshalEd25519PublicKey returns a public key from input bytes.

func (*Ed25519PublicKey) Bytes

func (k *Ed25519PublicKey) Bytes() []byte

Bytes public key bytes.

func (*Ed25519PublicKey) Equals added in v0.4.0

func (k *Ed25519PublicKey) Equals(o Key) bool

Equals compares two ed25519 public keys.

func (*Ed25519PublicKey) Type added in v0.4.0

func (k *Ed25519PublicKey) Type() KeyType

Type of the public key (Ed25519).

func (*Ed25519PublicKey) Verify

func (k *Ed25519PublicKey) Verify(data []byte, sig []byte) (success bool, err error)

Verify checks a signature against the input data.

type Key added in v0.4.0

type Key interface {
	// Equals checks whether two Keys are the same.
	Equals(Key) bool

	// Bytes returns the raw bytes of the key.
	Bytes() []byte

	// Type returns the key type.
	Type() KeyType
}

Key represents a public or private key that can be serialized and compared to another key. The Type method will indicate the type of key used. See KeyType for the supported key types.

type KeyDefinition added in v0.4.0

type KeyDefinition interface {
	Type() KeyType      // name of key type e.g. "secp256k1"
	EncodeFlag() uint32 // prefix for compact unique binary encoding

	UnmarshalPrivateKey(b []byte) (PrivateKey, error)
	UnmarshalPublicKey(b []byte) (PublicKey, error)

	Generate() PrivateKey
}

func KeyTypeDefinition added in v0.4.0

func KeyTypeDefinition(kt KeyType) (KeyDefinition, bool)

type KeyType added in v0.4.0

type KeyType string

KeyType is the type of key, which may be public or private depending on context.

const (
	KeyTypeSecp256k1 KeyType = "secp256k1"
	KeyTypeEd25519   KeyType = "ed25519"
)

The native key types are secp256k1 and ed25519.

func ParseKeyType added in v0.4.0

func ParseKeyType(s string) (KeyType, error)

ParseKeyType parses a string into a KeyType. This ensures that the string has a registered key definition.

func ParseKeyTypeID added in v0.4.0

func ParseKeyTypeID(id uint32) (KeyType, error)

func WireDecodeKeyType added in v0.4.0

func WireDecodeKeyType(b []byte) (KeyType, []byte, error)

func (KeyType) Bytes added in v0.4.0

func (kt KeyType) Bytes() []byte

func (KeyType) MarshalBinary added in v0.4.0

func (kt KeyType) MarshalBinary() ([]byte, error)

func (*KeyType) ReadFrom added in v0.4.0

func (kt *KeyType) ReadFrom(r io.Reader) (int64, error)

func (KeyType) String added in v0.4.0

func (kt KeyType) String() string

func (*KeyType) UnmarshalBinary added in v0.4.0

func (kt *KeyType) UnmarshalBinary(data []byte) error

func (KeyType) WriteTo added in v0.4.0

func (kt KeyType) WriteTo(w io.Writer) (int64, error)

type PrivateKey added in v0.4.0

type PrivateKey interface {
	Key

	// Sign cryptographically signs the given bytes.
	Sign([]byte) ([]byte, error)

	// Public returns the public key associated with this private key.
	Public() PublicKey
}

PrivateKey represents a private key that can be used to sign data.

func GeneratePrivateKey added in v0.4.0

func GeneratePrivateKey(kt KeyType) (PrivateKey, error)

func UnmarshalPrivateKey added in v0.4.0

func UnmarshalPrivateKey(b []byte, kt KeyType) (PrivateKey, error)

func WireDecodePrivateKey added in v0.4.0

func WireDecodePrivateKey(b []byte) (PrivateKey, error)

type PublicKey added in v0.4.0

type PublicKey interface {
	Key

	// Verify that 'sig' is the signed hash of 'data'
	Verify(data []byte, sig []byte) (bool, error)
}

PublicKey is a public key, which can verify signatures.

func UnmarshalPublicKey added in v0.4.0

func UnmarshalPublicKey(b []byte, kt KeyType) (PublicKey, error)

func WireDecodePubKey added in v0.4.0

func WireDecodePubKey(b []byte) (PublicKey, error)

type Secp256k1Definition added in v0.4.0

type Secp256k1Definition struct{}

func (Secp256k1Definition) EncodeFlag added in v0.4.0

func (Secp256k1Definition) EncodeFlag() uint32

func (Secp256k1Definition) Generate added in v0.4.0

func (Secp256k1Definition) Generate() PrivateKey

func (Secp256k1Definition) String added in v0.4.0

func (Secp256k1Definition) String() string

func (Secp256k1Definition) Type added in v0.4.0

func (Secp256k1Definition) UnmarshalPrivateKey added in v0.4.0

func (Secp256k1Definition) UnmarshalPrivateKey(b []byte) (PrivateKey, error)

func (Secp256k1Definition) UnmarshalPublicKey added in v0.4.0

func (Secp256k1Definition) UnmarshalPublicKey(b []byte) (PublicKey, error)

type Secp256k1PrivateKey

type Secp256k1PrivateKey secp256k1.PrivateKey

Secp256k1PrivateKey is a Secp256k1 private key.

func Secp256k1PrivateKeyFromHex

func Secp256k1PrivateKeyFromHex(hexStr string) (k *Secp256k1PrivateKey, err error)

Secp256k1PrivateKeyFromHex returns a private key from a hex string.

func UnmarshalSecp256k1PrivateKey added in v0.4.0

func UnmarshalSecp256k1PrivateKey(data []byte) (k *Secp256k1PrivateKey, err error)

UnmarshalSecp256k1PrivateKey returns a private key from the key's raw bytes.

func (*Secp256k1PrivateKey) Bytes

func (k *Secp256k1PrivateKey) Bytes() []byte

Bytes returns the raw bytes of the key. To serialize for the wire or disk, use WireEncodePrivateKey to maintain the key type.

func (*Secp256k1PrivateKey) Equals added in v0.4.0

func (k *Secp256k1PrivateKey) Equals(o Key) bool

Equals compares two private keys. This accepts a Key to satisfy the PrivateKey interface.

func (*Secp256k1PrivateKey) Public added in v0.4.0

func (k *Secp256k1PrivateKey) Public() PublicKey

Public returns a public key. This is a Secp256k1PublicKey as a PublicKey to satisfy the PrivateKey interface.

func (*Secp256k1PrivateKey) Sign

func (k *Secp256k1PrivateKey) Sign(data []byte) (rawSig []byte, err error)

Sign returns a signature (uncompressed) from input data. The signature is of the sha256 hash of the data, not data itself. This is to match the other key types that internally use a hash function, unlike secp256k1, which does not.

func (*Secp256k1PrivateKey) SignRaw added in v0.4.0

func (k *Secp256k1PrivateKey) SignRaw(data []byte) (rawSig []byte, err error)

SignRaw is like Sign, but it does not internally hash the data. This is to support the Ethereum "personal sign" method.

func (*Secp256k1PrivateKey) Type added in v0.4.0

func (k *Secp256k1PrivateKey) Type() KeyType

Type returns the private key type.

type Secp256k1PublicKey

type Secp256k1PublicKey secp256k1.PublicKey

Secp256k1PublicKey is a Secp256k1 public key.

func RecoverSecp256k1Key added in v0.4.0

func RecoverSecp256k1Key(msg, sig []byte) (*Secp256k1PublicKey, error)

RecoverSecp256k1Key recovers a secp256k1 public key from a signature and signed message, which is hashed with sha256 internally.

func RecoverSecp256k1KeyFromSigHash added in v0.4.0

func RecoverSecp256k1KeyFromSigHash(hash, sig []byte) (*Secp256k1PublicKey, error)

RecoverSecp256k1KeyFromSigHash is like RecoverSecp256k1Key, except the input is the sighash, which was directly signed. This is the more proper implementation of secp256k1 pubkey recovery since a specific hash function is not associated with secp256k1 or ecdsa.

func UnmarshalSecp256k1PublicKey added in v0.4.0

func UnmarshalSecp256k1PublicKey(data []byte) (k *Secp256k1PublicKey, err error)

UnmarshalSecp256k1PublicKey returns a public key from the key's raw bytes.

func (*Secp256k1PublicKey) Bytes

func (k *Secp256k1PublicKey) Bytes() []byte

Bytes returns the bytes of the key.

func (*Secp256k1PublicKey) BytesUncompressed added in v0.4.0

func (k *Secp256k1PublicKey) BytesUncompressed() []byte

BytesUncompressed returns the bytes of the key.

func (*Secp256k1PublicKey) Equals added in v0.4.0

func (k *Secp256k1PublicKey) Equals(o Key) bool

Equals compares two public keys. This accepts a Key to satisfy the PublicKey interface.

func (*Secp256k1PublicKey) Type added in v0.4.0

func (k *Secp256k1PublicKey) Type() KeyType

Type returns the public key type.

func (*Secp256k1PublicKey) Verify

func (k *Secp256k1PublicKey) Verify(data, rawSig []byte) (success bool, err error)

Verify compares a signature against the input data. The data is hashed with sha256 internally.

func (*Secp256k1PublicKey) VerifyRaw added in v0.4.0

func (k *Secp256k1PublicKey) VerifyRaw(data, rawSig []byte) (success bool, err error)

VerifyRaw is like Verify, but does not hash the data.

Directories

Path Synopsis
Package auth provides the standard signing and verification methods used in Kwil.
Package auth provides the standard signing and verification methods used in Kwil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL