Skip to content

Commit 9bdd5ac

Browse files
acolytec3gabrocheleauholgerd77
authored
Add micro-eth-signer/kzg (#3674)
* 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 <[email protected]> Co-authored-by: Holger Drewes <[email protected]>
1 parent 75ce1ca commit 9bdd5ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+531
-400
lines changed

config/cspell-ts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
}
1313
],
1414
"words": [
15+
"paulmillr",
1516
"t8ntool",
1617
"!Json",
1718
"!Rpc",

package-lock.json

Lines changed: 44 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/block/examples/4844.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { createBlock } from '@ethereumjs/block'
22
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
33
import { createBlob4844Tx } from '@ethereumjs/tx'
44
import { createAddressFromPrivateKey } from '@ethereumjs/util'
5+
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
56
import { randomBytes } from 'crypto'
6-
import { loadKZG } from 'kzg-wasm'
7+
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
78

89
const main = async () => {
9-
const kzg = await loadKZG()
10+
const kzg = new microEthKZG(trustedSetup)
1011

1112
const common = new Common({
1213
chain: Mainnet,

packages/block/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"ethereum-cryptography": "^3.0.0"
5656
},
5757
"devDependencies": {
58-
"kzg-wasm": "^0.4.0"
58+
"@paulmillr/trusted-setups": "^0.1.2",
59+
"micro-eth-signer": "^0.11.0"
5960
},
6061
"engines": {
6162
"node": ">=18"

packages/block/test/eip4844block.spec.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
getBlobs,
77
randomBytes,
88
} from '@ethereumjs/util'
9-
import { loadKZG } from 'kzg-wasm'
10-
import { assert, beforeAll, describe, it } from 'vitest'
9+
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
10+
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
11+
import { assert, describe, it } from 'vitest'
1112

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

1819
import type { TypedTransaction } from '@ethereumjs/tx'
19-
import type { Kzg } from '@ethereumjs/util'
2020

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

24-
beforeAll(async () => {
25-
const kzg = await loadKZG()
26-
27-
common = createCommonFromGethGenesis(hardfork4844Data, {
28-
chain: 'customChain',
29-
hardfork: Hardfork.Cancun,
30-
customCrypto: { kzg },
31-
})
24+
const common = createCommonFromGethGenesis(hardfork4844Data, {
25+
chain: 'customChain',
26+
hardfork: Hardfork.Cancun,
27+
customCrypto: { kzg },
3228
})
3329

3430
it('should work', () => {
@@ -98,18 +94,16 @@ describe('EIP4844 header tests', () => {
9894
})
9995

10096
describe('blob gas tests', () => {
101-
let common: Common
102-
let blobGasPerBlob: bigint
103-
beforeAll(async () => {
104-
const kzg = await loadKZG()
105-
common = createCommonFromGethGenesis(hardfork4844Data, {
106-
chain: 'customChain',
107-
hardfork: Hardfork.Cancun,
108-
params: paramsBlock,
109-
customCrypto: { kzg },
110-
})
111-
blobGasPerBlob = common.param('blobGasPerBlob')
97+
const kzg = new microEthKZG(trustedSetup)
98+
99+
const common = createCommonFromGethGenesis(hardfork4844Data, {
100+
chain: 'customChain',
101+
hardfork: Hardfork.Cancun,
102+
params: paramsBlock,
103+
customCrypto: { kzg },
112104
})
105+
const blobGasPerBlob = common.param('blobGasPerBlob')
106+
113107
it('should work', () => {
114108
const preShardingHeader = createBlockHeader(
115109
{},
@@ -158,19 +152,16 @@ describe('blob gas tests', () => {
158152
})
159153

160154
describe('transaction validation tests', () => {
161-
let kzg: Kzg
162-
let common: Common
163-
let blobGasPerBlob: bigint
164-
beforeAll(async () => {
165-
kzg = await loadKZG()
166-
common = createCommonFromGethGenesis(hardfork4844Data, {
167-
chain: 'customChain',
168-
hardfork: Hardfork.Cancun,
169-
params: paramsBlock,
170-
customCrypto: { kzg },
171-
})
172-
blobGasPerBlob = common.param('blobGasPerBlob')
155+
const kzg = new microEthKZG(trustedSetup)
156+
157+
const common = createCommonFromGethGenesis(hardfork4844Data, {
158+
chain: 'customChain',
159+
hardfork: Hardfork.Cancun,
160+
params: paramsBlock,
161+
customCrypto: { kzg },
173162
})
163+
const blobGasPerBlob = common.param('blobGasPerBlob')
164+
174165
it('should work', () => {
175166
const blobs = getBlobs('hello world')
176167
const commitments = blobsToCommitments(kzg, blobs)

packages/block/test/from-beacon-payload.spec.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Hardfork, createCommonFromGethGenesis } from '@ethereumjs/common'
2-
import { loadKZG } from 'kzg-wasm'
3-
import { assert, beforeAll, describe, it } from 'vitest'
2+
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
3+
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
4+
import { assert, describe, it } from 'vitest'
45

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

13-
import type { Common } from '@ethereumjs/common'
14-
14+
const kzg = new microEthKZG(trustedSetup)
1515
describe('[fromExecutionPayloadJSON]: 4844 devnet 5', () => {
16-
let common: Common
17-
beforeAll(async () => {
18-
const kzg = await loadKZG()
19-
20-
const commonConfig = { ...devnet4844Config }
21-
commonConfig.config = { ...commonConfig.config, chainId: 4844001005 }
22-
const network = 'sharding'
23-
common = createCommonFromGethGenesis(commonConfig, { chain: network, customCrypto: { kzg } })
24-
// safely change chainId without modifying underlying json
25-
26-
common.setHardfork(Hardfork.Cancun)
16+
const commonConfig = { ...devnet4844Config }
17+
commonConfig.config = { ...commonConfig.config, chainId: 4844001005 }
18+
const network = 'sharding'
19+
const common = createCommonFromGethGenesis(commonConfig, {
20+
chain: network,
21+
customCrypto: { kzg },
2722
})
23+
// safely change chainId without modifying underlying json
24+
25+
common.setHardfork(Hardfork.Cancun)
2826

2927
it('reconstruct cancun block with blob txs', async () => {
3028
for (const payload of [payloadSlot87335Data, payloadSlot87475Data]) {

packages/client/bin/cli.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
setLengthLeft,
2929
short,
3030
} from '@ethereumjs/util'
31+
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
3132
import {
3233
keccak256 as keccak256WASM,
3334
secp256k1Expand,
@@ -41,8 +42,8 @@ import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat'
4142
import { sha256 } from 'ethereum-cryptography/sha256.js'
4243
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'
4344
import * as http from 'http'
44-
import { loadKZG } from 'kzg-wasm'
4545
import { Level } from 'level'
46+
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
4647
import { homedir } from 'os'
4748
import * as path from 'path'
4849
import * as promClient from 'prom-client'
@@ -927,15 +928,13 @@ async function run() {
927928
return helpRPC()
928929
}
929930

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

937+
const kzg = new microEthKZG(trustedSetup)
939938
// Initialize WASM crypto if JS crypto is not specified
940939
if (args.useJsCrypto === false) {
941940
await waitReadyPolkadotSha256()

packages/client/devnets/4844-interop/tools/txGenerator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212

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

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

2931
async function run(data: any) {
30-
const kzg = await loadKZG()
31-
3232
const common = createCommonFromGethGenesis(genesisJSON, {
3333
chain: genesisJSON.ChainName ?? 'devnet',
3434
hardfork: Hardfork.Cancun,

packages/client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
"ethereum-cryptography": "^3.0.0",
8484
"it-pipe": "^1.1.0",
8585
"jayson": "^4.0.0",
86-
"kzg-wasm": "^0.4.0",
86+
"@paulmillr/trusted-setups": "^0.1.2",
87+
"micro-eth-signer": "^0.11.0",
8788
"level": "^8.0.0",
8889
"mcl-wasm": "^1.5.0",
8990
"memory-level": "^1.0.0",

0 commit comments

Comments
 (0)