Skip to content

Commit

Permalink
test(poseidon): use jest instead of built in node test runner (#144)
Browse files Browse the repository at this point in the history
* test: check validity of poseidon artifacts

* chore: define `test` util package

* test: use local test package for poseidon test

* chore: fmt

* refactor

* chore: remove test package

* fix: update deps

* chore: update tsconfig `includes`

* test: update snapshot
  • Loading branch information
sripwoud authored Sep 25, 2024
1 parent 33661df commit 58e058a
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 88 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
1 change: 1 addition & 0 deletions packages/artifacts/test/cli/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Commands:
"poseidon
1.0.0
1.0.0-beta.1
1.0.0-beta.4
semaphore
1.0.0
4.0.0-beta.9
Expand Down
67 changes: 0 additions & 67 deletions packages/poseidon/index.test.mjs

This file was deleted.

11 changes: 7 additions & 4 deletions packages/poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
"scripts": {
"test": "node --test index.test.mjs"
},
"files": ["poseidon-*"],
"files": [
"poseidon-*"
],
"devDependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@types/snarkjs": "^0.7.8",
"@zk-kit/artifacts": "1.9.0",
"@zk-kit/poseidon-proof": "1.0.0-beta.4",
"@zk-kit/utils": "^1.2.0",
"ethers": "^6.13.1",
"@zk-kit/utils": "^1.2.1",
"ethers": "^6.13.2",
"ffjavascript": "^0.3.0",
"poseidon-lite": "^0.2.0",
"snarkjs": "^0.7.4"
}
}
34 changes: 34 additions & 0 deletions packages/poseidon/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Project } from '@zk-kit/artifacts'
import { generate } from '@zk-kit/poseidon-proof'
import { getCurveFromName } from 'ffjavascript'
import { join } from 'node:path'
import { SCOPE, verifyPoseidonProof } from './utils'

const INPUTS = Array.from({ length: 16 }, (_, i) => i + 1).map(i => ({
inputs: Array.from({ length: i }, (_, j) => j + 1),
numberOfInputs: i,
}))

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 proof = await generate(inputs, SCOPE, {
wasm: join(__dirname, '..', `${Project.POSEIDON}-${numberOfInputs}.wasm`),
zkey: join(__dirname, '..', `${Project.POSEIDON}-${numberOfInputs}.zkey`),
})
const result = await verifyPoseidonProof(proof)
expect(result).toBe(true)
},
)
})
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
20 changes: 20 additions & 0 deletions packages/poseidon/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Project } from '@zk-kit/artifacts'
import 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'

export const SCOPE = 'scope'

const hash = (message: string) => (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString()

export const verifyPoseidonProof = (
{ digest, numberOfInputs, proof, scope }: PoseidonProof,
) => {
const verifKey = JSON.parse(
readFileSync(join(__dirname, '..', `${Project.POSEIDON}-${numberOfInputs}.json`), 'utf8'),
)
return groth16.verify(verifKey, [digest, hash(scope)], unpackGroth16Proof(proof))
}
18 changes: 8 additions & 10 deletions pnpm-lock.yaml

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

11 changes: 9 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
"useUnknownInCatchVariables": false,
"declaration": true,
"allowSyntheticDefaultImports": true,
"declarationDir": "types"
"declarationDir": "types",
"typeRoots": [
"node_modules/@types",
"types"
]
},
"include": ["jest.config.ts"]
"include": [
"jest.config.ts",
"packages/*/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 58e058a

Please sign in to comment.