Skip to content

Commit c0d2051

Browse files
0xmountaintopjonastheisThegaram
authored
feat: implement RIP-7212/EIP-7212 (#943)
feat: implement RIP-7212/EIP-7212 (#798) Implement RIP-7212/EIP-7212 according to reference implementation at ulerdogan#1 Co-authored-by: Jonas Theis <4181434+jonastheis@users.noreply.github.com> Co-authored-by: Péter Garamvölgyi <peter@scroll.io>
1 parent f8bfb3a commit c0d2051

File tree

6 files changed

+5571
-0
lines changed

6 files changed

+5571
-0
lines changed

core/vm/contracts.go

+41
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/scroll-tech/go-ethereum/crypto/bls12381"
3131
"github.com/scroll-tech/go-ethereum/crypto/bn256"
3232
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
33+
"github.com/scroll-tech/go-ethereum/crypto/secp256r1"
3334
"github.com/scroll-tech/go-ethereum/params"
3435
"golang.org/x/crypto/ripemd160"
3536
)
@@ -126,6 +127,12 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{
126127
common.BytesToAddress([]byte{18}): &bls12381MapG2{},
127128
}
128129

130+
// PrecompiledContractsP256Verify contains the precompiled Ethereum
131+
// contract specified in EIP-7212/RIP-7212. This is exported for testing purposes.
132+
var PrecompiledContractsP256Verify = map[common.Address]PrecompiledContract{
133+
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
134+
}
135+
129136
// PrecompiledContractsArchimedes contains the default set of pre-compiled Ethereum
130137
// contracts used in the Archimedes release. Same as Berlin but without sha2, blake2f, ripemd160
131138
var PrecompiledContractsArchimedes = map[common.Address]PrecompiledContract{
@@ -1224,3 +1231,37 @@ func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
12241231

12251232
return h
12261233
}
1234+
1235+
// P256VERIFY (secp256r1 signature verification)
1236+
// implemented as a native contract
1237+
type p256Verify struct{}
1238+
1239+
// RequiredGas returns the gas required to execute the precompiled contract
1240+
func (c *p256Verify) RequiredGas(input []byte) uint64 {
1241+
return params.P256VerifyGas
1242+
}
1243+
1244+
// Run executes the precompiled contract with given 160 bytes of param, returning the output and the used gas
1245+
func (c *p256Verify) Run(input []byte) ([]byte, error) {
1246+
// Required input length is 160 bytes
1247+
const p256VerifyInputLength = 160
1248+
// Check the input length
1249+
if len(input) != p256VerifyInputLength {
1250+
// Input length is invalid
1251+
return nil, nil
1252+
}
1253+
1254+
// Extract the hash, r, s, x, y from the input
1255+
hash := input[0:32]
1256+
r, s := new(big.Int).SetBytes(input[32:64]), new(big.Int).SetBytes(input[64:96])
1257+
x, y := new(big.Int).SetBytes(input[96:128]), new(big.Int).SetBytes(input[128:160])
1258+
1259+
// Verify the secp256r1 signature
1260+
if secp256r1.Verify(hash, r, s, x, y) {
1261+
// Signature is valid
1262+
return common.LeftPadBytes([]byte{1}, 32), nil
1263+
} else {
1264+
// Signature is invalid
1265+
return nil, nil
1266+
}
1267+
}

core/vm/contracts_test.go

+16
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{0x01, 0x00}): &p256Verify{},
7072
}
7173

7274
// EIP-152 test vectors
@@ -398,3 +400,17 @@ func BenchmarkPrecompiledBLS12381G2MultiExpWorstCase(b *testing.B) {
398400
}
399401
benchmarkPrecompiled("0f", testcase, b)
400402
}
403+
404+
// Benchmarks the sample inputs from the P256VERIFY precompile.
405+
func BenchmarkPrecompiledP256Verify(bench *testing.B) {
406+
t := precompiledTest{
407+
Input: "4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4da73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d604aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e",
408+
Expected: "0000000000000000000000000000000000000000000000000000000000000001",
409+
Name: "p256Verify",
410+
}
411+
benchmarkPrecompiled("100", t, bench)
412+
}
413+
414+
func TestPrecompiledP256Verify(t *testing.T) {
415+
testJson("p256Verify", "100", t)
416+
}

0 commit comments

Comments
 (0)