diff --git a/circuits/circuits/tests/utils/leafHasher_tester.circom b/circuits/circuits/tests/utils/commitment_tester.circom similarity index 100% rename from circuits/circuits/tests/utils/leafHasher_tester.circom rename to circuits/circuits/tests/utils/commitment_tester.circom diff --git a/circuits/tests/other_circuits/commitment.test.ts b/circuits/tests/other_circuits/commitment.test.ts new file mode 100644 index 00000000..7dde9040 --- /dev/null +++ b/circuits/tests/other_circuits/commitment.test.ts @@ -0,0 +1,59 @@ +import { expect } from 'chai'; +import path from 'path'; +import { wasm as wasm_tester } from 'circom_tester'; + +import { formatDg2Hash, formatMrz, packBytes } from '../../../common/src/utils/utils'; +import { getLeaf, generateCommitment } from '../../../common/src/utils/pubkeyTree'; + +import { genMockPassportData } from '../../../common/src/utils/genMockPassportData'; +import { formatInput } from '../../../common/src/utils/generateInputs'; + +describe('commitment hasher', function () { + this.timeout(0); + let circuit; + + this.beforeAll(async () => { + const circuitPath = path.resolve( + __dirname, + '../../circuits/tests/utils/commitment_tester.circom' + ); + circuit = await wasm_tester(circuitPath, { + include: [ + 'node_modules', + './node_modules/@zk-kit/binary-merkle-root.circom/src', + './node_modules/circomlib/circuits', + ], + }); + }); + describe('generate commitment', async () => { + const passportData = genMockPassportData('rsa_sha256', 'FRA', '000101', '300101'); + const formattedMrz = formatMrz(passportData.mrz); + const dg2HashFormatted = formatDg2Hash(passportData.dg2Hash); + const secret = 0; + const attestation_id = 1; + const leaf = getLeaf(passportData.dsc); + const inputs = { + secret: formatInput(secret), + attestation_id: formatInput(attestation_id), + leaf: formatInput(leaf), + dg1: formatInput(formattedMrz), + dg2_hash: dg2HashFormatted, + }; + + it('commitment from circuits should be equal to commitment from js', async () => { + const witness = await circuit.calculateWitness(inputs, true); + const leafValueCircom = (await circuit.getOutput(witness, ['out'])).out; + console.log('\x1b[34m', 'hashValueCircom: ', leafValueCircom, '\x1b[0m'); + const mrz_bytes_packed = packBytes(formattedMrz); + const commitment = generateCommitment( + BigInt(secret).toString(), + BigInt(attestation_id).toString(), + BigInt(leaf).toString(), + mrz_bytes_packed, + dg2HashFormatted + ); + console.log('\x1b[34m', 'commitment in js : ', commitment, '\x1b[0m'); + expect(BigInt(leafValueCircom).toString()).to.equal(BigInt(commitment).toString()); + }); + }); +}); diff --git a/circuits/tests/other_circuits/leaf_hasher.test.ts b/circuits/tests/other_circuits/leaf_hasher.test.ts deleted file mode 100644 index cb364d4d..00000000 --- a/circuits/tests/other_circuits/leaf_hasher.test.ts +++ /dev/null @@ -1,157 +0,0 @@ -import { expect } from 'chai'; -import { X509Certificate } from 'crypto'; -import path from 'path'; -import { wasm as wasm_tester } from 'circom_tester'; -import forge from 'node-forge'; - -import { - mock_dsc_sha256_rsa_2048, - mock_csca_sha256_rsa_2048, - mock_dsc_sha1_rsa_2048, - mock_csca_sha1_rsa_2048, - mock_dsc_sha256_ecdsa, -} from '../../../common/src/constants/mockCertificates'; -import { - formatDg2Hash, - formatMrz, - hexToDecimal, - packBytes, - splitToWords, - toUnsignedByte, -} from '../../../common/src/utils/utils'; -import { getLeaf, customHasher, generateCommitment } from '../../../common/src/utils/pubkeyTree'; -import { - k_dsc, - k_dsc_ecdsa, - n_dsc, - n_dsc_ecdsa, - PASSPORT_ATTESTATION_ID, - SignatureAlgorithmIndex, -} from '../../../common/src/constants/constants'; -import { - parseCertificate, - parseDSC, -} from '../../../common/src/utils/certificates/handleCertificate'; -import { genMockPassportData } from '../../../common/src/utils/genMockPassportData'; -import { generateCircuitInputsInCircuits } from '../utils/generateMockInputsInCircuits'; -import { formatInput, generateCircuitInputsProve } from '../../../common/src/utils/generateInputs'; - -function loadCertificates(dscCertContent: string, cscaCertContent: string) { - const dscCert = new X509Certificate(dscCertContent); - const cscaCert = new X509Certificate(cscaCertContent); - const dscCert_forge = forge.pki.certificateFromPem(dscCertContent); - const cscaCert_forge = forge.pki.certificateFromPem(cscaCertContent); - - return { dscCert, cscaCert, dscCert_forge, cscaCert_forge }; -} - -describe('LeafHasher Light', function () { - this.timeout(0); - let circuit; - - this.beforeAll(async () => { - const circuitPath = path.resolve( - __dirname, - '../../circuits/tests/utils/leafHasher_tester.circom' - ); - circuit = await wasm_tester(circuitPath, { - include: [ - 'node_modules', - './node_modules/@zk-kit/binary-merkle-root.circom/src', - './node_modules/circomlib/circuits', - ], - }); - }); - - // describe('CustomHasher - getLeaf ECDSA', async () => { - // const cert = mock_dsc_sha256_ecdsa; - // const { signatureAlgorithm, hashFunction, x, y, bits, curve, exponent } = parseCertificate(cert); - // console.log(parseCertificate(cert)); - // const leaf_light = getLeaf(cert, n_dsc_ecdsa, k_dsc_ecdsa); - // console.log('\x1b[34m', 'customHasher output: ', leaf_light, '\x1b[0m'); - - // const passportData = genMockPassportData('ecdsa_sha256', 'FRA', '000101', '300101'); - // const mock_inputs = generateCircuitInputsInCircuits(passportData, 'register'); - - // const signatureAlgorithmIndex = SignatureAlgorithmIndex[`${signatureAlgorithm}_${curve || exponent}_${hashFunction}_${bits}`]; - // console.log('\x1b[34m', 'signatureAlgorithmIndex: ', signatureAlgorithmIndex, '\x1b[0m'); - // it('should extract and log certificate information', async () => { - // const inputs = { - // in: mock_inputs.pubKey, - // sigAlg: signatureAlgorithmIndex, - // }; - // const witness = await circuit.calculateWitness(inputs, true); - // const leafValueCircom = (await circuit.getOutput(witness, ['out'])).out; - // console.log('\x1b[34m', 'leafValueCircom: ', leafValueCircom, '\x1b[0m'); - // expect(leafValueCircom).to.equal(leaf_light); - // }); - // }); - - // describe('CustomHasher - customHasher', async () => { - // const passportData = genMockPassportData('rsa_sha256', 'FRA', '000101', '300101'); - // it('should extract and log certificate information', async () => { - // const inputs = { - // in: passportData.dg2Hash.map((x) => toUnsignedByte(x).toString()), - // }; - // const witness = await circuit.calculateWitness(inputs, true); - // const leafValueCircom = (await circuit.getOutput(witness, ['out'])).out; - // console.log('\x1b[34m', 'hashValueCircom: ', leafValueCircom, '\x1b[0m'); - - // const hashValue = customHasher(passportData.dg2Hash.map((x) => toUnsignedByte(x).toString())); - // console.log('\x1b[34m', 'hashValue: ', hashValue, '\x1b[0m'); - // }); - // }); - describe('GenerateCommitment - computeCommitment', async () => { - const passportData = genMockPassportData('rsa_sha256', 'FRA', '000101', '300101'); - const formattedMrz = formatMrz(passportData.mrz); - const dg2HashFormatted = formatDg2Hash(passportData.dg2Hash); - const secret = 0; - const attestation_id = 1; - const leaf = getLeaf(passportData.dsc); - const inputs = { - secret: formatInput(secret), - attestation_id: formatInput(attestation_id), - leaf: formatInput(leaf), - dg1: formatInput(formattedMrz), - dg2_hash: dg2HashFormatted, - }; - // console.log(inputs); - - it('should generate the commitment', async () => { - const witness = await circuit.calculateWitness(inputs, true); - const leafValueCircom = (await circuit.getOutput(witness, ['out'])).out; - console.log('\x1b[34m', 'hashValueCircom: ', leafValueCircom, '\x1b[0m'); - const mrz_bytes_packed = packBytes(formattedMrz); - const commitment = generateCommitment( - BigInt(secret).toString(), - BigInt(attestation_id).toString(), - BigInt(leaf).toString(), - mrz_bytes_packed, - dg2HashFormatted - ); - console.log('\x1b[34m', 'commitment in js : ', commitment, '\x1b[0m'); - expect(BigInt(leafValueCircom).toString()).to.equal(BigInt(commitment).toString()); - }); - }); - - // describe('CustomHasher - getLeaf RSA', async () => { - // const cert = mock_dsc_sha1_rsa_2048; - // const { signatureAlgorithm, hashFunction, modulus, x, y, bits, curve, exponent } = - // parseCertificate(cert); - // console.log(parseCertificate(cert)); - // const leaf_light = getLeaf(cert, n_dsc, k_dsc); - // console.log('\x1b[34m', 'customHasher: ', leaf_light, '\x1b[0m'); - // it('should extract and log certificate information', async () => { - // const inputs = { - // in: splitToWords(BigInt(hexToDecimal(modulus)), n_dsc, k_dsc), - // sigAlg: - // SignatureAlgorithmIndex[ - // `${signatureAlgorithm}_${curve || exponent}_${hashFunction}_${bits}` - // ], - // }; - // const witness = await circuit.calculateWitness(inputs, true); - // const leafValueCircom = (await circuit.getOutput(witness, ['out'])).out; - // expect(leafValueCircom).to.equal(leaf_light); - // }); - // }); -});