diff --git a/packages/poseidon/package.json b/packages/poseidon/package.json index 56d0df3..1d59f7e 100644 --- a/packages/poseidon/package.json +++ b/packages/poseidon/package.json @@ -23,10 +23,7 @@ "devDependencies": { "@ethersproject/bignumber": "^5.7.0", "@types/snarkjs": "^0.7.8", - "@zk-kit/poseidon-proof": "1.0.0-beta.4", - "@zk-kit/utils": "^1.2.0", - "ethers": "^6.13.1", - "ffjavascript": "^0.3.0", - "snarkjs": "^0.7.4" + "@zk-kit/test": "workspace:^", + "ffjavascript": "^0.3.0" } } diff --git a/packages/poseidon/test/index.test.ts b/packages/poseidon/test/index.test.ts index b68e6a3..38ef24b 100644 --- a/packages/poseidon/test/index.test.ts +++ b/packages/poseidon/test/index.test.ts @@ -1,36 +1,10 @@ -import { generate, type PoseidonProof } from '@zk-kit/poseidon-proof' -import { unpackGroth16Proof } from '@zk-kit/utils' -import { keccak256 } from 'ethers/crypto' -import { toBeHex } from 'ethers/utils' +import { generatePoseidonProof, verifyPoseidonProof } from '@zk-kit/test' import { getCurveFromName } from 'ffjavascript' -import assert from 'node:assert/strict' -import { readFileSync } from 'node:fs' -import { join } from 'node:path' -import { groth16 } from 'snarkjs' const INPUTS = Array.from({ length: 16 }, (_, i) => i + 1).map(i => ({ inputs: Array.from({ length: i }, (_, j) => j + 1), numberOfInputs: i, })) -const SCOPE = 'scope' - -const hash = (message: string) => (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString() - -async function generateProof(preimages: number[]) { - const numParams = preimages.length - - return generate(preimages, SCOPE, { - wasm: join(__dirname, '..', `poseidon-${numParams}.wasm`), - zkey: join(__dirname, '..', `poseidon-${numParams}.zkey`), - }) -} - -async function verifyProof( - { digest, numberOfInputs, proof, scope }: PoseidonProof, -) { - const verifKey = JSON.parse(readFileSync(join(__dirname, '..', `poseidon-${numberOfInputs}.json`), 'utf8')) - return groth16.verify(verifKey, [digest, hash(scope)], unpackGroth16Proof(proof)) -} describe('poseidon', () => { let curve: any @@ -45,15 +19,10 @@ describe('poseidon', () => { it.each(INPUTS)( 'Should verify a poseidon proof with $numberOfInputs input(s)', - async ({ inputs, numberOfInputs }) => { - const s = `${numberOfInputs > 1 ? 's' : ''}` - const proof = await generateProof(inputs) - const result = await verifyProof(proof) - assert.strictEqual( - result, - true, - `Proof verification failed for ${numberOfInputs} parameter${s}`, - ) + async ({ inputs }) => { + const proof = await generatePoseidonProof(inputs) + const result = await verifyPoseidonProof(proof) + expect(result).toBe(true) }, ) }) diff --git a/packages/test/index.ts b/packages/test/index.ts new file mode 100644 index 0000000..61bc4f3 --- /dev/null +++ b/packages/test/index.ts @@ -0,0 +1,30 @@ +import { Project } from '@zk-kit/artifacts' +import { generate, type PoseidonProof } from '@zk-kit/poseidon-proof' +import { unpackGroth16Proof } from '@zk-kit/utils' +import { keccak256, toBeHex } from 'ethers' +import { readFileSync } from 'node:fs' +import { join } from 'node:path' +import { groth16 } from 'snarkjs' +const SCOPE = 'scope' + +const hash = (message: string) => (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString() + +const generateProof = (project: Project) => async (preimages: number[]) => { + const numParams = preimages.length + + return generate(preimages, SCOPE, { + wasm: join(__dirname, '..', project, `${project}-${numParams}.wasm`), + zkey: join(__dirname, '..', project, `${project}-${numParams}.zkey`), + }) +} + +const verifyProof = (project: Project) => +async ( + { digest, numberOfInputs, proof, scope }: PoseidonProof, +) => { + const verifKey = JSON.parse(readFileSync(join(__dirname, '..', project, `${project}-${numberOfInputs}.json`), 'utf8')) + return groth16.verify(verifKey, [digest, hash(scope)], unpackGroth16Proof(proof)) +} + +export const generatePoseidonProof = generateProof(Project.POSEIDON) +export const verifyPoseidonProof = verifyProof(Project.POSEIDON) diff --git a/packages/test/package.json b/packages/test/package.json new file mode 100644 index 0000000..a23a5f7 --- /dev/null +++ b/packages/test/package.json @@ -0,0 +1,14 @@ +{ + "name": "@zk-kit/test", + "files": ["index.ts"], + "main": "index.ts", + "private": true, + "devDependencies": { + "@types/snarkjs": "^0.7.8", + "@zk-kit/artifacts": "workspace:^", + "@zk-kit/poseidon-proof": "1.0.0-beta.4", + "@zk-kit/utils": "^1.2.0", + "ethers": "^6.13.1", + "snarkjs": "^0.7.4" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index aec9c9f..fb582e3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -142,6 +142,25 @@ importers: '@types/snarkjs': specifier: ^0.7.8 version: 0.7.8 + '@zk-kit/test': + specifier: workspace:^ + version: link:../test + ffjavascript: + specifier: ^0.3.0 + version: 0.3.0 + + packages/semaphore: {} + + packages/semaphore-identity: {} + + packages/test: + devDependencies: + '@types/snarkjs': + specifier: ^0.7.8 + version: 0.7.8 + '@zk-kit/artifacts': + specifier: workspace:^ + version: link:../artifacts '@zk-kit/poseidon-proof': specifier: 1.0.0-beta.4 version: 1.0.0-beta.4 @@ -151,17 +170,10 @@ importers: ethers: specifier: ^6.13.1 version: 6.13.2 - ffjavascript: - specifier: ^0.3.0 - version: 0.3.0 snarkjs: specifier: ^0.7.4 version: 0.7.4 - packages/semaphore: {} - - packages/semaphore-identity: {} - packages: '@adraffy/ens-normalize@1.10.1':