refactor(halo2-eth-mship-worker): simplify verify fn signature (#527)

* refactor: remove unncessary `await`

* refactor: simplify type

* chore: remove unncessary `eslint-ignore` directive

* refactor: use `K` constant

* chore: ellude implicit types

* chore: add changeset
This commit is contained in:
sripwoud
2024-09-18 16:26:16 +02:00
committed by GitHub
parent e234101b41
commit 7c4d5f24ec
3 changed files with 15 additions and 11 deletions

View File

@@ -0,0 +1,5 @@
---
"@anonklub/halo2-eth-membership-worker": minor
---
Use single positional argument for `verifyMembership` instead of object argument with one key

View File

@@ -19,9 +19,7 @@ export interface ProveInputs {
merkleProofBytesSerialized: Uint8Array
}
export interface VerifyInputs {
membershipProofSerialized: Uint8Array
}
export type VerifyInputs = Uint8Array
export type ProveMembershipFn = (
proveInputs: ProveInputs,

View File

@@ -1,14 +1,16 @@
import { expose } from 'comlink'
import { hashMessage, hexToSignature } from 'viem'
import type { IHalo2EthMembershipWasm, IHalo2EthMembershipWorker } from './interface'
import type { IHalo2EthMembershipWasm, IHalo2EthMembershipWorker, VerifyInputs } from './interface'
import { calculateSigRecovery, fetchKzgParams, hexToLittleEndianBytes } from './utils'
// Benchmarking showed that k = 14 offers the best performance
const K = 14
let halo2EthMembershipWasm: IHalo2EthMembershipWasm
let initialized = false
let params: Uint8Array
export const Halo2EthMembershipWorker: IHalo2EthMembershipWorker = {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async prepare() {
halo2EthMembershipWasm = await import('@anonklub/halo2-eth-membership')
@@ -19,21 +21,20 @@ export const Halo2EthMembershipWorker: IHalo2EthMembershipWorker = {
const response = await fetch(wasmModuleUrl)
const bufferSource = await response.arrayBuffer()
await halo2EthMembershipWasm.initSync(bufferSource)
await halo2EthMembershipWasm.initPanicHook()
halo2EthMembershipWasm.initSync(bufferSource)
halo2EthMembershipWasm.initPanicHook()
if (!initialized) {
const numThreads = navigator.hardwareConcurrency
await halo2EthMembershipWasm.initThreadPool(numThreads)
// Please note that K = 14 only supported for now until benchmarking is finished
params = await fetchKzgParams(14)
params = await fetchKzgParams(K)
initialized = true
}
},
async proveMembership({ merkleProofBytesSerialized, message, sig }): Promise<Uint8Array> {
async proveMembership({ merkleProofBytesSerialized, message, sig }) {
const { r, s, v } = hexToSignature(sig)
const sBytes = hexToLittleEndianBytes(s, 32)
@@ -52,7 +53,7 @@ export const Halo2EthMembershipWorker: IHalo2EthMembershipWorker = {
)
},
async verifyMembership({ membershipProofSerialized }): Promise<boolean> {
async verifyMembership(membershipProofSerialized: VerifyInputs) {
return halo2EthMembershipWasm.verify_membership(
membershipProofSerialized,
params,