cipher

package module
v1.2.205 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: Apache-2.0 Imports: 14 Imported by: 6

README

go-cipher

Documentation

Index

Constants

View Source
const (
	HashLength    = 32
	SECP256K1SHA3 = "secp256k1_sha3"
)

Variables

View Source
var (
	ErrInvalidCurve               = errors.New("invalid elliptic curve")
	ErrSharedKeyIsPointAtInfinity = errors.New("shared key is point at infinity")
	ErrSharedKeyTooBig            = errors.New("shared key params are too big")
)

Functions

func CompressPubkey

func CompressPubkey(pubkey *ecdsa.PublicKey) []byte

CompressPubkey encodes a public key to the 33-byte compressed format.

func DecompressPubkey

func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error)

DecompressPubkey parses a public key in the 33-byte compressed format.

func Ecrecover

func Ecrecover(hash, sig []byte) ([]byte, error)

Ecrecover returns the uncompressed public key that created the given signature.

func MaxSharedKeyLength

func MaxSharedKeyLength(p ECCPublicKey) int

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

func S256

func S256() elliptic.Curve

S256 returns an instance of the secp256k1 curve.

func Sign

func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error)

Sign calculates an ECDSA signature.

This function is susceptible to chosen plaintext attacks that can leak information about the private key that is used for signing. Callers must be aware that the given hash cannot be chosen by an adversery. Common solution is to hash any input before calculating the signature.

The produced signature is in the [R || S || V] format where V is 0 or 1.

func SignDecode

func SignDecode(sign []byte) (r, s *big.Int, err error)

func SignEncode

func SignEncode(r, s *big.Int) []byte

func ValidateSecpSigValues

func ValidateSecpSigValues(v byte, r, s *big.Int) bool

func VerifySignature

func VerifySignature(pubkey, hash, signature []byte) bool

VerifySignature checks that the given public key created signature over hash. The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. The signature should have the 64 byte [R || S] format.

func VrfEval

func VrfEval(priv ECCPrivateKey, m []byte) (index [32]byte, proof []byte)

func VrfProofToHash

func VrfProofToHash(pub ECCPublicKey, m, proof []byte) (index [32]byte, err error)

func VrfVerify

func VrfVerify(pub ECCPublicKey, seed, proof []byte, index [32]byte) bool

Types

type Cipher

type Cipher interface {
	Name() string
	GenerateKey() (priv ECCPrivateKey, err error)
	Sign(priv []byte, hash []byte) (sig []byte, err error)
	Verify(pub []byte, hash []byte, sig []byte) bool
	RecoverPub(hash, sig []byte) ([]byte, error)

	PrivToBytes(priv ECCPrivateKey) []byte
	PubToBytes(pub ECCPublicKey) []byte
	BytesToPriv(d []byte) (ECCPrivateKey, error)
	BytesToPub(pub []byte) (ECCPublicKey, error)
	PubFromNodeId(id []byte) []byte
	PubToNodeIdBytes(pub []byte) ([]byte, error)

	Hasher() hash.Hash

	LengthOfPublicKey() int  // number of bytes of public key
	LengthOfPrivateKey() int // number of bytes of private key
	LengthOfSignature() int  // number of bytes of signature
	LengthOfHash() int       // number of bytes of input Hash
}

func NewCipher

func NewCipher(name string) Cipher

func NewSecpCipher

func NewSecpCipher() Cipher

create a signer of the specified type

type ECCPrivateKey

type ECCPrivateKey interface {
	elliptic.Curve
	crypto.Signer
	GetPublicKey() ECCPublicKey
	ToECDSA() *ecdsa.PrivateKey
	FromECDSA(key *ecdsa.PrivateKey) ECCPrivateKey
	ToBytes() []byte
	FromBytes(priv []byte) (ECCPrivateKey, error)
	VrfEval(m []byte) (index [32]byte, proof []byte)
	GenerateShared(k ECCPublicKey, skLen, macLen int) (sk []byte, err error)
}

type ECCPublicKey

type ECCPublicKey interface {
	elliptic.Curve
	ToECDSA() *ecdsa.PublicKey
	FromECDSA(key *ecdsa.PublicKey) ECCPublicKey
	ToBytes() []byte
	FromBytes(pub []byte) (ECCPublicKey, error)
	ToNodeIDBytes() []byte
	FromBytesNodeID(id []byte) ECCPublicKey
	ToAddress() []byte
	VrfProofToHash(m, proof []byte) (index [32]byte, err error)
	VrfVerify(m, proof []byte, index [32]byte) bool
}

type Hash

type Hash [HashLength]byte

type Secp256k1PrivateKey

type Secp256k1PrivateKey ecdsa.PrivateKey

func (*Secp256k1PrivateKey) ByteSize

func (p *Secp256k1PrivateKey) ByteSize() int

func (*Secp256k1PrivateKey) FromBytes

func (p *Secp256k1PrivateKey) FromBytes(priv []byte) (ECCPrivateKey, error)

func (*Secp256k1PrivateKey) FromECDSA

func (*Secp256k1PrivateKey) GenerateShared

func (p *Secp256k1PrivateKey) GenerateShared(k ECCPublicKey, skLen, macLen int) (sk []byte, err error)

func (*Secp256k1PrivateKey) GetPublicKey

func (p *Secp256k1PrivateKey) GetPublicKey() ECCPublicKey

func (*Secp256k1PrivateKey) Public

func (p *Secp256k1PrivateKey) Public() crypto.PublicKey

func (*Secp256k1PrivateKey) Sign

func (p *Secp256k1PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error)

func (*Secp256k1PrivateKey) ToBytes

func (p *Secp256k1PrivateKey) ToBytes() []byte

func (*Secp256k1PrivateKey) ToECDSA

func (p *Secp256k1PrivateKey) ToECDSA() *ecdsa.PrivateKey

func (*Secp256k1PrivateKey) VrfEval

func (p *Secp256k1PrivateKey) VrfEval(m []byte) (index [32]byte, proof []byte)

type Secp256k1PublicKey

type Secp256k1PublicKey ecdsa.PublicKey

func (*Secp256k1PublicKey) ByteSize

func (p *Secp256k1PublicKey) ByteSize() int

func (*Secp256k1PublicKey) FromBytes

func (p *Secp256k1PublicKey) FromBytes(pub []byte) (ECCPublicKey, error)

func (*Secp256k1PublicKey) FromBytesNodeID

func (p *Secp256k1PublicKey) FromBytesNodeID(id []byte) ECCPublicKey

func (*Secp256k1PublicKey) FromECDSA

func (p *Secp256k1PublicKey) FromECDSA(key *ecdsa.PublicKey) ECCPublicKey

func (*Secp256k1PublicKey) Hash256s

func (p *Secp256k1PublicKey) Hash256s(in ...[]byte) []byte

func (*Secp256k1PublicKey) PubkeyToAddress

func (p *Secp256k1PublicKey) PubkeyToAddress() []byte

func (*Secp256k1PublicKey) ToAddress

func (p *Secp256k1PublicKey) ToAddress() []byte

func (*Secp256k1PublicKey) ToBytes

func (p *Secp256k1PublicKey) ToBytes() []byte

func (*Secp256k1PublicKey) ToECDSA

func (p *Secp256k1PublicKey) ToECDSA() *ecdsa.PublicKey

func (*Secp256k1PublicKey) ToNodeIDBytes

func (p *Secp256k1PublicKey) ToNodeIDBytes() []byte

func (*Secp256k1PublicKey) VrfProofToHash

func (p *Secp256k1PublicKey) VrfProofToHash(m, proof []byte) (index [32]byte, err error)

func (*Secp256k1PublicKey) VrfVerify

func (p *Secp256k1PublicKey) VrfVerify(m, proof []byte, index [32]byte) bool

type Secp256k1Signer

type Secp256k1Signer struct{}

func (Secp256k1Signer) BytesToPriv

func (s Secp256k1Signer) BytesToPriv(d []byte) (ECCPrivateKey, error)

func (Secp256k1Signer) BytesToPub

func (s Secp256k1Signer) BytesToPub(pub []byte) (ECCPublicKey, error)

func (Secp256k1Signer) GenerateKey

func (s Secp256k1Signer) GenerateKey() (priv ECCPrivateKey, err error)

func (Secp256k1Signer) Hasher

func (s Secp256k1Signer) Hasher() hash.Hash

func (Secp256k1Signer) LengthOfHash

func (s Secp256k1Signer) LengthOfHash() int

func (Secp256k1Signer) LengthOfPrivateKey

func (s Secp256k1Signer) LengthOfPrivateKey() int

func (Secp256k1Signer) LengthOfPublicKey

func (s Secp256k1Signer) LengthOfPublicKey() int

func (Secp256k1Signer) LengthOfSignature

func (s Secp256k1Signer) LengthOfSignature() int

func (Secp256k1Signer) Name

func (s Secp256k1Signer) Name() string

func (Secp256k1Signer) PrivToBytes

func (s Secp256k1Signer) PrivToBytes(priv ECCPrivateKey) []byte

func (Secp256k1Signer) PubFromNodeId

func (s Secp256k1Signer) PubFromNodeId(nid []byte) []byte

func (Secp256k1Signer) PubToBytes

func (s Secp256k1Signer) PubToBytes(pub ECCPublicKey) []byte

func (Secp256k1Signer) PubToNodeIdBytes

func (s Secp256k1Signer) PubToNodeIdBytes(pub []byte) ([]byte, error)

func (Secp256k1Signer) RecoverPub added in v1.0.204

func (s Secp256k1Signer) RecoverPub(hash, sig []byte) ([]byte, error)

func (Secp256k1Signer) Sign

func (s Secp256k1Signer) Sign(priv []byte, hash []byte) (sig []byte, err error)

func (Secp256k1Signer) String

func (s Secp256k1Signer) String() string

func (Secp256k1Signer) Verify

func (s Secp256k1Signer) Verify(pub []byte, hash []byte, sig []byte) bool

Directories

Path Synopsis
vrf

Jump to

Keyboard shortcuts

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