Skip to content

Commit

Permalink
add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Oct 1, 2024
1 parent 9d956d3 commit fbdf5bc
Show file tree
Hide file tree
Showing 29 changed files with 864 additions and 1,174 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages/helpers/src/lib
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"tabWidth": 2,
"printWidth": 120
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"license": "MIT",
"private": true,
"scripts": {
"lint": "yarn prettier --write packages/**/**.ts",
"test": "jest"
},
"workspaces": [
Expand All @@ -12,5 +13,8 @@
"packageManager": "[email protected]",
"engines": {
"node": ">=14.0.0"
},
"devDependencies": {
"prettier": "^3.3.3"
}
}
87 changes: 42 additions & 45 deletions packages/circuits/tests/base64.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,53 @@ import { wasm } from "circom_tester";
import path from "path";

describe("Base64 Lookup", () => {
jest.setTimeout(30 * 60 * 1000); // 30 minutes
jest.setTimeout(30 * 60 * 1000); // 30 minutes

let circuit: any;
let circuit: any;

beforeAll(async () => {
circuit = await wasm(
path.join(__dirname, "./test-circuits/base64-test.circom"),
{
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
// output: path.join(__dirname, "./compiled-test-circuits"),
}
);
beforeAll(async () => {
circuit = await wasm(path.join(__dirname, "./test-circuits/base64-test.circom"), {
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
// output: path.join(__dirname, "./compiled-test-circuits"),
});
});

it("should decode valid base64 chars", async function () {
const inputs = [
[65, 0], // A
[90, 25], // Z
[97, 26], // a
[122, 51], // z
[48, 52], // 0
[57, 61], // 9
[43, 62], // +
[47, 63], // /
[61, 0], // =
];
it("should decode valid base64 chars", async function () {
const inputs = [
[65, 0], // A
[90, 25], // Z
[97, 26], // a
[122, 51], // z
[48, 52], // 0
[57, 61], // 9
[43, 62], // +
[47, 63], // /
[61, 0], // =
];

for (const [input, output] of inputs) {
const witness = await circuit.calculateWitness({
in: input,
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, { out: output });
}
});
for (const [input, output] of inputs) {
const witness = await circuit.calculateWitness({
in: input,
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, { out: output });
}
});

it("should fail with invalid chars", async function () {
const inputs = [34, 64, 91, 44];
it("should fail with invalid chars", async function () {
const inputs = [34, 64, 91, 44];

expect.assertions(inputs.length);
for (const input of inputs) {
try {
const witness = await circuit.calculateWitness({
in: input,
});
await circuit.checkConstraints(witness);
} catch (error) {
expect((error as Error).message).toMatch("Assert Failed");
}
}
});
expect.assertions(inputs.length);
for (const input of inputs) {
try {
const witness = await circuit.calculateWitness({
in: input,
});
await circuit.checkConstraints(witness);
} catch (error) {
expect((error as Error).message).toMatch("Assert Failed");
}
}
});
});
69 changes: 33 additions & 36 deletions packages/circuits/tests/byte-mask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,45 @@ import { wasm as wasm_tester } from "circom_tester";
import path from "path";

describe("ByteMask Circuit", () => {
let circuit: any;
let circuit: any;

beforeAll(async () => {
circuit = await wasm_tester(
path.join(__dirname, "./test-circuits/byte-mask-test.circom"),
{
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
output: path.join(__dirname, "./compiled-test-circuits"),
}
);
beforeAll(async () => {
circuit = await wasm_tester(path.join(__dirname, "./test-circuits/body-masker-test.circom"), {
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
output: path.join(__dirname, "./compiled-test-circuits"),
});
});

it("should mask the body correctly", async () => {
const input = {
in: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
mask: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
};
it("should mask the body correctly", async () => {
const input = {
in: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
mask: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
};

const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {
out: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0],
});
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {
out: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0],
});
});

it("should fail if mask has non-bit numbers", async () => {
const input = {
body: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
mask: [1, 2, 1, 0, 1, 0, 1, 0, 1, 0], // Mask with non-bit number (2)
};
it("should fail if mask has non-bit numbers", async () => {
const input = {
body: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
mask: [1, 2, 1, 0, 1, 0, 1, 0, 1, 0], // Mask with non-bit number (2)
};

try {
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {
maskedBody: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0],
});
} catch (error) {
expect(error).toBeTruthy();
}
try {
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {
maskedBody: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0],
});
} catch (error) {
expect(error).toBeTruthy();
}

expect.assertions(1);
});
expect.assertions(1);
});
});
54 changes: 21 additions & 33 deletions packages/circuits/tests/email-verifier-no-body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,31 @@ import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";

describe("EmailVerifier : Without body check", () => {
jest.setTimeout(30 * 60 * 1000); // 30 minutes
jest.setTimeout(30 * 60 * 1000); // 30 minutes

let dkimResult: DKIMVerificationResult;
let circuit: any;
let dkimResult: DKIMVerificationResult;
let circuit: any;

beforeAll(async () => {
const rawEmail = fs.readFileSync(
path.join(__dirname, "./test-emails/test.eml"),
"utf8"
);
dkimResult = await verifyDKIMSignature(rawEmail);
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"),
}
);
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);
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);
});
});
79 changes: 32 additions & 47 deletions packages/circuits/tests/email-verifier-with-body-mask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,39 @@ import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";

describe("EmailVerifier : With body masking", () => {
jest.setTimeout(30 * 60 * 1000); // 30 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"),
}
);
jest.setTimeout(30 * 60 * 1000); // 30 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));

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,
});
const witness = await circuit.calculateWitness(emailVerifierInputs);
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {
maskedBody: expectedMaskedBody,
});
});
});
Loading

0 comments on commit fbdf5bc

Please sign in to comment.