bn254

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: MIT Imports: 12 Imported by: 23

Documentation

Index

Constants

View Source
const (
	G1PointSize = 64  // 32 bytes for x, 32 bytes for y
	G2PointSize = 128 // 64 bytes for x, 64 bytes for y
)

Precompile format constants

Variables

View Source
var (
	ErrInvalidPointFormat = errors.New("invalid point format for precompile")
	ErrPointNotInSubgroup = errors.New("point not in correct subgroup")
	ErrInvalidFieldOrder  = errors.New("number not in valid field order")
)

Error types for precompile compatibility

View Source
var FieldModulus = func() *big.Int {
	n, _ := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10)
	return n
}()

FieldModulus is the BN254 field modulus

Functions

func AggregateVerify

func AggregateVerify(publicKeys []*PublicKey, messages [][]byte, aggSignature *Signature) (bool, error)

AggregateVerify verifies an aggregated signature against multiple public keys and multiple messages

func BatchVerify

func BatchVerify(publicKeys []*PublicKey, message []byte, signatures []*Signature) (bool, error)

BatchVerify verifies multiple signatures in a single batch operation

func BatchVerifySolidityCompatible added in v0.0.3

func BatchVerifySolidityCompatible(publicKeys []*PublicKey, messageHash [32]byte, signatures []*Signature) (bool, error)

BatchVerifySolidityCompatible verifies multiple signatures in a single batch operation using the Solidity-compatible hash-to-curve method

func GenerateKeyPair

func GenerateKeyPair() (*PrivateKey, *PublicKey, error)

GenerateKeyPair creates a new random private key and the corresponding public key

func GenerateKeyPairFromSeed

func GenerateKeyPairFromSeed(seed []byte) (*PrivateKey, *PublicKey, error)

GenerateKeyPairFromSeed creates a deterministic private key and the corresponding public key from a seed

func SolidityHashToG1 added in v0.0.3

func SolidityHashToG1(hash [32]byte) (*bn254.G1Affine, error)

SolidityHashToG1 implements the same hash-to-curve algorithm as the Solidity BN254 library This uses the try-and-increment method that matches BN254.hashToG1() in Solidity

func ValidateFieldOrder

func ValidateFieldOrder(n *big.Int) bool

ValidateFieldOrder checks if a number is in the correct field

Types

type G1Point

type G1Point struct {
	*bn254.G1Affine
}

func G1PointFromPrecompileFormat

func G1PointFromPrecompileFormat(data []byte) (*G1Point, error)

FromPrecompileFormat creates a G1 point from the Ethereum precompile format

func NewG1Point

func NewG1Point(x, y *big.Int) *G1Point

func NewZeroG1Point

func NewZeroG1Point() *G1Point

func (*G1Point) Add

func (p *G1Point) Add(p2 *G1Point) *G1Point

Add another G1 point to this one

func (*G1Point) AddPublicKey

func (p *G1Point) AddPublicKey(pk *PublicKey) *G1Point

AddPublicKey adds the G1 point from a public key to this point

func (*G1Point) Sub

func (p *G1Point) Sub(p2 *G1Point) *G1Point

Sub another G1 point from this one

func (*G1Point) ToPrecompileFormat

func (p *G1Point) ToPrecompileFormat() ([]byte, error)

ToPrecompileFormat converts a G1 point to the format expected by the Ethereum precompile

type G2Point

type G2Point struct {
	*bn254.G2Affine
}

func G2PointFromPrecompileFormat

func G2PointFromPrecompileFormat(data []byte) (*G2Point, error)

FromPrecompileFormat creates a G2 point from the Ethereum precompile format

func NewG2Point

func NewG2Point(x0, x1, y0, y1 *big.Int) *G2Point

func NewZeroG2Point

func NewZeroG2Point() *G2Point

func (*G2Point) Add

func (p *G2Point) Add(p2 *G2Point) *G2Point

Add another G2 point to this one

func (*G2Point) AddPublicKey

func (p *G2Point) AddPublicKey(pk *PublicKey) *G2Point

AddPublicKey adds the G2 point from a public key to this point

func (*G2Point) Sub

func (p *G2Point) Sub(p2 *G2Point) *G2Point

Sub another G2 point from this one

func (*G2Point) ToPrecompileFormat

func (p *G2Point) ToPrecompileFormat() ([]byte, error)

ToPrecompileFormat converts a G2 point to the format expected by the Ethereum precompile

type PrivateKey

type PrivateKey struct {
	ScalarBytes []byte
	// contains filtered or unexported fields
}

PrivateKey represents a BLS private key

func NewPrivateKeyFromBytes

func NewPrivateKeyFromBytes(data []byte) (*PrivateKey, error)

NewPrivateKeyFromBytes creates a private key from bytes

func NewPrivateKeyFromHexString added in v0.0.2

func NewPrivateKeyFromHexString(hexStr string) (*PrivateKey, error)

NewPrivateKeyFromHexString creates a private key from a hex string

func (*PrivateKey) Bytes

func (pk *PrivateKey) Bytes() []byte

Bytes returns the private key as a byte slice

func (*PrivateKey) Public

func (pk *PrivateKey) Public() *PublicKey

Public returns the public key corresponding to the private key

func (*PrivateKey) Sign

func (pk *PrivateKey) Sign(message []byte) (*Signature, error)

Sign signs a message using the private key

func (*PrivateKey) SignG1Point

func (pk *PrivateKey) SignG1Point(hashPoint *bn254.G1Affine) (*Signature, error)

func (*PrivateKey) SignSolidityCompatible added in v0.0.3

func (pk *PrivateKey) SignSolidityCompatible(messageHash [32]byte) (*Signature, error)

SignSolidityCompatible signs a message hash using the Solidity-compatible hash-to-curve method

func (*PrivateKey) ToHex added in v0.0.2

func (pk *PrivateKey) ToHex() (string, error)

ToHex returns the private key as a hex string

type PublicKey

type PublicKey struct {
	PointBytes []byte
	// contains filtered or unexported fields
}

PublicKey represents a BLS public key

func AggregatePublicKeys

func AggregatePublicKeys(pubKeys []*PublicKey) (*PublicKey, error)

AggregatePublicKeys combines multiple public keys into a single aggregated public key.

func NewPublicKeyFromBytes

func NewPublicKeyFromBytes(data []byte) (*PublicKey, error)

NewPublicKeyFromBytes creates a public key from bytes

func NewPublicKeyFromHexString

func NewPublicKeyFromHexString(pubHex string) (*PublicKey, error)

func NewPublicKeyFromSolidity

func NewPublicKeyFromSolidity(g1 *SolidityBN254G1Point, g2 *SolidityBN254G2Point) (*PublicKey, error)

NewPublicKeyFromSolidity creates a public key from a Solidity G1 and G2 points

func (*PublicKey) Bytes

func (pk *PublicKey) Bytes() []byte

Bytes returns the public key as a byte slice

func (*PublicKey) GetG1Point

func (pk *PublicKey) GetG1Point() *bn254.G1Affine

GetG1Point returns the G1 point of the public key

func (*PublicKey) GetG2Point

func (pk *PublicKey) GetG2Point() *bn254.G2Affine

GetG2Point returns the G2 point of the public key

func (*PublicKey) Sub

func (pk *PublicKey) Sub(other *PublicKey) *PublicKey

Sub subtracts another public key from this one

type Scheme

type Scheme struct{}

Scheme implements the SigningScheme interface for BN254

func NewScheme

func NewScheme() *Scheme

NewScheme creates a new BN254 signing scheme

func (*Scheme) AggregateSignatures

func (s *Scheme) AggregateSignatures(signatures []signing.Signature) (signing.Signature, error)

AggregateSignatures combines multiple signatures into a single signature

func (*Scheme) AggregateVerify

func (s *Scheme) AggregateVerify(publicKeys []signing.PublicKey, messages [][]byte, aggSignature signing.Signature) (bool, error)

AggregateVerify verifies an aggregated signature against multiple public keys and multiple messages

func (*Scheme) BatchVerify

func (s *Scheme) BatchVerify(publicKeys []signing.PublicKey, message []byte, signatures []signing.Signature) (bool, error)

BatchVerify verifies multiple signatures in a single batch operation

func (*Scheme) GenerateKeyPair

func (s *Scheme) GenerateKeyPair() (signing.PrivateKey, signing.PublicKey, error)

GenerateKeyPair creates a new random private key and the corresponding public key

func (*Scheme) GenerateKeyPairEIP2333

func (s *Scheme) GenerateKeyPairEIP2333(seed []byte, path []uint32) (signing.PrivateKey, signing.PublicKey, error)

GenerateKeyPairEIP2333 creates a deterministic private key and the corresponding public key using the EIP-2333 standard

func (*Scheme) GenerateKeyPairFromSeed

func (s *Scheme) GenerateKeyPairFromSeed(seed []byte) (signing.PrivateKey, signing.PublicKey, error)

GenerateKeyPairFromSeed creates a deterministic private key and the corresponding public key from a seed

func (*Scheme) NewPrivateKeyFromBytes

func (s *Scheme) NewPrivateKeyFromBytes(data []byte) (signing.PrivateKey, error)

NewPrivateKeyFromBytes creates a private key from bytes

func (*Scheme) NewPrivateKeyFromHexString added in v0.0.2

func (s *Scheme) NewPrivateKeyFromHexString(hex string) (signing.PrivateKey, error)

func (*Scheme) NewPublicKeyFromBytes

func (s *Scheme) NewPublicKeyFromBytes(data []byte) (signing.PublicKey, error)

NewPublicKeyFromBytes creates a public key from bytes

func (*Scheme) NewPublicKeyFromHexString

func (s *Scheme) NewPublicKeyFromHexString(hex string) (signing.PublicKey, error)

func (*Scheme) NewSignatureFromBytes

func (s *Scheme) NewSignatureFromBytes(data []byte) (signing.Signature, error)

NewSignatureFromBytes creates a signature from bytes

type Signature

type Signature struct {
	SigBytes []byte
	// contains filtered or unexported fields
}

Signature represents a BLS signature

func AggregateSignatures

func AggregateSignatures(signatures []*Signature) (*Signature, error)

AggregateSignatures combines multiple signatures into a single signature

func NewSignatureFromBytes

func NewSignatureFromBytes(data []byte) (*Signature, error)

NewSignatureFromBytes creates a signature from bytes

func (*Signature) Add

func (s *Signature) Add(other *Signature) *Signature

Add adds another signature to this one

func (*Signature) Bytes

func (s *Signature) Bytes() []byte

Bytes returns the signature as a byte slice

func (*Signature) GetG1Point

func (s *Signature) GetG1Point() *bn254.G1Affine

func (*Signature) Sub

func (s *Signature) Sub(other *Signature) *Signature

Sub subtracts another signature from this one

func (*Signature) Verify

func (s *Signature) Verify(publicKey *PublicKey, message []byte) (bool, error)

Verify verifies a signature against a message and public key

func (*Signature) VerifySolidityCompatible added in v0.0.3

func (s *Signature) VerifySolidityCompatible(publicKey *PublicKey, messageHash [32]byte) (bool, error)

VerifySolidityCompatible verifies a signature against a message hash using Solidity-compatible hash-to-curve

type SolidityBN254G1Point added in v0.0.3

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

type SolidityBN254G2Point added in v0.0.3

type SolidityBN254G2Point struct {
	X [2]*big.Int
	Y [2]*big.Int
}

Jump to

Keyboard shortcuts

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