-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from zk-passport/feat/custom-hasher
Add customeHasherTester
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); | ||
}); |