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 ab907e3 commit eaa79de
Show file tree
Hide file tree
Showing 9 changed files with 262 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");
}
}
});
});
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
191 changes: 125 additions & 66 deletions packages/circuits/tests/rsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,142 @@ import { wasm as wasm_tester } from "circom_tester";
import { generateEmailVerifierInputs } from "@zk-email/helpers/src/input-generators";
import { toCircomBigIntBytes } from "@zk-email/helpers/src/binary-format";


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

let circuit: any;
let rawEmail: Buffer;
jest.setTimeout(30 * 60 * 1000); // 30 minutes

beforeAll(async () => {
circuit = await wasm_tester(
path.join(__dirname, "./test-circuits/rsa-test.circom"),
{
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
// output: path.join(__dirname, "./compiled-test-circuits"),
}
);
rawEmail = fs.readFileSync(path.join(__dirname, "./test-emails/test.eml"));
});
let circuit: any;
let rawEmail: Buffer;

it("should verify 2048 bit rsa signature correctly", async function () {
const emailVerifierInputs = await generateEmailVerifierInputs(rawEmail, {
maxHeadersLength: 640,
maxBodyLength: 768,
beforeAll(async () => {
circuit = await wasm_tester(
path.join(__dirname, "./test-circuits/rsa-test.circom"),
{
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
// output: path.join(__dirname, "./compiled-test-circuits"),
}
);
rawEmail = fs.readFileSync(
path.join(__dirname, "./test-emails/test.eml")
);
});

it("should verify 2048 bit rsa signature correctly", async function () {
const emailVerifierInputs = await generateEmailVerifierInputs(
rawEmail,
{
maxHeadersLength: 640,
maxBodyLength: 768,
}
);

const witness = await circuit.calculateWitness({
signature: emailVerifierInputs.signature,
modulus: emailVerifierInputs.pubkey,
// TODO: generate this from the input
message: ["1156466847851242602709362303526378170", "191372789510123109308037416804949834", "7204", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
const witness = await circuit.calculateWitness({
signature: emailVerifierInputs.signature,
modulus: emailVerifierInputs.pubkey,
// TODO: generate this from the input
message: [
"1156466847851242602709362303526378170",
"191372789510123109308037416804949834",
"7204",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
],
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {});
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {})
});

it("should verify 1024 bit rsa signature correctly", async function () {
const signature = toCircomBigIntBytes(
BigInt(
102386562682221859025549328916727857389789009840935140645361501981959969535413501251999442013082353139290537518086128904993091119534674934202202277050635907008004079788691412782712147797487593510040249832242022835902734939817209358184800954336078838331094308355388211284440290335887813714894626653613586546719n
)
);
it("should verify 1024 bit rsa signature correctly", async function () {
const signature = toCircomBigIntBytes(
BigInt(
102386562682221859025549328916727857389789009840935140645361501981959969535413501251999442013082353139290537518086128904993091119534674934202202277050635907008004079788691412782712147797487593510040249832242022835902734939817209358184800954336078838331094308355388211284440290335887813714894626653613586546719n
)
);

const pubkey = toCircomBigIntBytes(
BigInt(
106773687078109007595028366084970322147907086635176067918161636756354740353674098686965493426431314019237945536387044259034050617425729739578628872957481830432099721612688699974185290306098360072264136606623400336518126533605711223527682187548332314997606381158951535480830524587400401856271050333371205030999n
)
);
const pubkey = toCircomBigIntBytes(
BigInt(
106773687078109007595028366084970322147907086635176067918161636756354740353674098686965493426431314019237945536387044259034050617425729739578628872957481830432099721612688699974185290306098360072264136606623400336518126533605711223527682187548332314997606381158951535480830524587400401856271050333371205030999n
)
);

const witness = await circuit.calculateWitness({
signature: signature,
modulus: pubkey,
// TODO: generate this from the input
message: ["1156466847851242602709362303526378170", "191372789510123109308037416804949834", "7204", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
const witness = await circuit.calculateWitness({
signature: signature,
modulus: pubkey,
// TODO: generate this from the input
message: [
"1156466847851242602709362303526378170",
"191372789510123109308037416804949834",
"7204",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
],
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {});
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {});
});

it("should fail when verifying with an incorrect signature", async function () {
const emailVerifierInputs = await generateEmailVerifierInputs(rawEmail, {
maxHeadersLength: 640,
maxBodyLength: 768,
});
it("should fail when verifying with an incorrect signature", async function () {
const emailVerifierInputs = await generateEmailVerifierInputs(
rawEmail,
{
maxHeadersLength: 640,
maxBodyLength: 768,
}
);


expect.assertions(1);
try {
const witness = await circuit.calculateWitness({
signature: emailVerifierInputs.signature,
modulus: emailVerifierInputs.pubkey,
message: ["1156466847851242602709362303526378171", "191372789510123109308037416804949834", "7204", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {})
} catch (error) {
expect((error as Error).message).toMatch("Assert Failed");
}
});
expect.assertions(1);
try {
const witness = await circuit.calculateWitness({
signature: emailVerifierInputs.signature,
modulus: emailVerifierInputs.pubkey,
message: [
"1156466847851242602709362303526378171",
"191372789510123109308037416804949834",
"7204",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
],
});
await circuit.checkConstraints(witness);
await circuit.assertOut(witness, {});
} catch (error) {
expect((error as Error).message).toMatch("Assert Failed");
}
});
});
Loading

0 comments on commit eaa79de

Please sign in to comment.