Documentation ¶
Index ¶
- Constants
- Variables
- func Decrypt(priv *PrivateKey, in []byte) ([]byte, error)
- func Encrypt(pubkey *PublicKey, in []byte) ([]byte, error)
- func GenerateKey(rand io.Reader) (priv []byte, x, y *big.Int, err error)
- func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte
- func NonceRFC6979(privkey *big.Int, hash []byte, extra []byte, version []byte) *big.Int
- func PrivKeyFromBytes(pkBytes []byte) (*PrivateKey, *PublicKey)
- func PrivKeyFromScalar(p []byte) (*PrivateKey, *PublicKey, error)
- func PrivKeyFromSecret(s []byte) (*PrivateKey, *PublicKey)
- func Sign(priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
- func SignFromScalar(priv *PrivateKey, nonce []byte, hash []byte) (r, s *big.Int, err error)
- func SignFromSecret(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
- func SignFromSecretNoReader(priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
- func SignThreshold(priv *PrivateKey, groupPub *PublicKey, hash []byte, privNonce *PrivateKey, ...) (r, s *big.Int, err error)
- func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool
- type PrivateKey
- func (p PrivateKey) GetD() *big.Int
- func (p PrivateKey) GetType() int
- func (p *PrivateKey) PubKey() *PublicKey
- func (p PrivateKey) Public() (*big.Int, *big.Int)
- func (p PrivateKey) Serialize() []byte
- func (p PrivateKey) SerializeSecret() []byte
- func (p PrivateKey) Sign(hash []byte) (*Signature, error)
- func (p PrivateKey) ToECDSA() *ecdsa.PrivateKey
- type PublicKey
- func (p PublicKey) GetCurve() interface{}
- func (p PublicKey) GetType() int
- func (p PublicKey) GetX() *big.Int
- func (p PublicKey) GetY() *big.Int
- func (p PublicKey) Serialize() []byte
- func (p PublicKey) SerializeCompressed() []byte
- func (p PublicKey) SerializeUncompressed() []byte
- func (p PublicKey) ToECDSA() *ecdsa.PublicKey
- type Signature
- type TwistedEdwardsCurve
- func (curve *TwistedEdwardsCurve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)
- func (curve *TwistedEdwardsCurve) Double(x1, y1 *big.Int) (x, y *big.Int)
- func (curve *TwistedEdwardsCurve) IsOnCurve(x *big.Int, y *big.Int) bool
- func (curve TwistedEdwardsCurve) Params() *elliptic.CurveParams
- func (curve *TwistedEdwardsCurve) ScalarBaseMult(k []byte) (x, y *big.Int)
- func (curve *TwistedEdwardsCurve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)
Constants ¶
const ( PrivScalarSize = 32 PrivKeyBytesLen = 64 )
These constants define the lengths of serialized private keys.
const (
PubKeyBytesLen = 32
)
These constants define the lengths of serialized public keys.
const SignatureSize = 64
SignatureSize is the size of an encoded ECDSA signature.
Variables ¶
var ( // ErrInvalidMAC occurs when Message Authentication Check (MAC) fails // during decryption. This happens because of either invalid private key or // corrupt ciphertext. ErrInvalidMAC = errors.New("invalid mac hash") )
var Sha512VersionStringRFC6979 = []byte("Edwards+SHA512 ")
Sha512VersionStringRFC6979 is the RFC6979 nonce version for a Schnorr signature over the Curve25519 curve using BLAKE256 as the hash function.
Functions ¶
func Decrypt ¶
func Decrypt(priv *PrivateKey, in []byte) ([]byte, error)
Decrypt decrypts data that was encrypted using the Encrypt function.
func Encrypt ¶
Encrypt encrypts data for the target public key using AES-256-CBC. It also generates a private key (the pubkey of which is also in the output).
struct { // Initialization Vector used for AES-256-CBC IV [16]byte // Public Key: curve(2) + len_of_pubkeyX(2) + pubkeyY (curve = 0xFFFF) PublicKey [36]byte // Cipher text Data []byte // HMAC-SHA-256 Message Authentication Code HMAC [32]byte }
The primary aim is to ensure byte compatibility with Pyelliptic. Additionally, refer to section 5.8.1 of ANSI X9.63 for rationale on this format.
func GenerateKey ¶
GenerateKey generates a key using a random number generator, returning the private scalar and the corresponding public key points from a random secret.
func GenerateSharedSecret ¶
func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte
GenerateSharedSecret generates a shared secret based on a private key and a private key using Diffie-Hellman key exchange (ECDH) (RFC 4753). RFC5903 Section 9 states we should only return y.
func NonceRFC6979 ¶
NonceRFC6979 generates an ECDSA nonce (`k`) deterministically according to RFC 6979. It takes a 32-byte hash as an input and returns 32-byte nonce to be used in ECDSA algorithm.
func PrivKeyFromBytes ¶
func PrivKeyFromBytes(pkBytes []byte) (*PrivateKey, *PublicKey)
PrivKeyFromBytes returns a private and public key for `curve' based on the private key passed as an argument as a byte slice.
func PrivKeyFromScalar ¶
func PrivKeyFromScalar(p []byte) (*PrivateKey, *PublicKey, error)
PrivKeyFromScalar returns a private and public key for `curve' based on the 32-byte private scalar passed as an argument as a byte slice (encoded big endian int).
func PrivKeyFromSecret ¶
func PrivKeyFromSecret(s []byte) (*PrivateKey, *PublicKey)
PrivKeyFromSecret returns a private and public key for `curve' based on the 32-byte private key secret passed as an argument as a byte slice.
func Sign ¶
func Sign(priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
Sign is the generalized and exported version of Ed25519 signing, that handles both standard private secrets and non-standard scalars.
func SignFromScalar ¶
SignFromScalar signs a message 'hash' using the given private scalar priv. It uses RFC6979 to generate a deterministic nonce. Considered experimental. r = kG, where k is the RFC6979 nonce s = r + hash512(k || A || M) * a
func SignFromSecret ¶
SignFromSecret signs a message 'hash' using the given private key priv. It doesn't actually user the random reader (the lib is maybe deterministic???).
func SignFromSecretNoReader ¶
func SignFromSecretNoReader(priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
SignFromSecretNoReader signs a message 'hash' using the given private key priv. It doesn't actually user the random reader.
func SignThreshold ¶
func SignThreshold(priv *PrivateKey, groupPub *PublicKey, hash []byte, privNonce *PrivateKey, pubNonceSum *PublicKey) (r, s *big.Int, err error)
SignThreshold signs a message 'hash' using the given private scalar priv in a threshold group signature. It uses RFC6979 to generate a deterministic nonce. Considered experimental. As opposed to the threshold signing function for secp256k1, this function takes the entirety of the public nonce point (all points added) instead of the public nonce point with n-1 keys added. r = K_Sum s = r + hash512(k || A || M) * a
Types ¶
type PrivateKey ¶
type PrivateKey struct {
// contains filtered or unexported fields
}
PrivateKey wraps an ecdsa.PrivateKey as a convenience mainly for signing things with the private key without having to directly import the ecdsa package.
func GeneratePrivateKey ¶
func GeneratePrivateKey() (*PrivateKey, error)
GeneratePrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey instead of the normal ecdsa.PrivateKey.
func NewPrivateKey ¶
func NewPrivateKey(d *big.Int) *PrivateKey
NewPrivateKey instantiates a new private key from a scalar encoded as a big integer.
func (PrivateKey) GetD ¶
func (p PrivateKey) GetD() *big.Int
GetD satisfies the chainec PrivateKey interface.
func (PrivateKey) GetType ¶
func (p PrivateKey) GetType() int
GetType satisfies the chainec PrivateKey interface.
func (*PrivateKey) PubKey ¶
func (p *PrivateKey) PubKey() *PublicKey
PubKey returns the PublicKey corresponding to this private key.
func (PrivateKey) Public ¶
func (p PrivateKey) Public() (*big.Int, *big.Int)
Public returns the PublicKey corresponding to this private key.
func (PrivateKey) Serialize ¶
func (p PrivateKey) Serialize() []byte
Serialize returns the private key as a 32 byte big endian number.
func (PrivateKey) SerializeSecret ¶
func (p PrivateKey) SerializeSecret() []byte
SerializeSecret returns the 32 byte secret along with its public key as 64 bytes.
func (PrivateKey) Sign ¶
func (p PrivateKey) Sign(hash []byte) (*Signature, error)
Sign is the generalized and exported version of Ed25519 signing, that handles both standard private secrets and non-standard scalars.
func (PrivateKey) ToECDSA ¶
func (p PrivateKey) ToECDSA() *ecdsa.PrivateKey
ToECDSA returns the private key as a *ecdsa.PrivateKey.
type PublicKey ¶
PublicKey is an ecdsa.PublicKey with an additional function to serialize.
func NewPublicKey ¶
NewPublicKey instantiates a new public key.
func ParsePubKey ¶
ParsePubKey parses a public key for an edwards curve from a bytestring into a ecdsa.Publickey, verifying that it is valid.
func RecoverCompact ¶
RecoverCompact uses a signature and a hash to recover is private key, is not yet implemented. TODO: Implement.
func (PublicKey) GetCurve ¶
func (p PublicKey) GetCurve() interface{}
GetCurve satisfies the chainec PublicKey interface.
func (PublicKey) Serialize ¶
Serialize serializes a public key in a 32-byte compressed little endian format.
func (PublicKey) SerializeCompressed ¶
SerializeCompressed satisfies the chainec PublicKey interface.
func (PublicKey) SerializeUncompressed ¶
SerializeUncompressed satisfies the chainec PublicKey interface.
type Signature ¶
Signature is a type representing an ecdsa signature.
func NewSignature ¶
NewSignature instantiates a new signature given some R,S values.
func ParseDERSignature ¶
ParseDERSignature offers a legacy function for plugging into Decred, which is based off btcec.
func ParseSignature ¶
ParseSignature parses a signature in BER format for the curve type `curve' into a Signature type, performing some basic sanity checks.
func (*Signature) IsEqual ¶
IsEqual compares this Signature instance to the one passed, returning true if both Signatures are equivalent. A signature is equivalent to another, if they both have the same scalar value for R and S.
type TwistedEdwardsCurve ¶
type TwistedEdwardsCurve struct { *elliptic.CurveParams H int // Cofactor of the curve A, D, I *big.Int // Edwards curve equation parameter constants // contains filtered or unexported fields }
TwistedEdwardsCurve extended an elliptical curve set of parameters to satisfy the interface of the elliptic package.
func Edwards ¶
func Edwards() *TwistedEdwardsCurve
Edwards returns a Curve which implements Ed25519.
func (*TwistedEdwardsCurve) Add ¶
func (curve *TwistedEdwardsCurve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)
Add adds two points represented by pairs of big integers on the elliptical curve.
func (*TwistedEdwardsCurve) Double ¶
func (curve *TwistedEdwardsCurve) Double(x1, y1 *big.Int) (x, y *big.Int)
Double adds the same pair of big integer coordinates to itself on the elliptical curve.
func (*TwistedEdwardsCurve) IsOnCurve ¶
IsOnCurve returns bool to say if the point (x,y) is on the curve by checking (y^2 - x^2 - 1 - dx^2y^2) % P == 0.
func (TwistedEdwardsCurve) Params ¶
func (curve TwistedEdwardsCurve) Params() *elliptic.CurveParams
Params returns the parameters for the curve.
func (*TwistedEdwardsCurve) ScalarBaseMult ¶
func (curve *TwistedEdwardsCurve) ScalarBaseMult(k []byte) (x, y *big.Int)
ScalarBaseMult returns k*G, where G is the base point of the group and k is an integer in big-endian form. TODO Optimize this with field elements
func (*TwistedEdwardsCurve) ScalarMult ¶
ScalarMult returns k*(Bx,By) where k is a number in big-endian form. This uses the repeated doubling method, which is variable time. TODO use a constant time method to prevent side channel attacks.