curve

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 14 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PedersenParamsRaw []byte

Functions

func ComputeHashOnElements added in v0.7.1

func ComputeHashOnElements(elems []*big.Int) (hash *big.Int)

ComputeHashOnElements computes the hash on the given elements using a golang Pedersen Hash implementation.

The function appends the length of `elems` to the slice and then calls the `HashPedersenElements` method passing in `elems` as an argument. The resulting hash is returned.

Parameters: - elems: slice of big.Int pointers to be hashed Returns: - hash: The hash of the list of elements

func DivMod

func DivMod(n, m, p *big.Int) *big.Int

DivMod calculates the quotient and remainder of a division operation between two big integers (0 <= x < p such that (m * x) % p == n). (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/math_utils.py#L50)

Parameters: - n: a pointer to a big integer representing the dividend - m: a pointer to a big integer representing the divisor - p: a pointer to a big integer representing the modulus Returns: - *big.Int: a pointer to a big integer representing the remainder of the division operation.

func FmtKecBytes

func FmtKecBytes(in *big.Int, rolen int) (buf []byte)

FmtKecBytes formats a big integer into a Keccak hash.

Parameters: - in: a pointer to a big.Int - rolen: an integer representing the desired length of the output byte slice Returns: - buf: a byte slice containing the formatted Keccak hash

func HashPedersenElements added in v0.7.1

func HashPedersenElements(elems []*big.Int) (hash *big.Int)

HashPedersenElements calculates the hash of a list of elements using a golang Pedersen Hash. Parameters: - elems: slice of big.Int pointers to be hashed Returns: - hash: The hash of the list of elements

func MaskBits

func MaskBits(mask, wordSize int, slice []byte) (ret []byte)

MaskBits masks the specified (excess) bits in a byte slice.

Parameters: - mask: is an integer representing the number of bits to mask - wordSize: is an integer representing the number of bits in each element of the slice - slice: is a byte slice on which the masking operation is performed Returns: - []byte: a new byte slice that contains the masked bits

func Pedersen added in v0.7.1

func Pedersen(a, b *felt.Felt) *felt.Felt

Pedersen is a function that implements the Pedersen hash. NOTE: This function just wraps the Juno implementation (ref: https://github.com/NethermindEth/juno/blob/32fd743c774ec11a1bb2ce3dceecb57515f4873e/core/crypto/pedersen_hash.go#L20)

Parameters: - a: a pointers to felt.Felt to be hashed. - b: a pointers to felt.Felt to be hashed. Returns: - *felt.Felt: a pointer to a felt.Felt storing the resulting hash.

func PedersenArray added in v0.7.1

func PedersenArray(felts ...*felt.Felt) *felt.Felt

PedersenArray is a function that takes a variadic number of felt.Felt pointers as parameters and calls the PedersenArray function from the junoCrypto package with the provided parameters. NOTE: This function just wraps the Juno implementation (ref: https://github.com/NethermindEth/juno/blob/32fd743c774ec11a1bb2ce3dceecb57515f4873e/core/crypto/pedersen_hash.go#L12)

Parameters: - felts: A variadic number of pointers to felt.Felt Returns: - *felt.Felt: pointer to a felt.Felt

func Poseidon added in v0.7.3

func Poseidon(a, b *felt.Felt) *felt.Felt

Poseidon is a function that implements the Poseidon hash. NOTE: This function just wraps the Juno implementation (ref: https://github.com/NethermindEth/juno/blob/32fd743c774ec11a1bb2ce3dceecb57515f4873e/core/crypto/poseidon_hash.go#L59)

Parameters: - a: a pointers to felt.Felt to be hashed. - b: a pointers to felt.Felt to be hashed. Returns: - *felt.Felt: a pointer to a felt.Felt storing the resulting hash.

func PoseidonArray added in v0.7.3

func PoseidonArray(felts ...*felt.Felt) *felt.Felt

PoseidonArray is a function that takes a variadic number of felt.Felt pointers as parameters and calls the PoseidonArray function from the junoCrypto package with the provided parameters. NOTE: This function just wraps the Juno implementation (ref: https://github.com/NethermindEth/juno/blob/main/core/crypto/poseidon_hash.go#L74)

Parameters: - felts: A variadic number of pointers to felt.Felt Returns: - *felt.Felt: pointer to a felt.Felt

func StarknetKeccak added in v0.7.3

func StarknetKeccak(b []byte) *felt.Felt

StarknetKeccak computes the Starknet Keccak hash of the given byte slice. NOTE: This function just wraps the Juno implementation (ref: https://github.com/NethermindEth/juno/blob/main/core/crypto/keccak.go#L11)

Parameters: - b: The byte slice to hash Returns: - *felt.Felt: pointer to a felt.Felt - error: An error if any

func VerifySignature added in v0.7.3

func VerifySignature(msgHash, r, s, pubKey string) bool

VerifySignature verifies the ECDSA signature of a given message hash using the provided public key.

It takes the message hash, the r and s values of the signature, and the public key as strings and verifies the signature using the public key.

Parameters: - msgHash: The hash of the message to be verified as a string - r: The r value (the first part) of the signature as a string - s: The s value (the second part) of the signature as a string - pubKey: The public key (only the x coordinate) as a string Return values: - bool: A boolean indicating whether the signature is valid - error: An error if any occurred during the verification process

Types

type CurveOption

type CurveOption interface {
	// contains filtered or unexported methods
}

func WithConstants

func WithConstants(paramsPath ...string) CurveOption

WithConstants creates a CurveOption (a curve initialized with constant points) that initializes the constants of the curve.

Parameters: - paramsPath: a variadic parameter of type string, representing the path(s) to the parameters Returns: - a new instance of CurveOption

type StarkCurve

type StarkCurve struct {
	*elliptic.CurveParams
	EcGenX           *big.Int
	EcGenY           *big.Int
	MinusShiftPointX *big.Int
	MinusShiftPointY *big.Int
	Max              *big.Int
	Alpha            *big.Int
	ConstantPoints   [][]*big.Int
}

Returned stark curve includes several values above and beyond what the 'elliptic' interface calls for to facilitate common starkware functions

var Curve StarkCurve

func (StarkCurve) Add

func (sc StarkCurve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)

Add computes the sum of two points on the StarkCurve. Assumes affine form (x, y) is spread (x1 *big.Int, y1 *big.Int) (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/math_utils.py#L59)

Parameters: - x1, y1: The coordinates of the first point as pointers to big.Int on the curve - x2, y2: The coordinates of the second point as pointers to big.Int on the curve Returns: - x, y: two pointers to big.Int, representing the x and y coordinates of the sum of the two input points

func (StarkCurve) Double

func (sc StarkCurve) Double(x1, y1 *big.Int) (x, y *big.Int)

Double calculates the double of a point on a StarkCurve (equation y^2 = x^3 + alpha*x + beta mod p). Assumes affine form (x, y) is spread (x1 *big.Int, y1 *big.Int) (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/math_utils.py#L79)

The function takes two pointers to big.Int values, x1 and y1, which represent the coordinates of the point to be doubled on the StarkCurve. It returns two pointers to big.Int values, x and y, which represent the coordinates of the resulting point after the doubling operation.

Parameters: - x1, y1: The coordinates of the point to be doubled on the StarkCurve. Returns: - x, y: two pointers to big.Int, representing the x and y coordinates of the resulting point

func (StarkCurve) EcMult

func (sc StarkCurve) EcMult(m, x1, y1 *big.Int) (x, y *big.Int)

EcMult multiplies a point (equation y^2 = x^3 + alpha*x + beta mod p) on the StarkCurve by a scalar value. Assumes affine form (x, y) is spread (x1 *big.Int, y1 *big.Int) and that 0 < m < order(point). (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/math_utils.py#L91)

Parameters: - m: The scalar value to multiply the point by. - x1, y1: The coordinates of the point on the curve. Returns: - x, y: The coordinates of the resulting point after multiplication.

func (StarkCurve) GenerateSecret

func (sc StarkCurve) GenerateSecret(msgHash, privKey, seed *big.Int) (secret *big.Int)

GenerateSecret generates a secret using the StarkCurve struct. implementation based on https://github.com/codahale/rfc6979/blob/master/rfc6979.go for the specification, see https://tools.ietf.org/html/rfc6979#section-3.2

Parameters: - msgHash: a pointer to a big.Int representing the message hash - privKey: a pointer to a big.Int representing the private key - seed: a pointer to a big.Int representing the seed Returns: - secret: a pointer to a big.Int representing the generated secret

func (StarkCurve) GetRandomPrivateKey

func (sc StarkCurve) GetRandomPrivateKey() (priv *big.Int, err error)

GetRandomPrivateKey generates a random private key for the StarkCurve struct. NOTE: to be used for testing purposes

Parameters: - none Returns: - priv: a pointer to a big.Int representing the generated private key - err: an error if any

func (StarkCurve) GetYCoordinate

func (sc StarkCurve) GetYCoordinate(starkX *big.Int) *big.Int

GetYCoordinate calculates the y-coordinate of a point on the StarkCurve. (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/signature.py#L84) point (x,y) is on the curve. Note: the real y coordinate is either y or -y.

Parameters: - starkX: The x-coordinate of the point Returns: - *big.Int: The calculated y-coordinate of the point a possible y coordinate such that together the point (x,y) is on the curve Note: the real y coordinate is either y or -y

func (StarkCurve) InvModCurveSize

func (sc StarkCurve) InvModCurveSize(x *big.Int) *big.Int

InvModCurveSize calculates the inverse modulus of a given big integer 'x' with respect to the StarkCurve 'sc'. (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/math_utils.py)

Parameters: - x: The big integer to calculate the inverse modulus for Returns: - The inverse modulus of 'x' with respect to 'sc.N'

func (StarkCurve) IsOnCurve

func (sc StarkCurve) IsOnCurve(x, y *big.Int) bool

IsOnCurve checks if the given point (x, y) lies on the curve defined by the StarkCurve instance.

Parameters: - x: the x-coordinate of the point - y: the y-coordinate of the point Return type: bool

func (StarkCurve) MimicEcMultAir

func (sc StarkCurve) MimicEcMultAir(mout, x1, y1, x2, y2 *big.Int) (x *big.Int, y *big.Int, err error)

MimicEcMultAir performs a computation on the StarkCurve struct (m * point + shift_point) using the same steps like the AIR. (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/signature.py#L176) AIR : Algebraic Intermediate Representation of computation

Parameters: - mout: a pointer to a big.Int variable - x1, y1: a pointer to a big.Int point on the curve - x2, y2: a pointer to a big.Int point on the curve Returns: - x, y: a pointer to a big.Int point on the curve - err: an error if any

func (StarkCurve) PrivateToPoint

func (sc StarkCurve) PrivateToPoint(privKey *big.Int) (x, y *big.Int, err error)

PrivateToPoint generates a point on the StarkCurve from a private key.

It takes a private key as a parameter and returns the x and y coordinates of the generated point on the curve. If the private key is not within the range of the curve, it returns an error.

Parameters: - privKey: The private key used to generate the point Return values: - x: The x coordinate of the generated point - y: The y coordinate of the generated point - err: An error if the private key is not within the curve range

func (StarkCurve) ScalarBaseMult

func (sc StarkCurve) ScalarBaseMult(k []byte) (x, y *big.Int)

ScalarBaseMult returns the result of multiplying the base point of the StarkCurve by the given scalar value.

Parameters: - k: The scalar value to multiply the base point by Returns: - x: The x-coordinate of the resulting point - y: The y-coordinate of the resulting point

func (StarkCurve) ScalarMult

func (sc StarkCurve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)

ScalarMult performs scalar multiplication on a point (x1, y1) with a scalar value k.

Parameters: - x1: The x-coordinate of the point to be multiplied. - y1: The y-coordinate of the point to be multiplied. - k: The scalar value to multiply the point with. Returns: - x: The x-coordinate of the resulting point. - y: The y-coordinate of the resulting point.

func (StarkCurve) Sign

func (sc StarkCurve) Sign(msgHash, privKey *big.Int, seed ...*big.Int) (x, y *big.Int, err error)

Sign calculates the signature of a message using the StarkCurve algorithm. Secret is generated using a golang implementation of RFC 6979. Implementation does not yet include "extra entropy" or "retry gen". (ref: https://datatracker.ietf.org/doc/html/rfc6979)

Parameters: - msgHash: The hash of the message to be signed - privKey: The private key used for signing - seed: (Optional) Additional seed values used for generating the secret Returns: - x, y: The coordinates of the signature point on the curve - err: An error if any occurred during the signing process

func (StarkCurve) SignFelt

func (sc StarkCurve) SignFelt(msgHash, privKey *felt.Felt) (*felt.Felt, *felt.Felt, error)

SignFelt signs a message hash with a private key using the StarkCurve. just wraps Sign (previous function).

Parameters: - msgHash: the message hash to be signed - privKey: the private key used for signing Returns: - xFelt: The x-coordinate of the signed message - yFelt: The y-coordinate of the signed message - error: An error if the signing process fails

func (StarkCurve) Verify

func (sc StarkCurve) Verify(msgHash, r, s, pubX, pubY *big.Int) bool

Verify verifies the validity of the signature for a given message hash using the StarkCurve. (ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/signature.py#L217)

Parameters: - msgHash: The message hash to be verified - r: The r component of the signature - s: The s component of the signature - pubX: The x-coordinate of the public key used for verification - pubY: The y-coordinate of the public key used for verification Returns: - bool: true if the signature is valid, false otherwise

type StarkCurvePayload

type StarkCurvePayload struct {
	License        []string     `json:"_license"`
	Comment        string       `json:"_comment"`
	FieldPrime     *big.Int     `json:"FIELD_PRIME"`
	FieldGen       int          `json:"FIELD_GEN"`
	EcOrder        *big.Int     `json:"EC_ORDER"`
	Alpha          int64        `json:"ALPHA"`
	Beta           *big.Int     `json:"BETA"`
	ConstantPoints [][]*big.Int `json:"CONSTANT_POINTS"`
}

struct definition for parsing 'pedersen_params.json'

var PedersenParams StarkCurvePayload

Jump to

Keyboard shortcuts

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