Skip to content

Commit

Permalink
Add verkle module to Util, replace EVM function calls, remove @ethere…
Browse files Browse the repository at this point in the history
…umjs/verkle dependency
  • Loading branch information
holgerd77 committed Jun 18, 2024
1 parent 1b528c6 commit f7e22d2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 4 additions & 6 deletions packages/evm/src/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
1 change: 1 addition & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ export * from './lock.js'
export * from './mapDB.js'
export * from './provider.js'
export * from './requests.js'
export * from './verkle.js'
83 changes: 83 additions & 0 deletions packages/util/src/verkle.ts
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit f7e22d2

Please sign in to comment.