From 698c312e706be31ba190807c5a9bcb3b383d78ee Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Tue, 23 Jul 2024 12:28:34 +0900 Subject: [PATCH] Updating circuits --- packages/circuits/helpers/account_creation.ts | 22 -- packages/circuits/helpers/psi_points.ts | 18 ++ packages/circuits/package.json | 15 +- packages/circuits/scripts/account_creation.ts | 67 ------ packages/circuits/scripts/psi_points.ts | 64 ++++++ packages/circuits/src/account_creation.circom | 116 ----------- packages/circuits/src/announcement.circom | 6 +- packages/circuits/src/claim.circom | 12 +- packages/circuits/src/email_sender.circom | 157 +++----------- packages/circuits/src/psi_points.circom | 31 +++ .../circuits/src/regexes/invitation_code.json | 12 -- .../src/regexes/invitation_code_regex.circom | 185 ----------------- .../regexes/invitation_code_with_prefix.json | 8 - .../invitation_code_with_prefix_regex.circom | 192 ------------------ .../circuits/src/utils/account_salt.circom | 19 -- packages/circuits/src/utils/bytes2ints.circom | 41 ---- packages/circuits/src/utils/constants.circom | 26 --- packages/circuits/src/utils/digit2int.circom | 30 --- .../src/utils/email_addr_commit.circom | 18 -- .../circuits/src/utils/email_nullifier.circom | 25 --- packages/circuits/src/utils/hash_sign.circom | 39 ---- packages/circuits/src/utils/hex2int.circom | 69 ------- yarn.lock | 95 ++++----- 23 files changed, 203 insertions(+), 1064 deletions(-) delete mode 100644 packages/circuits/helpers/account_creation.ts create mode 100644 packages/circuits/helpers/psi_points.ts delete mode 100644 packages/circuits/scripts/account_creation.ts create mode 100644 packages/circuits/scripts/psi_points.ts delete mode 100644 packages/circuits/src/account_creation.circom create mode 100644 packages/circuits/src/psi_points.circom delete mode 100644 packages/circuits/src/regexes/invitation_code.json delete mode 100644 packages/circuits/src/regexes/invitation_code_regex.circom delete mode 100644 packages/circuits/src/regexes/invitation_code_with_prefix.json delete mode 100644 packages/circuits/src/regexes/invitation_code_with_prefix_regex.circom delete mode 100644 packages/circuits/src/utils/account_salt.circom delete mode 100644 packages/circuits/src/utils/bytes2ints.circom delete mode 100644 packages/circuits/src/utils/constants.circom delete mode 100644 packages/circuits/src/utils/digit2int.circom delete mode 100644 packages/circuits/src/utils/email_addr_commit.circom delete mode 100644 packages/circuits/src/utils/email_nullifier.circom delete mode 100644 packages/circuits/src/utils/hash_sign.circom delete mode 100644 packages/circuits/src/utils/hex2int.circom diff --git a/packages/circuits/helpers/account_creation.ts b/packages/circuits/helpers/account_creation.ts deleted file mode 100644 index 184ca8de..00000000 --- a/packages/circuits/helpers/account_creation.ts +++ /dev/null @@ -1,22 +0,0 @@ -import fs from "fs"; -import { promisify } from "util"; -const emailWalletUtils = require("@zk-email/relayer-utils"); - -export async function genAccountCreationInput( - emailFilePath: string, - relayerRand: string, -): Promise<{ - in_padded: string[]; - pubkey: string[]; - signature: string[]; - in_padded_len: string; - relayer_rand: string; - sender_email_idx: number; - code_idx: number; - domain_idx: number; - timestamp_idx: number; -}> { - const emailRaw = await promisify(fs.readFile)(emailFilePath, "utf8"); - const jsonStr = await emailWalletUtils.genAccountCreationInput(emailRaw, relayerRand); - return JSON.parse(jsonStr); -} diff --git a/packages/circuits/helpers/psi_points.ts b/packages/circuits/helpers/psi_points.ts new file mode 100644 index 00000000..56fc2043 --- /dev/null +++ b/packages/circuits/helpers/psi_points.ts @@ -0,0 +1,18 @@ +const emailWalletUtils = require("@zk-email/relayer-utils"); + +export async function genPsiPointsInput( + emailAddr: string, + accountCode: string, + relayerRand: string, +): Promise<{ + email_addr: number[]; + account_code: string; + relayer_rand: string; +}> { + const paddedEmailAddr = emailWalletUtils.padEmailAddr(emailAddr); + return { + email_addr: paddedEmailAddr, + account_code: accountCode, + relayer_rand: relayerRand, + }; +} diff --git a/packages/circuits/package.json b/packages/circuits/package.json index 938f27a2..bad195f0 100644 --- a/packages/circuits/package.json +++ b/packages/circuits/package.json @@ -3,16 +3,16 @@ "license": "MIT", "version": "1.0.0", "scripts": { - "build": "mkdir -p build && yarn build-account-creation && yarn build-claim && yarn build-email-sender && yarn build-announcement", - "build-account-creation": "mkdir -p build/account_creation && circom src/account_creation.circom --r1cs --wasm --sym -l ../../node_modules -o ./build/account_creation", + "build": "mkdir -p build && yarn build-claim && yarn build-email-sender && yarn build-announcement && yarn build-psi-points", "build-claim": "mkdir -p build/claim && circom src/claim.circom --r1cs --wasm --sym -l ../../node_modules -o ./build/claim", "build-email-sender": "mkdir -p build/email_sender && circom src/email_sender.circom --r1cs --wasm --sym -l ../../node_modules -o ./build/email_sender", "build-announcement": "mkdir -p build/announcement && circom src/announcement.circom --r1cs --wasm --sym -l ../../node_modules -o ./build/announcement", + "build-psi-points": "mkdir -p build/psi_points && circom src/psi_points.circom --r1cs --wasm --sym -l ../../node_modules -o ./build/psi_points", "dev-setup": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/dev-setup.ts --output ./build", - "gen-account-creation-input": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/account_creation.ts", "gen-claim-input": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/claim.ts", "gen-email-sender-input": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/email_sender.ts", "gen-announcement-input": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/announcement.ts", + "gen-psi-points-input": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/psi_points.ts", "gen-random-proofs": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/gen_random_proofs.ts", "verify-proofs": "NODE_OPTIONS=--max_old_space_size=8192 npx ts-node scripts/verify_proofs.ts", "extract-email-sign-rand": "npx ts-node scripts/extract_email_sign_rand.ts", @@ -22,10 +22,11 @@ "test": "NODE_OPTIONS=--max_old_space_size=8192 jest" }, "dependencies": { - "@zk-email/circuits": "^3.2.3", - "@zk-email/helpers": "^3.1.3", - "@zk-email/zk-regex-circom": "^1.2.2", - "@zk-email/relayer-utils": "^0.2.3", + "@zk-email/circuits": "^6.1.1", + "@zk-email/ether-email-auth-circom": "/Users/suegamisora/codes/ether-email-auth/packages/circuits", + "@zk-email/helpers": "^6.1.1", + "@zk-email/relayer-utils": "/Users/suegamisora/codes/relayer-utils", + "@zk-email/zk-regex-circom": "^2.1.0", "circom-grumpkin": "https://github.com/zkemail/circom-grumpkin.git", "commander": "^11.0.0", "snarkjs": "^0.7.0" diff --git a/packages/circuits/scripts/account_creation.ts b/packages/circuits/scripts/account_creation.ts deleted file mode 100644 index 40c6ee40..00000000 --- a/packages/circuits/scripts/account_creation.ts +++ /dev/null @@ -1,67 +0,0 @@ -/** - * - * This script is for generating input for the account inititalization circuit. - * - */ - - -import { program } from "commander"; -import fs from "fs"; -import { promisify } from "util"; -import { genAccountCreationInput } from "../helpers/account_creation"; -import path from "path"; -const snarkjs = require("snarkjs"); - -program - .requiredOption( - "--email-file ", - "Path to an email file" - ) - .requiredOption( - "--relayer-rand ", - "Relayer's randomness" - ) - .requiredOption( - "--input-file ", - "Path of a json file to write the generated input" - ) - .option("--silent", "No console logs") - .option("--prove", "Also generate proof"); - -program.parse(); -const args = program.opts(); - -function log(...message: any) { - if (!args.silent) { - console.log(...message); - } -} - -async function generate() { - if (!args.inputFile.endsWith(".json")) { - throw new Error("--input file path arg must end with .json"); - } - - log("Generating Inputs for:", args); - - const circuitInputs = await genAccountCreationInput(args.emailFile, args.relayerRand); - log("\n\nGenerated Inputs:", circuitInputs, "\n\n"); - - await promisify(fs.writeFile)(args.inputFile, JSON.stringify(circuitInputs, null, 2)); - - log("Inputs written to", args.inputFile); - - if (args.prove) { - const dir = path.dirname(args.inputFile); - const { proof, publicSignals } = await snarkjs.groth16.fullProve(circuitInputs, path.join(dir, "account_creation.wasm"), path.join(dir, "account_creation.zkey"), console); - await promisify(fs.writeFile)(path.join(dir, "account_creation_proof.json"), JSON.stringify(proof, null, 2)); - await promisify(fs.writeFile)(path.join(dir, "account_creation_public.json"), JSON.stringify(publicSignals, null, 2)); - log("✓ Proof for account creation circuit generated"); - } - process.exit(0); -} - -generate().catch((err) => { - console.error("Error generating inputs", err); - process.exit(1); -}); diff --git a/packages/circuits/scripts/psi_points.ts b/packages/circuits/scripts/psi_points.ts new file mode 100644 index 00000000..fba3a5bf --- /dev/null +++ b/packages/circuits/scripts/psi_points.ts @@ -0,0 +1,64 @@ +/** + * + * This script is for generating input for the claim circuit. + * + */ + +import { program } from "commander"; +import fs from "fs"; +import { promisify } from "util"; +import { genPsiPointsInput } from "../helpers/psi_points"; +import path from "path"; +const snarkjs = require("snarkjs"); + +program + .requiredOption("--email-addr ", "User's email address") + .requiredOption("--account-code ", "User's account code") + .requiredOption("--relayer-rand ", "Relayer's randomness") + .requiredOption("--input-file ", "Path of a json file to write the generated input") + .option("--silent", "No console logs") + .option("--prove", "Also generate proof"); + +program.parse(); +const args = program.opts(); + +function log(...message: any) { + if (!args.silent) { + console.log(...message); + } +} + +async function generate() { + if (!args.inputFile.endsWith(".json")) { + throw new Error("--input file path arg must end with .json"); + } + + log("Generating Inputs for:", args); + + const circuitInputs = await genPsiPointsInput(args.emailAddr, args.accountCode, args.relayerRand); + + log("\n\nGenerated Inputs:", circuitInputs, "\n\n"); + + await promisify(fs.writeFile)(args.inputFile, JSON.stringify(circuitInputs, null, 2)); + + log("Inputs written to", args.inputFile); + + if (args.prove) { + const dir = path.dirname(args.inputFile); + const { proof, publicSignals } = await snarkjs.groth16.fullProve( + circuitInputs, + path.join(dir, "psi_points.wasm"), + path.join(dir, "psi_points.zkey"), + console, + ); + await promisify(fs.writeFile)(path.join(dir, "psi_points_proof.json"), JSON.stringify(proof, null, 2)); + await promisify(fs.writeFile)(path.join(dir, "psi_points_public.json"), JSON.stringify(publicSignals, null, 2)); + log("✓ Proof for psi_points circuit generated"); + } + process.exit(0); +} + +generate().catch((err) => { + console.error("Error generating inputs", err); + process.exit(1); +}); diff --git a/packages/circuits/src/account_creation.circom b/packages/circuits/src/account_creation.circom deleted file mode 100644 index d10aef8f..00000000 --- a/packages/circuits/src/account_creation.circom +++ /dev/null @@ -1,116 +0,0 @@ - -pragma circom 2.1.5; - -include "circomlib/circuits/bitify.circom"; -include "circomlib/circuits/comparators.circom"; -include "circomlib/circuits/poseidon.circom"; -include "@zk-email/circuits/email-verifier.circom"; -include "@zk-email/circuits/helpers/extract.circom"; -// include "@zk-email/circuits/regexes/from_regex.circom"; -include "./utils/constants.circom"; -// include "./utils/email_addr_pointer.circom"; -// include "./utils/account_code_commit.circom"; -include "./utils/account_salt.circom"; -include "./utils/hex2int.circom"; -include "./utils/hash_sign.circom"; -include "./utils/email_nullifier.circom"; -include "./utils/bytes2ints.circom"; -include "./utils/digit2int.circom"; -include "@zk-email/zk-regex-circom/circuits/common/from_addr_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/email_domain_regex.circom"; -include "./regexes/invitation_code_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/timestamp_regex.circom"; -include "circom-grumpkin/circuits/hash_to_curve.circom"; -include "circom-grumpkin/circuits/point_mul.circom"; - -// Prove an email from user contain the accountCode. Used to initialize account when user reply to invitation email -// Here, n and k are the biginteger parameters for RSA -// This is because the number is chunked into k pack_size of n bits each -template AccountInit(n, k, max_header_bytes) { - signal input in_padded[max_header_bytes]; - signal input pubkey[k]; - signal input signature[k]; - signal input in_padded_len; - signal input relayer_rand; - signal input sender_email_idx; // index of the from email address (= sender email address) in the header - signal input code_idx; // index of the invitation code in the header - signal input domain_idx; - signal input timestamp_idx; - - var email_max_bytes = email_max_bytes_const(); - var domain_len = domain_len_const(); - var domain_filed_len = compute_ints_size(domain_len); - var code_len = invitation_code_len_const(); - var timestamp_len = timestamp_len_const(); - - signal output domain_name[domain_filed_len]; - signal output pubkey_hash; - signal output email_nullifier; - signal output timestamp; - signal output account_salt; - signal output psi_point[2]; - - - - component email_verifier = EmailVerifier(max_header_bytes, 0, n, k, 1); - email_verifier.in_padded <== in_padded; - email_verifier.pubkey <== pubkey; - email_verifier.signature <== signature; - email_verifier.in_len_padded_bytes <== in_padded_len; - signal header_hash[256] <== email_verifier.sha; - pubkey_hash <== email_verifier.pubkey_hash; - - // FROM HEADER REGEX - signal from_regex_out, from_regex_reveal[max_header_bytes]; - (from_regex_out, from_regex_reveal) <== FromAddrRegex(max_header_bytes)(in_padded); - from_regex_out === 1; - signal sender_email_addr[email_max_bytes]; - sender_email_addr <== VarShiftMaskedStr(max_header_bytes, email_max_bytes)(from_regex_reveal, sender_email_idx); - - // INVITATION CODE REGEX - signal code_regex_out, code_regex_reveal[max_header_bytes]; - (code_regex_out, code_regex_reveal) <== InvitationCodeRegex(max_header_bytes)(in_padded); - code_regex_out === 1; - signal invitation_code_hex[code_len] <== VarShiftMaskedStr(max_header_bytes, code_len)(code_regex_reveal, code_idx); - signal account_code <== Hex2Field()(invitation_code_hex); - - // DOMAIN NAME HEADER REGEX - signal domain_regex_out, domain_regex_reveal[email_max_bytes]; - (domain_regex_out, domain_regex_reveal) <== EmailDomainRegex(email_max_bytes)(sender_email_addr); - domain_regex_out === 1; - signal domain_name_bytes[domain_len]; - domain_name_bytes <== VarShiftMaskedStr(email_max_bytes, domain_len)(domain_regex_reveal, domain_idx); - domain_name <== Bytes2Ints(domain_len)(domain_name_bytes); - - signal sign_hash; - (sign_hash, _) <== HashSign(n,k)(signature); - // signal sender_relayer_rand_hash_input[1]; - // sender_relayer_rand_hash_input[0] <== sender_relayer_rand; - // sender_relayer_rand_hash <== Poseidon(1)([sender_relayer_rand]); - email_nullifier <== EmailNullifier()(sign_hash); - - // TIMESTAMP REGEX - signal timestamp_regex_out, timestamp_regex_reveal[max_header_bytes]; - (timestamp_regex_out, timestamp_regex_reveal) <== TimestampRegex(max_header_bytes)(in_padded); - // timestamp_regex_out === 1; - signal timestamp_str[timestamp_len]; - timestamp_str <== VarShiftMaskedStr(max_header_bytes, timestamp_len)(timestamp_regex_reveal, timestamp_idx); - signal raw_timestamp <== Digit2Int(timestamp_len)(timestamp_str); - timestamp <== timestamp_regex_out * raw_timestamp; - - var num_email_addr_ints = compute_ints_size(email_max_bytes); - signal sender_email_addr_ints[num_email_addr_ints] <== Bytes2Ints(email_max_bytes)(sender_email_addr); - account_salt <== AccountSalt(num_email_addr_ints)(sender_email_addr_ints, account_code); - signal hashed_point[2]; - hashed_point <== HashToCurve(email_max_bytes)(sender_email_addr); - psi_point <== PointScalarMul(254)(hashed_point, relayer_rand); - // sender_pointer <== EmailAddrPointer(num_email_addr_ints)(sender_relayer_rand, sender_email_addr_ints); - // sender_ak_commit <== AccountCodeCommit(num_email_addr_ints)(sender_ak, sender_email_addr_ints, sender_relayer_rand_hash); - -} - -// Args: -// * n = 121 is the number of bits in each chunk of the modulus (RSA parameter) -// * k = 17 is the number of chunks in the modulus (RSA parameter) -// * max_header_bytes = 1024 is the max number of bytes in the header -component main = AccountInit(121, 17, 1024); diff --git a/packages/circuits/src/announcement.circom b/packages/circuits/src/announcement.circom index 91c1fc59..80552498 100644 --- a/packages/circuits/src/announcement.circom +++ b/packages/circuits/src/announcement.circom @@ -1,9 +1,9 @@ pragma circom 2.1.5; include "circomlib/circuits/poseidon.circom"; -include "./utils/constants.circom"; -include "./utils/email_addr_commit.circom"; -include "./utils/bytes2ints.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/constants.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/email_addr_commit.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/bytes2ints.circom"; // Prove the commitment to an email address using a randomness // Can be used to register unclaimed funds with announcement diff --git a/packages/circuits/src/claim.circom b/packages/circuits/src/claim.circom index 8dd96a1f..466e635f 100644 --- a/packages/circuits/src/claim.circom +++ b/packages/circuits/src/claim.circom @@ -1,11 +1,10 @@ pragma circom 2.1.5; include "circomlib/circuits/poseidon.circom"; -include "./utils/constants.circom"; -include "./utils/account_salt.circom"; -// include "./utils/email_addr_pointer.circom"; -include "./utils/email_addr_commit.circom"; -include "./utils/bytes2ints.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/constants.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/account_salt.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/email_addr_commit.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/bytes2ints.circom"; // Verify emailAddr commitmetn and pointer has the same email address. // Used for claiming unclaimed funds @@ -18,9 +17,6 @@ template Claim() { signal output email_addr_commit; signal output account_salt; - // signal recipient_relayer_rand_hash_input[1]; - // recipient_relayer_rand_hash_input[0] <== recipient_relayer_rand; - // recipient_relayer_rand_hash <== Poseidon(1)(recipient_relayer_rand_hash_input); var num_email_addr_ints = compute_ints_size(email_max_bytes); signal email_addr_ints[num_email_addr_ints] <== Bytes2Ints(email_max_bytes)(email_addr); email_addr_commit <== EmailAddrCommit(num_email_addr_ints)(cm_rand, email_addr_ints); diff --git a/packages/circuits/src/email_sender.circom b/packages/circuits/src/email_sender.circom index 502c0f71..4ce1e3a3 100644 --- a/packages/circuits/src/email_sender.circom +++ b/packages/circuits/src/email_sender.circom @@ -4,21 +4,24 @@ pragma circom 2.1.5; include "circomlib/circuits/bitify.circom"; include "circomlib/circuits/comparators.circom"; include "circomlib/circuits/poseidon.circom"; -include "@zk-email/circuits/email-verifier.circom"; -include "@zk-email/circuits/helpers/extract.circom"; -include "./utils/constants.circom"; -include "./utils/account_salt.circom"; -include "./utils/email_addr_commit.circom"; -include "./utils/hash_sign.circom"; -include "./utils/email_nullifier.circom"; -include "./utils/bytes2ints.circom"; -include "./utils/digit2int.circom"; -include "./regexes/invitation_code_with_prefix_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/from_addr_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/email_addr_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/email_domain_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/subject_all_regex.circom"; -include "@zk-email/zk-regex-circom/circuits/common/timestamp_regex.circom"; +// include "@zk-email/circuits/email-verifier.circom"; +// include "@zk-email/circuits/helpers/extract.circom"; +// include "./utils/constants.circom"; +// include "./utils/account_salt.circom"; +// include "./utils/email_addr_commit.circom"; +// include "./utils/hash_sign.circom"; +// include "./utils/email_nullifier.circom"; +// include "./utils/bytes2ints.circom"; +// include "./utils/digit2int.circom"; +// include "./regexes/invitation_code_with_prefix_regex.circom"; +// include "@zk-email/zk-regex-circom/circuits/common/from_addr_regex.circom"; +// include "@zk-email/zk-regex-circom/circuits/common/email_addr_regex.circom"; +// include "@zk-email/zk-regex-circom/circuits/common/email_domain_regex.circom"; +// include "@zk-email/zk-regex-circom/circuits/common/subject_all_regex.circom"; +// include "@zk-email/zk-regex-circom/circuits/common/timestamp_regex.circom"; +include "@zk-email/ether-email-auth-circom/src/email_auth_template.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/bytes2ints.circom"; +include "@zk-email/ether-email-auth-circom/src/utils/constants.circom"; // Verify email from user (sender) and extract subject, timestmap, recipient email (commitment), etc. // * n - the number of bits in each chunk of the RSA public key (modulust) @@ -26,17 +29,19 @@ include "@zk-email/zk-regex-circom/circuits/common/timestamp_regex.circom"; // * max_header_bytes - max number of bytes in the email header // * max_subject_bytes - max number of bytes in the email subject template EmailSender(n, k, max_header_bytes, max_subject_bytes) { - signal input in_padded[max_header_bytes]; // email data (only header part) - signal input pubkey[k]; // RSA pubkey (modulus), k parts of n bits each. + signal input padded_header[max_header_bytes]; // email data (only header part) + signal input public_key[k]; // RSA public key (modulus), k parts of n bits each. signal input signature[k]; // RSA signature, k parts of n bits each. - signal input in_padded_len; // length of in email data including the padding - // signal input sender_relayer_rand; // Private randomness of the relayer + signal input padded_header_len; // length of in email data including the padding signal input sender_account_code; - signal input sender_email_idx; // Index of the from email address (= sender email address) in the email header + signal input from_addr_idx; // Index of the from email address (= sender email address) in the email header signal input subject_idx; // Index of the subject in the header - signal input recipient_email_idx; // Index of the recipient email address in the subject signal input domain_idx; // Index of the domain name in the from email address signal input timestamp_idx; // Index of the timestamp in the header + signal input code_idx; // index of the invitation code in the header + signal input recipient_email_idx; // Index of the recipient email address in the subject + // signal input relayer_rand; // Private randomness of the relayer + var email_max_bytes = email_max_bytes_const(); var subject_field_len = compute_ints_size(max_subject_bytes); @@ -49,117 +54,17 @@ template EmailSender(n, k, max_header_bytes, max_subject_bytes) { var timestamp_len = timestamp_len_const(); signal output domain_name[domain_filed_len]; - signal output pubkey_hash; + signal output public_key_hash; signal output email_nullifier; signal output timestamp; - signal output masked_subject_str[subject_field_len]; + signal output masked_subject[subject_field_len]; signal output sender_account_salt; + signal output is_code_exist; signal output has_email_recipient; signal output recipient_email_addr_commit; - - // Verify Email Signature - component email_verifier = EmailVerifier(max_header_bytes, 0, n, k, 1); - email_verifier.in_padded <== in_padded; - email_verifier.pubkey <== pubkey; - email_verifier.signature <== signature; - email_verifier.in_len_padded_bytes <== in_padded_len; - signal header_hash[256] <== email_verifier.sha; - pubkey_hash <== email_verifier.pubkey_hash; - - // FROM HEADER REGEX - signal from_regex_out, from_regex_reveal[max_header_bytes]; - (from_regex_out, from_regex_reveal) <== FromAddrRegex(max_header_bytes)(in_padded); - from_regex_out === 1; - signal sender_email_addr[email_max_bytes]; - sender_email_addr <== VarShiftMaskedStr(max_header_bytes, email_max_bytes)(from_regex_reveal, sender_email_idx); - - // SUBJECT HEADER REGEX - signal subject_regex_out, subject_regex_reveal[max_header_bytes]; - (subject_regex_out, subject_regex_reveal) <== SubjectAllRegex(max_header_bytes)(in_padded); - subject_regex_out === 1; - signal subject_all[max_subject_bytes]; - subject_all <== VarShiftMaskedStr(max_header_bytes, max_subject_bytes)(subject_regex_reveal, subject_idx); - signal recipient_email_regex_out, recipient_email_regex_reveal[max_subject_bytes]; - (recipient_email_regex_out, recipient_email_regex_reveal) <== EmailAddrRegex(max_subject_bytes)(subject_all); - has_email_recipient <== IsZero()(recipient_email_regex_out-1); - signal replaced_email_regex_reveal[max_subject_bytes]; - for(var i=0; i0) { - num_chunk += 1; - } - return num_chunk; -} - -template Bytes2Ints(bytes_size) { - var num_chunk = compute_ints_size(bytes_size); - signal input bytes[bytes_size]; - signal output ints[num_chunk]; - - var pack_bytes = pack_bytes_const(); - signal ints_sums[num_chunk][pack_bytes]; - for(var i=0; i=bytes_size) { - ints_sums[i][j] <== ints_sums[i][j-1]; - } else if (j==0){ - ints_sums[i][j] <== bytes[idx]; - } else { - ints_sums[i][j] <== ints_sums[i][j-1] + (1<<(8*j)) * bytes[idx]; - } - } - } - for(var i=0; i> 1; - if(k % 2 == 1) { - k2_chunked_size += 1; - } - signal output sign_ints[k2_chunked_size]; - - // signal pubkey_hash_input[k2_chunked_size]; - // for(var i = 0; i < k2_chunked_size; i++) { - // if(i==k2_chunked_size-1 && k2_chunked_size % 2 == 1) { - // pubkey_hash_input[i] <== pubkey[2*i]; - // } else { - // pubkey_hash_input[i] <== pubkey[2*i] + (1<