Documentation
¶
Overview ¶
Package secp256k1 wraps the bitcoin secp256k1 C library.
Index ¶
- Constants
- Variables
- func CompressPubkey(x, y *big.Int) []byte
- func DecompressPubkey(pubkey []byte) (x, y *big.Int)
- func Keccak256(data ...[]byte) []byte
- func PaddedBigBytes(bigint *big.Int, n int) []byte
- func PubkeyBytesToAddress(pubkey []byte) []byte
- func PubkeyToAddress(p ecdsa.PublicKey) common.Address
- func RecoverPubkey(msg []byte, sig []byte) ([]byte, error)
- func Sign(msg []byte, seckey []byte) ([]byte, error)
- func VerifySignature(pubkey, msg, signature []byte) bool
- type BitCurve
- func (bitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)
- func (bitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int)
- func (bitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool
- func (bitCurve *BitCurve) Marshal(x, y *big.Int) []byte
- func (bitCurve *BitCurve) Params() *elliptic.CurveParams
- func (bitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int)
- func (bitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int)
- func (bitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int)
- type PrivateKey
- func (k *PrivateKey) Address() ids.ShortID
- func (k *PrivateKey) Bytes() []byte
- func (k *PrivateKey) MarshalText() ([]byte, error)
- func (k *PrivateKey) PublicKey() *PublicKey
- func (k *PrivateKey) Sign(msg []byte) ([]byte, error)
- func (k *PrivateKey) SignArray(msg []byte) ([SignatureLen]byte, error)
- func (k *PrivateKey) SignHash(hash []byte) ([]byte, error)
- func (k *PrivateKey) SignHashArray(hash []byte) ([SignatureLen]byte, error)
- func (k *PrivateKey) String() string
- func (k *PrivateKey) ToECDSA() *ecdsa.PrivateKey
- func (k *PrivateKey) UnmarshalJSON(data []byte) error
- func (k *PrivateKey) UnmarshalText(text []byte) error
- type PublicKey
- func (k *PublicKey) Address() ids.ShortID
- func (k *PublicKey) Bytes() []byte
- func (k *PublicKey) CompressedBytes() []byte
- func (k *PublicKey) String() string
- func (k *PublicKey) ToECDSA() *ecdsa.PublicKey
- func (k *PublicKey) Verify(msg, sig []byte) bool
- func (k *PublicKey) VerifyHash(hash, sig []byte) bool
- type RecoverCacheType
Constants ¶
const ( // SignatureLen is the number of bytes in a secp256k1 recoverable signature SignatureLen = 65 // PrivateKeyLen is the number of bytes in a secp256k1 private key PrivateKeyLen = 32 // PublicKeyLen is the number of bytes in a secp256k1 public key PublicKeyLen = 33 PrivateKeyPrefix = "PrivateKey-" )
Variables ¶
var ( ErrInvalidMsgLen = errors.New("invalid message length, need 32 bytes") ErrInvalidSignatureLen = errors.New("invalid signature length") ErrInvalidRecoveryID = errors.New("invalid signature recovery id") ErrInvalidKey = errors.New("invalid private key") ErrInvalidPubkey = errors.New("invalid public key") ErrSignFailed = errors.New("signing failed") ErrRecoverFailed = errors.New("recovery failed") )
var (
ErrInvalidSig = errors.New("invalid signature")
)
var RecoverCache = lru.NewCache[string, *PublicKey](2048)
RecoverCache is a cache for recovered public keys
Functions ¶
func CompressPubkey ¶
CompressPubkey encodes a public key to 33-byte compressed format.
func DecompressPubkey ¶
DecompressPubkey parses a public key in the 33-byte compressed format. It returns non-nil coordinates if the public key is valid.
func Keccak256 ¶ added in v1.1.2
Keccak256 calculates and returns the Keccak256 hash of the input data.
func PaddedBigBytes ¶ added in v1.1.2
PaddedBigBytes encodes a big integer as a big-endian byte slice. The byte slice is padded with zeros.
func PubkeyBytesToAddress ¶ added in v1.1.2
PubkeyBytesToAddress converts public key bytes to an address using SHA256 + RIPEMD160
func PubkeyToAddress ¶ added in v1.1.2
PubkeyToAddress returns the Ethereum address for the given public key
func RecoverPubkey ¶
RecoverPubkey returns the public key of the signer. msg must be the 32-byte hash of the message to be signed. sig must be a 65-byte compact ECDSA signature containing the recovery id as the last element.
func Sign ¶
Sign creates a recoverable ECDSA signature. The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1.
The caller is responsible for ensuring that msg cannot be chosen directly by an attacker. It is usually preferable to use a cryptographic hash function on any input before handing it to this function.
func VerifySignature ¶
VerifySignature checks that the given pubkey created signature over message. The signature should be in [R || S] format.
Types ¶
type BitCurve ¶
type BitCurve struct {
P *big.Int // the order of the underlying field
N *big.Int // the order of the base point
B *big.Int // the constant of the BitCurve equation
Gx, Gy *big.Int // (x,y) of the base point
BitSize int // the size of the underlying field
}
A BitCurve represents a Koblitz Curve with a=0. See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html
func (*BitCurve) Marshal ¶
Marshal converts a point into the form specified in section 4.3.6 of ANSI X9.62.
func (*BitCurve) Params ¶
func (bitCurve *BitCurve) Params() *elliptic.CurveParams
func (*BitCurve) ScalarBaseMult ¶
ScalarBaseMult returns k*G, where G is the base point of the group and k is an integer in big-endian form.
func (*BitCurve) ScalarMult ¶
type PrivateKey ¶ added in v1.1.2
type PrivateKey struct {
// contains filtered or unexported fields
}
PrivateKey wraps an ecdsa.PrivateKey
func NewPrivateKey ¶ added in v1.1.2
func NewPrivateKey() (*PrivateKey, error)
NewPrivateKey generates a new private key
func TestKeys ¶ added in v1.1.2
func TestKeys() []*PrivateKey
TestKeys returns a set of test keys for testing purposes
func ToPrivateKey ¶ added in v1.1.2
func ToPrivateKey(b []byte) (*PrivateKey, error)
ToPrivateKey converts bytes to a private key
func (*PrivateKey) Address ¶ added in v1.1.2
func (k *PrivateKey) Address() ids.ShortID
Address returns the address of the private key (via its public key)
func (*PrivateKey) Bytes ¶ added in v1.1.2
func (k *PrivateKey) Bytes() []byte
Bytes returns the private key bytes
func (*PrivateKey) MarshalText ¶ added in v1.1.2
func (k *PrivateKey) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler
func (*PrivateKey) PublicKey ¶ added in v1.1.2
func (k *PrivateKey) PublicKey() *PublicKey
PublicKey returns the public key
func (*PrivateKey) Sign ¶ added in v1.1.2
func (k *PrivateKey) Sign(msg []byte) ([]byte, error)
Sign signs a message with the private key
func (*PrivateKey) SignArray ¶ added in v1.1.2
func (k *PrivateKey) SignArray(msg []byte) ([SignatureLen]byte, error)
SignArray signs a message and returns a fixed-size array
func (*PrivateKey) SignHash ¶ added in v1.1.2
func (k *PrivateKey) SignHash(hash []byte) ([]byte, error)
SignHash signs a hash with the private key
func (*PrivateKey) SignHashArray ¶ added in v1.1.2
func (k *PrivateKey) SignHashArray(hash []byte) ([SignatureLen]byte, error)
SignHashArray signs a hash and returns a fixed-size array
func (*PrivateKey) String ¶ added in v1.1.2
func (k *PrivateKey) String() string
String returns the string representation of the private key
func (*PrivateKey) ToECDSA ¶ added in v1.4.16
func (k *PrivateKey) ToECDSA() *ecdsa.PrivateKey
ToECDSA returns the underlying ecdsa.PrivateKey
func (*PrivateKey) UnmarshalJSON ¶ added in v1.4.18
func (k *PrivateKey) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler It handles JSON-encoded strings by stripping quotes and calling the shared unmarshal logic
func (*PrivateKey) UnmarshalText ¶ added in v1.1.2
func (k *PrivateKey) UnmarshalText(text []byte) error
UnmarshalText implements encoding.TextUnmarshaler It handles direct text unmarshaling without quotes
type PublicKey ¶ added in v1.1.2
type PublicKey struct {
// contains filtered or unexported fields
}
PublicKey wraps an ecdsa.PublicKey
func RecoverPublicKey ¶ added in v1.1.2
RecoverPublicKey recovers the public key from a message and signature
func RecoverPublicKeyFromHash ¶ added in v1.1.2
RecoverPublicKeyFromHash recovers the public key from a hash and signature
func ToPublicKey ¶ added in v1.1.2
ToPublicKey converts bytes to a public key
func (*PublicKey) Address ¶ added in v1.1.2
Address returns the address of the public key as an ids.ShortID
func (*PublicKey) CompressedBytes ¶ added in v1.1.2
CompressedBytes returns the compressed public key bytes (33 bytes)
func (*PublicKey) String ¶ added in v1.1.2
String returns the string representation of the public key
func (*PublicKey) VerifyHash ¶ added in v1.1.2
VerifyHash verifies a signature against a hash
type RecoverCacheType ¶ added in v1.1.2
type RecoverCacheType struct {
// contains filtered or unexported fields
}
RecoverCacheType provides a cache for public key recovery with methods
func NewRecoverCache ¶ added in v1.1.2
func NewRecoverCache(size int) RecoverCacheType
NewRecoverCache creates a new recover cache
func (RecoverCacheType) RecoverPublicKey ¶ added in v1.1.2
func (r RecoverCacheType) RecoverPublicKey(msg, sig []byte) (*PublicKey, error)
RecoverPublicKey recovers a public key from a message and signature
func (RecoverCacheType) RecoverPublicKeyFromHash ¶ added in v1.1.2
func (r RecoverCacheType) RecoverPublicKeyFromHash(hash, sig []byte) (*PublicKey, error)
RecoverPublicKeyFromHash recovers a public key from a hash and signature