From b50ecdd1a4289e3f1f82419355c2d755d2b0aacc Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 6 Nov 2024 15:28:26 +0000 Subject: [PATCH] Update benchmarks. Add kzg bench --- benchmark/kzg.js | 103 +++++++++++++++++++++++++++++++++++++++++ benchmark/package.json | 1 + benchmark/verkle.js | 9 ++-- 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 benchmark/kzg.js diff --git a/benchmark/kzg.js b/benchmark/kzg.js new file mode 100644 index 0000000..197d92a --- /dev/null +++ b/benchmark/kzg.js @@ -0,0 +1,103 @@ +import { compare, utils as butils } from 'micro-bmark'; +import { loadKZG } from 'kzg-wasm'; +import * as kzg from '../esm/kzg.js'; +import { jsonGZ, __dirname } from '../test/util.js'; +import { trustedSetup as s_fast } from '@paulmillr/trusted-setups/fast.js'; +import { trustedSetup } from '@paulmillr/trusted-setups'; + +// Test cases +const VIEM = Object.fromEntries( + [ + 'blob-to-kzg-commitment', + 'blobs', + 'compute-blob-kzg-proof', + 'compute-kzg-proof', + 'invalid-blobs', + 'verify-blob-kzg-proof-batch', + 'verify-blob-kzg-proof', + 'verify-kzg-proof', + ].map((i) => [i, jsonGZ(`../test/vectors/viem/test/kzg/${i}.json.gz`)]) +); + +const strip0x = (items) => items.map((i) => i.substring(2)).join(''); + +export async function main() { + const opts = { + n1: 4096, + n2: 65, + g1: strip0x(trustedSetup.g1_lagrange), + g2: strip0x(trustedSetup.g2_monomial), + }; + const wasmKZG = await loadKZG(opts); + const nobleKZG = new kzg.KZG(s_fast); + + await compare('init', 1, { + wasm: () => loadKZG(opts), + noble: () => new kzg.KZG(s_fast), + }); + + const i0 = VIEM['blob-to-kzg-commitment'][1].input; + deepStrictEqual( + nobleKZG.blobToKzgCommitment(i0.blob), + wasmKZG.blobToKZGCommitment(i0.blob).toLowerCase() + ); + await compare('blobToKzgCommitment', 5, { + wasm: () => wasmKZG.blobToKZGCommitment(i0.blob), + noble: () => nobleKZG.blobToKzgCommitment(i0.blob), + }); + + const i1 = VIEM['compute-kzg-proof'][0].input; + await compare('computeKzgProof', 100, { + // wasm: () => wasmKZG.computeKzgProof(unhex(i1.blob), unhex(i1.z)), + noble: () => nobleKZG.computeProof(i1.blob, i1.z), + }); + + const i2 = VIEM['compute-blob-kzg-proof'][3].input; + deepStrictEqual( + nobleKZG.computeBlobProof(i2.blob, i2.commitment), + wasmKZG.computeBlobKZGProof(i2.blob, i2.commitment).toLowerCase() + ); + await compare('computeBlobKzgProof', 5, { + wasm: () => wasmKZG.computeBlobKZGProof(i2.blob, i2.commitment), + noble: () => nobleKZG.computeBlobProof(i2.blob, i2.commitment), + }); + + const i3 = VIEM['verify-kzg-proof'][0].input; + deepStrictEqual( + nobleKZG.verifyProof(i3.commitment, i3.z, i3.y, i3.proof), + wasmKZG.verifyKZGProof(i3.commitment, i3.z, i3.y, i3.proof) + ); + await compare('verifyKzgProof', 200, { + wasm: () => wasmKZG.verifyKZGProof(i3.commitment, i3.z, i3.y, i3.proof), + noble: () => nobleKZG.verifyProof(i3.commitment, i3.z, i3.y, i3.proof), + }); + + const i4 = VIEM['verify-blob-kzg-proof'][0].input; + deepStrictEqual( + nobleKZG.verifyBlobProof(i4.blob, i4.commitment, i4.proof), + wasmKZG.verifyBlobKZGProof(i4.blob, i4.commitment, i4.proof) + ); + await compare('verifyBlobKzgProof', 100, { + wasm: () => wasmKZG.verifyBlobKZGProof(i4.blob, i4.commitment, i4.proof), + noble: () => nobleKZG.verifyBlobProof(i4.blob, i4.commitment, i4.proof), + }); + + const i5 = VIEM['verify-blob-kzg-proof-batch'][1].input; + deepStrictEqual( + nobleKZG.verifyBlobProofBatch(i5.blobs, i5.commitments, i5.proofs), + wasmKZG.verifyBlobKZGProofBatch(i5.blobs, i5.commitments, i5.proofs) + ); + await compare('verifyBlobKzgProofBatch', 10, { + wasm: () => wasmKZG.verifyBlobKZGProofBatch(i5.blobs, i5.commitments, i5.proofs), + noble: () => nobleKZG.verifyBlobProofBatch(i5.blobs, i5.commitments, i5.proofs), + }); + + butils.logMem(); +} + +// ESM is broken. +import url from 'node:url'; +import { deepStrictEqual } from 'node:assert'; +if (import.meta.url === url.pathToFileURL(process.argv[1]).href) { + main(); +} diff --git a/benchmark/package.json b/benchmark/package.json index dec166e..bb07737 100644 --- a/benchmark/package.json +++ b/benchmark/package.json @@ -19,6 +19,7 @@ "@hazae41/cubane": "0.1.14", "@lodestar/types": "^1.5.0", "ethers": "6.11.0", + "kzg-wasm": "^0.5.0", "verkle-cryptography-wasm": "0.4.8", "viem": "2.7.8" } diff --git a/benchmark/verkle.js b/benchmark/verkle.js index f0a56f2..fd6bd3f 100644 --- a/benchmark/verkle.js +++ b/benchmark/verkle.js @@ -21,14 +21,15 @@ export async function main() { const SAMPLES = { commitToScalars: 10_000, createProof: 50, - getTreeKey: 10_000, - hashCommitment: 1_000_000, - serializeCommitment: 2_000_000, - updateCommitment: 10_000, + getTreeKey: 100_000, + hashCommitment: 2_000_000, + serializeCommitment: 3_000_000, + updateCommitment: 500_000, verifyExecutionWitnessPreState: 10_000, verifyProof: 100, }; for (const k in VERKLE_MONOREPO) { + //if (k !== 'getTreeKey') continue; const v = VERKLE_MONOREPO[k][0]; const args = v.arguments; await compare(`${k}`, SAMPLES[k], {