Skip to content

Commit

Permalink
Fix all circuit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Jul 27, 2024
1 parent f736c19 commit 608b0d0
Show file tree
Hide file tree
Showing 7 changed files with 1,505 additions and 1,526 deletions.
45 changes: 31 additions & 14 deletions packages/circuits/tests/account_creation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ const emailWalletUtils = require("@zk-email/relayer-utils");
const option = {
include: path.join(__dirname, "../../../node_modules"),
};
import { genAccountCreationInput } from "../helpers/account_creation";
import { genEmailSenderInput } from "../helpers/email_sender";
import { readFileSync } from "fs";
import { hash_to_curve, point_scalar_mul } from "circom-grumpkin";

jest.setTimeout(2080000);
describe("Account Initialization", () => {
let circuit: any;
beforeAll(async () => {
circuit = await wasm_tester(path.join(__dirname, "../src/email_sender.circom"), option);

});

it("init an account", async () => {
const emailFilePath = path.join(__dirname, "./emails/account_creation_test1.eml");
const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await emailWalletUtils.parseEmail(emailRaw);
const relayerRand = emailWalletUtils.genRelayerRand();
const circuitInputs = await genAccountCreationInput(emailFilePath, relayerRand);
const circuit = await wasm_tester(path.join(__dirname, "../src/account_creation.circom"), option);
const accountCode = "0x01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";
const circuitInputs = await genEmailSenderInput(emailFilePath, accountCode);
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
const domainName = "gmail.com";
Expand All @@ -27,18 +33,29 @@ describe("Account Initialization", () => {
expect(BigInt(domainFields[idx])).toEqual(witness[1 + idx]);
}
const expectedPubKeyHash = emailWalletUtils.publicKeyHash(parsedEmail.publicKey);
expect(BigInt(expectedPubKeyHash)).toEqual(witness[10]);
expect(BigInt(expectedPubKeyHash)).toEqual(witness[1 + domainFields.length]);
const expectedEmailNullifier = emailWalletUtils.emailNullifier(parsedEmail.signature);
expect(BigInt(expectedEmailNullifier)).toEqual(witness[11]);
expect(BigInt(expectedEmailNullifier)).toEqual(witness[1 + domainFields.length + 1]);
const timestamp = 1707866192n;
expect(timestamp).toEqual(witness[12]);
const emailAddr = "[email protected]";
const accountCode = "0x01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";
const expectedAccountSalt = emailWalletUtils.accountSalt(emailAddr, accountCode);
expect(BigInt(expectedAccountSalt)).toEqual(witness[13]);
const hashedPoint = hash_to_curve(emailWalletUtils.padEmailAddr(emailAddr));
const expectedPsiPoint = point_scalar_mul(hashedPoint.x, hashedPoint.y, BigInt(relayerRand));
expect(expectedPsiPoint.x).toEqual(witness[14]);
expect(expectedPsiPoint.y).toEqual(witness[15]);
expect(timestamp).toEqual(witness[1 + domainFields.length + 2]);
const maskedSubject = "Send 0.12 ETH to ";
const paddedMaskedSubject = emailWalletUtils.padString(maskedSubject, 605);
const maskedSubjectFields = emailWalletUtils.bytes2Fields(paddedMaskedSubject);
for (let idx = 0; idx < maskedSubjectFields.length; ++idx) {
expect(BigInt(maskedSubjectFields[idx])).toEqual(witness[1 + domainFields.length + 3 + idx]);
}
const senderEmailAddr = "[email protected]";
const accountSalt = emailWalletUtils.accountSalt(senderEmailAddr, accountCode);
expect(BigInt(accountSalt)).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length]);
expect(1n).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length + 1]);
expect(1n).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length + 2]);
const recipientEmailAddr = "[email protected]";
const expectedRecipientEmailAddrCommit = emailWalletUtils.emailAddrCommitWithSignature(
recipientEmailAddr,
parsedEmail.signature,
);
expect(BigInt(expectedRecipientEmailAddrCommit)).toEqual(
witness[1 + domainFields.length + 3 + maskedSubjectFields.length + 3],
);
});
});

This file was deleted.

6 changes: 2 additions & 4 deletions packages/circuits/tests/email_sender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ describe("Email Sender", () => {
const emailFilePath = path.join(__dirname, "./emails/email_sender_test1.eml");
const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await emailWalletUtils.parseEmail(emailRaw);
console.log(parsedEmail.canonicalizedHeader);
const accountCode = await emailWalletUtils.genAccountCode();
const circuitInputs = await genEmailSenderInput(emailFilePath, accountCode);
console.log(circuitInputs);
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
const domainName = "gmail.com";
Expand Down Expand Up @@ -182,7 +180,7 @@ describe("Email Sender", () => {
const emailRaw = readFileSync(emailFilePath, "utf8");
const parsedEmail = await emailWalletUtils.parseEmail(emailRaw);
console.log(parsedEmail.canonicalizedHeader);
const accountCode = await emailWalletUtils.genAccountCode();
const accountCode = "0x01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";
const circuitInputs = await genEmailSenderInput(emailFilePath, accountCode);
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
Expand All @@ -207,7 +205,7 @@ describe("Email Sender", () => {
const senderEmailAddr = "[email protected]";
const accountSalt = emailWalletUtils.accountSalt(senderEmailAddr, accountCode);
expect(BigInt(accountSalt)).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length]);
expect(0n).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length + 1]);
expect(1n).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length + 1]);
expect(1n).toEqual(witness[1 + domainFields.length + 3 + maskedSubjectFields.length + 2]);
const recipientEmailAddr = "[email protected]";
const expectedRecipientEmailAddrCommit = emailWalletUtils.emailAddrCommitWithSignature(
Expand Down
78 changes: 0 additions & 78 deletions packages/circuits/tests/invitation_code_regex.test.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/circuits/tests/psi_point.test.ts

This file was deleted.

51 changes: 51 additions & 0 deletions packages/circuits/tests/psi_points.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const ff = require("ffjavascript");
const emailWalletUtils = require("@zk-email/relayer-utils");
import { hash_to_curve, point_scalar_mul } from "circom-grumpkin";
import { genPsiPointsInput } from "../helpers/psi_points";
const circom_tester = require("circom_tester");
const wasm_tester = circom_tester.wasm;
import * as path from "path";
const option = {
include: path.join(__dirname, "../../../node_modules"),
};

jest.setTimeout(120000);
describe("PSI point", () => {
let circuit: any;
beforeAll(async () => {
circuit = await wasm_tester(path.join(__dirname, "../src/psi_points.circom"), option);
});

it("psi point calculation test", async () => {
const emailAddr = "[email protected]";
const paddedEmailAddr = emailWalletUtils.padEmailAddr(emailAddr);
const hashedPoint = hash_to_curve(paddedEmailAddr);
const relayerRand = emailWalletUtils.genRelayerRand();
const clientRand = emailWalletUtils.genRelayerRand();
const requestPoint = point_scalar_mul(hashedPoint.x, hashedPoint.y, BigInt(clientRand));
const responsePoint = point_scalar_mul(requestPoint.x, requestPoint.y, BigInt(relayerRand));
const scalarField = new ff.F1Field("21888242871839275222246405745257275088696311157297823662689037894645226208583");
const invClientRand = scalarField.inv(BigInt(clientRand));
const invedPoint = point_scalar_mul(responsePoint.x, responsePoint.y, invClientRand);
const psiPoint = point_scalar_mul(hashedPoint.x, hashedPoint.y, BigInt(relayerRand));
expect(psiPoint.x).toEqual(invedPoint.x);
expect(psiPoint.y).toEqual(invedPoint.y);
});

it("psi circuits test", async () => {
const emailAddr = "[email protected]";
const accountCode = emailWalletUtils.genAccountCode();
const relayerRand = emailWalletUtils.genRelayerRand();
const circuitInputs = await genPsiPointsInput(emailAddr, accountCode, relayerRand);
const witness = await circuit.calculateWitness(circuitInputs);
await circuit.checkConstraints(witness);
const expectedAccountSalt = emailWalletUtils.accountSalt(emailAddr, accountCode);
expect(BigInt(expectedAccountSalt)).toEqual(witness[1]);
const paddedEmailAddr = emailWalletUtils.padEmailAddr(emailAddr);
const hashedPoint = hash_to_curve(paddedEmailAddr);
const psiPoint = point_scalar_mul(hashedPoint.x, hashedPoint.y, BigInt(relayerRand));
expect(psiPoint.x).toEqual(witness[2]);
expect(psiPoint.y).toEqual(witness[3]);
});

});
Loading

0 comments on commit 608b0d0

Please sign in to comment.