Skip to content

Commit

Permalink
Optimize the compiler by reducing multi_or.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Nov 3, 2023
1 parent f3c7f72 commit 9cd365d
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 395 deletions.
2 changes: 1 addition & 1 deletion packages/apis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zk-regex-apis"
version = "1.1.5"
version = "1.1.6"
license = "MIT"
edition = "2018"
exclude = ["index.node"]
Expand Down
2 changes: 1 addition & 1 deletion packages/apis/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/zk-regex-apis",
"version": "1.1.5",
"version": "1.1.6",
"description": "apis compatible with [zk-regex](https://github.com/zkemail/zk-regex/tree/main).",
"contributors": [
"Javier Su <[email protected]>",
Expand Down
130 changes: 47 additions & 83 deletions packages/circom/circuits/common/body_hash_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template BodyHashRegex(msg_bytes) {
component eq[48][num_bytes];
component lt[16][num_bytes];
component and[47][num_bytes];
component multi_or[15][num_bytes];
component multi_or[12][num_bytes];
signal states[num_bytes+1][28];
component state_changed[num_bytes];

Expand Down Expand Up @@ -216,36 +216,16 @@ template BodyHashRegex(msg_bytes) {
and[13][i].b <== multi_or[4][i].out;
and[14][i] = AND();
and[14][i].a <== states[i][5];
multi_or[5][i] = MultiOR(16);
multi_or[5][i].in[0] <== and[11][i].out;
multi_or[5][i].in[1] <== and[12][i].out;
multi_or[5][i].in[2] <== eq[7][i].out;
multi_or[5][i].in[3] <== eq[8][i].out;
multi_or[5][i].in[4] <== eq[9][i].out;
multi_or[5][i].in[5] <== eq[10][i].out;
multi_or[5][i].in[6] <== eq[11][i].out;
multi_or[5][i].in[7] <== eq[12][i].out;
multi_or[5][i].in[8] <== eq[13][i].out;
multi_or[5][i].in[9] <== eq[14][i].out;
multi_or[5][i].in[10] <== eq[15][i].out;
multi_or[5][i].in[11] <== eq[16][i].out;
multi_or[5][i].in[12] <== eq[17][i].out;
multi_or[5][i].in[13] <== eq[18][i].out;
multi_or[5][i].in[14] <== eq[19][i].out;
multi_or[5][i].in[15] <== eq[20][i].out;
and[14][i].b <== multi_or[5][i].out;
and[14][i].b <== multi_or[4][i].out;
and[15][i] = AND();
and[15][i].a <== states[i][6];
multi_or[6][i] = MultiOR(2);
multi_or[6][i].in[0] <== and[8][i].out;
multi_or[6][i].in[1] <== and[9][i].out;
and[15][i].b <== multi_or[6][i].out;
multi_or[7][i] = MultiOR(4);
multi_or[7][i].in[0] <== and[10][i].out;
multi_or[7][i].in[1] <== and[13][i].out;
multi_or[7][i].in[2] <== and[14][i].out;
multi_or[7][i].in[3] <== and[15][i].out;
states[i+1][2] <== multi_or[7][i].out;
and[15][i].b <== multi_or[3][i].out;
multi_or[5][i] = MultiOR(4);
multi_or[5][i].in[0] <== and[10][i].out;
multi_or[5][i].in[1] <== and[13][i].out;
multi_or[5][i].in[2] <== and[14][i].out;
multi_or[5][i].in[3] <== and[15][i].out;
states[i+1][2] <== multi_or[5][i].out;
state_changed[i].in[1] <== states[i+1][2];
eq[21][i] = IsEqual();
eq[21][i].in[0] <== in[i];
Expand Down Expand Up @@ -310,57 +290,41 @@ template BodyHashRegex(msg_bytes) {
eq[34][i].in[1] <== 57;
and[19][i] = AND();
and[19][i].a <== states[i][3];
multi_or[8][i] = MultiOR(15);
multi_or[8][i].in[0] <== and[18][i].out;
multi_or[8][i].in[1] <== and[0][i].out;
multi_or[8][i].in[2] <== eq[23][i].out;
multi_or[8][i].in[3] <== eq[24][i].out;
multi_or[8][i].in[4] <== eq[25][i].out;
multi_or[8][i].in[5] <== eq[26][i].out;
multi_or[8][i].in[6] <== eq[27][i].out;
multi_or[8][i].in[7] <== eq[28][i].out;
multi_or[8][i].in[8] <== eq[29][i].out;
multi_or[8][i].in[9] <== eq[30][i].out;
multi_or[8][i].in[10] <== eq[31][i].out;
multi_or[8][i].in[11] <== eq[32][i].out;
multi_or[8][i].in[12] <== eq[33][i].out;
multi_or[8][i].in[13] <== eq[34][i].out;
multi_or[8][i].in[14] <== eq[21][i].out;
and[19][i].b <== multi_or[8][i].out;
multi_or[6][i] = MultiOR(15);
multi_or[6][i].in[0] <== and[18][i].out;
multi_or[6][i].in[1] <== and[0][i].out;
multi_or[6][i].in[2] <== eq[23][i].out;
multi_or[6][i].in[3] <== eq[24][i].out;
multi_or[6][i].in[4] <== eq[25][i].out;
multi_or[6][i].in[5] <== eq[26][i].out;
multi_or[6][i].in[6] <== eq[27][i].out;
multi_or[6][i].in[7] <== eq[28][i].out;
multi_or[6][i].in[8] <== eq[29][i].out;
multi_or[6][i].in[9] <== eq[30][i].out;
multi_or[6][i].in[10] <== eq[31][i].out;
multi_or[6][i].in[11] <== eq[32][i].out;
multi_or[6][i].in[12] <== eq[33][i].out;
multi_or[6][i].in[13] <== eq[34][i].out;
multi_or[6][i].in[14] <== eq[21][i].out;
and[19][i].b <== multi_or[6][i].out;
and[20][i] = AND();
and[20][i].a <== states[i][5];
multi_or[9][i] = MultiOR(15);
multi_or[9][i].in[0] <== and[18][i].out;
multi_or[9][i].in[1] <== and[0][i].out;
multi_or[9][i].in[2] <== eq[23][i].out;
multi_or[9][i].in[3] <== eq[24][i].out;
multi_or[9][i].in[4] <== eq[25][i].out;
multi_or[9][i].in[5] <== eq[26][i].out;
multi_or[9][i].in[6] <== eq[27][i].out;
multi_or[9][i].in[7] <== eq[28][i].out;
multi_or[9][i].in[8] <== eq[29][i].out;
multi_or[9][i].in[9] <== eq[30][i].out;
multi_or[9][i].in[10] <== eq[31][i].out;
multi_or[9][i].in[11] <== eq[32][i].out;
multi_or[9][i].in[12] <== eq[33][i].out;
multi_or[9][i].in[13] <== eq[34][i].out;
multi_or[9][i].in[14] <== eq[21][i].out;
and[20][i].b <== multi_or[9][i].out;
multi_or[10][i] = MultiOR(2);
multi_or[10][i].in[0] <== and[19][i].out;
multi_or[10][i].in[1] <== and[20][i].out;
states[i+1][5] <== multi_or[10][i].out;
and[20][i].b <== multi_or[6][i].out;
multi_or[7][i] = MultiOR(2);
multi_or[7][i].in[0] <== and[19][i].out;
multi_or[7][i].in[1] <== and[20][i].out;
states[i+1][5] <== multi_or[7][i].out;
state_changed[i].in[4] <== states[i+1][5];
and[21][i] = AND();
and[21][i].a <== states[i][1];
and[21][i].b <== eq[21][i].out;
and[22][i] = AND();
and[22][i].a <== states[i][26];
and[22][i].b <== eq[21][i].out;
multi_or[11][i] = MultiOR(2);
multi_or[11][i].in[0] <== and[21][i].out;
multi_or[11][i].in[1] <== and[22][i].out;
states[i+1][6] <== multi_or[11][i].out;
multi_or[8][i] = MultiOR(2);
multi_or[8][i].in[0] <== and[21][i].out;
multi_or[8][i].in[1] <== and[22][i].out;
states[i+1][6] <== multi_or[8][i].out;
state_changed[i].in[5] <== states[i+1][6];
eq[35][i] = IsEqual();
eq[35][i].in[0] <== in[i];
Expand All @@ -371,10 +335,10 @@ template BodyHashRegex(msg_bytes) {
and[24][i] = AND();
and[24][i].a <== states[i][8];
and[24][i].b <== eq[35][i].out;
multi_or[12][i] = MultiOR(2);
multi_or[12][i].in[0] <== and[23][i].out;
multi_or[12][i].in[1] <== and[24][i].out;
states[i+1][7] <== multi_or[12][i].out;
multi_or[9][i] = MultiOR(2);
multi_or[9][i].in[0] <== and[23][i].out;
multi_or[9][i].in[1] <== and[24][i].out;
states[i+1][7] <== multi_or[9][i].out;
state_changed[i].in[6] <== states[i+1][7];
and[25][i] = AND();
and[25][i].a <== states[i][5];
Expand All @@ -390,10 +354,10 @@ template BodyHashRegex(msg_bytes) {
and[27][i] = AND();
and[27][i].a <== states[i][10];
and[27][i].b <== eq[36][i].out;
multi_or[13][i] = MultiOR(2);
multi_or[13][i].in[0] <== and[26][i].out;
multi_or[13][i].in[1] <== and[27][i].out;
states[i+1][9] <== multi_or[13][i].out;
multi_or[10][i] = MultiOR(2);
multi_or[10][i].in[0] <== and[26][i].out;
multi_or[10][i].in[1] <== and[27][i].out;
states[i+1][9] <== multi_or[10][i].out;
state_changed[i].in[8] <== states[i+1][9];
eq[37][i] = IsEqual();
eq[37][i].in[0] <== in[i];
Expand All @@ -407,10 +371,10 @@ template BodyHashRegex(msg_bytes) {
and[29][i] = AND();
and[29][i].a <== states[i][9];
and[29][i].b <== eq[38][i].out;
multi_or[14][i] = MultiOR(2);
multi_or[14][i].in[0] <== and[28][i].out;
multi_or[14][i].in[1] <== and[29][i].out;
states[i+1][10] <== multi_or[14][i].out;
multi_or[11][i] = MultiOR(2);
multi_or[11][i].in[0] <== and[28][i].out;
multi_or[11][i].in[1] <== and[29][i].out;
states[i+1][10] <== multi_or[11][i].out;
state_changed[i].in[9] <== states[i+1][10];
and[30][i] = AND();
and[30][i].a <== states[i][10];
Expand Down
27 changes: 6 additions & 21 deletions packages/circom/circuits/common/email_addr_regex.circom
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template EmailAddrRegex(msg_bytes) {
component eq[25][num_bytes];
component lt[8][num_bytes];
component and[9][num_bytes];
component multi_or[6][num_bytes];
component multi_or[5][num_bytes];
signal states[num_bytes+1][4];
component state_changed[num_bytes];

Expand Down Expand Up @@ -229,26 +229,11 @@ template EmailAddrRegex(msg_bytes) {
and[7][i].b <== multi_or[3][i].out;
and[8][i] = AND();
and[8][i].a <== states[i][3];
multi_or[4][i] = MultiOR(14);
multi_or[4][i].in[0] <== and[0][i].out;
multi_or[4][i].in[1] <== and[6][i].out;
multi_or[4][i].in[2] <== eq[8][i].out;
multi_or[4][i].in[3] <== eq[9][i].out;
multi_or[4][i].in[4] <== eq[11][i].out;
multi_or[4][i].in[5] <== eq[12][i].out;
multi_or[4][i].in[6] <== eq[13][i].out;
multi_or[4][i].in[7] <== eq[14][i].out;
multi_or[4][i].in[8] <== eq[15][i].out;
multi_or[4][i].in[9] <== eq[16][i].out;
multi_or[4][i].in[10] <== eq[17][i].out;
multi_or[4][i].in[11] <== eq[18][i].out;
multi_or[4][i].in[12] <== eq[19][i].out;
multi_or[4][i].in[13] <== eq[20][i].out;
and[8][i].b <== multi_or[4][i].out;
multi_or[5][i] = MultiOR(2);
multi_or[5][i].in[0] <== and[7][i].out;
multi_or[5][i].in[1] <== and[8][i].out;
states[i+1][3] <== multi_or[5][i].out;
and[8][i].b <== multi_or[3][i].out;
multi_or[4][i] = MultiOR(2);
multi_or[4][i].in[0] <== and[7][i].out;
multi_or[4][i].in[1] <== and[8][i].out;
states[i+1][3] <== multi_or[4][i].out;
state_changed[i].in[2] <== states[i+1][3];
states[i+1][0] <== 1 - state_changed[i].out;
}
Expand Down
Loading

0 comments on commit 9cd365d

Please sign in to comment.