Skip to content

Commit

Permalink
Feat/circuits update (#10)
Browse files Browse the repository at this point in the history
* Update the zk-email version

* Update groth16 verifier

* Modify the proving key path
  • Loading branch information
SoraSuegami authored Apr 25, 2024
1 parent 5bb5820 commit 44d3f1c
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 669 deletions.
3 changes: 1 addition & 2 deletions packages/circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"test": "NODE_OPTIONS=--max_old_space_size=8192 jest"
},
"dependencies": {
"@zk-email/circuits": "^3.2.4",
"@zk-email/helpers": "^3.1.3",
"@zk-email/circuits": "^6.0.2",
"@zk-email/zk-regex-circom": "^2.0.2",
"commander": "^11.0.0",
"snarkjs": "^0.7.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/circuits/src/email_auth.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.1.5;
pragma circom 2.1.6;

include "./email_auth_template.circom";

Expand Down
23 changes: 11 additions & 12 deletions packages/circuits/src/email_auth_template.circom
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

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/utils/regex.circom";
include "./utils/constants.circom";
include "./utils/account_salt.circom";
include "./utils/hash_sign.circom";
Expand Down Expand Up @@ -65,26 +64,26 @@ template EmailAuth(n, k, max_header_bytes, max_subject_bytes, recipient_enabled)

// Verify Email Signature
component email_verifier = EmailVerifier(max_header_bytes, 0, n, k, 1);
email_verifier.in_padded <== padded_header;
email_verifier.emailHeader <== padded_header;
email_verifier.pubkey <== public_key;
email_verifier.signature <== signature;
email_verifier.in_len_padded_bytes <== padded_header_len;
email_verifier.emailHeaderLength <== padded_header_len;
signal header_hash[256] <== email_verifier.sha;
public_key_hash <== email_verifier.pubkey_hash;
public_key_hash <== email_verifier.pubkeyHash;

// FROM HEADER REGEX
signal from_regex_out, from_regex_reveal[max_header_bytes];
(from_regex_out, from_regex_reveal) <== FromAddrRegex(max_header_bytes)(padded_header);
from_regex_out === 1;
signal from_email_addr[email_max_bytes];
from_email_addr <== VarShiftMaskedStr(max_header_bytes, email_max_bytes)(from_regex_reveal, from_addr_idx);
from_email_addr <== SelectRegexReveal(max_header_bytes, email_max_bytes)(from_regex_reveal, from_addr_idx);

// DOMAIN NAME HEADER REGEX
signal domain_regex_out, domain_regex_reveal[email_max_bytes];
(domain_regex_out, domain_regex_reveal) <== EmailDomainRegex(email_max_bytes)(from_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_bytes <== SelectRegexReveal(email_max_bytes, domain_len)(domain_regex_reveal, domain_idx);
domain_name <== Bytes2Ints(domain_len)(domain_name_bytes);

signal sign_hash;
Expand All @@ -98,14 +97,14 @@ template EmailAuth(n, k, max_header_bytes, max_subject_bytes, recipient_enabled)
(subject_regex_out, subject_regex_reveal) <== SubjectAllRegex(max_header_bytes)(padded_header);
subject_regex_out === 1;
signal subject_all[max_subject_bytes];
subject_all <== VarShiftMaskedStr(max_header_bytes, max_subject_bytes)(subject_regex_reveal, subject_idx);
subject_all <== SelectRegexReveal(max_header_bytes, max_subject_bytes)(subject_regex_reveal, subject_idx);

// Timestamp regex + convert to decimal format
signal timestamp_regex_out, timestamp_regex_reveal[max_header_bytes];
(timestamp_regex_out, timestamp_regex_reveal) <== TimestampRegex(max_header_bytes)(padded_header);
// timestamp_regex_out === 1;
signal timestamp_str[timestamp_len];
timestamp_str <== VarShiftMaskedStr(max_header_bytes, timestamp_len)(timestamp_regex_reveal, timestamp_idx);
timestamp_str <== SelectRegexReveal(max_header_bytes, timestamp_len)(timestamp_regex_reveal, timestamp_idx);
signal raw_timestamp <== Digit2Int(timestamp_len)(timestamp_str);
timestamp <== timestamp_regex_out * raw_timestamp;

Expand Down Expand Up @@ -142,7 +141,7 @@ template EmailAuth(n, k, max_header_bytes, max_subject_bytes, recipient_enabled)
replaced_code_regex_reveal[i] <== code_regex_reveal[i] * is_code_exist;
}
}
signal shifted_code_hex[code_len] <== VarShiftMaskedStr(max_header_bytes, code_len)(replaced_code_regex_reveal, code_idx);
signal shifted_code_hex[code_len] <== SelectRegexReveal(max_header_bytes, code_len)(replaced_code_regex_reveal, code_idx);
signal invitation_code_hex[code_len];
for(var i=0; i<code_len; i++) {
invitation_code_hex[i] <== is_code_exist * (shifted_code_hex[i] - 48) + 48;
Expand Down Expand Up @@ -177,7 +176,7 @@ template EmailAuth(n, k, max_header_bytes, max_subject_bytes, recipient_enabled)
}
}
signal shifted_email_addr[email_max_bytes];
shifted_email_addr <== VarShiftMaskedStr(max_subject_bytes, email_max_bytes)(replaced_email_addr_regex_reveal, subject_email_addr_idx);
shifted_email_addr <== SelectRegexReveal(max_subject_bytes, email_max_bytes)(replaced_email_addr_regex_reveal, subject_email_addr_idx);
signal recipient_email_addr[email_max_bytes];
for(var i=0; i<email_max_bytes; i++) {
recipient_email_addr[i] <== shifted_email_addr[i] * has_email_recipient;
Expand Down
2 changes: 1 addition & 1 deletion packages/circuits/src/utils/account_salt.circom
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/poseidon.circom";

Expand Down
30 changes: 4 additions & 26 deletions packages/circuits/src/utils/bytes2ints.circom
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/bitify.circom";
include "circomlib/circuits/comparators.circom";
include "circomlib/circuits/poseidon.circom";
include "./constants.circom";
include "@zk-email/circuits/utils/bytes.circom";

function compute_ints_size(bytes_size) {
var pack_bytes = pack_bytes_const();
var remain = bytes_size % pack_bytes;
var num_chunk = (bytes_size - remain) / pack_bytes;
if(remain>0) {
num_chunk += 1;
}
return num_chunk;
return computeIntChunkLength(bytes_size);
}

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<num_chunk; i++) {
for(var j=0; j<pack_bytes; j++) {
var idx = pack_bytes*i+j;
if(idx>=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<num_chunk; i++) {
ints[i] <== ints_sums[i][pack_bytes-1];
}
ints <== PackBytes(bytes_size)(bytes);
}
11 changes: 7 additions & 4 deletions packages/circuits/src/utils/constants.circom
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
pragma circom 2.1.5;
pragma circom 2.1.6;

include "@zk-email/circuits/utils/constants.circom";


function email_max_bytes_const() {
return 256;
return EMAIL_ADDR_MAX_BYTES();
}

function domain_len_const() {
return 255;
return DOMAIN_MAX_BYTES();
}

function invitation_code_len_const() {
Expand All @@ -17,7 +20,7 @@ function field_pack_bits_const() {
}

function pack_bytes_const() {
return 31;
return MAX_BYTES_IN_FIELD();
}

function timestamp_len_const() {
Expand Down
18 changes: 2 additions & 16 deletions packages/circuits/src/utils/digit2int.circom
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/bitify.circom";
include "circomlib/circuits/comparators.circom";
Expand All @@ -12,19 +11,6 @@ template Digit2Int(n) {
signal input in[n];
signal output out;

component digit2int[n];
signal sums[n+1];
sums[0] <== 0;
for(var i = 0; i < n; i++) {
digit2int[i] = Digit2Int1();
digit2int[i].in <== in[i];
sums[i+1] <== 10 * sums[i] + digit2int[i].out;
}
out <== sums[n];
out <== DigitBytesToInt(n)(in);
}

template Digit2Int1() {
signal input in;
signal output out;
out <== in - 48;
}
3 changes: 1 addition & 2 deletions packages/circuits/src/utils/email_addr_commit.circom
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/poseidon.circom";

Expand Down
12 changes: 1 addition & 11 deletions packages/circuits/src/utils/email_nullifier.circom
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/poseidon.circom";

Expand All @@ -9,15 +8,6 @@ template EmailNullifier() {

signal output email_nullifier;

// var field_pack_bits = field_pack_bits_const();

// signal header_hash_int[field_pack_bits+1];
// header_hash_int[0] <== 0;
// for(var i = 0; i < field_pack_bits; i++) {
// header_hash_int[i+1] <== 2 * header_hash_int[i] + header_hash[i];
// }
// signal email_nullifier_input[1];
// email_nullifier_input[0] <== sign_hash;
email_nullifier <== Poseidon(1)([sign_hash]);
}

Expand Down
14 changes: 1 addition & 13 deletions packages/circuits/src/utils/hash_sign.circom
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/poseidon.circom";

template HashSign(n,k) {
// signal input pubkey[k];
signal input signature[k];

// signal output pubkey_hash;
signal output sign_hash;

var k2_chunked_size = k >> 1;
Expand All @@ -16,15 +13,6 @@ template HashSign(n,k) {
}
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<<n) * pubkey[2*i+1];
// }
// }
// pubkey_hash <== Poseidon(k2_chunked_size)(pubkey_hash_input);
for(var i = 0; i < k2_chunked_size; i++) {
if(i==k2_chunked_size-1 && k2_chunked_size % 2 == 1) {
sign_ints[i] <== signature[2*i];
Expand Down
3 changes: 1 addition & 2 deletions packages/circuits/src/utils/hex2int.circom
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

pragma circom 2.1.5;
pragma circom 2.1.6;

include "circomlib/circuits/bitify.circom";
include "circomlib/circuits/comparators.circom";
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ $ yarn test

Run integration tests

Before running integration tests, you need to make a `packages/contracts/test/build_integration` directory, download the zip file from the following link, and place its unziped directory under that directory.
https://drive.google.com/file/d/1waD-GzzpCmo3xN4rdDqQ2A2B7id1JR7V/view?usp=sharing
Before running integration tests, you need to make a `packages/contracts/test/build_integration` directory, download the zip file from the following link, and place its unzipped directory under that directory.
https://drive.google.com/file/d/13_RItK372JdfQFM3TmQFU_svN7q0k5iF/view?usp=drive_link

Then, move `email_auth.zkey` and `email_auth.wasm` in the unzipped directory `params` to `build_integration`.

Expand Down
Loading

0 comments on commit 44d3f1c

Please sign in to comment.