Update to use go-eth versions

This commit is contained in:
Zach Kelling
2025-08-03 01:35:05 +00:00
parent 1765dfc504
commit f6730ab3c1
15 changed files with 81 additions and 142 deletions
+15 -13
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -30,7 +30,9 @@ import (
"os" "os"
"sync" "sync"
"github.com/luxfi/crypto/utils" "github.com/luxfi/geth/common"
"github.com/luxfi/geth/common/math"
"github.com/luxfi/geth/rlp"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
@@ -79,7 +81,7 @@ var hasherPool = sync.Pool{
} }
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash // HashData hashes the provided data using the KeccakState and returns a 32 byte hash
func HashData(kh KeccakState, data []byte) (h utils.Hash) { func HashData(kh KeccakState, data []byte) (h common.Hash) {
kh.Reset() kh.Reset()
kh.Write(data) kh.Write(data)
kh.Read(h[:]) kh.Read(h[:])
@@ -101,7 +103,7 @@ func Keccak256(data ...[]byte) []byte {
// Keccak256Hash calculates and returns the Keccak256 hash of the input data, // Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// converting it to an internal Hash data structure. // converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) (h utils.Hash) { func Keccak256Hash(data ...[]byte) (h common.Hash) {
d := hasherPool.Get().(KeccakState) d := hasherPool.Get().(KeccakState)
d.Reset() d.Reset()
for _, b := range data { for _, b := range data {
@@ -122,15 +124,15 @@ func Keccak512(data ...[]byte) []byte {
} }
// CreateAddress creates an ethereum address given the bytes and the nonce // CreateAddress creates an ethereum address given the bytes and the nonce
func CreateAddress(b utils.Address, nonce uint64) utils.Address { func CreateAddress(b common.Address, nonce uint64) common.Address {
data, _ := utils.EncodeToBytes([]interface{}{b, nonce}) data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
return utils.BytesToAddress(Keccak256(data)[12:]) return common.BytesToAddress(Keccak256(data)[12:])
} }
// CreateAddress2 creates an ethereum address given the address bytes, initial // CreateAddress2 creates an ethereum address given the address bytes, initial
// contract code hash and a salt. // contract code hash and a salt.
func CreateAddress2(b utils.Address, salt [32]byte, inithash []byte) utils.Address { func CreateAddress2(b common.Address, salt [32]byte, inithash []byte) common.Address {
return utils.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash)[12:]) return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash)[12:])
} }
// ToECDSA creates a private key with the given D value. // ToECDSA creates a private key with the given D value.
@@ -178,7 +180,7 @@ func FromECDSA(priv *ecdsa.PrivateKey) []byte {
if priv == nil { if priv == nil {
return nil return nil
} }
return utils.PaddedBigBytes(priv.D, priv.Params().BitSize/8) return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8)
} }
// UnmarshalPubkey converts bytes to a secp256k1 public key. // UnmarshalPubkey converts bytes to a secp256k1 public key.
@@ -284,7 +286,7 @@ func GenerateKey() (*ecdsa.PrivateKey, error) {
// ValidateSignatureValues verifies whether the signature values are valid with // ValidateSignatureValues verifies whether the signature values are valid with
// the given chain rules. The v value is assumed to be either 0 or 1. // the given chain rules. The v value is assumed to be either 0 or 1.
func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool { func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool {
if r.Cmp(utils.Big1) < 0 || s.Cmp(utils.Big1) < 0 { if r.Cmp(common.Big1) < 0 || s.Cmp(common.Big1) < 0 {
return false return false
} }
// reject upper range of s values (ECDSA malleability) // reject upper range of s values (ECDSA malleability)
@@ -296,9 +298,9 @@ func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool {
return r.Cmp(secp256k1N) < 0 && s.Cmp(secp256k1N) < 0 && (v == 0 || v == 1) return r.Cmp(secp256k1N) < 0 && s.Cmp(secp256k1N) < 0 && (v == 0 || v == 1)
} }
func PubkeyToAddress(p ecdsa.PublicKey) utils.Address { func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
pubBytes := FromECDSAPub(&p) pubBytes := FromECDSAPub(&p)
return utils.BytesToAddress(Keccak256(pubBytes[1:])[12:]) return common.BytesToAddress(Keccak256(pubBytes[1:])[12:])
} }
func zeroBytes(bytes []byte) { func zeroBytes(bytes []byte) {
+15 -22
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -26,20 +26,13 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/luxfi/crypto/utils" "github.com/luxfi/geth/common"
"github.com/luxfi/geth/common/hexutil"
) )
var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791" var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032" var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
func mustDecodeBig(s string) *big.Int {
v, ok := utils.ParseBig256(s)
if !ok {
panic("invalid big integer")
}
return v
}
// These tests are sanity checks. // These tests are sanity checks.
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256 // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
// and that the sha3 library uses keccak-f permutation. // and that the sha3 library uses keccak-f permutation.
@@ -86,8 +79,8 @@ func TestUnmarshalPubkey(t *testing.T) {
enc, _ = hex.DecodeString("04760c4460e5336ac9bbd87952a3c7ec4363fc0a97bd31c86430806e287b437fd1b01abc6e1db640cf3106b520344af1d58b00b57823db3e1407cbc433e1b6d04d") enc, _ = hex.DecodeString("04760c4460e5336ac9bbd87952a3c7ec4363fc0a97bd31c86430806e287b437fd1b01abc6e1db640cf3106b520344af1d58b00b57823db3e1407cbc433e1b6d04d")
dec = &ecdsa.PublicKey{ dec = &ecdsa.PublicKey{
Curve: S256(), Curve: S256(),
X: mustDecodeBig("0x760c4460e5336ac9bbd87952a3c7ec4363fc0a97bd31c86430806e287b437fd1"), X: hexutil.MustDecodeBig("0x760c4460e5336ac9bbd87952a3c7ec4363fc0a97bd31c86430806e287b437fd1"),
Y: mustDecodeBig("0xb01abc6e1db640cf3106b520344af1d58b00b57823db3e1407cbc433e1b6d04d"), Y: hexutil.MustDecodeBig("0xb01abc6e1db640cf3106b520344af1d58b00b57823db3e1407cbc433e1b6d04d"),
} }
) )
key, err = UnmarshalPubkey(enc) key, err = UnmarshalPubkey(enc)
@@ -101,7 +94,7 @@ func TestUnmarshalPubkey(t *testing.T) {
func TestSign(t *testing.T) { func TestSign(t *testing.T) {
key, _ := HexToECDSA(testPrivHex) key, _ := HexToECDSA(testPrivHex)
addr := utils.HexToAddress(testAddrHex) addr := common.HexToAddress(testAddrHex)
msg := Keccak256([]byte("foo")) msg := Keccak256([]byte("foo"))
sig, err := Sign(msg, key) sig, err := Sign(msg, key)
@@ -140,7 +133,7 @@ func TestInvalidSign(t *testing.T) {
func TestNewContractAddress(t *testing.T) { func TestNewContractAddress(t *testing.T) {
key, _ := HexToECDSA(testPrivHex) key, _ := HexToECDSA(testPrivHex)
addr := utils.HexToAddress(testAddrHex) addr := common.HexToAddress(testAddrHex)
genAddr := PubkeyToAddress(key.PublicKey) genAddr := PubkeyToAddress(key.PublicKey)
// sanity check before using addr to create contract address // sanity check before using addr to create contract address
checkAddr(t, genAddr, addr) checkAddr(t, genAddr, addr)
@@ -148,9 +141,9 @@ func TestNewContractAddress(t *testing.T) {
caddr0 := CreateAddress(addr, 0) caddr0 := CreateAddress(addr, 0)
caddr1 := CreateAddress(addr, 1) caddr1 := CreateAddress(addr, 1)
caddr2 := CreateAddress(addr, 2) caddr2 := CreateAddress(addr, 2)
checkAddr(t, utils.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0) checkAddr(t, common.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0)
checkAddr(t, utils.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1) checkAddr(t, common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1)
checkAddr(t, utils.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2) checkAddr(t, common.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2)
} }
func TestLoadECDSA(t *testing.T) { func TestLoadECDSA(t *testing.T) {
@@ -238,9 +231,9 @@ func TestValidateSignatureValues(t *testing.T) {
} }
} }
minusOne := big.NewInt(-1) minusOne := big.NewInt(-1)
one := utils.Big1 one := common.Big1
zero := utils.Big0 zero := common.Big0
secp256k1nMinus1 := new(big.Int).Sub(secp256k1N, utils.Big1) secp256k1nMinus1 := new(big.Int).Sub(secp256k1N, common.Big1)
// correct v,r,s // correct v,r,s
check(true, 0, one, one) check(true, 0, one, one)
@@ -284,7 +277,7 @@ func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte
} }
} }
func checkAddr(t *testing.T, addr0, addr1 utils.Address) { func checkAddr(t *testing.T, addr0, addr1 common.Address) {
if addr0 != addr1 { if addr0 != addr1 {
t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1) t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1)
} }
@@ -299,7 +292,7 @@ func TestPythonIntegration(t *testing.T) {
msg0 := Keccak256([]byte("foo")) msg0 := Keccak256([]byte("foo"))
sig0, _ := Sign(msg0, k0) sig0, _ := Sign(msg0, k0)
msg1 := utils.FromHex("00000000000000000000000000000000") msg1 := common.FromHex("00000000000000000000000000000000")
sig1, _ := Sign(msg0, k0) sig1, _ := Sign(msg0, k0)
t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0) t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0)
+3 -3
View File
@@ -41,7 +41,7 @@ import (
"io" "io"
"math/big" "math/big"
ethcrypto "github.com/luxfi/crypto" "github.com/luxfi/crypto"
) )
var ( var (
@@ -257,7 +257,7 @@ func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err e
d := messageTag(params.Hash, Km, em, s2) d := messageTag(params.Hash, Km, em, s2)
if curve, ok := pub.Curve.(ethcrypto.EllipticCurve); ok { if curve, ok := pub.Curve.(crypto.EllipticCurve); ok {
Rb := curve.Marshal(R.PublicKey.X, R.PublicKey.Y) Rb := curve.Marshal(R.PublicKey.X, R.PublicKey.Y)
ct = make([]byte, len(Rb)+len(em)+len(d)) ct = make([]byte, len(Rb)+len(em)+len(d))
copy(ct, Rb) copy(ct, Rb)
@@ -303,7 +303,7 @@ func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {
R := new(PublicKey) R := new(PublicKey)
R.Curve = prv.PublicKey.Curve R.Curve = prv.PublicKey.Curve
if curve, ok := R.Curve.(ethcrypto.EllipticCurve); ok { if curve, ok := R.Curve.(crypto.EllipticCurve); ok {
R.X, R.Y = curve.Unmarshal(c[:rLen]) R.X, R.Y = curve.Unmarshal(c[:rLen])
if R.X == nil { if R.X == nil {
return nil, ErrInvalidPublicKey return nil, ErrInvalidPublicKey
+8 -8
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -24,7 +24,7 @@ import (
"reflect" "reflect"
"sync/atomic" "sync/atomic"
"github.com/luxfi/crypto/utils" "github.com/luxfi/geth/common/hexutil"
) )
//go:embed trusted_setup.json //go:embed trusted_setup.json
@@ -43,12 +43,12 @@ type Blob [131072]byte
// UnmarshalJSON parses a blob in hex syntax. // UnmarshalJSON parses a blob in hex syntax.
func (b *Blob) UnmarshalJSON(input []byte) error { func (b *Blob) UnmarshalJSON(input []byte) error {
return utils.UnmarshalFixedJSON(blobT, input, b[:]) return hexutil.UnmarshalFixedJSON(blobT, input, b[:])
} }
// MarshalText returns the hex representation of b. // MarshalText returns the hex representation of b.
func (b *Blob) MarshalText() ([]byte, error) { func (b *Blob) MarshalText() ([]byte, error) {
return utils.Bytes(b[:]).MarshalText() return hexutil.Bytes(b[:]).MarshalText()
} }
// Commitment is a serialized commitment to a polynomial. // Commitment is a serialized commitment to a polynomial.
@@ -56,12 +56,12 @@ type Commitment [48]byte
// UnmarshalJSON parses a commitment in hex syntax. // UnmarshalJSON parses a commitment in hex syntax.
func (c *Commitment) UnmarshalJSON(input []byte) error { func (c *Commitment) UnmarshalJSON(input []byte) error {
return utils.UnmarshalFixedJSON(commitmentT, input, c[:]) return hexutil.UnmarshalFixedJSON(commitmentT, input, c[:])
} }
// MarshalText returns the hex representation of c. // MarshalText returns the hex representation of c.
func (c Commitment) MarshalText() ([]byte, error) { func (c Commitment) MarshalText() ([]byte, error) {
return utils.Bytes(c[:]).MarshalText() return hexutil.Bytes(c[:]).MarshalText()
} }
// Proof is a serialized commitment to the quotient polynomial. // Proof is a serialized commitment to the quotient polynomial.
@@ -69,12 +69,12 @@ type Proof [48]byte
// UnmarshalJSON parses a proof in hex syntax. // UnmarshalJSON parses a proof in hex syntax.
func (p *Proof) UnmarshalJSON(input []byte) error { func (p *Proof) UnmarshalJSON(input []byte) error {
return utils.UnmarshalFixedJSON(proofT, input, p[:]) return hexutil.UnmarshalFixedJSON(proofT, input, p[:])
} }
// MarshalText returns the hex representation of p. // MarshalText returns the hex representation of p.
func (p Proof) MarshalText() ([]byte, error) { func (p Proof) MarshalText() ([]byte, error) {
return utils.Bytes(p[:]).MarshalText() return hexutil.Bytes(p[:]).MarshalText()
} }
// Point is a BLS field element. // Point is a BLS field element.
+5 -5
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -25,7 +25,7 @@ import (
gokzg4844 "github.com/crate-crypto/go-eth-kzg" gokzg4844 "github.com/crate-crypto/go-eth-kzg"
ckzg4844 "github.com/ethereum/c-kzg-4844/v2/bindings/go" ckzg4844 "github.com/ethereum/c-kzg-4844/v2/bindings/go"
"github.com/luxfi/crypto/utils" "github.com/luxfi/geth/common/hexutil"
) )
// ckzgAvailable signals whether the library was compiled into Geth. // ckzgAvailable signals whether the library was compiled into Geth.
@@ -49,15 +49,15 @@ func ckzgInit() {
} }
g1Lag := make([]byte, len(params.SetupG1Lagrange)*(len(params.SetupG1Lagrange[0])-2)/2) g1Lag := make([]byte, len(params.SetupG1Lagrange)*(len(params.SetupG1Lagrange[0])-2)/2)
for i, g1 := range params.SetupG1Lagrange { for i, g1 := range params.SetupG1Lagrange {
copy(g1Lag[i*(len(g1)-2)/2:], utils.MustDecode(g1)) copy(g1Lag[i*(len(g1)-2)/2:], hexutil.MustDecode(g1))
} }
g1s := make([]byte, len(params.SetupG1Monomial)*(len(params.SetupG1Monomial[0])-2)/2) g1s := make([]byte, len(params.SetupG1Monomial)*(len(params.SetupG1Monomial[0])-2)/2)
for i, g1 := range params.SetupG1Monomial { for i, g1 := range params.SetupG1Monomial {
copy(g1s[i*(len(g1)-2)/2:], utils.MustDecode(g1)) copy(g1s[i*(len(g1)-2)/2:], hexutil.MustDecode(g1))
} }
g2s := make([]byte, len(params.SetupG2)*(len(params.SetupG2[0])-2)/2) g2s := make([]byte, len(params.SetupG2)*(len(params.SetupG2[0])-2)/2)
for i, g2 := range params.SetupG2 { for i, g2 := range params.SetupG2 {
copy(g2s[i*(len(g2)-2)/2:], utils.MustDecode(g2)) copy(g2s[i*(len(g2)-2)/2:], hexutil.MustDecode(g2))
} }
// The last parameter determines the multiplication table, see https://notes.ethereum.org/@jtraglia/windowed_multiplications // The last parameter determines the multiplication table, see https://notes.ethereum.org/@jtraglia/windowed_multiplications
// I think 6 is an decent compromise between size and speed // I think 6 is an decent compromise between size and speed
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
+10 -43
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -23,15 +23,14 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"errors" "errors"
"fmt" "fmt"
"math/big"
"github.com/luxfi/crypto/secp256k1" "github.com/luxfi/geth/common/math"
"github.com/luxfi/crypto/utils" luxSecp256k1 "github.com/luxfi/crypto/secp256k1"
) )
// Ecrecover returns the uncompressed public key that created the given signature. // Ecrecover returns the uncompressed public key that created the given signature.
func Ecrecover(hash, sig []byte) ([]byte, error) { func Ecrecover(hash, sig []byte) ([]byte, error) {
return secp256k1.RecoverPubkey(hash, sig) return luxSecp256k1.RecoverPubkey(hash, sig)
} }
// SigToPub returns the public key that created the given signature. // SigToPub returns the public key that created the given signature.
@@ -55,21 +54,21 @@ func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
if len(digestHash) != DigestLength { if len(digestHash) != DigestLength {
return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash)) return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash))
} }
seckey := utils.PaddedBigBytes(prv.D, prv.Params().BitSize/8) seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
defer zeroBytes(seckey) defer zeroBytes(seckey)
return secp256k1.Sign(digestHash, seckey) return luxSecp256k1.Sign(digestHash, seckey)
} }
// VerifySignature checks that the given public key created signature over digest. // VerifySignature checks that the given public key created signature over digest.
// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. // The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
// The signature should have the 64 byte [R || S] format. // The signature should have the 64 byte [R || S] format.
func VerifySignature(pubkey, digestHash, signature []byte) bool { func VerifySignature(pubkey, digestHash, signature []byte) bool {
return secp256k1.VerifySignature(pubkey, digestHash, signature) return luxSecp256k1.VerifySignature(pubkey, digestHash, signature)
} }
// DecompressPubkey parses a public key in the 33-byte compressed format. // DecompressPubkey parses a public key in the 33-byte compressed format.
func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) { func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
x, y := secp256k1.DecompressPubkey(pubkey) x, y := luxSecp256k1.DecompressPubkey(pubkey)
if x == nil { if x == nil {
return nil, errors.New("invalid public key") return nil, errors.New("invalid public key")
} }
@@ -78,42 +77,10 @@ func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
// CompressPubkey encodes a public key to the 33-byte compressed format. // CompressPubkey encodes a public key to the 33-byte compressed format.
func CompressPubkey(pubkey *ecdsa.PublicKey) []byte { func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
return secp256k1.CompressPubkey(pubkey.X, pubkey.Y) return luxSecp256k1.CompressPubkey(pubkey.X, pubkey.Y)
} }
// S256 returns an instance of the secp256k1 curve. // S256 returns an instance of the secp256k1 curve.
func S256() EllipticCurve { func S256() EllipticCurve {
return btCurve{secp256k1.S256()} return luxSecp256k1.S256()
}
type btCurve struct {
*secp256k1.BitCurve
}
// Marshal converts a point given as (x, y) into a byte slice.
func (curve btCurve) Marshal(x, y *big.Int) []byte {
byteLen := (curve.Params().BitSize + 7) / 8
ret := make([]byte, 1+2*byteLen)
ret[0] = 4 // uncompressed point
x.FillBytes(ret[1 : 1+byteLen])
y.FillBytes(ret[1+byteLen : 1+2*byteLen])
return ret
}
// Unmarshal converts a point, serialised by Marshal, into an x, y pair. On
// error, x = nil.
func (curve btCurve) Unmarshal(data []byte) (x, y *big.Int) {
byteLen := (curve.Params().BitSize + 7) / 8
if len(data) != 1+2*byteLen {
return nil, nil
}
if data[0] != 4 { // uncompressed form
return nil, nil
}
x = new(big.Int).SetBytes(data[1 : 1+byteLen])
y = new(big.Int).SetBytes(data[1+byteLen:])
return
} }
+1 -18
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -21,7 +21,6 @@ package crypto
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
@@ -165,22 +164,6 @@ type btCurve struct {
*secp256k1.KoblitzCurve *secp256k1.KoblitzCurve
} }
// Params returns the parameters for the curve.
func (curve btCurve) Params() *elliptic.CurveParams {
return curve.CurveParams
}
// IsOnCurve returns whether the given (x,y) lies on the curve.
func (curve btCurve) IsOnCurve(x, y *big.Int) bool {
return curve.KoblitzCurve.IsOnCurve(x, y)
}
// ScalarBaseMult returns k*G, where G is the base point of the group
// and k is an integer in big-endian form.
func (curve btCurve) ScalarBaseMult(k []byte) (x, y *big.Int) {
return curve.KoblitzCurve.ScalarBaseMult(k)
}
// Marshal converts a point given as (x, y) into a byte slice. // Marshal converts a point given as (x, y) into a byte slice.
func (curve btCurve) Marshal(x, y *big.Int) []byte { func (curve btCurve) Marshal(x, y *big.Int) []byte {
byteLen := (curve.Params().BitSize + 7) / 8 byteLen := (curve.Params().BitSize + 7) / 8
+16 -22
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -22,24 +22,18 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/luxfi/crypto/utils" "github.com/luxfi/geth/common"
"github.com/luxfi/geth/common/hexutil"
"github.com/luxfi/geth/common/math"
) )
var ( var (
testmsg = mustDecode("0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008") testmsg = hexutil.MustDecode("0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
testsig = mustDecode("0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301") testsig = hexutil.MustDecode("0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
testpubkey = mustDecode("0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652") testpubkey = hexutil.MustDecode("0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
testpubkeyc = mustDecode("0x02e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a") testpubkeyc = hexutil.MustDecode("0x02e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a")
) )
func mustDecode(s string) []byte {
b := utils.FromHex(s)
if b == nil {
panic("invalid hex string")
}
return b
}
func TestEcrecover(t *testing.T) { func TestEcrecover(t *testing.T) {
pubkey, err := Ecrecover(testmsg, testsig) pubkey, err := Ecrecover(testmsg, testsig)
if err != nil { if err != nil {
@@ -68,13 +62,13 @@ func TestVerifySignature(t *testing.T) {
if VerifySignature(testpubkey, testmsg, nil) { if VerifySignature(testpubkey, testmsg, nil) {
t.Errorf("nil signature valid") t.Errorf("nil signature valid")
} }
if VerifySignature(testpubkey, testmsg, append(utils.CopyBytes(sig), 1, 2, 3)) { if VerifySignature(testpubkey, testmsg, append(common.CopyBytes(sig), 1, 2, 3)) {
t.Errorf("signature valid with extra bytes at the end") t.Errorf("signature valid with extra bytes at the end")
} }
if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) { if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) {
t.Errorf("signature valid even though it's incomplete") t.Errorf("signature valid even though it's incomplete")
} }
wrongkey := utils.CopyBytes(testpubkey) wrongkey := common.CopyBytes(testpubkey)
wrongkey[10]++ wrongkey[10]++
if VerifySignature(wrongkey, testmsg, sig) { if VerifySignature(wrongkey, testmsg, sig) {
t.Errorf("signature valid with wrong public key") t.Errorf("signature valid with wrong public key")
@@ -83,9 +77,9 @@ func TestVerifySignature(t *testing.T) {
// This test checks that VerifySignature rejects malleable signatures with s > N/2. // This test checks that VerifySignature rejects malleable signatures with s > N/2.
func TestVerifySignatureMalleable(t *testing.T) { func TestVerifySignatureMalleable(t *testing.T) {
sig := mustDecode("0x638a54215d80a6713c8d523a6adc4e6e73652d859103a36b700851cb0e61b66b8ebfc1a610c57d732ec6e0a8f06a9a7a28df5051ece514702ff9cdff0b11f454") sig := hexutil.MustDecode("0x638a54215d80a6713c8d523a6adc4e6e73652d859103a36b700851cb0e61b66b8ebfc1a610c57d732ec6e0a8f06a9a7a28df5051ece514702ff9cdff0b11f454")
key := mustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138") key := hexutil.MustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138")
msg := mustDecode("0xd301ce462d3e639518f482c7f03821fec1e602018630ce621e1e7851c12343a6") msg := hexutil.MustDecode("0xd301ce462d3e639518f482c7f03821fec1e602018630ce621e1e7851c12343a6")
if VerifySignature(key, msg, sig) { if VerifySignature(key, msg, sig) {
t.Error("VerifySignature returned true for malleable signature") t.Error("VerifySignature returned true for malleable signature")
} }
@@ -105,7 +99,7 @@ func TestDecompressPubkey(t *testing.T) {
if _, err := DecompressPubkey(testpubkeyc[:5]); err == nil { if _, err := DecompressPubkey(testpubkeyc[:5]); err == nil {
t.Errorf("no error for incomplete pubkey") t.Errorf("no error for incomplete pubkey")
} }
if _, err := DecompressPubkey(append(utils.CopyBytes(testpubkeyc), 1, 2, 3)); err == nil { if _, err := DecompressPubkey(append(common.CopyBytes(testpubkeyc), 1, 2, 3)); err == nil {
t.Errorf("no error for pubkey with extra bytes at the end") t.Errorf("no error for pubkey with extra bytes at the end")
} }
} }
@@ -113,8 +107,8 @@ func TestDecompressPubkey(t *testing.T) {
func TestCompressPubkey(t *testing.T) { func TestCompressPubkey(t *testing.T) {
key := &ecdsa.PublicKey{ key := &ecdsa.PublicKey{
Curve: S256(), Curve: S256(),
X: utils.MustParseBig256("0xe32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a"), X: math.MustParseBig256("0xe32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a"),
Y: utils.MustParseBig256("0x0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652"), Y: math.MustParseBig256("0x0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652"),
} }
compressed := CompressPubkey(key) compressed := CompressPubkey(key)
if !bytes.Equal(compressed, testpubkeyc) { if !bytes.Equal(compressed, testpubkeyc) {
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2020-2025, Lux Industries Inc // Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@@ -24,7 +24,7 @@ import (
"os" "os"
"testing" "testing"
minisign "github.com/jedisct1/go-minisign" "github.com/jedisct1/go-minisign"
) )
var ( var (