Skip to content

Commit

Permalink
Merge pull request #9 from ethereumjs/update-loadKZG
Browse files Browse the repository at this point in the history
Add trusted setup param to loadKZG
  • Loading branch information
g11tech committed Mar 15, 2024
2 parents e874c6c + 6d82761 commit 1a8433b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
(modification: no type change headlines) and this project adheres to
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.3.1 - 2024-03-14

- Add optional trusted setup parameter to `loadKZG`, PR [#9](https://github.com/ethereumjs/kzg-wasm/pull/9)

## 0.3.0 - 2024-03-14

- Allow optional trusted setup in `loadTrustedSetup`, PR [#7](https://github.com/ethereumjs/kzg-wasm/pull/7)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kzg-wasm",
"version": "0.3.0",
"description": "",
"version": "0.3.1",
"description": "a WASM compilation of c-kzg-4844",
"scripts": {
"build": "npm run build:ts && npm run transpileCJS && npm run fixRequire",
"build:ts": "scripts/ts-build.sh",
Expand Down
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 1a8433b

Please sign in to comment.