ethereum.forks.constantinople.vm.precompiled_contracts.ecrecoverethereum.forks.istanbul.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 | """ |
| 37 | data = evm.message.data |
| 38 | |
| 39 | # GAS |
| 40 | charge_gas(evm, GAS_ECRECOVER) |
| 41 | |
| 42 | # OPERATION |
| 43 | message_hash_bytes = buffer_read(data, U256(0), U256(32)) |
| 44 | message_hash = Hash32(message_hash_bytes) |
| 45 | v = U256.from_be_bytes(buffer_read(data, U256(32), U256(32))) |
| 46 | r = U256.from_be_bytes(buffer_read(data, U256(64), U256(32))) |
| 47 | s = U256.from_be_bytes(buffer_read(data, U256(96), U256(32))) |
| 48 | |
| 49 | if v != U256(27) and v != U256(28): |
| 50 | return |
| 51 | if U256(0) >= r or r >= SECP256K1N: |
| 52 | return |
| 53 | if U256(0) >= s or s >= SECP256K1N: |
| 54 | return |
| 55 | |
| 56 | try: |
| 57 | public_key = secp256k1_recover(r, s, v - U256(27), message_hash) |
| 58 | except InvalidSignatureError: |
| 59 | # unable to extract public key |
| 60 | return |
| 61 | |
| 62 | address = keccak256(public_key)[12:32] |
| 63 | padded_address = left_pad_zero_bytes(address, 32) |
| 64 | evm.output = padded_address |