Skip to content

Commit

Permalink
Add trusted setup param to loadKZG
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Mar 15, 2024
1 parent e874c6c commit 22b7838
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export type TrustedSetup = {
/**
* Initialization function that instantiates WASM code and returns an object matching the `KZG` interface exposed by `@ethereumjs/util`
*
* @param setupPath Optional setup, otherwise official KZG setup from the KZG ceremony is used
* @param trustedSetup Optional trusted setup, otherwise official KZG setup from the KZG ceremony is used
*
* @returns object - the KZG methods required for all 4844 related operations
*/
export const loadKZG = async () => {
export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) => {
const module = await kzgWasm()

const loadTrustedSetupWasm = module.cwrap('load_trusted_setup_from_wasm', 'number', ['string', 'number','string', 'number']) as (g1: string, n1: number, g2: string, n2: number) => number
Expand All @@ -30,9 +30,6 @@ export const loadKZG = async () => {
* @returns 0 if loaded successfully or 1 otherwise
*/
const loadTrustedSetup = (trustedSetup: TrustedSetup = mainnetTrustedSetup) => {
if (trustedSetup === undefined) {
trustedSetup = mainnetTrustedSetup
}
return loadTrustedSetupWasm(trustedSetup.g1, trustedSetup.n1, trustedSetup.g2, trustedSetup.n2)
}

Expand Down Expand Up @@ -99,6 +96,8 @@ export const loadKZG = async () => {
return res === 'true'
}

loadTrustedSetup(trustedSetup)

return {
loadTrustedSetup, freeTrustedSetup, blobToKzgCommitment, computeBlobKzgProof, verifyBlobKzgProofBatch, verifyKzgProof, verifyBlobKzgProof
}
Expand Down
10 changes: 6 additions & 4 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ const FIELD_ELEMENTS_PER_BLOB = 32
const BYTES_PER_BLOB = BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB

describe('kzg initialization', () => {
let kzg
beforeAll(async () => {
kzg = await loadKZG()
})

it('should initialize', async () => {
const kzg = await loadKZG()
assert.typeOf(kzg.computeBlobKzgProof, 'function' , 'initialized KZG object')
kzg.freeTrustedSetup()
})
it('should return nonzero when invalid trusted setup is provided', async () => {
const kzg = await loadKZG()
it('should return nonzero when invalid trusted setup is provided', () => {
const res = kzg.loadTrustedSetup({ g1: 'x12', n1: -1, g2: 'bad coordinates', n2: 0})
assert.notOk(res === 0)
})
Expand All @@ -23,7 +26,6 @@ describe('kzg API tests', () => {
let kzg
beforeAll(async () => {
kzg = await loadKZG()
await kzg.loadTrustedSetup()
})

it('should generate kzg commitments and verify proofs', async () => {
Expand Down
2 changes: 0 additions & 2 deletions test/script.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const loadKZG = require('../dist/cjs/index')

const main = async () => {
const kzg = await loadKZG.loadKZG()
const json = require('../dist/cjs/trustedSetup')
kzg.loadTrustedSetup(json)
console.log(kzg)
}

Expand Down

0 comments on commit 22b7838

Please sign in to comment.