From 71fc5baa4d2be90a95c51feef4be2e04fd4f6b7f Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Sun, 14 Jul 2024 20:14:15 +0200 Subject: [PATCH] clean rsapss tests --- circuits/test/RSSSSAPSS_2048.test.ts | 152 ------------------ circuits/test/RSSSSAPSS_2057.test.ts | 151 ----------------- .../mgf1_sha256.test.ts} | 0 .../tests/rsassapss/rsassapss_2048.test.ts | 66 ++++++++ .../tests/rsassapss/rsassapss_2057.test.ts | 64 ++++++++ 5 files changed, 130 insertions(+), 303 deletions(-) delete mode 100644 circuits/test/RSSSSAPSS_2048.test.ts delete mode 100644 circuits/test/RSSSSAPSS_2057.test.ts rename circuits/tests/{utils/Mgf1_sha256.test.ts => rsassapss/mgf1_sha256.test.ts} (100%) create mode 100644 circuits/tests/rsassapss/rsassapss_2048.test.ts create mode 100644 circuits/tests/rsassapss/rsassapss_2057.test.ts diff --git a/circuits/test/RSSSSAPSS_2048.test.ts b/circuits/test/RSSSSAPSS_2048.test.ts deleted file mode 100644 index 58420f7b..00000000 --- a/circuits/test/RSSSSAPSS_2048.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { describe } from 'mocha' -import { assert, expect } from 'chai' -import path from "path"; -const wasm_tester = require("circom_tester").wasm; -import { poseidon1, poseidon6 } from "poseidon-lite"; -import { generateCircuitInputsRegister } from '../../common/src/utils/generateInputs'; -import { assembleEContent, formatAndConcatenateDataHashes, formatMrz, hash, packBytes } from '../../common/src/utils/utils'; -import { PassportData } from '../../common/src/utils/types'; -import * as forge from 'node-forge'; -import crypto from 'crypto'; - - -describe("Proof of Passport - Circuits - RSASSAPSS - 2048", function () { - this.timeout(0); - let inputs: any; - let circuit: any; - let attestation_id: string; - - const sampleMRZ = "P byte < 128 ? byte : byte - 256); - - return { - mrz: sampleMRZ, - signatureAlgorithm: signatureAlgorithm, - pubKey: { - modulus: modulus, - exponent: exponent, - }, - dataGroupHashes: concatenatedDataHashes, - eContent: eContent, - encryptedDigest: signatureArray, - photoBase64: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIw3..." - } - } - - let passportData = genMockPassportData_sha256WithRSASSAPSS_65537(); - - - before(async () => { - circuit = await wasm_tester( - path.join(__dirname, "../circuits/tests/RSASSAPSS/rsassapss_2048.circom"), - { - include: [ - "node_modules", - "node_modules/@zk-email/circuits/helpers/sha.circom", - // "./node_modules/@zk-kit/binary-merkle-root.circom/src", - "./node_modules/circomlib/circuits" - ] - }, - ); - - const secret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); - console.log("secret", secret); - - const attestation_name = "E-PASSPORT"; - attestation_id = poseidon1([ - BigInt(Buffer.from(attestation_name).readUIntBE(0, 6)) - ]).toString(); - - inputs = generateCircuitInputsRegister( - secret, - attestation_id, - passportData, - [passportData], - ); - }); - - 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({ - signature: inputs.signature, - pubkey: inputs.pubkey, - eContentBytes: inputs.signed_attributes, - }); - await circuit.checkConstraints(w); - }); - - it("should fail to calculate witness with invalid signature", async function () { - try { - const invalidInputs = { - pubkey: inputs.pubkey, - eContentBytes: inputs.signed_attributes, - signature: inputs.signature.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"); - } - }); - -}); \ No newline at end of file diff --git a/circuits/test/RSSSSAPSS_2057.test.ts b/circuits/test/RSSSSAPSS_2057.test.ts deleted file mode 100644 index 30836932..00000000 --- a/circuits/test/RSSSSAPSS_2057.test.ts +++ /dev/null @@ -1,151 +0,0 @@ -import { describe } from 'mocha' -import { assert, expect } from 'chai' -import path from "path"; -const wasm_tester = require("circom_tester").wasm; -import { poseidon1, poseidon6 } from "poseidon-lite"; -import { generateCircuitInputsRegister } from '../../common/src/utils/generateInputs'; -import { assembleEContent, bytesToBigDecimal, formatAndConcatenateDataHashes, formatMrz, hash, packBytes, splitToWords, toUnsignedByte } from '../../common/src/utils/utils'; -import { PassportData } from '../../common/src/utils/types'; -import * as forge from 'node-forge'; -import crypto from 'crypto'; - - -describe("Proof of Passport - Circuits - RSASSAPSS - 2057", function () { - this.timeout(0); - let inputs: any; - let circuit: any; - let attestation_id: string; - - const sampleMRZ = "P byte < 128 ? byte : byte - 256); - - return { - mrz: sampleMRZ, - signatureAlgorithm: signatureAlgorithm, - pubKey: { - modulus: modulus, - exponent: exponent, - }, - dataGroupHashes: concatenatedDataHashes, - eContent: eContent, - encryptedDigest: signatureArray, - photoBase64: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIw3..." - } - } - - let passportData = genMockPassportData_sha256WithRSASSAPSS_65537(); - - - before(async () => { - circuit = await wasm_tester( - path.join(__dirname, "../circuits/tests/RSASSAPSS/rsassapss_2057.circom"), - { - include: [ - "node_modules", - "node_modules/@zk-email/circuits/helpers/sha.circom", - // "./node_modules/@zk-kit/binary-merkle-root.circom/src", - "./node_modules/circomlib/circuits" - ] - }, - ); - - inputs = { - signed_attributes: passportData.eContent.map(toUnsignedByte).map(byte => String(byte)), - signature: splitToWords( - BigInt(bytesToBigDecimal(passportData.encryptedDigest)), - BigInt(121), - BigInt(17) - ), - pubkey: splitToWords( - BigInt(passportData.pubKey.modulus as string), - BigInt(121), - BigInt(17) - ), - } - }); - - 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({ - signature: inputs.signature, - pubkey: inputs.pubkey, - eContentBytes: inputs.signed_attributes, - }); - await circuit.checkConstraints(w); - }); - - it("should fail to calculate witness with invalid signature", async function () { - try { - const invalidInputs = { - pubkey: inputs.pubkey, - eContentBytes: inputs.signed_attributes, - signature: inputs.signature.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"); - } - }); - -}); \ No newline at end of file diff --git a/circuits/tests/utils/Mgf1_sha256.test.ts b/circuits/tests/rsassapss/mgf1_sha256.test.ts similarity index 100% rename from circuits/tests/utils/Mgf1_sha256.test.ts rename to circuits/tests/rsassapss/mgf1_sha256.test.ts diff --git a/circuits/tests/rsassapss/rsassapss_2048.test.ts b/circuits/tests/rsassapss/rsassapss_2048.test.ts new file mode 100644 index 00000000..3ffab6ee --- /dev/null +++ b/circuits/tests/rsassapss/rsassapss_2048.test.ts @@ -0,0 +1,66 @@ +import { describe } from 'mocha'; +import { assert, expect } from 'chai'; +import path from 'path'; +const wasm_tester = require('circom_tester').wasm; +import { poseidon1 } from 'poseidon-lite'; +import { generateCircuitInputsRegister } from '../../../common/src/utils/generateInputs'; +import { mockPassportData_sha256_rsapss_65537 } from '../../../common/src/constants/mockPassportData'; + +describe('Proof of Passport - Circuits - RSASSAPSS - 2048', function () { + this.timeout(0); + let inputs: any; + let circuit: any; + let circuit_inputs: any; + let attestation_id: string; + const n_dsc = 64; + const k_dsc = 32; + + + let passportData = mockPassportData_sha256_rsapss_65537; + + before(async () => { + circuit = await wasm_tester( + path.join(__dirname, '../../circuits/tests/rsassapss/rsassapss_2048.circom'), + { + include: [ + 'node_modules', + 'node_modules/@zk-email/circuits/helpers/sha.circom', + './node_modules/circomlib/circuits', + ], + } + ); + + const secret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); + const attestation_name = 'E-PASSPORT'; + attestation_id = poseidon1([BigInt(Buffer.from(attestation_name).readUIntBE(0, 6))]).toString(); + inputs = generateCircuitInputsRegister(secret, secret, attestation_id, passportData, n_dsc, k_dsc); + circuit_inputs = { + signature: inputs.signature, + pubkey: inputs.dsc_modulus, + eContentBytes: inputs.signed_attributes, + }; + }); + + 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(circuit_inputs); + await circuit.checkConstraints(w); + }); + + it('should fail to calculate witness with invalid signature', async function () { + try { + const invalidInputs = { + pubkey: inputs.dsc_modulus, + eContentBytes: inputs.signed_attributes, + signature: inputs.signature.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'); + } + }); +}); diff --git a/circuits/tests/rsassapss/rsassapss_2057.test.ts b/circuits/tests/rsassapss/rsassapss_2057.test.ts new file mode 100644 index 00000000..c476b93c --- /dev/null +++ b/circuits/tests/rsassapss/rsassapss_2057.test.ts @@ -0,0 +1,64 @@ +import { describe } from 'mocha'; +import { assert, expect } from 'chai'; +import path from 'path'; +const wasm_tester = require('circom_tester').wasm; +import { poseidon1, poseidon6 } from 'poseidon-lite'; +import { generateCircuitInputsRegister } from '../../../common/src/utils/generateInputs'; +import { mockPassportData_sha256_rsapss_65537 } from '../../../common/src/constants/mockPassportData'; + +describe('Proof of Passport - Circuits - RSASSAPSS - 2057', function () { + this.timeout(0); + let inputs: any; + let circuit: any; + let attestation_id: string; + const n_dsc = 121; + const k_dsc = 17; + + let passportData = mockPassportData_sha256_rsapss_65537; + + before(async () => { + circuit = await wasm_tester( + path.join(__dirname, '../../circuits/tests/rsassapss/rsassapss_2057.circom'), + { + include: [ + 'node_modules', + 'node_modules/@zk-email/circuits/helpers/sha.circom', + './node_modules/circomlib/circuits', + ], + } + ); + + const secret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(); + const attestation_name = 'E-PASSPORT'; + attestation_id = poseidon1([BigInt(Buffer.from(attestation_name).readUIntBE(0, 6))]).toString(); + inputs = generateCircuitInputsRegister(secret, secret, attestation_id, passportData, n_dsc, k_dsc); + + }); + + 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({ + signature: inputs.signature, + pubkey: inputs.dsc_modulus, + eContentBytes: inputs.signed_attributes, + }); + await circuit.checkConstraints(w); + }); + + it('should fail to calculate witness with invalid signature', async function () { + try { + const invalidInputs = { + pubkey: inputs.dsc_modulus, + eContentBytes: inputs.signed_attributes, + signature: inputs.signature.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'); + } + }); +});