Ethereum Virtual Machine (EVM) ECRECOVER PRECOMPILED CONTRACT

Introduction

Implementation of the ECRECOVER precompiled contract.

Module Contents

Functions

ecrecover

Decrypts the address using elliptic curve DSA recovery mechanism and writes

Module Details

ecrecover

ecrecover(evm: ethereum.dao_fork.vm.Evm)None

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:
    data = evm.message.data

    # GAS
    charge_gas(evm, GAS_ECRECOVER)

    # OPERATION
    message_hash_bytes = buffer_read(data, U256(0), U256(32))
    message_hash = Hash32(message_hash_bytes)
    v = U256.from_be_bytes(buffer_read(data, U256(32), U256(32)))
    r = U256.from_be_bytes(buffer_read(data, U256(64), U256(32)))
    s = U256.from_be_bytes(buffer_read(data, U256(96), U256(32)))

    if v != 27 and v != 28:
        return
    if 0 >= r or r >= SECP256K1N:
        return
    if 0 >= s or s >= SECP256K1N:
        return

    try:
        public_key = secp256k1_recover(r, s, v - 27, message_hash)
    except ValueError:
        # unable to extract public key
        return

    address = keccak256(public_key)[12:32]
    padded_address = left_pad_zero_bytes(address, 32)
    evm.output = padded_address