starkcurve

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcHash

func CalcHash(input []*big.Int) []byte

func FastHash

func FastHash(x *big.Int, y *big.Int) []byte

func GetMsgHash

func GetMsgHash(instuctType, vault0, vault1, amount0, amount1 *big.Int, token0, token1OrPubkey []byte, nouce, expirationTimestamp uint64, condition []byte) []byte

return hash(hash(token0, token1_or_pub_key), packed_message)

func InitHashParams

func InitHashParams()

func InitStarkCurveParams

func InitStarkCurveParams()

func Sign

func Sign(privkey []byte, hash []byte) (*big.Int, *big.Int, error)

Reference https://github.com/apisit/rfc6979

func Sign2

func Sign2(privkey []byte, hash []byte) (*big.Int, *big.Int, error)

Reference https://github.com/apisit/rfc6979

func SignV3

func SignV3(privkey []byte, hash []byte) (*big.Int, *big.Int, error)

Reference https://github.com/apisit/rfc6979

func Verify

func Verify(hash []byte, pubkeyX, pubkeyY, r, s *big.Int) bool

func VerifyPubKey

func VerifyPubKey(pubkey []byte) bool

func VerifyV3

func VerifyV3(hash []byte, pubkeyX, pubkeyY, r, s *big.Int) bool

Types

type CurvePoint

type CurvePoint struct {
	X *big.Int
	Y *big.Int
}

type HashPoint

type HashPoint struct {
	X *big.Int
	Y *big.Int
}

type StarkCfg

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

type StarkCurve

type StarkCurve 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 StarkCurve equation
	Gx, Gy  *big.Int // (x,y) of the base point
	BitSize int      // the size of the underlying field
}

A StarkCurve represents a starkware Curve with a=1. See https://docs.starkware.co/starkex-v4/crypto/stark-curve

func NewStarkCurve

func NewStarkCurve() *StarkCurve

func (*StarkCurve) Add

func (starkCurve *StarkCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)

Affine addition formulas: (x1,y1)+(x2,y2)=(x3,y3) where

x3 = (y2-y1)^2/(x2-x1)^2-x1-x2
y3 = (2*x1+x2)*(y2-y1)/(x2-x1)-(y2-y1)^3/(x2-x1)^3-y1

func (*StarkCurve) Add1

func (starkCurve *StarkCurve) Add1(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)

Affine addition formulas: (x1,y1)+(x2,y2)=(x3,y3) where

x3 = (y2-y1)^2/(x2-x1)^2-x1-x2
y3 = (2*x1+x2)*(y2-y1)/(x2-x1)-(y2-y1)^3/(x2-x1)^3-y1

func (*StarkCurve) Double

func (starkCurve *StarkCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int)

func (*StarkCurve) Double1

func (starkCurve *StarkCurve) Double1(x1, y1 *big.Int) (*big.Int, *big.Int)

func (*StarkCurve) GenerateKey

func (starkCurve *StarkCurve) GenerateKey(rand io.Reader) (priv []byte, x, y *big.Int, err error)

TODO: double check if it is okay GenerateKey returns a public/private key pair. The private key is generated using the given reader, which must return random data.

func (*StarkCurve) GenerateKeyV3

func (starkCurve *StarkCurve) GenerateKeyV3(rand io.Reader) (priv []byte, x, y *big.Int, err error)

TODO: double check if it is okay GenerateKey returns a public/private key pair. The private key is generated using the given reader, which must return random data.

func (*StarkCurve) GetYCoordinate

func (starkCurve *StarkCurve) GetYCoordinate(x *big.Int) (*big.Int, *big.Int)

IsOnCurve returns true if the given (x,y) lies on the StarkCurve.

func (*StarkCurve) IsOnCurve

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

IsOnCurve returns true if the given (x,y) lies on the StarkCurve.

func (*StarkCurve) Marshal

func (starkCurve *StarkCurve) Marshal(x, y *big.Int) []byte

Marshal converts a point into the form specified in section 4.3.6 of ANSI X9.62.

func (*StarkCurve) Params

func (StarkCurve *StarkCurve) Params() *elliptic.CurveParams

func (*StarkCurve) ScalarBaseMult

func (starkCurve *StarkCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int)

func (*StarkCurve) ScalarBaseMultV2

func (starkCurve *StarkCurve) ScalarBaseMultV2(k []byte) (*big.Int, *big.Int)

 一个优化点可以计算 g 2g 4g 8g ....的值,然后用add方法来计算,理论上可以减少计算量,进行一定的计算 An optimization point can calculate the values of g 2g 4g 8g...., and then use the add method to calculate, which theoretically can reduce the amount of calculation

func (*StarkCurve) ScalarBaseMultV3

func (starkCurve *StarkCurve) ScalarBaseMultV3(k []byte) (*big.Int, *big.Int)

func (*StarkCurve) ScalarMult

func (starkCurve *StarkCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int)

// Affine negation formulas: -(x1,y1)=(x1,-y1).

func (starkCurve *StarkCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
	// See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
	x3, y3, z3 := new(big.Int), new(big.Int), new(big.Int)
	if z1.Sign() == 0 {
		x3.Set(x2)
		y3.Set(y2)
		z3.Set(z2)
		return x3, y3, z3
	}
	if z2.Sign() == 0 {
		x3.Set(x1)
		y3.Set(y1)
		z3.Set(z1)
		return x3, y3, z3
	}

	z1z1 := new(big.Int).Mul(z1, z1)
	z1z1.Mod(z1z1, starkCurve.P)
	z2z2 := new(big.Int).Mul(z2, z2)
	z2z2.Mod(z2z2, starkCurve.P)

	u1 := new(big.Int).Mul(x1, z2z2)
	u1.Mod(u1, starkCurve.P)
	u2 := new(big.Int).Mul(x2, z1z1)
	u2.Mod(u2, starkCurve.P)
	h := new(big.Int).Sub(u2, u1)
	xEqual := h.Sign() == 0
	if h.Sign() == -1 {
		h.Add(h, starkCurve.P)
	}
	i := new(big.Int).Lsh(h, 1)
	i.Mul(i, i)
	j := new(big.Int).Mul(h, i)

	s1 := new(big.Int).Mul(y1, z2)
	s1.Mul(s1, z2z2)
	s1.Mod(s1, starkCurve.P)
	s2 := new(big.Int).Mul(y2, z1)
	s2.Mul(s2, z1z1)
	s2.Mod(s2, starkCurve.P)
	r := new(big.Int).Sub(s2, s1)
	if r.Sign() == -1 {
		r.Add(r, starkCurve.P)
	}
	yEqual := r.Sign() == 0
	if xEqual && yEqual {
		//return starkCurve.doubleJacobian(x1, y1, z1)
	}
	r.Lsh(r, 1)
	v := new(big.Int).Mul(u1, i)

	x3.Set(r)
	x3.Mul(x3, x3)
	x3.Sub(x3, j)
	x3.Sub(x3, v)
	x3.Sub(x3, v)
	x3.Mod(x3, starkCurve.P)

	y3.Set(r)
	v.Sub(v, x3)
	y3.Mul(y3, v)
	s1.Mul(s1, j)
	s1.Lsh(s1, 1)
	y3.Sub(y3, s1)
	y3.Mod(y3, starkCurve.P)

	z3.Add(z1, z2)
	z3.Mul(z3, z3)
	z3.Sub(z3, z1z1)
	z3.Sub(z3, z2z2)
	z3.Mul(z3, h)
	z3.Mod(z3, starkCurve.P)

	return x3, y3, z3
}

func (*StarkCurve) Unmarshal

func (starkCurve *StarkCurve) Unmarshal(data []byte) (x, y *big.Int)

Unmarshal converts a point, serialised by Marshal, into an x, y pair. On error, x = nil.

type StarkPoints

type StarkPoints struct {
	ConstantPoints [][]*big.Int `json:"const_points"`
}

Jump to

Keyboard shortcuts

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