Documentation
¶
Overview ¶
Package secp256k1 allows simple and abstracted operations in the secp256k1 group.
Example (ScalarMult) ¶
Example_ScalarMult shows how to do a scalar multiplication.
package main
import (
"github.com/bytemare/secp256k1"
)
func main() {
// Get an element. Here, we're taking the group generator.
g := secp256k1.Base()
// Get a scalar, e.g. a random one.
s := secp256k1.NewScalar().Random()
// Multiply. Boom.
g.Multiply(s)
}
Output:
Index ¶
- Constants
- func Ciphersuite() string
- func ElementLength() int
- func Order() []byte
- func ScalarLength() int
- func Secp256Polynomial(y, x *field.Element)
- type Element
- func (e *Element) Add(element *Element) *Element
- func (e *Element) Base() *Element
- func (e *Element) Copy() *Element
- func (e *Element) Decode(data []byte) error
- func (e *Element) DecodeCompressed(data []byte) error
- func (e *Element) DecodeCoordinates(x, y [32]byte) error
- func (e *Element) DecodeHex(h string) error
- func (e *Element) DecodeUncompressed(data []byte) error
- func (e *Element) Double() *Element
- func (e *Element) Encode() []byte
- func (e *Element) EncodeUncompressed() []byte
- func (e *Element) Equal(element *Element) int
- func (e *Element) Hex() string
- func (e *Element) Identity() *Element
- func (e *Element) IsIdentity() bool
- func (e *Element) MarshalBinary() ([]byte, error)
- func (e *Element) Multiply(scalar *Scalar) *Element
- func (e *Element) Negate() *Element
- func (e *Element) Set(element *Element) *Element
- func (e *Element) Subtract(element *Element) *Element
- func (e *Element) UnmarshalBinary(data []byte) error
- func (e *Element) XCoordinate() []byte
- type Scalar
- func (s *Scalar) Add(t *Scalar) *Scalar
- func (s *Scalar) Bits() [256]uint8
- func (s *Scalar) CSelect(cond uint64, u, v *Scalar) error
- func (s *Scalar) Copy() *Scalar
- func (s *Scalar) Decode(in []byte) error
- func (s *Scalar) DecodeHex(h string) error
- func (s *Scalar) Encode() []byte
- func (s *Scalar) Equal(t *Scalar) int
- func (s *Scalar) Hex() string
- func (s *Scalar) Invert() *Scalar
- func (s *Scalar) IsOne() bool
- func (s *Scalar) IsZero() bool
- func (s *Scalar) LessOrEqual(t *Scalar) uint64
- func (s *Scalar) MarshalBinary() ([]byte, error)
- func (s *Scalar) MinusOne() *Scalar
- func (s *Scalar) Multiply(t *Scalar) *Scalar
- func (s *Scalar) One() *Scalar
- func (s *Scalar) Pow(t *Scalar) *Scalar
- func (s *Scalar) Random() *Scalar
- func (s *Scalar) Set(t *Scalar) *Scalar
- func (s *Scalar) SetUInt64(i uint64) *Scalar
- func (s *Scalar) Square() *Scalar
- func (s *Scalar) Subtract(t *Scalar) *Scalar
- func (s *Scalar) UnmarshalBinary(data []byte) error
- func (s *Scalar) Zero() *Scalar
Examples ¶
Constants ¶
const ( // H2CSECP256K1 represents the hash-to-curve string identifier for secp256k1. H2CSECP256K1 = "secp256k1_XMD:SHA-256_SSWU_RO_" // E2CSECP256K1 represents the encode-to-curve string identifier for secp256k1. E2CSECP256K1 = "secp256k1_XMD:SHA-256_SSWU_NU_" )
Variables ¶
This section is empty.
Functions ¶
func Ciphersuite ¶
func Ciphersuite() string
Ciphersuite returns the hash-to-curve ciphersuite identifier.
func ElementLength ¶
func ElementLength() int
ElementLength returns the byte size of an encoded element.
func ScalarLength ¶
func ScalarLength() int
ScalarLength returns the byte size of an encoded Scalar.
func Secp256Polynomial ¶ added in v0.2.0
Secp256Polynomial applies y^2=x^3+ax+b (with a = 0) to recover y^2 from x.
Types ¶
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
Element implements the Element interface for the secp256k1 group element.
func EncodeToGroup ¶
EncodeToGroup returns a non-uniform mapping of the arbitrary input to an Element in the Group. The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func HashToGroup ¶
HashToGroup returns a safe mapping of the arbitrary input to an Element in the Group. The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func IsogenySecp256k13iso ¶ added in v0.2.1
IsogenySecp256k13iso is a 3-degree isogeny from secp256k1 3-ISO to the secp256k1 elliptic curve. It handles exceptional cases where inversions to denominators evaluate to 0.
func NewElement ¶
func NewElement() *Element
NewElement returns a new element set to the identity point.
func SSWU ¶ added in v0.2.1
SSWU applies the Simplified Shallue-van de Woestijne-Ulas (SWU) method to map e to a point on the secp256k1 3-ISO curve in affine coordinates. Note that calling IsogenySecp256k13iso() is necessary to then get a point on secpk256k1.
func (*Element) Add ¶
Add sets the receiver to the sum of the input and the receiver, and returns the receiver.
func (*Element) Decode ¶
Decode sets the receiver to a decoding of the input data, and returns an error on failure.
Example ¶
ExampleElement_Decode shows how to decode data into elements.
package main
import (
"encoding/hex"
"fmt"
"github.com/bytemare/secp256k1"
)
func main() {
// Let's say we have this element.
g := secp256k1.Base()
// Let's have a look at it.
fmt.Println(g.Hex())
// Which yields the following:
out := "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
// Let's get its byte representation.
b, _ := hex.DecodeString(out)
// And decode it into another element.
e := secp256k1.NewElement()
if err := e.Decode(b); err != nil {
fmt.Println(err.Error())
}
// Let's check for completeness.
if e.Equal(g) != 1 {
fmt.Println("something went wrong")
}
}
Output: 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
func (*Element) DecodeCompressed ¶ added in v0.2.0
DecodeCompressed sets the receiver to a decoding of the input data in compressed form, and returns an error on failure.
func (*Element) DecodeCoordinates ¶ added in v0.2.0
DecodeCoordinates set the receiver to the decoding of the affine coordinates given by x and y, and returns an error on failure.
func (*Element) DecodeHex ¶ added in v0.1.4
DecodeHex sets e to the decoding of the hex encoded element.
func (*Element) DecodeUncompressed ¶ added in v0.2.0
DecodeUncompressed sets the receiver to a decoding of the input data in uncompressed form, and returns an error on failure.
func (*Element) EncodeUncompressed ¶ added in v0.2.0
EncodeUncompressed returns the uncompressed byte encoding of the element.
func (*Element) Identity ¶
Identity sets the element to the point at infinity of the Group's underlying curve.
func (*Element) IsIdentity ¶
IsIdentity returns whether the Element is the point at infinity of the Group's underlying curve.
func (*Element) MarshalBinary ¶
MarshalBinary returns the compressed byte encoding of the element.
func (*Element) Multiply ¶
Multiply sets the receiver to the Scalar multiplication of the receiver with the given Scalar, and returns it.
func (*Element) Subtract ¶
Subtract subtracts the input from the receiver, and returns the receiver.
func (*Element) UnmarshalBinary ¶
UnmarshalBinary sets e to the decoding of the byte encoded element.
func (*Element) XCoordinate ¶
XCoordinate returns the encoded x coordinate of the element, which is the same as Encode() without the header.
type Scalar ¶
type Scalar struct {
S scalar.MontgomeryDomainFieldElement
// contains filtered or unexported fields
}
Scalar implements the Scalar interface for Edwards25519 group scalars.
func HashToScalar ¶
HashToScalar returns a safe mapping of the arbitrary input to a Scalar. The DST must not be empty or nil, and is recommended to be longer than 16 bytes.
func (*Scalar) Add ¶
Add sets the receiver to the sum of the input and the receiver, and returns the receiver.
func (*Scalar) CSelect ¶ added in v0.2.0
CSelect sets the receiver to u if cond == 0, and to v otherwise, in constant-time.
func (*Scalar) Decode ¶
Decode sets the receiver to a decoding of the input data, and returns an error on failure.
func (*Scalar) DecodeHex ¶ added in v0.1.4
DecodeHex sets s to the decoding of the hex encoded Scalar.
func (*Scalar) LessOrEqual ¶
LessOrEqual returns 1 if s <= t and 0 otherwise.
func (*Scalar) MarshalBinary ¶
MarshalBinary returns the compressed byte encoding of the Scalar.
func (*Scalar) Multiply ¶
Multiply multiplies the receiver with the input, and returns the receiver.
func (*Scalar) Pow ¶
Pow sets s to s^t modulo the group order, and returns s. If t is nil or equals 0, s is set to 1. Now using variable time big.Int because for some reason I can't get the constant time algorithm to work on Fiat.
func (*Scalar) Random ¶
Random sets the current Scalar to a new random Scalar and returns it. The random source is crypto/rand, and this functions is guaranteed to return a non-zero Scalar.
func (*Scalar) Set ¶
Set sets the receiver to the value of the argument Scalar, and returns the receiver.
func (*Scalar) SetUInt64 ¶ added in v0.1.3
SetUInt64 sets s to i modulo the group order, and returns it.
func (*Scalar) UnmarshalBinary ¶
UnmarshalBinary sets e to the decoding of the byte encoded Scalar.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
field
Package field implements the base field for elements on the curve SECP256k1 building on Fiat-Crypto.
|
Package field implements the base field for elements on the curve SECP256k1 building on Fiat-Crypto. |
|
scalar
Package scalar implements prime-order scalar operations in the SECP256k1 group building on Fiat-Crypto.
|
Package scalar implements prime-order scalar operations in the SECP256k1 group building on Fiat-Crypto. |