From e39155c02ccd7da2b79f1a18969ef7f557ffe757 Mon Sep 17 00:00:00 2001 From: seshanthS <35675963+seshanthS@users.noreply.github.com> Date: Thu, 31 Oct 2024 00:00:33 +0530 Subject: [PATCH 1/2] add customeHasherTester --- .../tests/utils/customHasher_tester.circom | 5 +++ .../other_circuits/custom_hasher.test.ts | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 circuits/circuits/tests/utils/customHasher_tester.circom create mode 100644 circuits/tests/other_circuits/custom_hasher.test.ts diff --git a/circuits/circuits/tests/utils/customHasher_tester.circom b/circuits/circuits/tests/utils/customHasher_tester.circom new file mode 100644 index 00000000..0f21ee95 --- /dev/null +++ b/circuits/circuits/tests/utils/customHasher_tester.circom @@ -0,0 +1,5 @@ +pragma circom 2.1.6; + +include "../../utils/passport/customHashers.circom"; + +component main = CustomHasher(16); diff --git a/circuits/tests/other_circuits/custom_hasher.test.ts b/circuits/tests/other_circuits/custom_hasher.test.ts new file mode 100644 index 00000000..e3b19c45 --- /dev/null +++ b/circuits/tests/other_circuits/custom_hasher.test.ts @@ -0,0 +1,45 @@ +import { expect } from 'chai'; +import path from 'path'; +import { wasm as wasm_tester } from 'circom_tester'; +import { customHasher } from '../../../common/src/utils/pubkeyTree'; +import { formatInput } from '../../../common/src/utils/generateInputs'; + +describe('CustomHasher', function () { + this.timeout(0); + let circuit; + + this.beforeAll(async () => { + const circuitPath = path.resolve( + __dirname, + '../../circuits/tests/utils/customHasher_tester.circom' + ); + circuit = await wasm_tester(circuitPath, { + include: [ + 'node_modules', + './node_modules/@zk-kit/binary-merkle-root.circom/src', + './node_modules/circomlib/circuits', + ], + }); + }); + + describe('custom hasher', async () => { + + const randomNumbers = Array(16).fill(0).map(() => { + const maxVal = BigInt(2) ** BigInt(250); + const randomVal = BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)) * + BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); + return (randomVal % maxVal).toString(); + }); + const inputs = { "in": formatInput(randomNumbers) }; + + it('customHasher output should be the same between circom and js implementation', async () => { + const witness = await circuit.calculateWitness(inputs, true); + const hashValueCircom = (await circuit.getOutput(witness, ['out'])).out; + console.log('\x1b[34m', 'hashValueCircom: ', hashValueCircom, '\x1b[0m'); + const hashValueJs = customHasher(randomNumbers); + console.log('\x1b[34m', 'hashValueJs: ', hashValueJs, '\x1b[0m'); + expect(BigInt(hashValueCircom).toString()).to.equal(BigInt(hashValueJs).toString()); + }); + }); + +}); From 414cc729580eb29845f604ac90f348a403f09314 Mon Sep 17 00:00:00 2001 From: seshanthS <35675963+seshanthS@users.noreply.github.com> Date: Thu, 31 Oct 2024 00:12:32 +0530 Subject: [PATCH 2/2] run linter --- .../other_circuits/custom_hasher.test.ts | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/circuits/tests/other_circuits/custom_hasher.test.ts b/circuits/tests/other_circuits/custom_hasher.test.ts index e3b19c45..1a5fbe1e 100644 --- a/circuits/tests/other_circuits/custom_hasher.test.ts +++ b/circuits/tests/other_circuits/custom_hasher.test.ts @@ -5,41 +5,42 @@ import { customHasher } from '../../../common/src/utils/pubkeyTree'; import { formatInput } from '../../../common/src/utils/generateInputs'; describe('CustomHasher', function () { - this.timeout(0); - let circuit; + this.timeout(0); + let circuit; - this.beforeAll(async () => { - const circuitPath = path.resolve( - __dirname, - '../../circuits/tests/utils/customHasher_tester.circom' - ); - circuit = await wasm_tester(circuitPath, { - include: [ - 'node_modules', - './node_modules/@zk-kit/binary-merkle-root.circom/src', - './node_modules/circomlib/circuits', - ], - }); + this.beforeAll(async () => { + const circuitPath = path.resolve( + __dirname, + '../../circuits/tests/utils/customHasher_tester.circom' + ); + circuit = await wasm_tester(circuitPath, { + include: [ + 'node_modules', + './node_modules/@zk-kit/binary-merkle-root.circom/src', + './node_modules/circomlib/circuits', + ], }); + }); - describe('custom hasher', async () => { + describe('custom hasher', async () => { + const randomNumbers = Array(16) + .fill(0) + .map(() => { + const maxVal = BigInt(2) ** BigInt(250); + const randomVal = + BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)) * + BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); + return (randomVal % maxVal).toString(); + }); + const inputs = { in: formatInput(randomNumbers) }; - const randomNumbers = Array(16).fill(0).map(() => { - const maxVal = BigInt(2) ** BigInt(250); - const randomVal = BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)) * - BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); - return (randomVal % maxVal).toString(); - }); - const inputs = { "in": formatInput(randomNumbers) }; - - it('customHasher output should be the same between circom and js implementation', async () => { - const witness = await circuit.calculateWitness(inputs, true); - const hashValueCircom = (await circuit.getOutput(witness, ['out'])).out; - console.log('\x1b[34m', 'hashValueCircom: ', hashValueCircom, '\x1b[0m'); - const hashValueJs = customHasher(randomNumbers); - console.log('\x1b[34m', 'hashValueJs: ', hashValueJs, '\x1b[0m'); - expect(BigInt(hashValueCircom).toString()).to.equal(BigInt(hashValueJs).toString()); - }); + it('customHasher output should be the same between circom and js implementation', async () => { + const witness = await circuit.calculateWitness(inputs, true); + const hashValueCircom = (await circuit.getOutput(witness, ['out'])).out; + console.log('\x1b[34m', 'hashValueCircom: ', hashValueCircom, '\x1b[0m'); + const hashValueJs = customHasher(randomNumbers); + console.log('\x1b[34m', 'hashValueJs: ', hashValueJs, '\x1b[0m'); + expect(BigInt(hashValueCircom).toString()).to.equal(BigInt(hashValueJs).toString()); }); - + }); });