-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: check validity of poseidon artifacts
- Loading branch information
Showing
9 changed files
with
78 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
) | ||
}, | ||
) | ||
}) |
6 changes: 3 additions & 3 deletions
6
packages/artifacts/test/cli/jest.config.ts → packages/poseidon/test/jest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |