diff --git a/packages/evm/package.json b/packages/evm/package.json index b5a3d071a7..fba80237d7 100644 --- a/packages/evm/package.json +++ b/packages/evm/package.json @@ -58,7 +58,6 @@ "@ethereumjs/statemanager": "^2.3.0", "@ethereumjs/tx": "^5.3.0", "@ethereumjs/util": "^9.0.3", - "@ethereumjs/verkle": "^0.0.2", "@types/debug": "^4.1.9", "debug": "^4.3.3", "ethereum-cryptography": "^2.1.3", diff --git a/packages/evm/src/opcodes/functions.ts b/packages/evm/src/opcodes/functions.ts index d6bdfc423d..7807bc82c7 100644 --- a/packages/evm/src/opcodes/functions.ts +++ b/packages/evm/src/opcodes/functions.ts @@ -26,12 +26,12 @@ import { bytesToHex, concatBytes, ecrecover, + getTreeIndexesForStorageSlot, hexToBytes, publicToAddress, setLengthLeft, setLengthRight, } from '@ethereumjs/util' -import { getTreeIndexesForStorageSlot } from '@ethereumjs/verkle' import { keccak256 } from 'ethereum-cryptography/keccak.js' import { ERROR } from '../exceptions.js' diff --git a/packages/evm/src/opcodes/gas.ts b/packages/evm/src/opcodes/gas.ts index 0ed6688aea..010f996a92 100644 --- a/packages/evm/src/opcodes/gas.ts +++ b/packages/evm/src/opcodes/gas.ts @@ -2,21 +2,19 @@ import { Hardfork } from '@ethereumjs/common' import { Account, Address, + BALANCE_LEAF_KEY, BIGINT_0, BIGINT_1, BIGINT_3, BIGINT_31, BIGINT_32, - bigIntToBytes, - setLengthLeft, -} from '@ethereumjs/util' -import { - BALANCE_LEAF_KEY, CODE_HASH_LEAF_KEY, CODE_SIZE_LEAF_KEY, VERSION_LEAF_KEY, + bigIntToBytes, getTreeIndexesForStorageSlot, -} from '@ethereumjs/verkle' + setLengthLeft, +} from '@ethereumjs/util' import { ERROR } from '../exceptions.js' diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 3b99e04a01..bc3e1462cf 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -65,3 +65,4 @@ export * from './lock.js' export * from './mapDB.js' export * from './provider.js' export * from './requests.js' +export * from './verkle.js' diff --git a/packages/util/src/verkle.ts b/packages/util/src/verkle.ts new file mode 100644 index 0000000000..75694a4167 --- /dev/null +++ b/packages/util/src/verkle.ts @@ -0,0 +1,83 @@ +import { concatBytes, intToBytes } from './bytes.js' + +/** + * Verkle related constants and helper functions + * + * Experimental (do not use in production!) + */ + +// TODO: this comes with a deprecation of the respective constants and methods within the +// @ethereumjs/verkle package. Due to ease of parallel work this has not yet been executed upon, +// so this still needs a small follow-up clean up PR. +// +// Along with the PR likely more/additional helper functionality should be moved over +// (basically everything generic not depending on Verkle cryptography) +// +// Holger Drewes, 2024-06-18 + +export enum LeafType { + Version = 0, + Balance = 1, + Nonce = 2, + CodeHash = 3, + CodeSize = 4, +} + +export const VERSION_LEAF_KEY = intToBytes(LeafType.Version) +export const BALANCE_LEAF_KEY = intToBytes(LeafType.Balance) +export const NONCE_LEAF_KEY = intToBytes(LeafType.Nonce) +export const CODE_HASH_LEAF_KEY = intToBytes(LeafType.CodeHash) +export const CODE_SIZE_LEAF_KEY = intToBytes(LeafType.CodeSize) + +export const HEADER_STORAGE_OFFSET = 64 +export const CODE_OFFSET = 128 +export const VERKLE_NODE_WIDTH = 256 +export const MAIN_STORAGE_OFFSET = BigInt(256) ** BigInt(31) + +/** + * @dev Returns the tree key for a given verkle tree stem, and sub index. + * @dev Assumes that the verkle node width = 256 + * @param stem The 31-bytes verkle tree stem as a Uint8Array. + * @param subIndex The sub index of the tree to generate the key for as a Uint8Array. + * @return The tree key as a Uint8Array. + */ + +export const getKey = (stem: Uint8Array, leaf: LeafType | Uint8Array) => { + switch (leaf) { + case LeafType.Version: + return concatBytes(stem, VERSION_LEAF_KEY) + case LeafType.Balance: + return concatBytes(stem, BALANCE_LEAF_KEY) + case LeafType.Nonce: + return concatBytes(stem, NONCE_LEAF_KEY) + case LeafType.CodeHash: + return concatBytes(stem, CODE_HASH_LEAF_KEY) + case LeafType.CodeSize: + return concatBytes(stem, CODE_SIZE_LEAF_KEY) + default: + return concatBytes(stem, leaf) + } +} + +export function getTreeIndexesForStorageSlot(storageKey: bigint): { + treeIndex: bigint + subIndex: number +} { + let position: bigint + if (storageKey < CODE_OFFSET - HEADER_STORAGE_OFFSET) { + position = BigInt(HEADER_STORAGE_OFFSET) + storageKey + } else { + position = MAIN_STORAGE_OFFSET + storageKey + } + + const treeIndex = position / BigInt(VERKLE_NODE_WIDTH) + const subIndex = Number(position % BigInt(VERKLE_NODE_WIDTH)) + + return { treeIndex, subIndex } +} + +export function getTreeIndicesForCodeChunk(chunkId: number) { + const treeIndex = Math.floor((CODE_OFFSET + chunkId) / VERKLE_NODE_WIDTH) + const subIndex = (CODE_OFFSET + chunkId) % VERKLE_NODE_WIDTH + return { treeIndex, subIndex } +}