Skip to content

Commit

Permalink
test(poseidon): check validity of artifacts (#143)
Browse files Browse the repository at this point in the history
* test(poseidon): verify proofs

* test: update
  • Loading branch information
sripwoud authored Sep 20, 2024
1 parent eae23d9 commit c873086
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 38 deletions.
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

0 comments on commit c873086

Please sign in to comment.