Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(poseidon): check validity of artifacts #143

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/poseidon/index.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { generate } from '@zk-kit/poseidon-proof'
import { unpackGroth16Proof } from '@zk-kit/utils/proof-packing'
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 { dirname, join } from 'node:path'
import { after, before, describe, test } from 'node:test'
import * as poseidons from 'poseidon-lite'
import { groth16 } from 'snarkjs'

function hash(message) {
return (BigInt(keccak256(toBeHex(message, 32))) >> BigInt(8)).toString()
}

const PWD = dirname(import.meta.url.replace('file://', ''))
const SCOPE = 'scope'
const INPUTS = Array.from({ length: 16 }, (_, i) => i + 1).map(i => Array.from({ length: i }, (_, j) => j + 1))

async function generateProof(inputs) {
const numParams = inputs.length

return generate(inputs, SCOPE, {
wasm: join(PWD, `poseidon-${numParams}.wasm`),
zkey: join(PWD, `poseidon-${numParams}.zkey`),
})
}

async function verifyProof({ digest, numberOfInputs, proof, scope }) {
const verifKey = JSON.parse(readFileSync(join(PWD, `poseidon-${numberOfInputs}.json`)))
return groth16.verify(verifKey, [digest, hash(scope)], unpackGroth16Proof(proof))
}

describe('poseidon', () => {
const scope = 'scope'
let curve
let digest
const proofs = []

before(async () => {
curve = await getCurveFromName('bn128')

for (const inputs of INPUTS) {
const proof = await generateProof(inputs)
proofs.push(proof)
}
}, 30_000)

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

test('Should verify all Poseidon proofs', async (t) => {
for (const proof of proofs) {
const { numberOfInputs } = proof
await t.test(`Should verify a Poseidon proof with ${numberOfInputs} parameter(s)`, async (t) => {
const result = await verifyProof(proof)
assert.strictEqual(
result,
true,
`Proof verification failed for ${numberOfInputs} parameter${numberOfInputs > 1 ? 's' : ''}`,
)
})
}
})
})
13 changes: 13 additions & 0 deletions packages/poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,18 @@
},
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "node --test index.test.mjs"
},
"files": ["poseidon-*"],
"devDependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@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"
}
}
Loading