# EVM Compatibility Layer

The EVM Compatibility Layer implements the Ethereum Virtual Machine specification to enable Solidity smart contracts to run on IBVM.

## EVM Implementation

IBVM implements the full EVM instruction set as specified in the Ethereum Yellow Paper, with the following modifications:

1. Gas costs denominated in BTC (satoshis) instead of ETH
2. Block gas limit calibrated for Bitcoin block times
3. Modified precompiled contracts for Bitcoin-specific operations

### **Instruction Set Support**

The EVM implementation supports all standard EVM opcodes including:

* Stack manipulation (PUSH, POP, DUP, SWAP)
* Arithmetic operations (ADD, MUL, SUB, DIV, etc.)
* Bitwise operations (AND, OR, XOR, NOT)
* Comparison operations (LT, GT, EQ, etc.)
* Environmental information (CALLER, CALLVALUE, etc.)
* Block information (BLOCKHASH, COINBASE, etc.)
* Memory operations (MLOAD, MSTORE, etc.)
* Storage operations (SLOAD, SSTORE)
* Program control flow (JUMP, JUMPI, etc.)
* System operations (CREATE, CALL, etc.)

### **Gas Computation**

Gas cost in IBVM is calculated similarly to Ethereum but denominated in satoshis:

$gas\_cost = \sum\_{op \in tx} gas(op) \times gas\_price$

Where:

* $gas(op)$ is the base gas cost of each operation
* $gas\_price$ is the user-specified gas price in satoshis/gas unit

## State Representation

The IBVM state is organized as a Merkle Patricia Trie, similar to Ethereum:

1. Account State: A mapping of addresses to account states
   1. Each account has: nonce, balance, storageRoot, codeHash
   2. $AccountState = (nonce, balance, storageRoot, codeHash)$
2. Storage Trie: For each account, a separate trie maps 256-bit keys to 256-bit values
   1. $Storage: 2^{256} \rightarrow 2^{256}$
3. State Root: A 256-bit hash that uniquely identifies the entire state
   1. $StateRoot = H(AccountState\_1 || AccountState\_2 || ... || AccountState\_n)$

## Account Representation

Each account in IBVM is represented as:

$Account = (nonce, balance, storageRoot, codeHash)$

Where:

* $nonce$: Counter used to ensure each transaction is processed only once
* $balance$: Amount of BTC held by the account (in satoshis)
* $storageRoot$: Root hash of the storage trie
* $codeHash$: Hash of the account's contract code (or 0 if not a contract)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ibvm.gitbook.io/ibvm-doc/evm-compatibility-layer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
