ethereum.berlin.vm.precompiled_contracts.ecrecoverethereum.london.vm.precompiled_contracts.ecrecover

Ethereum Virtual Machine (EVM) ECRECOVER PRECOMPILED CONTRACT ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementation of the ECRECOVER precompiled contract.

ecrecover

Decrypts the address using elliptic curve DSA recovery mechanism and writes the address to output.

Parameters

evm : The current EVM frame.

def ecrecover(evm: Evm) -> None:
25
    """
26
    Decrypts the address using elliptic curve DSA recovery mechanism and writes
27
    the address to output.
28
29
    Parameters
30
    ----------
31
    evm :
32
        The current EVM frame.
33
    """
34
    data = evm.message.data
35
36
    # GAS
37
    charge_gas(evm, GAS_ECRECOVER)
38
39
    # OPERATION
40
    message_hash_bytes = buffer_read(data, U256(0), U256(32))
41
    message_hash = Hash32(message_hash_bytes)
42
    v = U256.from_be_bytes(buffer_read(data, U256(32), U256(32)))
43
    r = U256.from_be_bytes(buffer_read(data, U256(64), U256(32)))
44
    s = U256.from_be_bytes(buffer_read(data, U256(96), U256(32)))
45
46
    if v != 27 and v != 28:
47
        return
48
    if 0 >= r or r >= SECP256K1N:
49
        return
50
    if 0 >= s or s >= SECP256K1N:
51
        return
52
53
    try:
54
        public_key = secp256k1_recover(r, s, v - 27, message_hash)
55
    except ValueError:
56
        # unable to extract public key
57
        return
58
59
    address = keccak256(public_key)[12:32]
60
    padded_address = left_pad_zero_bytes(address, 32)
61
    evm.output = padded_address