Skip to content

Commit

Permalink
chore(poseidon): add verification key json files (#125)
Browse files Browse the repository at this point in the history
* chore(poseidon): add verification key json files

* chore(poseidon): bump version in package.json

* chore: update .gitignore

* chore(cli): update `start.cli` script

* refactor

* feat(poseidon): add verification keys

* test(poseidon): check validity of artifacts (#143)

* test(poseidon): verify proofs

* test: update
  • Loading branch information
sripwoud authored Sep 23, 2024
1 parent 28548cc commit 33661df
Show file tree
Hide file tree
Showing 54 changed files with 1,895 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
.envrc
.tool-versions
.DS_Store
snark-artifacts
136 changes: 68 additions & 68 deletions packages/artifacts/package.json
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
{
"name": "@zk-kit/artifacts",
"version": "2.0.1",
"type": "module",
"bin": {
"snarkli": "./dist/cli/index.js"
},
"description": "Utilities for downloading snark artifacts",
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
"test": "jest -c test/jest.config.ts",
"prepublishOnly": "pnpm run build",
"start.cli": "[ ! -f dist/cli/index.js ] && pnpm run build; node dist/cli",
"typecheck": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/privacy-scaling-explorations/snark-artifacts.git"
},
"keywords": [
"snark",
"artifacts",
"circom",
"zk"
],
"contributors": [
"cedoor <[email protected]>",
"sripwoud <[email protected]"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/privacy-scaling-explorations/snark-artifacts/issues"
},
"homepage": "https://github.com/privacy-scaling-explorations/snark-artifacts/tree/main/packages/artifacts",
"publishConfig": {
"access": "public"
},
"main": "./dist/index.node.js",
"types": "./dist/types/index.node.d.ts",
"exports": {
".": {
"types": "./dist/types/index.browser.d.ts",
"node": {
"types": "./dist/types/index.node.d.ts",
"import": "./dist/index.node.js",
"require": "./dist/index.node.cjs",
"default": "./dist/index.node.cjs"
},
"browser": "./dist/index.browser.js",
"default": "./dist/index.browser.js"
}
},
"files": [
"dist",
"src"
],
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.12.10",
"rimraf": "^5.0.5",
"rollup": "^4.18.1"
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"@inquirer/input": "^2.1.8",
"@inquirer/select": "^2.3.4",
"circomkit": "^0.2.1",
"ora": "^5.4.1",
"snarkjs": "^0.7.4"
"name": "@zk-kit/artifacts",
"version": "2.0.1",
"type": "module",
"bin": {
"snarkli": "./dist/cli/index.js"
},
"description": "Utilities for downloading snark artifacts",
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
"test": "jest -c test/jest.config.ts",
"prepublishOnly": "pnpm run build",
"start.cli": "pnpm run build && node dist/cli",
"typecheck": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/privacy-scaling-explorations/snark-artifacts.git"
},
"keywords": [
"snark",
"artifacts",
"circom",
"zk"
],
"contributors": [
"cedoor <[email protected]>",
"sripwoud <[email protected]"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/privacy-scaling-explorations/snark-artifacts/issues"
},
"homepage": "https://github.com/privacy-scaling-explorations/snark-artifacts/tree/main/packages/artifacts",
"publishConfig": {
"access": "public"
},
"main": "./dist/index.node.js",
"types": "./dist/types/index.node.d.ts",
"exports": {
".": {
"types": "./dist/types/index.browser.d.ts",
"node": {
"types": "./dist/types/index.node.d.ts",
"import": "./dist/index.node.js",
"require": "./dist/index.node.cjs",
"default": "./dist/index.node.cjs"
},
"browser": "./dist/index.browser.js",
"default": "./dist/index.browser.js"
}
},
"files": [
"dist",
"src"
],
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.12.10",
"rimraf": "^5.0.5",
"rollup": "^4.18.1"
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"@inquirer/input": "^2.1.8",
"@inquirer/select": "^2.3.4",
"circomkit": "^0.2.1",
"ora": "^5.4.1",
"snarkjs": "^0.7.4"
}
}
6 changes: 3 additions & 3 deletions packages/artifacts/src/cli/commands/generate/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ export async function generateActionNoExit(
validateOrThrow(destination, existsSync)

config ??= await getCircomkitConfigInput()
const dirBuild = destination ?? (await getDestinationInput(`${cwd()}/snark-artifacts`))
const result = await setup(circuit, params, config, dirBuild)
destination ??= await getDestinationInput(`${cwd()}/snark-artifacts`)
const result = await setup(circuit, params, config, destination)

spinner.succeed(
`Snark artifacts for ${circuit ?? result.circuit} with parameters ${
params ?? result.params
} generated successfully in ${dirBuild}`,
} generated successfully in ${destination}`,
)
}

Expand Down
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' : ''}`,
)
})
}
})
})
15 changes: 14 additions & 1 deletion packages/poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zk-kit/poseidon-artifacts",
"description": "zk-kit poseidon artifacts",
"license": "MIT",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.4",
"repository": {
"type": "git",
"url": "https://github.com/privacy-scaling-explorations/snark-artifacts.git"
Expand All @@ -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"
}
}
99 changes: 99 additions & 0 deletions packages/poseidon/poseidon-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 2,
"vk_alpha_1": [
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
"1"
],
"vk_beta_2": [
[
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
],
[
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"3157975945221760019780653021768983303240570188057358198718286948576293769637",
"1168852709034697964767879935082196399493566161364132837006457069681024105649"
],
[
"16163470123996341422922694561925306221648024098447326302911331111121753640584",
"9276021473371070258982295893437827370362831845456822584750788309275437040607"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"19490069286251317200471893224761952280235157078692599655063040494106083015102",
"15613730057977833735664106983317680013118142165231654768046521650638333652991",
"1"
],
[
"1563543155852853229359605494188815884199915022658219002707722789976065966419",
"858819375930654753672617171465307097688802650498051619587167586479724200799",
"1"
],
[
"3808889614445935800597561392085733302718838702771107544944545050886958022904",
"13293649293049947010793838294353767499934999769633605908974566715226392122400",
"1"
]
]
}
Binary file modified packages/poseidon/poseidon-1.wasm
Binary file not shown.
Binary file modified packages/poseidon/poseidon-1.zkey
Binary file not shown.
Loading

0 comments on commit 33661df

Please sign in to comment.