Skip to content

Commit

Permalink
Email auth test passed.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Dec 22, 2024
1 parent 3ccdbfd commit a4a45c8
Show file tree
Hide file tree
Showing 14 changed files with 5,537 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@zk-email/circuits": "=6.3.2",
"@zk-email/relayer-utils": "=0.4.59",
"@zk-email/relayer-utils": "https://gitpkg.vercel.app/zkemail/relayer-utils/pkg?feat/fix-selector-bugs",
"@zk-email/zk-regex-circom": "=2.3.2",
"commander": "^12.1.0",
"snarkjs": "=0.7.5"
Expand Down
8 changes: 4 additions & 4 deletions packages/circuits/tests/emai_auth_legacy_recipient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("Email Auth Legacy with Recipient", () => {
expect(BigInt(domainFields[idx])).toEqual(witness[1 + idx]);
}
const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -139,7 +139,7 @@ describe("Email Auth Legacy with Recipient", () => {
expect(BigInt(domainFields[idx])).toEqual(witness[1 + idx]);
}
const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -222,7 +222,7 @@ describe("Email Auth Legacy with Recipient", () => {
expect(BigInt(domainFields[idx])).toEqual(witness[1 + idx]);
}
const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -303,7 +303,7 @@ describe("Email Auth Legacy with Recipient", () => {
expect(BigInt(domainFields[idx])).toEqual(witness[1 + idx]);
}
const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down
20 changes: 10 additions & 10 deletions packages/circuits/tests/emai_auth_recipient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const option = {
output: path.join(__dirname, "../build"),
recompile: true,
};
const shaPrecomputeSelector = '\\\[zkemail-begin\\\]';


jest.setTimeout(1440000);
describe("Email Auth with Recipient", () => {
Expand All @@ -36,18 +38,17 @@ describe("Email Auth with Recipient", () => {

const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await relayerUtils.parseEmail(emailRaw);
console.log(parsedEmail);
const accountCode = await relayerUtils.generateAccountCode();

const {
subject_idx,
...emailAuthInput
} =
await genEmailCircuitInput(emailFilePath, accountCode, {
maxHeaderLength: 640,
maxHeaderLength: 768,
maxBodyLength: 768,
ignoreBodyHashCheck: false,
shaPrecomputeSelector: '(<(=\r\n)?d(=\r\n)?i(=\r\n)?v(=\r\n)? (=\r\n)?i(=\r\n)?d(=\r\n)?=3D(=\r\n)?"(=\r\n)?[^"]*(=\r\n)?z(=\r\n)?k(=\r\n)?e(=\r\n)?m(=\r\n)?a(=\r\n)?i(=\r\n)?l(=\r\n)?[^"]*(=\r\n)?"(=\r\n)?[^>]*(=\r\n)?>(=\r\n)?)(=\r\n)?([^<>/]+)(<(=\r\n)?/(=\r\n)?d(=\r\n)?i(=\r\n)?v(=\r\n)?>(=\r\n)?)',
shaPrecomputeSelector
});
const command = "Send 0.1 ETH to [email protected]";
const recipientInput = await genRecipientInput(command, parsedEmail.signature);
Expand All @@ -67,7 +68,7 @@ describe("Email Auth with Recipient", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand All @@ -80,7 +81,7 @@ describe("Email Auth with Recipient", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = BigInt(1729865810);
const timestamp = BigInt(1734798639);
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Send 0.1 ETH to ";
Expand Down Expand Up @@ -130,18 +131,17 @@ describe("Email Auth with Recipient", () => {

const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await relayerUtils.parseEmail(emailRaw);
console.log(parsedEmail);
const accountCode = await relayerUtils.generateAccountCode();

const {
subject_idx,
...emailAuthInput
} =
await genEmailCircuitInput(emailFilePath, accountCode, {
maxHeaderLength: 640,
maxHeaderLength: 768,
maxBodyLength: 768,
ignoreBodyHashCheck: false,
shaPrecomputeSelector: '(<(=\r\n)?d(=\r\n)?i(=\r\n)?v(=\r\n)? (=\r\n)?i(=\r\n)?d(=\r\n)?=3D(=\r\n)?"(=\r\n)?[^"]*(=\r\n)?z(=\r\n)?k(=\r\n)?e(=\r\n)?m(=\r\n)?a(=\r\n)?i(=\r\n)?l(=\r\n)?[^"]*(=\r\n)?"(=\r\n)?[^>]*(=\r\n)?>(=\r\n)?)(=\r\n)?([^<>/]+)(<(=\r\n)?/(=\r\n)?d(=\r\n)?i(=\r\n)?v(=\r\n)?>(=\r\n)?)',
shaPrecomputeSelector
});
const command = "Swap 1 ETH to DAI";
const recipientInput = await genRecipientInput(command, parsedEmail.signature);
Expand All @@ -160,7 +160,7 @@ describe("Email Auth with Recipient", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand All @@ -173,7 +173,7 @@ describe("Email Auth with Recipient", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = BigInt(1729865832);
const timestamp = BigInt(1734798636);
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Swap 1 ETH to DAI";
Expand Down
158 changes: 145 additions & 13 deletions packages/circuits/tests/email_auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const option = {
recompile: true,
};

const shaPrecomputeSelector = '\\\[zkemail-begin\\\][^<>/\\\[\\\]]+\\\[zkemail-end\\\]';
console.log("shaPrecomputeSelector: ", shaPrecomputeSelector);
const shaPrecomputeSelector = '\\\[zkemail-begin\\\]';
jest.setTimeout(1440000);
describe("Email Auth", () => {
let circuit;
Expand All @@ -36,6 +35,7 @@ describe("Email Auth", () => {

const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await relayerUtils.parseEmail(emailRaw);
console.log(parsedEmail);
const accountCode = await relayerUtils.generateAccountCode();

const circuitInputs =
Expand All @@ -55,7 +55,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand All @@ -68,7 +68,7 @@ describe("Email Auth", () => {
witness[1 + domainFields.length + 1]
);

const timestamp = BigInt(1734798640);
const timestamp = BigInt(1734798639);
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);

const maskedCommand = "Send 0.1 ETH to ";
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -186,7 +186,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -319,7 +319,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -386,7 +386,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -441,7 +441,6 @@ describe("Email Auth", () => {
ignoreBodyHashCheck: false,
shaPrecomputeSelector,
});
console.log(circuitInputs.padded_header.length);
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);

Expand All @@ -453,7 +452,7 @@ describe("Email Auth", () => {
}

const expectedPubKeyHash = await relayerUtils.publicKeyHash(
parsedEmail.public_key
parsedEmail.publicKey
);
expect(BigInt(expectedPubKeyHash)).toEqual(
witness[1 + domainFields.length]
Expand Down Expand Up @@ -492,6 +491,139 @@ describe("Email Auth", () => {
);
});

it("Verify a sent email whose subject has Ynt:", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_test8.eml"
);
const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await relayerUtils.parseEmail(emailRaw);
const accountCode =
"0x01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";

const circuitInputs =
await genEmailCircuitInput(emailFilePath, accountCode, {
maxHeaderLength: 768, maxBodyLength: 768,
ignoreBodyHashCheck: false,
shaPrecomputeSelector,
});
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);

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

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

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

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

const maskedCommand =
"Accept guardian request for 0x04884491560f38342C56E26BDD0fEAbb68E2d2FC";
const paddedMaskedCommand = relayerUtils.padString(maskedCommand, 605);
const maskedCommandFields =
await relayerUtils.bytesToFields(paddedMaskedCommand);
for (let idx = 0; idx < maskedCommandFields.length; ++idx) {
expect(BigInt(maskedCommandFields[idx])).toEqual(
witness[1 + domainFields.length + 3 + idx]
);
}
const fromAddr = "[email protected]";
const accountSalt = await relayerUtils.generateAccountSalt(fromAddr, accountCode);
expect(BigInt(accountSalt)).toEqual(
witness[1 + domainFields.length + 3 + maskedCommandFields.length]
);

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


it("Verify a sent email whose body has html tags", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_test9.eml"
);
const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await relayerUtils.parseEmail(emailRaw);
const accountCode =
"0x01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";

const circuitInputs =
await genEmailCircuitInput(emailFilePath, accountCode, {
maxHeaderLength: 768, maxBodyLength: 768,
ignoreBodyHashCheck: false,
shaPrecomputeSelector,
});
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);

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

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

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

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

const maskedCommand =
"Accept guardian request for 0x04884491560f38342C56E26BDD0fEAbb68E2d2FC";
const paddedMaskedCommand = relayerUtils.padString(maskedCommand, 605);
const maskedCommandFields =
await relayerUtils.bytesToFields(paddedMaskedCommand);
for (let idx = 0; idx < maskedCommandFields.length; ++idx) {
expect(BigInt(maskedCommandFields[idx])).toEqual(
witness[1 + domainFields.length + 3 + idx]
);
}
const fromAddr = "[email protected]";
const accountSalt = await relayerUtils.generateAccountSalt(fromAddr, accountCode);
expect(BigInt(accountSalt)).toEqual(
witness[1 + domainFields.length + 3 + maskedCommandFields.length]
);

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

it("Verify a sent email with a too large from_addr_idx", async () => {
const emailFilePath = path.join(
__dirname,
Expand Down Expand Up @@ -590,7 +722,7 @@ describe("Email Auth", () => {
it("Verify a sent email without the forced subject", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_invalid_test1.eml"
"./emails/email_auth_test10_invalid.eml"
);

const accountCode =
Expand All @@ -612,7 +744,7 @@ describe("Email Auth", () => {
it("Verify a sent email with a non-utf8 character", async () => {
const emailFilePath = path.join(
__dirname,
"./emails/email_auth_invalid_test2.eml"
"./emails/email_auth_test11_invalid.eml"
);

const accountCode =
Expand Down
Loading

0 comments on commit a4a45c8

Please sign in to comment.