Skip to content

Commit

Permalink
Merge pull request #234 from zk-passport/feat/custom-hasher
Browse files Browse the repository at this point in the history
Add customeHasherTester
  • Loading branch information
remicolin authored Oct 30, 2024
2 parents 2e4489e + 414cc72 commit c58e609
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions circuits/circuits/tests/utils/customHasher_tester.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.6;

include "../../utils/passport/customHashers.circom";

component main = CustomHasher(16);
46 changes: 46 additions & 0 deletions circuits/tests/other_circuits/custom_hasher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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());
});
});
});

0 comments on commit c58e609

Please sign in to comment.