diff --git a/packages/circuits/tests/email-verifier-no-body.test.ts b/packages/circuits/tests/email-verifier-no-body.test.ts new file mode 100644 index 00000000..21780215 --- /dev/null +++ b/packages/circuits/tests/email-verifier-no-body.test.ts @@ -0,0 +1,48 @@ +import fs from "fs"; +import { wasm as wasm_tester } from "circom_tester"; +import path from "path"; +import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; +import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; +import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; + +describe("EmailVerifier : Without body check", () => { + jest.setTimeout(10 * 60 * 1000); // 10 minutes + + let dkimResult: DKIMVerificationResult; + let circuit: any; + + beforeAll(async () => { + const rawEmail = fs.readFileSync( + path.join(__dirname, "./test-emails/test.eml"), + "utf8" + ); + dkimResult = await verifyDKIMSignature(rawEmail); + + circuit = await wasm_tester( + path.join( + __dirname, + "./test-circuits/email-verifier-no-body-test.circom" + ), + { + recompile: true, + include: path.join(__dirname, "../../../node_modules"), + // output: path.join(__dirname, "./compiled-test-circuits"), + } + ); + }); + + it("should verify email when ignore_body_hash_check is true", async function () { + // The result wont have shaPrecomputeSelector, maxHeadersLength, maxBodyLength, ignoreBodyHashCheck + const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( + dkimResult, + { + maxHeadersLength: 640, + maxBodyLength: 768, + ignoreBodyHashCheck: true, + } + ); + + const witness = await circuit.calculateWitness(emailVerifierInputs); + await circuit.checkConstraints(witness); + }); +}); diff --git a/packages/circuits/tests/email-verifier-with-body-mask.test.ts b/packages/circuits/tests/email-verifier-with-body-mask.test.ts new file mode 100644 index 00000000..11427173 --- /dev/null +++ b/packages/circuits/tests/email-verifier-with-body-mask.test.ts @@ -0,0 +1,59 @@ +import fs from "fs"; +import { wasm as wasm_tester } from "circom_tester"; +import path from "path"; +import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; +import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; +import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; + +describe("EmailVerifier : With body masking", () => { + jest.setTimeout(10 * 60 * 1000); // 10 minutes + + let dkimResult: DKIMVerificationResult; + let circuit: any; + + beforeAll(async () => { + const rawEmail = fs.readFileSync( + path.join(__dirname, "./test-emails/test.eml") + ); + dkimResult = await verifyDKIMSignature(rawEmail); + + circuit = await wasm_tester( + path.join( + __dirname, + "./test-circuits/email-verifier-with-body-mask-test.circom" + ), + { + recompile: true, + include: path.join(__dirname, "../../../node_modules"), + output: path.join(__dirname, "./compiled-test-circuits"), + } + ); + }); + + it("should verify email with body masking", async function () { + const mask = Array.from({ length: 768 }, (_, i) => + i > 25 && i < 50 ? 1 : 0 + ); + + const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( + dkimResult, + { + maxHeadersLength: 640, + maxBodyLength: 768, + ignoreBodyHashCheck: false, + enableBodyMasking: true, + bodyMask: mask.map((value) => (value ? 1 : 0)), + } + ); + + const expectedMaskedBody = emailVerifierInputs.emailBody!.map( + (byte, i) => (mask[i] === 1 ? byte : 0) + ); + + const witness = await circuit.calculateWitness(emailVerifierInputs); + await circuit.checkConstraints(witness); + await circuit.assertOut(witness, { + maskedBody: expectedMaskedBody, + }); + }); +}); diff --git a/packages/circuits/tests/email-verifier-with-header-mask.test.ts b/packages/circuits/tests/email-verifier-with-header-mask.test.ts new file mode 100644 index 00000000..1fac9cd5 --- /dev/null +++ b/packages/circuits/tests/email-verifier-with-header-mask.test.ts @@ -0,0 +1,59 @@ +import fs from "fs"; +import { wasm as wasm_tester } from "circom_tester"; +import path from "path"; +import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; +import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; +import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; + +describe("EmailVerifier : With header masking", () => { + jest.setTimeout(10 * 60 * 1000); // 10 minutes + + let dkimResult: DKIMVerificationResult; + let circuit: any; + + beforeAll(async () => { + const rawEmail = fs.readFileSync( + path.join(__dirname, "./test-emails/test.eml") + ); + dkimResult = await verifyDKIMSignature(rawEmail); + + circuit = await wasm_tester( + path.join( + __dirname, + "./test-circuits/email-verifier-with-header-mask-test.circom" + ), + { + recompile: true, + include: path.join(__dirname, "../../../node_modules"), + output: path.join(__dirname, "./compiled-test-circuits"), + } + ); + }); + + it("should verify email with header masking", async function () { + const mask = Array.from({ length: 640 }, (_, i) => + i > 25 && i < 50 ? 1 : 0 + ); + + const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( + dkimResult, + { + maxHeadersLength: 640, + maxBodyLength: 768, + ignoreBodyHashCheck: false, + enableHeaderMasking: true, + headerMask: mask.map((value) => (value ? 1 : 0)), + } + ); + + const expectedMaskedHeader = emailVerifierInputs.emailHeader!.map( + (byte, i) => (mask[i] === 1 ? byte : 0) + ); + + const witness = await circuit.calculateWitness(emailVerifierInputs); + await circuit.checkConstraints(witness); + await circuit.assertOut(witness, { + maskedHeader: expectedMaskedHeader, + }); + }); +}); diff --git a/packages/circuits/tests/email-verifier-with-soft-line-breaks.test.ts b/packages/circuits/tests/email-verifier-with-soft-line-breaks.test.ts new file mode 100644 index 00000000..9240521e --- /dev/null +++ b/packages/circuits/tests/email-verifier-with-soft-line-breaks.test.ts @@ -0,0 +1,48 @@ +import fs from "fs"; +import { wasm as wasm_tester } from "circom_tester"; +import path from "path"; +import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; +import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src/input-generators"; +import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim"; + +describe("EmailVerifier : With soft line breaks", () => { + jest.setTimeout(10 * 60 * 1000); // 10 minutes + + let dkimResult: DKIMVerificationResult; + let circuit: any; + + beforeAll(async () => { + const rawEmail = fs.readFileSync( + path.join(__dirname, "./test-emails/lorem_ipsum.eml"), + "utf8" + ); + dkimResult = await verifyDKIMSignature(rawEmail); + + circuit = await wasm_tester( + path.join( + __dirname, + "./test-circuits/email-verifier-with-soft-line-breaks-test.circom" + ), + { + recompile: true, + include: path.join(__dirname, "../../../node_modules"), + output: path.join(__dirname, "./compiled-test-circuits"), + } + ); + }); + + it("should verify email when removeSoftLineBreaks is true", async function () { + const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( + dkimResult, + { + maxHeadersLength: 640, + maxBodyLength: 1408, + ignoreBodyHashCheck: false, + removeSoftLineBreaks: true, + } + ); + + const witness = await circuit.calculateWitness(emailVerifierInputs); + await circuit.checkConstraints(witness); + }); +}); diff --git a/packages/circuits/tests/email-verifier.test.ts b/packages/circuits/tests/email-verifier.test.ts index c520b7d1..93eee579 100644 --- a/packages/circuits/tests/email-verifier.test.ts +++ b/packages/circuits/tests/email-verifier.test.ts @@ -1,5 +1,4 @@ import fs from "fs"; -import { buildPoseidon } from "circomlibjs"; import { wasm as wasm_tester } from "circom_tester"; import path from "path"; import { DKIMVerificationResult } from "@zk-email/helpers/src/dkim"; @@ -207,193 +206,3 @@ describe("EmailVerifier", () => { }); }); }); - -describe("EmailVerifier : Without body check", () => { - jest.setTimeout(10 * 60 * 1000); // 10 minutes - - let dkimResult: DKIMVerificationResult; - let circuit: any; - - beforeAll(async () => { - const rawEmail = fs.readFileSync( - path.join(__dirname, "./test-emails/test.eml"), - "utf8" - ); - dkimResult = await verifyDKIMSignature(rawEmail); - - circuit = await wasm_tester( - path.join( - __dirname, - "./test-circuits/email-verifier-no-body-test.circom" - ), - { - recompile: true, - include: path.join(__dirname, "../../../node_modules"), - // output: path.join(__dirname, "./compiled-test-circuits"), - } - ); - }); - - it("should verify email when ignore_body_hash_check is true", async function () { - // The result wont have shaPrecomputeSelector, maxHeadersLength, maxBodyLength, ignoreBodyHashCheck - const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( - dkimResult, - { - maxHeadersLength: 640, - maxBodyLength: 768, - ignoreBodyHashCheck: true, - } - ); - - const witness = await circuit.calculateWitness(emailVerifierInputs); - await circuit.checkConstraints(witness); - }); -}); - -describe("EmailVerifier : With body masking", () => { - jest.setTimeout(10 * 60 * 1000); // 10 minutes - - let dkimResult: DKIMVerificationResult; - let circuit: any; - - beforeAll(async () => { - const rawEmail = fs.readFileSync( - path.join(__dirname, "./test-emails/test.eml") - ); - dkimResult = await verifyDKIMSignature(rawEmail); - - circuit = await wasm_tester( - path.join( - __dirname, - "./test-circuits/email-verifier-with-body-mask-test.circom" - ), - { - recompile: true, - include: path.join(__dirname, "../../../node_modules"), - output: path.join(__dirname, "./compiled-test-circuits"), - } - ); - }); - - it("should verify email with body masking", async function () { - const mask = Array.from({ length: 768 }, (_, i) => - i > 25 && i < 50 ? 1 : 0 - ); - - const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( - dkimResult, - { - maxHeadersLength: 640, - maxBodyLength: 768, - ignoreBodyHashCheck: false, - enableBodyMasking: true, - bodyMask: mask.map((value) => (value ? 1 : 0)), - } - ); - - const expectedMaskedBody = emailVerifierInputs.emailBody!.map( - (byte, i) => (mask[i] === 1 ? byte : 0) - ); - - const witness = await circuit.calculateWitness(emailVerifierInputs); - await circuit.checkConstraints(witness); - await circuit.assertOut(witness, { - maskedBody: expectedMaskedBody, - }); - }); -}); - -describe("EmailVerifier : With soft line breaks", () => { - jest.setTimeout(10 * 60 * 1000); // 10 minutes - - let dkimResult: DKIMVerificationResult; - let circuit: any; - - beforeAll(async () => { - const rawEmail = fs.readFileSync( - path.join(__dirname, "./test-emails/lorem_ipsum.eml"), - "utf8" - ); - dkimResult = await verifyDKIMSignature(rawEmail); - - circuit = await wasm_tester( - path.join( - __dirname, - "./test-circuits/email-verifier-with-soft-line-breaks-test.circom" - ), - { - recompile: true, - include: path.join(__dirname, "../../../node_modules"), - output: path.join(__dirname, "./compiled-test-circuits"), - } - ); - }); - - it("should verify email when removeSoftLineBreaks is true", async function () { - const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( - dkimResult, - { - maxHeadersLength: 640, - maxBodyLength: 1408, - ignoreBodyHashCheck: false, - removeSoftLineBreaks: true, - } - ); - - const witness = await circuit.calculateWitness(emailVerifierInputs); - await circuit.checkConstraints(witness); - }); -}); - -describe("EmailVerifier : With header masking", () => { - jest.setTimeout(10 * 60 * 1000); // 10 minutes - - let dkimResult: DKIMVerificationResult; - let circuit: any; - - beforeAll(async () => { - const rawEmail = fs.readFileSync( - path.join(__dirname, "./test-emails/test.eml") - ); - dkimResult = await verifyDKIMSignature(rawEmail); - - circuit = await wasm_tester( - path.join( - __dirname, - "./test-circuits/email-verifier-with-header-mask-test.circom" - ), - { - recompile: true, - include: path.join(__dirname, "../../../node_modules"), - output: path.join(__dirname, "./compiled-test-circuits"), - } - ); - }); - - it("should verify email with header masking", async function () { - const mask = Array.from({ length: 640 }, (_, i) => - i > 25 && i < 50 ? 1 : 0 - ); - - const emailVerifierInputs = generateEmailVerifierInputsFromDKIMResult( - dkimResult, - { - maxHeadersLength: 640, - maxBodyLength: 768, - ignoreBodyHashCheck: false, - enableHeaderMasking: true, - headerMask: mask.map((value) => (value ? 1 : 0)), - } - ); - - const expectedMaskedHeader = emailVerifierInputs.emailHeader!.map( - (byte, i) => (mask[i] === 1 ? byte : 0) - ); - - const witness = await circuit.calculateWitness(emailVerifierInputs); - await circuit.checkConstraints(witness); - await circuit.assertOut(witness, { - maskedHeader: expectedMaskedHeader, - }); - }); -});