Skip to content

Commit

Permalink
feat: introduces BLS signature verification precompile (#52)
Browse files Browse the repository at this point in the history
* feat: adds BLS precompile

* feat: activates cancun upgrade
  • Loading branch information
ckartik authored Nov 25, 2024
1 parent 5219dc3 commit a9bcf58
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
32 changes: 32 additions & 0 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"math/big"

"github.com/cloudflare/circl/sign/bls"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -105,6 +106,9 @@ var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},

// primev pre-compiles start at 0xf addresses
common.BytesToAddress([]byte{0xf0}): &bls12381SignatureVerification{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
Expand Down Expand Up @@ -653,6 +657,34 @@ var (
errBLS12381G2PointSubgroup = errors.New("g2 point is not on correct subgroup")
)

// bls12381SignatureVerification implements BLS signature verification precompile.
type bls12381SignatureVerification struct{}

// RequiredGas returns the gas required to execute the pre-compiled contract.
func (c *bls12381SignatureVerification) RequiredGas(input []byte) uint64 {
return params.BlsSignVerifyGas
}

func (c *bls12381SignatureVerification) Run(input []byte) ([]byte, error) {
// Input format:
// - pubkey (48 bytes) - G1 point
// - message (32 bytes) - Hash of the message
// - signature (96 bytes) - G2 point
if len(input) != 176 {
return nil, errBLS12381InvalidInputLength
}

var pubKey bls.PublicKey[bls.G1]
if err := pubKey.UnmarshalBinary(input[:48]); err != nil {
return nil, err
}

if !bls.Verify(&pubKey, input[48:80], input[80:]) {
return nil, nil
}
return input[:48], nil
}

// bls12381G1Add implements EIP-2537 G1Add precompile.
type bls12381G1Add struct{}

Expand Down
1 change: 1 addition & 0 deletions geth-poa/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"cancunTime": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"clique": {
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/ethereum/go-ethereum

go 1.20
go 1.22.0

toolchain go1.23.1

require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
Expand Down Expand Up @@ -91,6 +93,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.5.0 // indirect
github.com/cockroachdb/errors v1.8.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/redact v1.0.8 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86c
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.5.0 h1:hxIWksrX6XN5a1L2TI/h53AGPhNHoUBo+TD1ms9+pys=
github.com/cloudflare/circl v1.5.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
github.com/cloudflare/cloudflare-go v0.79.0 h1:ErwCYDjFCYppDJlDJ/5WhsSmzegAUe2+K9qgFyQDg3M=
github.com/cloudflare/cloudflare-go v0.79.0/go.mod h1:gkHQf9xEubaQPEuerBuoinR9P8bf8a05Lq0X6WKy1Oc=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down
2 changes: 2 additions & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ const (
Bls12381MapG1Gas uint64 = 5500 // Gas price for BLS12-381 mapping field element to G1 operation
Bls12381MapG2Gas uint64 = 110000 // Gas price for BLS12-381 mapping field element to G2 operation

BlsSignVerifyGas uint64 = 150000 // Gas price for BLS12-381 signature verification

// The Refund Quotient is the cap on how much of the used gas can be refunded. Before EIP-3529,
// up to half the consumed gas could be refunded. Redefined as 1/5th in EIP-3529
RefundQuotient uint64 = 2
Expand Down

0 comments on commit a9bcf58

Please sign in to comment.