core/vm: add name method for each precompile

This commit is contained in:
lightclient 2025-07-18 08:53:44 -06:00
parent 52ec2b5f47
commit 4726af0f83
No known key found for this signature in database
GPG key ID: 657913021EF45A6A

View file

@ -47,6 +47,7 @@ import (
type PrecompiledContract interface {
RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use
Run(input []byte) ([]byte, error) // Run runs the precompiled contract
Name() string
}
// PrecompiledContracts contains the precompiled contracts supported at the given fork.
@ -309,6 +310,10 @@ func (c *ecrecover) Run(input []byte) ([]byte, error) {
return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil
}
func (c *ecrecover) Name() string {
return "ECREC"
}
// SHA256 implemented as a native contract.
type sha256hash struct{}
@ -324,6 +329,10 @@ func (c *sha256hash) Run(input []byte) ([]byte, error) {
return h[:], nil
}
func (c *sha256hash) Name() string {
return "SHA256"
}
// RIPEMD160 implemented as a native contract.
type ripemd160hash struct{}
@ -340,6 +349,10 @@ func (c *ripemd160hash) Run(input []byte) ([]byte, error) {
return common.LeftPadBytes(ripemd.Sum(nil), 32), nil
}
func (c *ripemd160hash) Name() string {
return "RIPEMD160"
}
// data copy implemented as a native contract.
type dataCopy struct{}
@ -354,6 +367,10 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
return common.CopyBytes(in), nil
}
func (c *dataCopy) Name() string {
return "ID"
}
// bigModExp implements a native big integer exponential modular operation.
type bigModExp struct {
eip2565 bool
@ -543,6 +560,10 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
return common.LeftPadBytes(v, int(modLen)), nil
}
func (c *bigModExp) Name() string {
return "MODEXP"
}
// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,
// returning it, or an error if the point is invalid.
func newCurvePoint(blob []byte) (*bn256.G1, error) {
@ -592,6 +613,10 @@ func (c *bn256AddIstanbul) Run(input []byte) ([]byte, error) {
return runBn256Add(input)
}
func (c *bn256AddIstanbul) Name() string {
return "BN256_ADD"
}
// bn256AddByzantium implements a native elliptic curve point addition
// conforming to Byzantium consensus rules.
type bn256AddByzantium struct{}
@ -605,6 +630,10 @@ func (c *bn256AddByzantium) Run(input []byte) ([]byte, error) {
return runBn256Add(input)
}
func (c *bn256AddByzantium) Name() string {
return "BN256_ADD"
}
// runBn256ScalarMul implements the Bn256ScalarMul precompile, referenced by
// both Byzantium and Istanbul operations.
func runBn256ScalarMul(input []byte) ([]byte, error) {
@ -630,6 +659,10 @@ func (c *bn256ScalarMulIstanbul) Run(input []byte) ([]byte, error) {
return runBn256ScalarMul(input)
}
func (c *bn256ScalarMulIstanbul) Name() string {
return "BN256_MUL"
}
// bn256ScalarMulByzantium implements a native elliptic curve scalar
// multiplication conforming to Byzantium consensus rules.
type bn256ScalarMulByzantium struct{}
@ -643,6 +676,10 @@ func (c *bn256ScalarMulByzantium) Run(input []byte) ([]byte, error) {
return runBn256ScalarMul(input)
}
func (c *bn256ScalarMulByzantium) Name() string {
return "BN256_MUL"
}
var (
// true32Byte is returned if the bn256 pairing check succeeds.
true32Byte = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
@ -698,6 +735,10 @@ func (c *bn256PairingIstanbul) Run(input []byte) ([]byte, error) {
return runBn256Pairing(input)
}
func (c *bn256PairingIstanbul) Name() string {
return "BN256_PAIRING"
}
// bn256PairingByzantium implements a pairing pre-compile for the bn256 curve
// conforming to Byzantium consensus rules.
type bn256PairingByzantium struct{}
@ -711,6 +752,10 @@ func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
return runBn256Pairing(input)
}
func (c *bn256PairingByzantium) Name() string {
return "BN256_PAIRING"
}
type blake2F struct{}
func (c *blake2F) RequiredGas(input []byte) uint64 {
@ -772,6 +817,10 @@ func (c *blake2F) Run(input []byte) ([]byte, error) {
return output, nil
}
func (c *blake2F) Name() string {
return "BLAKE2F"
}
var (
errBLS12381InvalidInputLength = errors.New("invalid input length")
errBLS12381InvalidFieldElementTopBytes = errors.New("invalid field element top bytes")
@ -815,6 +864,10 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
return encodePointG1(p0), nil
}
func (c *bls12381G1Add) Name() string {
return "BLS12_G1ADD"
}
// bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile.
type bls12381G1MultiExp struct{}
@ -875,6 +928,10 @@ func (c *bls12381G1MultiExp) Run(input []byte) ([]byte, error) {
return encodePointG1(r), nil
}
func (c *bls12381G1MultiExp) Name() string {
return "BLS12_G1MSM"
}
// bls12381G2Add implements EIP-2537 G2Add precompile.
type bls12381G2Add struct{}
@ -912,6 +969,10 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
return encodePointG2(r), nil
}
func (c *bls12381G2Add) Name() string {
return "BLS12_G2ADD"
}
// bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile.
type bls12381G2MultiExp struct{}
@ -972,6 +1033,10 @@ func (c *bls12381G2MultiExp) Run(input []byte) ([]byte, error) {
return encodePointG2(r), nil
}
func (c *bls12381G2MultiExp) Name() string {
return "BLS12_G2MSM"
}
// bls12381Pairing implements EIP-2537 Pairing precompile.
type bls12381Pairing struct{}
@ -1035,6 +1100,10 @@ func (c *bls12381Pairing) Run(input []byte) ([]byte, error) {
return out, nil
}
func (c *bls12381Pairing) Name() string {
return "BLS12_PAIRING_CHECK"
}
func decodePointG1(in []byte) (*bls12381.G1Affine, error) {
if len(in) != 128 {
return nil, errors.New("invalid g1 point length")
@ -1153,6 +1222,10 @@ func (c *bls12381MapG1) Run(input []byte) ([]byte, error) {
return encodePointG1(&r), nil
}
func (c *bls12381MapG1) Name() string {
return "BLS12_MAP_FP_TO_G1"
}
// bls12381MapG2 implements EIP-2537 MapG2 precompile.
type bls12381MapG2 struct{}
@ -1186,6 +1259,10 @@ func (c *bls12381MapG2) Run(input []byte) ([]byte, error) {
return encodePointG2(&r), nil
}
func (c *bls12381MapG2) Name() string {
return "BLS12_MAP_FP2_TO_G2"
}
// kzgPointEvaluation implements the EIP-4844 point evaluation precompile.
type kzgPointEvaluation struct{}
@ -1242,6 +1319,10 @@ func (b *kzgPointEvaluation) Run(input []byte) ([]byte, error) {
return common.Hex2Bytes(blobPrecompileReturnValue), nil
}
func (c *kzgPointEvaluation) Name() string {
return "KZG_POINT_EVALUATION"
}
// kZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
h := sha256.Sum256(kzg[:])
@ -1277,3 +1358,7 @@ func (c *p256Verify) Run(input []byte) ([]byte, error) {
}
return nil, nil
}
func (c *p256Verify) Name() string {
return "P256VERIFY"
}