Skip to content

Commit

Permalink
Add micro-eth-signer/kzg (#3674)
Browse files Browse the repository at this point in the history
* add jsKZG interface and modify tests

* export wrapper and add vm tester

* Add trusdest setup for initial verification

* move jsKZG to util

* use jsKZG in the tester

* move benchmarks to util

* switch blobTx example to use jsKZG

* update benchmarks

* partially migrate bytes to strings for kzg data [no ci]

* Adjust tx constructors

* use trusted-setups package

* change kzg capitalization

* lots o fixes

* hack to fix tester

* bye bye kzg-wasm

* spell check

* last fixes

* fix vm test

* one more fix

* Small tx dependency fix

* Fix loadKZG references

* Switch trusted setup to default export

* Update trusted-setups version

* ignore missing types for trusted-setups

* use named import

* Lint fixes

* revert bundler config change

* update trusted-setups again

* remove ts-ignore

---------

Co-authored-by: Gabriel Rocheleau <contact@rockwaterweb.com>
Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
3 people committed Sep 21, 2024
1 parent 75ce1ca commit 9bdd5ac
Show file tree
Hide file tree
Showing 59 changed files with 531 additions and 400 deletions.
1 change: 1 addition & 0 deletions config/cspell-ts.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
}
],
"words": [
"paulmillr",
"t8ntool",
"!Json",
"!Rpc",
Expand Down
52 changes: 44 additions & 8 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions packages/block/examples/4844.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createBlob4844Tx } from '@ethereumjs/tx'
import { createAddressFromPrivateKey } from '@ethereumjs/util'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import { randomBytes } from 'crypto'
import { loadKZG } from 'kzg-wasm'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'

const main = async () => {
const kzg = await loadKZG()
const kzg = new microEthKZG(trustedSetup)

const common = new Common({
chain: Mainnet,
Expand Down
3 changes: 2 additions & 1 deletion packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"ethereum-cryptography": "^3.0.0"
},
"devDependencies": {
"kzg-wasm": "^0.4.0"
"@paulmillr/trusted-setups": "^0.1.2",
"micro-eth-signer": "^0.11.0"
},
"engines": {
"node": ">=18"
Expand Down
61 changes: 26 additions & 35 deletions packages/block/test/eip4844block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
getBlobs,
randomBytes,
} from '@ethereumjs/util'
import { loadKZG } from 'kzg-wasm'
import { assert, beforeAll, describe, it } from 'vitest'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
import { assert, describe, it } from 'vitest'

import { fakeExponential, getNumBlobs } from '../src/helpers.js'
import { createBlock, createBlockHeader } from '../src/index.js'
Expand All @@ -16,19 +17,14 @@ import { paramsBlock } from '../src/params.js'
import { hardfork4844Data } from './testdata/4844-hardfork.js'

import type { TypedTransaction } from '@ethereumjs/tx'
import type { Kzg } from '@ethereumjs/util'

describe('EIP4844 header tests', () => {
let common: Common
const kzg = new microEthKZG(trustedSetup)

beforeAll(async () => {
const kzg = await loadKZG()

common = createCommonFromGethGenesis(hardfork4844Data, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
customCrypto: { kzg },
})
const common = createCommonFromGethGenesis(hardfork4844Data, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
customCrypto: { kzg },
})

it('should work', () => {
Expand Down Expand Up @@ -98,18 +94,16 @@ describe('EIP4844 header tests', () => {
})

describe('blob gas tests', () => {
let common: Common
let blobGasPerBlob: bigint
beforeAll(async () => {
const kzg = await loadKZG()
common = createCommonFromGethGenesis(hardfork4844Data, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
params: paramsBlock,
customCrypto: { kzg },
})
blobGasPerBlob = common.param('blobGasPerBlob')
const kzg = new microEthKZG(trustedSetup)

const common = createCommonFromGethGenesis(hardfork4844Data, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
params: paramsBlock,
customCrypto: { kzg },
})
const blobGasPerBlob = common.param('blobGasPerBlob')

it('should work', () => {
const preShardingHeader = createBlockHeader(
{},
Expand Down Expand Up @@ -158,19 +152,16 @@ describe('blob gas tests', () => {
})

describe('transaction validation tests', () => {
let kzg: Kzg
let common: Common
let blobGasPerBlob: bigint
beforeAll(async () => {
kzg = await loadKZG()
common = createCommonFromGethGenesis(hardfork4844Data, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
params: paramsBlock,
customCrypto: { kzg },
})
blobGasPerBlob = common.param('blobGasPerBlob')
const kzg = new microEthKZG(trustedSetup)

const common = createCommonFromGethGenesis(hardfork4844Data, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
params: paramsBlock,
customCrypto: { kzg },
})
const blobGasPerBlob = common.param('blobGasPerBlob')

it('should work', () => {
const blobs = getBlobs('hello world')
const commitments = blobsToCommitments(kzg, blobs)
Expand Down
28 changes: 13 additions & 15 deletions packages/block/test/from-beacon-payload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hardfork, createCommonFromGethGenesis } from '@ethereumjs/common'
import { loadKZG } from 'kzg-wasm'
import { assert, beforeAll, describe, it } from 'vitest'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
import { assert, describe, it } from 'vitest'

import { devnet4844Config } from '../../client/test/sim/configs/4844-devnet.js'
import { createBlockFromBeaconPayloadJSON, createBlockHeader } from '../src/index.js'
Expand All @@ -10,21 +11,18 @@ import { payloadSlot87335Data } from './testdata/payload-slot-87335.js'
import { payloadSlot87475Data } from './testdata/payload-slot-87475.js'
import { testnetVerkleKaustinenData } from './testdata/testnetVerkleKaustinen.js'

import type { Common } from '@ethereumjs/common'

const kzg = new microEthKZG(trustedSetup)
describe('[fromExecutionPayloadJSON]: 4844 devnet 5', () => {
let common: Common
beforeAll(async () => {
const kzg = await loadKZG()

const commonConfig = { ...devnet4844Config }
commonConfig.config = { ...commonConfig.config, chainId: 4844001005 }
const network = 'sharding'
common = createCommonFromGethGenesis(commonConfig, { chain: network, customCrypto: { kzg } })
// safely change chainId without modifying underlying json

common.setHardfork(Hardfork.Cancun)
const commonConfig = { ...devnet4844Config }
commonConfig.config = { ...commonConfig.config, chainId: 4844001005 }
const network = 'sharding'
const common = createCommonFromGethGenesis(commonConfig, {
chain: network,
customCrypto: { kzg },
})
// safely change chainId without modifying underlying json

common.setHardfork(Hardfork.Cancun)

it('reconstruct cancun block with blob txs', async () => {
for (const payload of [payloadSlot87335Data, payloadSlot87475Data]) {
Expand Down
7 changes: 3 additions & 4 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
setLengthLeft,
short,
} from '@ethereumjs/util'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import {
keccak256 as keccak256WASM,
secp256k1Expand,
Expand All @@ -41,8 +42,8 @@ import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat'
import { sha256 } from 'ethereum-cryptography/sha256.js'
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'
import * as http from 'http'
import { loadKZG } from 'kzg-wasm'
import { Level } from 'level'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
import { homedir } from 'os'
import * as path from 'path'
import * as promClient from 'prom-client'
Expand Down Expand Up @@ -927,15 +928,13 @@ async function run() {
return helpRPC()
}

// TODO sharding: Just initialize kzg library now, in future it can be optimized to be
// loaded and initialized on the sharding hardfork activation
// Give chainId priority over networkId
// Give networkId precedence over network name
const chainName = args.chainId ?? args.networkId ?? args.network ?? Chain.Mainnet
const chain = getPresetChainConfig(chainName)
const cryptoFunctions: CustomCrypto = {}
const kzg = await loadKZG()

const kzg = new microEthKZG(trustedSetup)
// Initialize WASM crypto if JS crypto is not specified
if (args.useJsCrypto === false) {
await waitReadyPolkadotSha256()
Expand Down
6 changes: 3 additions & 3 deletions packages/client/devnets/4844-interop/tools/txGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {

import { randomBytes } from '@ethereumjs/util'
import { Client } from 'jayson/promise'
import { loadKZG } from 'kzg-wasm'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
const kzg = new microEthKZG(trustedSetup)

// CLI Args
const clientPort = parseInt(process.argv[2]) // EL client port number
Expand All @@ -27,8 +29,6 @@ async function getNonce(client: Client, account: string) {
}

async function run(data: any) {
const kzg = await loadKZG()

const common = createCommonFromGethGenesis(genesisJSON, {
chain: genesisJSON.ChainName ?? 'devnet',
hardfork: Hardfork.Cancun,
Expand Down
3 changes: 2 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"ethereum-cryptography": "^3.0.0",
"it-pipe": "^1.1.0",
"jayson": "^4.0.0",
"kzg-wasm": "^0.4.0",
"@paulmillr/trusted-setups": "^0.1.2",
"micro-eth-signer": "^0.11.0",
"level": "^8.0.0",
"mcl-wasm": "^1.5.0",
"memory-level": "^1.0.0",
Expand Down
Loading

0 comments on commit 9bdd5ac

Please sign in to comment.