Skip to content

Commit cecdbdc

Browse files
fjlulerdogan
authored andcommitted
core/vm: implement EIP-7951 - precompile for secp256r1 (ethereum#31991)
ethereum/EIPs#9833 Based on ethereum#27540, ethereum#30043 --------- Co-authored-by: Ulaş Erdoğan <[email protected]>
1 parent ba21474 commit cecdbdc

File tree

5 files changed

+5554
-0
lines changed

5 files changed

+5554
-0
lines changed

core/vm/contracts.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ava-labs/libevm/crypto/bls12381"
3131
"github.com/ava-labs/libevm/crypto/bn256"
3232
"github.com/ava-labs/libevm/crypto/kzg4844"
33+
"github.com/ava-labs/libevm/crypto/secp256r1"
3334
"github.com/ava-labs/libevm/params"
3435
"golang.org/x/crypto/ripemd160"
3536
)
@@ -1135,3 +1136,31 @@ func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
11351136

11361137
return h
11371138
}
1139+
1140+
// P256VERIFY (secp256r1 signature verification)
1141+
// implemented as a native contract
1142+
type p256Verify struct{}
1143+
1144+
// RequiredGas returns the gas required to execute the precompiled contract
1145+
func (c *p256Verify) RequiredGas(input []byte) uint64 {
1146+
return params.P256VerifyGas
1147+
}
1148+
1149+
// Run executes the precompiled contract with given 160 bytes of param, returning the output and the used gas
1150+
func (c *p256Verify) Run(input []byte) ([]byte, error) {
1151+
const p256VerifyInputLength = 160
1152+
if len(input) != p256VerifyInputLength {
1153+
return nil, nil
1154+
}
1155+
1156+
// Extract hash, r, s, x, y from the input.
1157+
hash := input[0:32]
1158+
r, s := new(big.Int).SetBytes(input[32:64]), new(big.Int).SetBytes(input[64:96])
1159+
x, y := new(big.Int).SetBytes(input[96:128]), new(big.Int).SetBytes(input[128:160])
1160+
1161+
// Verify the signature.
1162+
if secp256r1.Verify(hash, r, s, x, y) {
1163+
return true32Byte, nil
1164+
}
1165+
return nil, nil
1166+
}

core/vm/contracts_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
6767
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381Pairing{},
6868
common.BytesToAddress([]byte{0x0f, 0x11}): &bls12381MapG1{},
6969
common.BytesToAddress([]byte{0x0f, 0x12}): &bls12381MapG2{},
70+
71+
common.BytesToAddress([]byte{0x0b}): &p256Verify{},
7072
}
7173

7274
// EIP-152 test vectors
@@ -395,3 +397,15 @@ func BenchmarkPrecompiledBLS12381G2MultiExpWorstCase(b *testing.B) {
395397
}
396398
benchmarkPrecompiled("0f", testcase, b)
397399
}
400+
401+
// Benchmarks the sample inputs from the P256VERIFY precompile.
402+
func BenchmarkPrecompiledP256Verify(bench *testing.B) {
403+
t := precompiledTest{
404+
Input: "4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4da73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d604aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e",
405+
Expected: "0000000000000000000000000000000000000000000000000000000000000001",
406+
Name: "p256Verify",
407+
}
408+
benchmarkPrecompiled("0b", t, bench)
409+
}
410+
411+
func TestPrecompiledP256Verify(t *testing.T) { testJson("p256Verify", "0b", t) }

0 commit comments

Comments
 (0)