Skip to content

Commit

Permalink
feat: sha precompute test
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 3, 2024
1 parent 80bdfc7 commit 6b84605
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 167 deletions.
2 changes: 1 addition & 1 deletion packages/circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@zk-email/circuits": "^6.1.5",
"@zk-email/zk-regex-circom": "^2.1.0",
"@zk-email/relayer-utils": "^0.3.0",
"@zk-email/relayer-utils": "^0.3.1",
"commander": "^11.0.0",
"snarkjs": "^0.7.0"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/circuits/tests/email_auth_with_body_parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("Email Auth With Body Parsing", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = 1725116446n;
const timestamp = 1725333972n;
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Send 0.1 ETH to ";
Expand All @@ -90,7 +90,7 @@ describe("Email Auth With Body Parsing", () => {
);
});

it("Verify a sent email whose subject does not have an email address", async () => {
it("Verify a sent email whose body does not have an email address", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_with_body_parsing_test2.eml"
Expand Down Expand Up @@ -131,7 +131,7 @@ describe("Email Auth With Body Parsing", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = 1725116459n;
const timestamp = 1725333989n;
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Swap 1 ETH to DAI";
Expand Down Expand Up @@ -200,7 +200,7 @@ describe("Email Auth With Body Parsing", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = 1725116474n;
const timestamp = 1725334002n;
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Send 1 ETH to ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("Email Auth With Body Parsing (QP Encoded)", () => {
// expect(BigInt(expectedEmailNullifier)).toEqual(
// witness[1 + domainFields.length + 1]
// );
// const timestamp = 1696967028n;
// const timestamp = 1725334030n;
// expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);
// const maskedSubject = "Send 1 ETH to ";
// const paddedMaskedSubject = relayerUtils.padString(maskedSubject, 605);
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("Email Auth With Body Parsing (QP Encoded)", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = 1725116497n;
const timestamp = 1725334030n;
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Send 0.12 ETH to ";
Expand All @@ -147,7 +147,7 @@ describe("Email Auth With Body Parsing (QP Encoded)", () => {
);
});

it("Verify a sent email whose subject has an invitation code", async () => {
it("Verify a sent email whose body has an invitation code", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_with_body_parsing_test5.eml"
Expand Down Expand Up @@ -188,7 +188,75 @@ describe("Email Auth With Body Parsing (QP Encoded)", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = 1725116520n;
const timestamp = 1725334056n;
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand =
"Accept guardian request for 0x04884491560f38342C56E26BDD0fEAbb68E2d2FC";
const paddedMaskedCommand = relayerUtils.padString(maskedCommand, 605);
const maskedCommandFields =
relayerUtils.bytes2Fields(paddedMaskedCommand);
for (let idx = 0; idx < maskedCommandFields.length; ++idx) {
expect(BigInt(maskedCommandFields[idx])).toEqual(
witness[1 + domainFields.length + 3 + idx]
);
}
const fromAddr = "zkemail.relayer.test@gmail.com";
const accountSalt = relayerUtils.accountSalt(fromAddr, accountCode);
expect(BigInt(accountSalt)).toEqual(
witness[1 + domainFields.length + 3 + maskedCommandFields.length]
);

expect(1n).toEqual(
witness[
1 + domainFields.length + 3 + maskedCommandFields.length + 1
]
);
});

it("Verify a sent email whose body has an invitation code with sha precompute string", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_with_body_parsing_test5.eml"
);
const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await relayerUtils.parseEmail(emailRaw);

const accountCode =
"0x01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";

const { subject_idx, ...circuitInputsRelevant } =
await genEmailCircuitInput(emailFilePath, accountCode, {
maxHeaderLength: 640,
maxBodyLength: 768,
ignoreBodyHashCheck: false,
shaPrecomputeSelector: '<div id=3D"zkemail">',
});
const witness = await circuit.calculateWitness(circuitInputsRelevant);
await circuit.checkConstraints(witness);

const domainName = "gmail.com";
const paddedDomain = relayerUtils.padString(domainName, 255);
const domainFields = relayerUtils.bytes2Fields(paddedDomain);
for (let idx = 0; idx < domainFields.length; ++idx) {
expect(BigInt(domainFields[idx])).toEqual(witness[1 + idx]);
}

const expectedPubKeyHash = relayerUtils.publicKeyHash(
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
);

const expectedEmailNullifier = relayerUtils.emailNullifier(
parsedEmail.signature
);
expect(BigInt(expectedEmailNullifier)).toEqual(
witness[1 + domainFields.length + 1]
);

const timestamp = 1725334056n;
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand =
Expand Down
Loading

0 comments on commit 6b84605

Please sign in to comment.