mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
EIP7212 / PIP27 (#1069)
add: EIP7217/PIP27 implementation and related test cases --------- Co-authored-by: Ulaş Erdoğan <uerdogan2001@hotmail.com>
This commit is contained in:
parent
20f92951cc
commit
f57c637814
7 changed files with 152 additions and 13 deletions
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/crypto/bls12381"
|
||||
"github.com/ethereum/go-ethereum/crypto/bn256"
|
||||
"github.com/ethereum/go-ethereum/crypto/kzg4844"
|
||||
"github.com/ethereum/go-ethereum/crypto/secp256r1"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
|
|
@ -105,6 +106,7 @@ var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{
|
|||
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
|
||||
common.BytesToAddress([]byte{9}): &blake2F{},
|
||||
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
|
||||
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
|
||||
}
|
||||
|
||||
// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
|
||||
|
|
@ -1194,3 +1196,37 @@ func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
|
|||
|
||||
return h
|
||||
}
|
||||
|
||||
// P256VERIFY (secp256r1 signature verification)
|
||||
// implemented as a native contract
|
||||
type p256Verify struct{}
|
||||
|
||||
// RequiredGas returns the gas required to execute the precompiled contract
|
||||
func (c *p256Verify) RequiredGas(input []byte) uint64 {
|
||||
return params.P256VerifyGas
|
||||
}
|
||||
|
||||
// Run executes the precompiled contract with given 160 bytes of param, returning the output and the used gas
|
||||
func (c *p256Verify) Run(input []byte) ([]byte, error) {
|
||||
// Required input length is 160 bytes
|
||||
const p256VerifyInputLength = 160
|
||||
// Check the input length
|
||||
if len(input) != p256VerifyInputLength {
|
||||
// Input length is invalid
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Extract the hash, r, s, x, y from the input
|
||||
hash := input[0:32]
|
||||
r, s := new(big.Int).SetBytes(input[32:64]), new(big.Int).SetBytes(input[64:96])
|
||||
x, y := new(big.Int).SetBytes(input[96:128]), new(big.Int).SetBytes(input[128:160])
|
||||
|
||||
// Verify the secp256r1 signature
|
||||
if secp256r1.Verify(hash, r, s, x, y) {
|
||||
// Signature is valid
|
||||
return common.LeftPadBytes(common.Big1.Bytes(), 32), nil
|
||||
} else {
|
||||
// Signature is invalid
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
|
|||
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381Pairing{},
|
||||
common.BytesToAddress([]byte{0x0f, 0x11}): &bls12381MapG1{},
|
||||
common.BytesToAddress([]byte{0x0f, 0x12}): &bls12381MapG2{},
|
||||
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
|
||||
}
|
||||
|
||||
// EIP-152 test vectors
|
||||
|
|
@ -415,3 +416,19 @@ func BenchmarkPrecompiledBLS12381G2MultiExpWorstCase(b *testing.B) {
|
|||
}
|
||||
benchmarkPrecompiled("0f", testcase, b)
|
||||
}
|
||||
|
||||
// Benchmarks the sample inputs from the P256VERIFY precompile.
|
||||
func BenchmarkPrecompiledP256Verify(bench *testing.B) {
|
||||
t := precompiledTest{
|
||||
Input: "4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4da73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d604aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e",
|
||||
Expected: "0000000000000000000000000000000000000000000000000000000000000001",
|
||||
Name: "p256Verify",
|
||||
}
|
||||
benchmarkPrecompiled("100", t, bench)
|
||||
}
|
||||
|
||||
func TestPrecompiledP256Verify(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testJson("p256Verify", "100", t)
|
||||
}
|
||||
|
|
|
|||
37
core/vm/testdata/precompiles/p256Verify.json
vendored
Normal file
37
core/vm/testdata/precompiles/p256Verify.json
vendored
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
[
|
||||
{
|
||||
"Input": "4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4da73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d604aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e",
|
||||
"Expected": "0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"Gas": 3450,
|
||||
"Name": "CallP256Verify",
|
||||
"NoBenchmark": false
|
||||
},
|
||||
{
|
||||
"Input": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9414de3726ee4d237b410c1d85ebcb05553dc578561d9f7942b7250795beb9b9027b657067322fc00ab35263fde0acabf998cd9fcf1282df9555f85dba7bdbbe2dc90f74c9e210bc3e0c60aeaa03729c9e6acde4a048ee58fd2e466c1e7b0374e606b8c22ad2985df7d792ff344f03ce94a079da801006b13640bc5af7932a7b9",
|
||||
"Expected": "0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"Gas": 3450,
|
||||
"Name": "CallP256Verify",
|
||||
"NoBenchmark": false
|
||||
},
|
||||
{
|
||||
"Input": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9b35d6a4f7f6fc5620c97d4287696f5174b3d37fa537b74b5fc26997ba79c725d62fe5e5fe6da76eec924e822c5ef853ede6c17069a9e9133a38f87d61599f68e7d5f3c812a255436846ee84a262b79ec4d0783afccf2433deabdca9ecf62bef5ff24e90988c7f139d378549c3a8bc6c94e6a1c911c1e02e6f48ed65aaf3d296e",
|
||||
"Expected": "0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"Gas": 3450,
|
||||
"Name": "CallP256Verify",
|
||||
"NoBenchmark": false
|
||||
},
|
||||
{
|
||||
"Input": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9c29c3df6ce3431b6f030b1b68b1589508ad9d1a149830489c638653aa4b08af93f6e86a9a7643403b6f5c593410d9f7234a8cd27309bce90447073ce17476850615ff147863bc8652be1e369444f90bbc5f9df05a26362e609f73ab1f1839fe3cd34fd2ae672c110671d49115825fc56b5148321aabe5ba39f2b46f71149cff9",
|
||||
"Expected": "",
|
||||
"Gas": 3450,
|
||||
"Name": "CallP256Verify",
|
||||
"NoBenchmark": false
|
||||
},
|
||||
{
|
||||
"Input": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
|
||||
"Expected": "",
|
||||
"Gas": 3450,
|
||||
"Name": "CallP256Verify",
|
||||
"NoBenchmark": false
|
||||
}
|
||||
]
|
||||
26
crypto/secp256r1/publickey.go
Normal file
26
crypto/secp256r1/publickey.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package secp256r1
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// Generates approptiate public key format from given coordinates
|
||||
func newPublicKey(x, y *big.Int) *ecdsa.PublicKey {
|
||||
// Check if the given coordinates are valid
|
||||
if x == nil || y == nil || !elliptic.P256().IsOnCurve(x, y) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if the given coordinates are the reference point (infinity)
|
||||
if x.Sign() == 0 && y.Sign() == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ecdsa.PublicKey{
|
||||
Curve: elliptic.P256(),
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
}
|
||||
21
crypto/secp256r1/verifier.go
Normal file
21
crypto/secp256r1/verifier.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package secp256r1
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// Verifies the given signature (r, s) for the given hash and public key (x, y).
|
||||
func Verify(hash []byte, r, s, x, y *big.Int) bool {
|
||||
// Create the public key format
|
||||
publicKey := newPublicKey(x, y)
|
||||
|
||||
// Check if they are invalid public key coordinates
|
||||
if publicKey == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Verify the signature with the public key,
|
||||
// then return true if it's valid, false otherwise
|
||||
return ecdsa.Verify(publicKey, hash, r, s)
|
||||
}
|
||||
|
|
@ -842,17 +842,17 @@ func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *bi
|
|||
return parentTotalDiff.Cmp(c.TerminalTotalDifficulty) < 0 && totalDiff.Cmp(c.TerminalTotalDifficulty) >= 0
|
||||
}
|
||||
|
||||
// IsShanghai returns whether time is either equal to the Shanghai fork time or greater.
|
||||
// IsShanghai returns whether num is either equal to the Shanghai fork block or greater.
|
||||
func (c *ChainConfig) IsShanghai(num *big.Int) bool {
|
||||
return isBlockForked(c.ShanghaiBlock, num)
|
||||
}
|
||||
|
||||
// IsCancun returns whether num is either equal to the Cancun fork time or greater.
|
||||
// IsCancun returns whether num is either equal to the Cancun fork block or greater.
|
||||
func (c *ChainConfig) IsCancun(num *big.Int) bool {
|
||||
return isBlockForked(c.CancunBlock, num)
|
||||
}
|
||||
|
||||
// IsPrague returns whether num is either equal to the Prague fork time or greater.
|
||||
// IsPrague returns whether num is either equal to the Prague fork block or greater.
|
||||
func (c *ChainConfig) IsPrague(num *big.Int) bool {
|
||||
return isBlockForked(c.PragueBlock, num)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -160,6 +160,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
|
||||
|
||||
P256VerifyGas uint64 = 3450 // secp256r1 elliptic curve signature verifier gas price
|
||||
|
||||
// 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
|
||||
|
|
|
|||
Loading…
Reference in a new issue