From 52d453f00725dfa373622004bfee2e5316d9040a Mon Sep 17 00:00:00 2001 From: Alok Date: Thu, 26 Jun 2025 15:56:09 +0530 Subject: [PATCH] feat: add bls precompile --- cmd/utils/flags.go | 8 ++++---- core/vm/contracts.go | 32 ++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ params/protocol_params.go | 17 +++++++++-------- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 71d337eabc3..f99aecc46fb 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -23,10 +23,6 @@ package utils import ( "crypto/ecdsa" "fmt" - "github.com/c2h5oh/datasize" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/urfave/cli/v2" "math/big" "path/filepath" "runtime" @@ -34,6 +30,7 @@ import ( "strings" "time" + "github.com/c2h5oh/datasize" "github.com/erigontech/erigon-lib/chain/networkid" "github.com/erigontech/erigon-lib/chain/networkname" "github.com/erigontech/erigon-lib/chain/snapcfg" @@ -66,6 +63,9 @@ import ( "github.com/erigontech/erigon/turbo/logging" "github.com/erigontech/erigon/txnprovider/shutter" "github.com/erigontech/erigon/txnprovider/txpool/txpoolcfg" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/urfave/cli/v2" ) // These are all the command line flags we support. diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 8387a802921..9c88d8244ed 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -25,6 +25,7 @@ import ( "errors" "math/big" + "github.com/cloudflare/circl/sign/bls" "github.com/consensys/gnark-crypto/ecc" bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381" "github.com/consensys/gnark-crypto/ecc/bls12-381/fp" @@ -101,6 +102,9 @@ var PrecompiledContractsBerlin = map[libcommon.Address]PrecompiledContract{ libcommon.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, libcommon.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, libcommon.BytesToAddress([]byte{9}): &blake2F{}, + + // primev pre-compiles start at 0xf addresses + libcommon.BytesToAddress([]byte{0xf0}): &bls12381SignatureVerification{}, } var PrecompiledContractsCancun = map[libcommon.Address]PrecompiledContract{ @@ -1173,3 +1177,31 @@ func (c *p256Verify) Run(input []byte) ([]byte, error) { return nil, nil } } + +// bls12381SignatureVerification implements BLS signature verification precompile.Add commentMore actions +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 +} diff --git a/go.mod b/go.mod index 05a5425b546..011b97afec4 100644 --- a/go.mod +++ b/go.mod @@ -116,6 +116,7 @@ require ( github.com/RoaringBitmap/roaring v1.9.4 // indirect github.com/alecthomas/atomic v0.1.0-alpha2 // indirect github.com/benesch/cgosymbolizer v0.0.0-20190515212042-bec6fe6e597b // indirect + github.com/cloudflare/circl v1.6.1 // indirect github.com/crate-crypto/go-ipa v0.0.0-20221111143132-9aa5d42120bc // indirect github.com/elastic/go-freelru v0.13.0 // indirect github.com/erigontech/speedtest v0.0.2 // indirect diff --git a/go.sum b/go.sum index f785f062521..8e5a708eb86 100644 --- a/go.sum +++ b/go.sum @@ -194,6 +194,8 @@ github.com/cilium/ebpf v0.2.0/go.mod h1:To2CFviqOWL/M0gIMsvSMlqe7em/l1ALkX1PyjrX github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y= github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0= +github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= diff --git a/params/protocol_params.go b/params/protocol_params.go index 845e198c7fc..530fe9b19a8 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -157,14 +157,15 @@ const ( Bn256PairingPerPointGasByzantium uint64 = 80000 // Byzantium per-point price for an elliptic curve pairing check Bn256PairingPerPointGasIstanbul uint64 = 34000 // Per-point price for an elliptic curve pairing check - Bls12381G1AddGas uint64 = 375 // Price for BLS12-381 elliptic curve G1 point addition - Bls12381G1MulGas uint64 = 12000 // Price for BLS12-381 elliptic curve G1 point scalar multiplication - Bls12381G2AddGas uint64 = 600 // Price for BLS12-381 elliptic curve G2 point addition - Bls12381G2MulGas uint64 = 22500 // Price for BLS12-381 elliptic curve G2 point scalar multiplication - Bls12381PairingBaseGas uint64 = 37700 // Base gas price for BLS12-381 elliptic curve pairing check - Bls12381PairingPerPairGas uint64 = 32600 // Per-point pair gas price for BLS12-381 elliptic curve pairing check - Bls12381MapFpToG1Gas uint64 = 5500 // Gas price for BLS12-381 mapping field element to G1 operation - Bls12381MapFp2ToG2Gas uint64 = 23800 // Gas price for BLS12-381 mapping field element to G2 operation + Bls12381G1AddGas uint64 = 375 // Price for BLS12-381 elliptic curve G1 point addition + Bls12381G1MulGas uint64 = 12000 // Price for BLS12-381 elliptic curve G1 point scalar multiplication + Bls12381G2AddGas uint64 = 600 // Price for BLS12-381 elliptic curve G2 point addition + Bls12381G2MulGas uint64 = 22500 // Price for BLS12-381 elliptic curve G2 point scalar multiplication + Bls12381PairingBaseGas uint64 = 37700 // Base gas price for BLS12-381 elliptic curve pairing check + Bls12381PairingPerPairGas uint64 = 32600 // Per-point pair gas price for BLS12-381 elliptic curve pairing check + Bls12381MapFpToG1Gas uint64 = 5500 // Gas price for BLS12-381 mapping field element to G1 operation + Bls12381MapFp2ToG2Gas uint64 = 23800 // 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.