# Somnia Gas Differences To Ethereum

Somnia's gas costs work very differently to Ethereum's.

For background: EVM gas fees are designed to approximate the load placed on the blockchain. They are used to charge users in proportion to the system resources they consume. A transaction's gas fee (in ETH or SOMI) is calculated as `number of units of gas * price per unit of gas`. The number of units of gas is just the sum of the gas costs of the transaction's operations.

Because the price of a unit of gas is variable, the absolute cost of an operation in units of gas does not ultimately matter. What matters is the relative difference between operations' gas costs. In other words, if you doubled the gas costs for all operations, and halved the price per unit of gas, there would be no functional change.

In regards to determining gas costs, there are two fundamental differences between Ethereum and Somnia:

1. Somnia's architecture and implementation has drastically reduced the real-world performance cost of most EVM operations, but not all. A few operations, such as third-party library precompiles, are just as performant on Ethereum as on Somnia - both use the same code. This relative difference requires us to adjust the relative gas costs of these operations. We could either reduce the gas costs of all operations that Somnia has improved, or increase the gas costs of all operations that Somnia has *not* improved - the result is equivalent. Somnia does the latter, as it fits better into the Ethereum developer ecosystem.
2. Ethereum charges a flat gas cost to read any existing value from storage for the first time in a transaction. This ends up hitting a database whose latency for this read can vary by multiple orders of magnitude. This gas cost inaccuracy is a big problem when scaling these blockchains, as it requires them to leave massive amounts of unused buffer in their execution budget to account for these unpredictable latencies. Somnia has built a new database, IceDb, that is designed to fix this problem. Only the (rare) reads which actually take a long time have a high gas cost; reading from cached state is far cheaper.

These two factors mean that Somnia requires different gas semantics to Ethereum. The differences are detailed in this document.

## Precompiles

These precompiles have flat multiples applied to [the same gas calculation that Ethereum uses](https://www.evm.codes/precompiled).

* `ecRecover (0x01)` costs **50** times the Ethereum calculated gas usage. (i.e. **150,000** gas instead of **3000** gas.)
* `SHA2-256 (0x02)` costs **50** times the Ethereum calculated gas usage.
* `RIPEMD-160 (0x03)` costs **10** times the Ethereum calculated gas usage.
* `modexp (0x05)` costs **10** times the Ethereum calculated gas usage.
* `ecAdd (0x06)` costs **50** times the Ethereum calculated gas usage.
* `ecMul (0x07)` costs **10** times the Ethereum calculated gas usage.
* `ecPairing (0x08)` costs **250** times the Ethereum calculated gas usage.
* `blake2f (0x09)` costs **10** times the Ethereum calculated gas usage.
* `point evaluation (0x0a)` costs **50** times the Ethereum calculated gas usage.

BLS operations:

* `BLS12_G1ADD (0x0b)` costs **220** times the Ethereum calculated gas usage.
* `BLS12_G1MSM (0x0c)` costs **110** times the Ethereum calculated gas usage.
* `BLS12_G2ADD (0x0d)` costs **330** times the Ethereum calculated gas usage.
* `BLS12_G2MSM (0x0e)` costs **50** times the Ethereum calculated gas usage.
* `BLS12_PAIRING_CHECK (0x0f)` costs **50** times the Ethereum calculated gas usage.
* `BLS12_MAP_FP_TO_G1 (0x10)` costs **120** times the Ethereum calculated gas usage.
* `BLS12_MAP_FP2_TO_G2 (0x11)` costs **50** times the Ethereum calculated gas usage.

## Non-storage EVM operations

* `SELFBALANCE` costs **305** gas instead of **5** gas.
* `KECCAK256` costs `1250 + 300 * minimum_word_size` gas instead of `30 + 6 * minimum_word_size` gas.
* `ADDMOD` costs **358** gas instead of **8** gas.
* `MULMOD` costs **358** gas instead of **8** gas.

## Storage EVM operations

Ethereum uses an [access list based gas model](https://www.evm.codes/about#access_list) for all storage operations, to charge extra gas depending on if the value being accessed has already been accessed *in the same transaction*, and if it is creating or deleting that state.

As discussed above, Somnia instead charges extra gas based on a more accurate model of the real world latency it takes to read or write the value in question:

### `SLOAD`

* If the storage slot key is in the set of most recently accessed **128 million** contract slot keys, **no extra gas is charged** (on top of the static op gas fee, which is **100** gas).
* If the key does not exist, the access requires **at least 1,000,000 gas remaining**, but that gas is **not charged**, so there is no extra cost.
* Otherwise, the read costs an additional **1,000,000** gas.

### `SSTORE`

* If the storage slot key is in the set of most recently accessed **128 million** contract slot keys, **no extra gas is charged** (on top of the static op gas fee, which is **100** gas).
* If the key does not exist and is being *written to zero*, the access requires **at least 1,000,000 gas remaining**, but that gas is **not charged**, so there is no extra cost.
* If the key does not exist and is being *written to a non-zero value*, the access requires **at least 1,000,000 gas remaining**, but the caller is charged **200,000** gas.
* Otherwise, the write costs an additional **1,000,000** gas.

### Account operations

The following EVM operations access account state:

* `BALANCE`
* `EXTCODESIZE`
* `EXTCODECOPY`
* `EXTCODEHASH`
* `CALL`
* `CALLCODE`
* `DELEGATECALL`
* `STATICCALL`
* `CREATE`
* `CREATE2`
* `SELFDESTRUCT`

These have an additional storage gas fee (in place of Ethereum's dynamic access list gas fee):

* If the account is in the set of most recently accessed **32 million** accounts, **no extra gas is charged**.
* If the account does not exist and is being *read*, the access requires **at least 1,000,000 gas remaining**, but that gas is **not charged**, so there is no extra cost.
* If the account does not exist and is being *created*, the access requires **at least 1,000,000 gas remaining**, but the caller is charged **400,000** gas for the new account.
* Otherwise, the access costs **1,000,000** gas.

## EVM Logs

Somnia charges an increased amount of gas for EVM logs due to the historical storage requirements.

Ethereum charges: `375 + 375 * topic_count + 8 * size + memory_expansion_cost` gas

Somnia charges: `3200 + 5120 * topic_count + 160 * size + memory_expansion_cost` gas

To illustrate these changes with the five different EVM log operations:

| EVM Op                     | Ethereum gas | Somnia gas |
| -------------------------- | -----------: | ---------: |
| LOG0 with 32 bytes of data |          631 |      8,320 |
| LOG1 with 32 bytes of data |        1,006 |     13,440 |
| LOG2 with 32 bytes of data |        1,381 |     18,560 |
| LOG3 with 32 bytes of data |        1,756 |     23,680 |
| LOG4 with 32 bytes of data |        2,131 |     28,800 |

## Contract bytecode deployment cost

Ethereum charges **200** gas per byte of deployed bytecode. Somnia charges **3125** gas per byte of deployed bytecode.

## EIP-7702 delegation authorisations

Ethereum charges **25,000** gas per authorisation in a [Set Code transaction](https://eips.ethereum.org/EIPS/eip-7702). **12,500** gas is refunded if account creation isn't required.

Somnia charges **1,570,000** gas per authorisation in a Set Code transaction. **400,000** gas is refunded if account creation isn't required.
