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