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): verify proofs #127

Closed
wants to merge 3 commits into from
Closed
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
69 changes: 69 additions & 0 deletions packages/poseidon/index.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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, it } 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()
})

it('Should verify a Poseidon proof', async () => {
const errors = []

for (const proof of proofs) {
const result = await verifyProof(proof)
try {
assert.strictEqual(result, true, `proof verification failed for ${proof.numberOfInputs}`)
} catch (error) {
errors.push(error)
}
}

if (errors.length > 0)
throw new Error(errors.map(error => error.message).join('\n'))
})
})
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": [
[
"2163542928548905947896678956684071685423781099324882879510801721907989064888",
"2340618149359620585312321030878631999659039320231581359038219236149631763816"
],
[
"5870132499270633845244269499623164405548018862360596940485582800596774127452",
"9254036701666827375186896088687192458784874184085158589215912574071739326602"
],
[
"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"
]
]
}
99 changes: 99 additions & 0 deletions packages/poseidon/poseidon-10.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": [
[
"1112447314063621540315834780069198739002661672132959536184933052484914376519",
"1474992390694772487643449158097297401087693785980712157977498493771925998352"
],
[
"7924668040342389689612350942509944838038496518970823739437452459671381800836",
"4724890550148168571595368666952711364466408779834445834046301015190767072984"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"798266006634704296356807896304782617057267109446481684272074776676559365377",
"2119139191057773416624154862008823601747165120947596460685518302760052477540",
"1"
],
[
"17743954615775269739473094351635203185883870143434239552566169722164063573412",
"4830438380339493939707702694553275524057591153138914848741234629532535265855",
"1"
],
[
"3796651289821923027753272300677071875561083096603899517484713434570556592125",
"6095027127012064440741913106185486005983523295112206123623056096004008125125",
"1"
]
]
}
99 changes: 99 additions & 0 deletions packages/poseidon/poseidon-11.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": [
[
"12249968644662304834073133907153089873558962714475272702200787816924511201762",
"10329255749665680246220781455468288633915525329241470488612989993889528759647"
],
[
"8379477850573296064523394139829085376151866600926553474464842731460723220137",
"13975202135139105440811287339327408011019650210177592566571220927186055282367"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"20896314949419669765122080719670563957005377809880391165219112342045724637250",
"4832396215740174951649707718991048955348422076401947958263135738277030640702",
"1"
],
[
"16593634394872419207033083223572861328698354554381035892795187358904450228380",
"9701239775839670407542964511868405320901788463030859950701125893205826970190",
"1"
],
[
"9167350343757520738096817169608613527226407725783625898259506700381039420314",
"15333436194900493791133741272915135408304144665526863658315589456211375313779",
"1"
]
]
}
Loading
Loading