diff --git a/circuits/tests/register/register.test.ts b/circuits/tests/register/register.test.ts index 1d3463630..975abc75b 100644 --- a/circuits/tests/register/register.test.ts +++ b/circuits/tests/register/register.test.ts @@ -12,11 +12,11 @@ import { getCircuitName, getSignatureAlgorithm } from '../../../common/src/utils import { SignatureAlgorithm } from '../../../common/tests/genMockPassportData.test'; const sigAlgs = [ - // { sigAlg: 'rsa', hashFunction: 'sha1' }, - // { sigAlg: 'rsa', hashFunction: 'sha256' }, - // { sigAlg: 'rsapss', hashFunction: 'sha256' }, + { sigAlg: 'rsa', hashFunction: 'sha1' }, + { sigAlg: 'rsa', hashFunction: 'sha256' }, + { sigAlg: 'rsapss', hashFunction: 'sha256' }, { sigAlg: 'ecdsa', hashFunction: 'sha256' }, - // { sigAlg: 'ecdsa', hashFunction: 'sha1' }, + { sigAlg: 'ecdsa', hashFunction: 'sha1' }, ]; sigAlgs.forEach(({ sigAlg, hashFunction }) => { @@ -69,6 +69,14 @@ sigAlgs.forEach(({ sigAlg, hashFunction }) => { console.log('\x1b[34m%s\x1b[0m', 'blinded_dsc_commitment', blinded_dsc_commitment); // const mrz_bytes = packBytes(inputs.mrz); + + // for ecdsa: + + // const leaf = getLeaf({ + // signatureAlgorithm: passportData.signatureAlgorithm, + // publicKeyQ: passportData.pubKey.publicKeyQ, + // }).toString(); + // const commitment_bytes = poseidon6([ // inputs.secret[0], // PASSPORT_ATTESTATION_ID, @@ -115,7 +123,8 @@ sigAlgs.forEach(({ sigAlg, hashFunction }) => { try { const invalidInputs = { ...inputs, - signature: inputs.signature.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)), + signature: inputs.signature ? inputs.signature.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)) : undefined, + signature_s: inputs.signature_s ? inputs.signature_s.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)) : undefined, }; await circuit.calculateWitness(invalidInputs); expect.fail('Expected an error but none was thrown.'); diff --git a/circuits/tests/register/register_ecdsa_sha1.test.ts b/circuits/tests/register/register_ecdsa_sha1.test.ts deleted file mode 100644 index 17bbb0c04..000000000 --- a/circuits/tests/register/register_ecdsa_sha1.test.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { describe } from 'mocha'; -import path from 'path'; -import { poseidon6 } from 'poseidon-lite'; -import { mockPassportData_sha1_ecdsa } from '../../../common/src/constants/mockPassportData'; -import { generateCircuitInputsRegister } from '../../../common/src/utils/generateInputs'; -import { BigintToArray, hexToDecimal, packBytes } from '../../../common/src/utils/utils'; -import { expect } from 'chai'; -import { getLeaf } from '../../../common/src/utils/pubkeyTree'; -import { wasm as wasm_tester } from 'circom_tester'; -import { PASSPORT_ATTESTATION_ID } from '../../../common/src/constants/constants'; - -describe('Register - SHA1 WITH ECDSA', function () { - this.timeout(0); - let circuit: any; - - const passportData = mockPassportData_sha1_ecdsa; - const n_dsc = 43; // 43 * 6 = 258 > 254 Cirom field size - const k_dsc = 6; - const secret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); - const dscSecret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); - - const inputs = generateCircuitInputsRegister( - secret, - dscSecret, - PASSPORT_ATTESTATION_ID, - passportData, - n_dsc, - k_dsc - ); - - before(async () => { - circuit = await wasm_tester( - path.join(__dirname, '../../circuits/register/register_ecdsa_sha1.circom'), - { - include: [ - 'node_modules', - './node_modules/@zk-kit/binary-merkle-root.circom/src', - './node_modules/circomlib/circuits', - './node_modules/dmpierre/sha1-circom/circuits', - ], - } - ); - }); - - it('should compile and load the circuit', async function () { - expect(circuit).to.not.be.undefined; - }); - - it('should calculate the witness with correct inputs', async function () { - const w = await circuit.calculateWitness(inputs); - await circuit.checkConstraints(w); - - const nullifier = (await circuit.getOutput(w, ['nullifier'])).nullifier; - console.log('\x1b[34m%s\x1b[0m', 'nullifier', nullifier); - const commitment_circom = (await circuit.getOutput(w, ['commitment'])).commitment; - console.log('\x1b[34m%s\x1b[0m', 'commitment', commitment_circom); - const blinded_dsc_commitment = (await circuit.getOutput(w, ['blinded_dsc_commitment'])) - .blinded_dsc_commitment; - console.log('\x1b[34m%s\x1b[0m', 'blinded_dsc_commitment', blinded_dsc_commitment); - - const mrz_bytes = packBytes(inputs.mrz); - const leaf = getLeaf({ - signatureAlgorithm: passportData.signatureAlgorithm, - publicKeyQ: passportData.pubKey.publicKeyQ, - }).toString(); - - const commitment_bytes = poseidon6([ - inputs.secret[0], - PASSPORT_ATTESTATION_ID, - leaf, - mrz_bytes[0], - mrz_bytes[1], - mrz_bytes[2], - ]); - const commitment_js = commitment_bytes.toString(); - expect(commitment_circom).to.be.equal(commitment_js); - }); - - it('should fail to calculate witness with invalid dataHashes', async function () { - try { - const invalidInputs = { - ...inputs, - dataHashes: inputs.dataHashes.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)), - }; - await circuit.calculateWitness(invalidInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } - }); - - it('should fail to calculate witness with invalid mrz', async function () { - try { - const invalidInputs = { - ...inputs, - mrz: Array(93) - .fill(0) - .map((byte) => BigInt(byte).toString()), - }; - await circuit.calculateWitness(invalidInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } - }); - - it('should fail to calculate witness with invalid signature', async function () { - let wrong_signature_s = BigintToArray(43, 6, BigInt(hexToDecimal('10') + 1)); - try { - const invalidInputs = { - ...inputs, - signature_s: wrong_signature_s, - }; - await circuit.calculateWitness(invalidInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } - }); -}); diff --git a/circuits/tests/register/register_ecdsa_sha256.test.ts b/circuits/tests/register/register_ecdsa_sha256.test.ts deleted file mode 100644 index 09e1b487a..000000000 --- a/circuits/tests/register/register_ecdsa_sha256.test.ts +++ /dev/null @@ -1,119 +0,0 @@ -import { describe } from 'mocha'; -import path from 'path'; -import { poseidon6 } from 'poseidon-lite'; -import { mockPassportData_sha256_ecdsa } from '../../../common/src/constants/mockPassportData'; -import { generateCircuitInputsRegister } from '../../../common/src/utils/generateInputs'; -import { BigintToArray, hexToDecimal, packBytes } from '../../../common/src/utils/utils'; -import { expect } from 'chai'; -import { getLeaf } from '../../../common/src/utils/pubkeyTree'; -import { wasm as wasm_tester } from 'circom_tester'; -import { PASSPORT_ATTESTATION_ID } from '../../../common/src/constants/constants'; - -describe('Register - SHA256 WITH ECDSA', function () { - this.timeout(0); - let circuit: any; - - const passportData = mockPassportData_sha256_ecdsa; - const n_dsc = 43; // 43 * 6 = 258 > 254 Cirom field size - const k_dsc = 6; - const secret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); - const dscSecret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); - - const inputs = generateCircuitInputsRegister( - secret, - dscSecret, - PASSPORT_ATTESTATION_ID, - passportData, - n_dsc, - k_dsc - ); - - before(async () => { - circuit = await wasm_tester( - path.join(__dirname, '../../circuits/register/register_ecdsa_sha256.circom'), - { - include: [ - 'node_modules', - './node_modules/@zk-kit/binary-merkle-root.circom/src', - './node_modules/circomlib/circuits', - ], - } - ); - }); - - it('should compile and load the circuit', async function () { - expect(circuit).to.not.be.undefined; - }); - - it('should calculate the witness with correct inputs', async function () { - const w = await circuit.calculateWitness(inputs); - await circuit.checkConstraints(w); - - const nullifier = (await circuit.getOutput(w, ['nullifier'])).nullifier; - console.log('\x1b[34m%s\x1b[0m', 'nullifier', nullifier); - const commitment_circom = (await circuit.getOutput(w, ['commitment'])).commitment; - console.log('\x1b[34m%s\x1b[0m', 'commitment', commitment_circom); - const blinded_dsc_commitment = (await circuit.getOutput(w, ['blinded_dsc_commitment'])) - .blinded_dsc_commitment; - console.log('\x1b[34m%s\x1b[0m', 'blinded_dsc_commitment', blinded_dsc_commitment); - - const mrz_bytes = packBytes(inputs.mrz); - const leaf = getLeaf({ - signatureAlgorithm: passportData.signatureAlgorithm, - publicKeyQ: passportData.pubKey.publicKeyQ, - }).toString(); - - const commitment_bytes = poseidon6([ - inputs.secret[0], - PASSPORT_ATTESTATION_ID, - leaf, - mrz_bytes[0], - mrz_bytes[1], - mrz_bytes[2], - ]); - const commitment_js = commitment_bytes.toString(); - expect(commitment_circom).to.be.equal(commitment_js); - }); - - it('should fail to calculate witness with invalid dataHashes', async function () { - try { - const invalidInputs = { - ...inputs, - dataHashes: inputs.dataHashes.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)), - }; - await circuit.calculateWitness(invalidInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } - }); - - it('should fail to calculate witness with invalid mrz', async function () { - try { - const invalidInputs = { - ...inputs, - mrz: Array(93) - .fill(0) - .map((byte) => BigInt(byte).toString()), - }; - await circuit.calculateWitness(invalidInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } - }); - - it('should fail to calculate witness with invalid signature', async function () { - let wrong_signature_s = BigintToArray(43, 6, BigInt(hexToDecimal('10') + 1)); - try { - const invalidInputs = { - ...inputs, - signature_s: wrong_signature_s, - }; - await circuit.calculateWitness(invalidInputs); - expect.fail('Expected an error but none was thrown.'); - } catch (error) { - expect(error.message).to.include('Assert Failed'); - } - }); -}); diff --git a/common/src/utils/genMockPassportData.ts b/common/src/utils/genMockPassportData.ts index 7764e8e0f..5e52a7aa7 100644 --- a/common/src/utils/genMockPassportData.ts +++ b/common/src/utils/genMockPassportData.ts @@ -133,7 +133,7 @@ function sign( const keyPair = ec.keyFromPrivate(privateKeyBuffer); - const md = forge.md.sha256.create(); + const md = hashFunction === 'sha1' ? forge.md.sha1.create() : forge.md.sha256.create(); md.update(forge.util.binary.raw.encode(new Uint8Array(eContent))); const signature = keyPair.sign(md.digest().toHex(), 'hex'); const signatureBytes = Array.from(Buffer.from(signature.toDER(), 'hex')); diff --git a/common/src/utils/generateInputs.ts b/common/src/utils/generateInputs.ts index b2410f4fd..ed35bdd2a 100644 --- a/common/src/utils/generateInputs.ts +++ b/common/src/utils/generateInputs.ts @@ -144,8 +144,6 @@ export function generateCircuitInputsRegister( }; } - console.log(signatureComponents); - console.log(dscModulusComponents); return { secret: [secret], mrz: formattedMrz.map((byte) => String(byte)), diff --git a/common/src/utils/handleCertificate.ts b/common/src/utils/handleCertificate.ts index 72497bc13..ea4abd330 100644 --- a/common/src/utils/handleCertificate.ts +++ b/common/src/utils/handleCertificate.ts @@ -34,7 +34,6 @@ export const getSignatureAlgorithm = (pemContent: string) => { const exponent = Buffer.from((rsaPublicKey as any).value[1].valueBlock.valueHexView).toString('hex'); publicKeyDetails = { modulus, exponent }; } - console.log(publicKeyDetails); return { signatureAlgorithm, hashFunction, hashLen, ...publicKeyDetails }; } diff --git a/common/tests/genMockPassportData.test.ts b/common/tests/genMockPassportData.test.ts index 66476085a..802341a6a 100644 --- a/common/tests/genMockPassportData.test.ts +++ b/common/tests/genMockPassportData.test.ts @@ -60,8 +60,9 @@ function verify(passportData: PassportData): boolean { const ec = new elliptic.ec(curveForElliptic); const key = ec.keyFromPublic(publicKeyBuffer); - const messageBuffer = Buffer.from(eContent); - const msgHash = crypto.createHash('sha256').update(messageBuffer).digest(); + const md = hashFunction === 'sha1' ? forge.md.sha1.create() : forge.md.sha256.create(); + md.update(forge.util.binary.raw.encode(new Uint8Array(eContent))); + const msgHash = md.digest().toHex() const signature_crypto = Buffer.from(encryptedDigest).toString('hex'); return key.verify(msgHash, signature_crypto);