Skip to content

Commit cdb6a3c

Browse files
authored
Merge pull request #22 from ETHCF/better-init
Compatibility
2 parents 2f6114b + f9f2ddf commit cdb6a3c

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/index.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { hexToBytes } from './util.js'
22
import { loadWasmModule } from './loader.cjs'
33
import kzgWasm from './kzg.js'
44
import mainnetTrustedSetup from './trustedSetup.js'
5+
56
export type TrustedSetup = {
67
g1_monomial: string
78
g1_lagrange: string
@@ -28,7 +29,7 @@ const BYTES_PER_CELL = 2048
2829
*
2930
* @returns object - the KZG methods required for all 4844 related operations
3031
*/
31-
export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) => {
32+
export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup, precompute: number = 8) => {
3233
// In Node.js environment, preload the WASM binary to avoid path resolution issues
3334
let wasmBinary = await loadWasmModule();
3435

@@ -84,7 +85,7 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup)
8485
return '0x' + result;
8586
}
8687

87-
const blobToKzgCommitment = blobToKZGCommitment // Alias with different casing for ethereumjs compatibility
88+
8889

8990
/**
9091
*
@@ -100,7 +101,7 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup)
100101
return '0x' + result;
101102
}
102103

103-
const computeBlobProof = computeBlobKZGProof // Alias with different casing for ethereumjs compatibility
104+
104105
/**
105106
*
106107
* @param blobs - an array of blobs
@@ -292,10 +293,44 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup)
292293
return res === 'true'
293294
}
294295

296+
const loadResult = loadTrustedSetup(trustedSetup, precompute);
297+
if (loadResult !== 0) {
298+
throw new Error(`Failed to load trusted setup, error code: ${loadResult}`);
299+
}
300+
301+
302+
// ALIASING
303+
const computeBlobProof = computeBlobKZGProof // Alias with different casing for ethereumjs compatibility
304+
const blobToKzgCommitment = blobToKZGCommitment // Alias with different casing for ethereumjs compatibility
305+
const verifyProof = verifyKZGProof // Alias with different casing for ethereumjs compatibility
306+
307+
const verifyCellKzgProofBatch = (commitments: string[], cellIndices: number[], cells: string[], proofs: string[]) => {
308+
return verifyCellKZGProofBatch(commitments, cellIndices, cells, proofs, cells.length)
309+
}
310+
311+
const recoverCellsAndProofs = (indices: number[], cells: string[]): [string[], string[]] => {
312+
const result = recoverCellsFromKZGProofs(indices, cells, cells.length);
313+
return [result.cells, result.proofs];
314+
}
315+
316+
const computeCells = (blob: string): string[] => {
317+
const result = computeCellsAndKZGProofs(blob);
318+
return result.cells;
319+
}
320+
321+
const computeCellsAndProofs = (blob: string): [string[], string[]] => {
322+
const result = computeCellsAndKZGProofs(blob);
323+
return [result.cells, result.proofs];
324+
}
325+
326+
const verifyBlobProofBatch = verifyBlobKZGProofBatch
295327

296328
return {
297329
loadTrustedSetup, freeTrustedSetup, blobToKZGCommitment, computeBlobKZGProof, verifyBlobKZGProofBatch, verifyKZGProof, verifyBlobKZGProof,
298-
computeCellsAndKZGProofs, recoverCellsFromKZGProofs, verifyCellKZGProof, verifyCellKZGProofBatch, blobToKzgCommitment, computeBlobProof
330+
computeCellsAndKZGProofs, recoverCellsFromKZGProofs, verifyCellKZGProof, verifyCellKZGProofBatch,
331+
// ALIASED/KZGJS COMPATIBILITY METHODS
332+
verifyCellKzgProofBatch, recoverCellsAndProofs, computeCells, computeCellsAndProofs, verifyBlobProofBatch,
333+
computeBlobProof, blobToKzgCommitment, verifyProof
299334
}
300335
}
301336

test/index.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ describe('kzg API tests', () => {
3131
let kzg: Awaited<ReturnType<typeof loadKZG>>
3232
beforeAll(async () => {
3333
kzg = await loadKZG()
34-
const result = kzg.loadTrustedSetup()
35-
assert.equal(result, 0, 'loaded trusted setup successfully')
3634
})
3735

3836
it('should generate kzg commitments and verify proofs', async () => {

0 commit comments

Comments
 (0)