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