Skip to content

Commit

Permalink
Implement Verkle in JS (#3785)
Browse files Browse the repository at this point in the history
* Implement Verkle in JS

* Fix lint

* Fix lint

* monorepo: remove commented out imports

* monorepo: remove verkle crypto wasm

* Remove rustbn-wasm from statemanager dev dependencies

---------

Co-authored-by: Gabriel Rocheleau <[email protected]>
Co-authored-by: Holger Drewes <[email protected]>
  • Loading branch information
3 people authored Nov 5, 2024
1 parent c77fb81 commit 974a5f4
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 41 deletions.
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"devDependencies": {
"@paulmillr/trusted-setups": "^0.1.2",
"micro-eth-signer": "^0.11.0"
"micro-eth-signer": "^0.12.1"
},
"engines": {
"node": ">=18"
Expand Down
3 changes: 1 addition & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@
"level": "^8.0.0",
"mcl-wasm": "^1.5.0",
"memory-level": "^1.0.0",
"micro-eth-signer": "^0.11.0",
"micro-eth-signer": "^0.12.1",
"prom-client": "^15.1.0",
"rustbn-wasm": "^0.4.0",
"verkle-cryptography-wasm": "^0.4.8",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5",
"yargs": "^17.7.1"
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
import { createVM, runBlock, runTx } from '@ethereumjs/vm'
import { writeFileSync } from 'fs'
import * as mcl from 'mcl-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { initRustBN } from 'rustbn-wasm'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'

import { Event } from '../types.js'
import { debugCodeReplayBlock } from '../util/debug.js'
Expand All @@ -42,6 +42,7 @@ import type { ExecutionOptions } from './execution.js'
import type { Block } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'
import type { RunBlockOpts, TxReceipt, VM } from '@ethereumjs/vm'
const loadVerkleCrypto = () => Promise.resolve(verkle)

export enum ExecStatus {
VALID = 'VALID',
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"level": "^8.0.0",
"mcl-wasm": "^1.5.0",
"memory-level": "^1.0.0",
"micro-eth-signer": "^0.11.0",
"micro-eth-signer": "^0.12.1",
"minimist": "^1.2.5",
"node-dir": "^0.1.17",
"rollup-plugin-visualizer": "^5.12.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/evm/test/verkle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
setLengthLeft,
} from '@ethereumjs/util'
import { createVerkleTree } from '@ethereumjs/verkle'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import { VerkleAccessWitness, createEVM } from '../src/index.js'

import type { VerkleCrypto } from '@ethereumjs/util'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('verkle tests', () => {
let verkleCrypto: VerkleCrypto
Expand Down
4 changes: 1 addition & 3 deletions packages/statemanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
"devDependencies": {
"@ethereumjs/block": "^6.0.0-alpha.1",
"@ethereumjs/genesis": "^0.3.0-alpha.1",
"@types/debug": "^4.1.9",
"rustbn-wasm": "^0.4.0",
"verkle-cryptography-wasm": "^0.4.8"
"@types/debug": "^4.1.9"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
setLengthLeft,
} from '@ethereumjs/util'
import { createVerkleTree } from '@ethereumjs/verkle'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import { Caches } from '../src/index.js'
import { StatefulVerkleStateManager } from '../src/statefulVerkleStateManager.js'

import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('Verkle Tree API tests', () => {
let verkleCrypto: VerkleCrypto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
hexToBytes,
randomBytes,
} from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it, test } from 'vitest'

import { CacheType, Caches, StatelessVerkleStateManager } from '../src/index.js'
Expand All @@ -22,6 +22,7 @@ import { testnetVerkleKaustinenData } from './testdata/testnetVerkleKaustinen.js
import { verkleKaustinen6Block72Data } from './testdata/verkleKaustinen6Block72.js'

import type { VerkleCrypto } from '@ethereumjs/util'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('StatelessVerkleStateManager: Kaustinen Verkle Block', () => {
let verkleCrypto: VerkleCrypto
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@types/minimist": "^1.2.0",
"@types/node-dir": "^0.0.34",
"@paulmillr/trusted-setups": "^0.1.2",
"micro-eth-signer": "^0.11.0",
"micro-eth-signer": "^0.12.1",
"minimist": "^1.2.0",
"node-dir": "^0.1.16",
"yargs": "^17.7.1"
Expand Down
3 changes: 1 addition & 2 deletions packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@
"devDependencies": {
"@paulmillr/trusted-setups": "^0.1.2",
"kzg-wasm": "^0.5.0",
"micro-eth-signer": "^0.11.0",
"verkle-cryptography-wasm": "^0.4.8"
"micro-eth-signer": "^0.12.1"
},
"engines": {
"node": ">=18"
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/verkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface VerifierInput {
/**
* @dev Returns the 31-bytes verkle tree stem for a given address and tree index.
* @dev Assumes that the verkle node width = 256
* @param {VerkleCrypto} verkleCrypto The {@link VerkleCrypto} foreign function interface object from verkle-cryptography-wasm.
* @param {VerkleCrypto} verkleCrypto The {@link VerkleCrypto} foreign function interface object from Verkle cryptography
* @param {Address} address The address to generate the tree key for.
* @param treeIndex The index of the tree to generate the key for. Defaults to 0.
* @return The 31-bytes verkle tree stem as a Uint8Array.
Expand All @@ -78,7 +78,7 @@ export function getVerkleStem(

/**
* Verifies that the executionWitness is valid for the given prestateRoot.
* @param {VerkleCrypto} verkleCrypto The {@link VerkleCrypto} foreign function interface object from verkle-cryptography-wasm.
* @param {VerkleCrypto} verkleCrypto The {@link VerkleCrypto} foreign function interface object from Verkle cryptography
* @param {VerkleExecutionWitness} executionWitness The verkle execution witness.
* @returns {boolean} Whether or not the executionWitness belongs to the prestateRoot.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/util/test/verkle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import { verkleKaustinen6Block72Data } from '../../statemanager/test/testdata/verkleKaustinen6Block72.js'
Expand All @@ -22,6 +22,7 @@ import {
randomBytes,
verifyVerkleProof,
} from '../src/index.js'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('Verkle cryptographic helpers', () => {
let verkle: VerkleCrypto
Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ If you prefer to instantiate the verkle tree class directly, you can do so by pa

import { MapDB, bytesToHex } from '@ethereumjs/util'
import { VerkleTree } from '@ethereumjs/verkle'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
const loadVerkleCrypto = () => Promise.resolve(verkle)

const verkleCrypto = await loadVerkleCrypto()

Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/examples/diyVerkle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MapDB, bytesToHex } from '@ethereumjs/util'
import { VerkleTree } from '@ethereumjs/verkle'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
const loadVerkleCrypto = () => Promise.resolve(verkle)

const verkleCrypto = await loadVerkleCrypto()

Expand Down
6 changes: 3 additions & 3 deletions packages/verkle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
"tsc": "../../config/cli/ts-compile.sh"
},
"dependencies": {
"@ethereumjs/rlp": "^6.0.0-alpha.1",
"@ethereumjs/util": "^10.0.0-alpha.1",
"debug": "^4.3.4",
"lru-cache": "10.1.0",
"verkle-cryptography-wasm": "^0.4.8",
"@ethereumjs/rlp": "^6.0.0-alpha.1",
"@ethereumjs/util": "^10.0.0-alpha.1"
"micro-eth-signer": "0.12.1"
},
"engines": {
"node": ">=18"
Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/src/constructors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { KeyEncoding, MapDB, ValueEncoding } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'

import { ROOT_DB_KEY } from './types.js'
import { VerkleTree } from './verkleTree.js'

import type { VerkleTreeOpts } from './types.js'
const loadVerkleCrypto = () => Promise.resolve(verkle)

export async function createVerkleTree(opts?: Partial<VerkleTreeOpts>) {
const key = ROOT_DB_KEY
Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/test/internalNode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type VerkleCrypto, equalsBytes, randomBytes } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import {
Expand All @@ -9,6 +9,7 @@ import {
isInternalVerkleNode,
} from '../src/node/index.js'
import { InternalVerkleNode } from '../src/node/internalNode.js'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('verkle node - internal', () => {
let verkleCrypto: VerkleCrypto
Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/test/interop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MapDB, bytesToHex } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import { createVerkleTree } from '../src/constructors.js'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('rust-verkle test vectors', () => {
let verkleCrypto: Awaited<ReturnType<typeof loadVerkleCrypto>>
Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/test/leafNode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type VerkleCrypto, equalsBytes, randomBytes, setLengthRight } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import {
Expand All @@ -10,6 +10,7 @@ import {
isLeafVerkleNode,
} from '../src/node/index.js'
import { LeafVerkleNode } from '../src/node/leafNode.js'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('verkle node - leaf', () => {
let verkleCrypto = undefined as never as VerkleCrypto
Expand Down
6 changes: 4 additions & 2 deletions packages/verkle/test/proof.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { MapDB, bigIntToBytes, hexToBytes, randomBytes, setLengthRight } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import { createVerkleTree } from '../src/constructors.js'
import { LeafVerkleNode } from '../src/index.js'

import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
import type { ProverInput, VerifierInput } from 'verkle-cryptography-wasm'
import type { ProverInput, VerifierInput } from 'micro-eth-signer/verkle'

const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('lets make proofs', () => {
let verkleCrypto: VerkleCrypto
Expand Down
3 changes: 2 additions & 1 deletion packages/verkle/test/verkle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapDB, bytesToHex, equalsBytes, hexToBytes, matchingBytesLength } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import {
Expand All @@ -14,6 +14,7 @@ import { dumpLeafValues } from '../src/util.js'

import type { VerkleNode } from '../src/index.js'
import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
const loadVerkleCrypto = () => Promise.resolve(verkle)

describe('Verkle tree', () => {
let verkleCrypto: VerkleCrypto
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"benchmark": "^2.1.4",
"ethers": "^6.13.2",
"mcl-wasm": "^1.5.0",
"micro-eth-signer": "^0.11.0",
"micro-eth-signer": "^0.12.1",
"minimist": "^1.2.5",
"node-dir": "^0.1.17",
"nyc": "^15.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/vm/test/api/EIPs/eip-6800-verkle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { createEVM } from '@ethereumjs/evm'
import { Caches, StatelessVerkleStateManager } from '@ethereumjs/statemanager'
import { createTxFromRLP } from '@ethereumjs/tx'
import { hexToBytes } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { describe, it } from 'vitest'

import { verkleKaustinen6Block72Data } from '../../../../statemanager/test/testdata/verkleKaustinen6Block72.js'
import { createVM, runBlock } from '../../../src/index.js'
const loadVerkleCrypto = () => Promise.resolve(verkle)

const customChainParams = { name: 'custom', chainId: 69420 }
const common = createCustomCommon(customChainParams, Mainnet, {
Expand Down
3 changes: 2 additions & 1 deletion packages/vm/test/api/runBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
utf8ToBytes,
} from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import * as verkle from 'micro-eth-signer/verkle'
import { assert, beforeAll, describe, it } from 'vitest'

import { createVM, runBlock } from '../../src/index.js'
Expand All @@ -59,6 +59,7 @@ import type {
VerkleCrypto,
VerkleExecutionWitness,
} from '@ethereumjs/util'
const loadVerkleCrypto = () => Promise.resolve(verkle)

const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin })
describe('runBlock() -> successful API parameter usage', async () => {
Expand Down

0 comments on commit 974a5f4

Please sign in to comment.