Skip to content

Commit

Permalink
chore: minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 13, 2024
1 parent 74958f1 commit eb66674
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 174 deletions.
105 changes: 52 additions & 53 deletions packages/circuits/tests/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
import { wasm } from "circom_tester";
import path from "path";


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

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"),
}
);
});

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 })
}
});

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");
}
}
});
jest.setTimeout(30 * 60 * 1000); // 30 minutes

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"),
}
);
});

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 });
}
});

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");
}
}
});
});
106 changes: 106 additions & 0 deletions packages/circuits/tests/count-substring-occurrences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { wasm as wasm_tester } from "circom_tester";
import path from "path";

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

let circuit: any;

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

it("should count single occurrence at the beginning", async () => {
const input = {
in: [1, 2, 3, 4, 5, ...Array(1019).fill(0)],
substring: [1, 2, 3, ...Array(125).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(1n);
});

it("should count multiple occurrences", async () => {
const input = {
in: [1, 2, 3, 4, 1, 2, 3, 5, 1, 2, 3, ...Array(1013).fill(0)],
substring: [1, 2, 3, ...Array(125).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(3n);
});

it("should return 0 for no occurrences", async () => {
const input = {
in: [1, 2, 4, 5, 6, ...Array(1019).fill(0)],
substring: [1, 2, 3, ...Array(125).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(0n);
});

it("should count occurrences with overlap", async () => {
const input = {
in: [1, 1, 1, 2, 1, 1, ...Array(1018).fill(0)],
substring: [1, 1, ...Array(126).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(3n);
});

it("should handle full match of input", async () => {
const repeatedPattern = [1, 2, 3, 4];
const input = {
in: Array(256)
.fill(repeatedPattern)
.flat()
.concat(Array(1024 - 256 * 4).fill(0)),
substring: [1, 2, 3, 4, ...Array(124).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(256n);
});

it("should handle single character substring", async () => {
const input = {
in: [1, 2, 1, 3, 1, 4, 1, ...Array(1017).fill(0)],
substring: [1, ...Array(127).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(4n);
});

it("should handle substring at the end of input", async () => {
const input = {
in: [...Array(1021).fill(0), 1, 2, 3],
substring: [1, 2, 3, ...Array(125).fill(0)],
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness[1]).toBe(1n);
});

it("should return 0 for empty substring", async () => {
const input = {
in: [1, 2, 3, 4, 5, ...Array(1019).fill(0)],
substring: Array(128).fill(0),
};
await expect(async () => {
await circuit.calculateWitness(input);
}).rejects.toThrow("Assert Failed");
});
});
2 changes: 1 addition & 1 deletion packages/circuits/tests/email-verifier-no-body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";

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

let dkimResult: DKIMVerificationResult;
let circuit: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";

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

let dkimResult: DKIMVerificationResult;
let circuit: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";

describe("EmailVerifier : With header masking", () => {
jest.setTimeout(10 * 60 * 1000); // 10 minutes
jest.setTimeout(30 * 60 * 1000); // 30 minutes

let dkimResult: DKIMVerificationResult;
let circuit: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generateEmailVerifierInputsFromDKIMResult } from "@zk-email/helpers/src
import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";

describe("EmailVerifier : With soft line breaks", () => {
jest.setTimeout(10 * 60 * 1000); // 10 minutes
jest.setTimeout(30 * 60 * 1000); // 30 minutes

let dkimResult: DKIMVerificationResult;
let circuit: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/circuits/tests/email-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { verifyDKIMSignature } from "@zk-email/helpers/src/dkim";
import { poseidonLarge } from "@zk-email/helpers/src/hash";

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

let dkimResult: DKIMVerificationResult;
let circuit: any;
Expand Down
Loading

0 comments on commit eb66674

Please sign in to comment.