Skip to content

Commit

Permalink
test: check validity of poseidon artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
sripwoud committed Sep 21, 2024
1 parent 4e8c557 commit cf05e1a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"overrides": [
{
"include": [
"types/ffjavascript/index.d.ts",
"packages/poseidon/test/index.test.ts",
"packages/artifacts/src/cli/spinner.ts",
"packages/artifacts/src/cli/commands/generate/action.ts",
],
Expand Down
3 changes: 1 addition & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from 'node:path'
import type { JestConfigWithTsJest } from 'ts-jest'
// import preset from 'ts-jest/'

const config: JestConfigWithTsJest = {
preset: 'ts-jest',
Expand All @@ -12,7 +11,7 @@ const config: JestConfigWithTsJest = {
},
moduleDirectories: ['node_modules', '<rootDir>/node_modules', '<rootDir>/src'],
moduleFileExtensions: ['js', 'ts'],
projects: ['packages/artifacts/test'],
projects: ['packages/artifacts/test', 'packages/poseidon/test'],
}

export default config
67 changes: 0 additions & 67 deletions packages/poseidon/index.test.mjs

This file was deleted.

6 changes: 4 additions & 2 deletions packages/poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
"scripts": {
"test": "node --test index.test.mjs"
},
"files": ["poseidon-*"],
"files": [
"poseidon-*"
],
"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",
"poseidon-lite": "^0.2.0",
"snarkjs": "^0.7.4"
}
}
59 changes: 59 additions & 0 deletions packages/poseidon/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 { 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

beforeAll(async () => {
curve = await getCurveFromName('bn128')
}, 30_000)

afterAll(async () => {
await curve.terminate()
})

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}`,
)
},
)
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { JestConfigWithTsJest } from 'ts-jest'
import _sharedJestConf from '../../../../jest.config'
import _sharedJestConf from '../../../jest.config'

const { collectCoverage, projects, ...sharedJestConf } = _sharedJestConf

const config: JestConfigWithTsJest = {
...sharedJestConf,
displayName: 'cli',
rootDir: '../..',
displayName: 'poseidon',
rootDir: '..',
}

export default config
11 changes: 3 additions & 8 deletions pnpm-lock.yaml

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

5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"useUnknownInCatchVariables": false,
"declaration": true,
"allowSyntheticDefaultImports": true,
"declarationDir": "types"
"declarationDir": "types",
"typeRoots": ["node_modules/@types", "types"]
},
"include": ["jest.config.ts"]
"include": ["jest.config.ts", "packages/artifacts/test", "packages/poseidon/test"]
}
3 changes: 3 additions & 0 deletions types/ffjavascript/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'ffjavascript' {
export function getCurveFromName(name: any, singleThread?: any, plugins?: any): any
}

0 comments on commit cf05e1a

Please sign in to comment.