Skip to content

Commit

Permalink
chore: define test util package
Browse files Browse the repository at this point in the history
  • Loading branch information
sripwoud committed Sep 21, 2024
1 parent cf05e1a commit 77bb121
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 48 deletions.
7 changes: 2 additions & 5 deletions packages/poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
41 changes: 5 additions & 36 deletions packages/poseidon/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
},
)
})
30 changes: 30 additions & 0 deletions packages/test/index.ts
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions packages/test/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
26 changes: 19 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77bb121

Please sign in to comment.