From bbd602e8da684b5a7f91bd57e070b905fcce437a Mon Sep 17 00:00:00 2001 From: Deepak Maram Date: Tue, 30 May 2023 20:01:26 -0400 Subject: [PATCH] Add poseidon-lite tests --- openid-zkp-auth/package-lock.json | 9 +++- openid-zkp-auth/package.json | 7 ++- openid-zkp-auth/test/hasher.circom.test.js | 57 +++++++++++++++++++--- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/openid-zkp-auth/package-lock.json b/openid-zkp-auth/package-lock.json index dc879bbee9..c1d820b145 100644 --- a/openid-zkp-auth/package-lock.json +++ b/openid-zkp-auth/package-lock.json @@ -27,7 +27,8 @@ "jose": "^4.3.7", "mocha": "^10.2.0", "node-fetch": "^2.6.1", - "node-jose": "^2.0.0" + "node-jose": "^2.0.0", + "poseidon-lite": "^0.2.0" } }, "node_modules/@ethersproject/abi": { @@ -2386,6 +2387,12 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/poseidon-lite": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/poseidon-lite/-/poseidon-lite-0.2.0.tgz", + "integrity": "sha512-vivDZnGmz8W4G/GzVA72PXkfYStjilu83rjjUfpL4PueKcC8nfX6hCPh2XhoC5FBgC6y0TA3YuUeUo5YCcNoig==", + "dev": true + }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/openid-zkp-auth/package.json b/openid-zkp-auth/package.json index 5c856477c1..4794f2ceb5 100644 --- a/openid-zkp-auth/package.json +++ b/openid-zkp-auth/package.json @@ -6,7 +6,9 @@ "test": "mocha --max-old-space-size=16000 -t 10000s", "prove": "node js/prove.js" }, - "bin": { "zkpinputs" : "js/genZKPinputs.js" }, + "bin": { + "zkpinputs": "js/genZKPinputs.js" + }, "author": "", "license": "ISC", "dependencies": { @@ -24,7 +26,8 @@ "circomlibjs": "^0.1.7", "jose": "^4.3.7", "mocha": "^10.2.0", + "node-fetch": "^2.6.1", "node-jose": "^2.0.0", - "node-fetch": "^2.6.1" + "poseidon-lite": "^0.2.0" } } diff --git a/openid-zkp-auth/test/hasher.circom.test.js b/openid-zkp-auth/test/hasher.circom.test.js index ef2bd972f6..ae8b13b50e 100644 --- a/openid-zkp-auth/test/hasher.circom.test.js +++ b/openid-zkp-auth/test/hasher.circom.test.js @@ -9,6 +9,42 @@ const testutils = require("./testutils"); const buildPoseidon = require("circomlibjs").buildPoseidon; +const poseidonlite = require("poseidon-lite"); + +const poseidonNumToHashFN = [ + undefined, + poseidonlite.poseidon1, + poseidonlite.poseidon2, + poseidonlite.poseidon3, + poseidonlite.poseidon4, + poseidonlite.poseidon5, + poseidonlite.poseidon6, + poseidonlite.poseidon7, + poseidonlite.poseidon8, + poseidonlite.poseidon9, + poseidonlite.poseidon10, + poseidonlite.poseidon11, + poseidonlite.poseidon12, + poseidonlite.poseidon13, + poseidonlite.poseidon14, + poseidonlite.poseidon15, +]; + +function litePoseidonHash(inputs) { + const hashFN = poseidonNumToHashFN[inputs.length]; + if (hashFN) { + return hashFN(inputs); + } else if (inputs.length <= 30) { + const hash1 = litePoseidonHash(inputs.slice(0, 15)); + const hash2 = litePoseidonHash(inputs.slice(15)); + return litePoseidonHash([hash1, hash2]); + } else { + throw new Error( + `Yet to implement: Unable to hash a vector of length ${inputs.length}` + ); + } +} + describe("Zk-friendly hashing (Poseidon) tests", () => { const P = BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); const circuit_path = path.join(__dirname, "../circuits/helpers", "hasher.circom"); @@ -21,22 +57,24 @@ describe("Zk-friendly hashing (Poseidon) tests", () => { cir = await testutils.genMain(circuit_path, "Hasher", [1]); await cir.loadSymbols(); input = [1]; - + const expected_hash = utils.poseidonHash(input, poseidon); + const witness = await cir.calculateWitness({ "in": input }); - assert.equal(testutils.getWitnessValue(witness, cir.symbols, "main.out"), - utils.poseidonHash(input, poseidon)); + assert.deepEqual(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(litePoseidonHash(input), expected_hash); }); it("Hashes two values", async () => { cir = await testutils.genMain(circuit_path, "Hasher", [2]); await cir.loadSymbols(); input = [1, 2]; + const expected_hash = utils.poseidonHash(input, poseidon); const witness = await cir.calculateWitness({ "in": input }); - assert.equal(testutils.getWitnessValue(witness, cir.symbols, "main.out"), - utils.poseidonHash(input, poseidon)); + assert.deepEqual(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(litePoseidonHash(input), expected_hash); }); it("Hashes 15 values", async () => { @@ -47,7 +85,8 @@ describe("Zk-friendly hashing (Poseidon) tests", () => { const witness = await cir.calculateWitness({ "in": input }); - assert.equal(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(litePoseidonHash(input), expected_hash); }); it("Hashes 16 values", async () => { @@ -58,7 +97,8 @@ describe("Zk-friendly hashing (Poseidon) tests", () => { const witness = await cir.calculateWitness({ "in": input }); - assert.equal(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(litePoseidonHash(input), expected_hash); }); it("Hashes 30 values", async () => { @@ -72,7 +112,8 @@ describe("Zk-friendly hashing (Poseidon) tests", () => { const witness = await cir.calculateWitness({ "in": input }); - assert.equal(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(testutils.getWitnessValue(witness, cir.symbols, "main.out"), expected_hash); + assert.deepEqual(litePoseidonHash(input), expected_hash); }); it("Nonce test", async () => {