From 0cba20182384992730b57b2f9840076f0046ef15 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Fri, 29 Sep 2023 21:07:33 +0900 Subject: [PATCH] Add negate tests. --- .../circom/circuits/common/timestamp.circom | 891 ------------------ .../circom/circuits/common/timestamp.json | 4 +- packages/circom/tests/circuits/negate1.json | 20 + .../tests/circuits/negate1_regex.circom | 114 +++ .../tests/circuits/test_negate1_regex.circom | 2 + packages/circom/tests/negate_regex.test.ts | 110 +++ packages/compiler/src/gen_circom.js | 34 +- packages/compiler/src/lib.rs | 15 +- 8 files changed, 274 insertions(+), 916 deletions(-) delete mode 100644 packages/circom/circuits/common/timestamp.circom create mode 100644 packages/circom/tests/circuits/negate1.json create mode 100644 packages/circom/tests/circuits/negate1_regex.circom create mode 100644 packages/circom/tests/circuits/test_negate1_regex.circom create mode 100644 packages/circom/tests/negate_regex.test.ts diff --git a/packages/circom/circuits/common/timestamp.circom b/packages/circom/circuits/common/timestamp.circom deleted file mode 100644 index 91597f5..0000000 --- a/packages/circom/circuits/common/timestamp.circom +++ /dev/null @@ -1,891 +0,0 @@ -pragma circom 2.1.5; - -include "zk-regex-circom/circuits/regex_helpers.circom"; - -template TimestampRegex(msg_bytes) { - signal input msg[msg_bytes]; - signal output out; - - var num_bytes = msg_bytes+1; - signal in[num_bytes]; - in[0]<==128; - for (var i = 0; i < msg_bytes; i++) { - in[i+1] <== msg[i]; - } - - component eq[147][num_bytes]; - component lt[22][num_bytes]; - component and[43][num_bytes]; - component multi_or[9][num_bytes]; - signal states[num_bytes+1][25]; - component state_changed[num_bytes]; - - states[0][0] <== 1; - for (var i = 1; i < 25; i++) { - states[0][i] <== 0; - } - - for (var i = 0; i < num_bytes; i++) { - state_changed[i] = MultiOR(24); - lt[0][i] = LessThan(8); - lt[0][i].in[0] <== 96; - lt[0][i].in[1] <== in[i]; - lt[1][i] = LessThan(8); - lt[1][i].in[0] <== in[i]; - lt[1][i].in[1] <== 123; - and[0][i] = AND(); - and[0][i].a <== lt[0][i].out; - and[0][i].b <== lt[1][i].out; - and[1][i] = AND(); - and[1][i].a <== states[i][1]; - and[1][i].b <== and[0][i].out; - eq[0][i] = IsEqual(); - eq[0][i].in[0] <== in[i]; - eq[0][i].in[1] <== 97; - eq[1][i] = IsEqual(); - eq[1][i].in[0] <== in[i]; - eq[1][i].in[1] <== 98; - eq[2][i] = IsEqual(); - eq[2][i].in[0] <== in[i]; - eq[2][i].in[1] <== 99; - eq[3][i] = IsEqual(); - eq[3][i].in[0] <== in[i]; - eq[3][i].in[1] <== 100; - eq[4][i] = IsEqual(); - eq[4][i].in[0] <== in[i]; - eq[4][i].in[1] <== 101; - eq[5][i] = IsEqual(); - eq[5][i].in[0] <== in[i]; - eq[5][i].in[1] <== 102; - eq[6][i] = IsEqual(); - eq[6][i].in[0] <== in[i]; - eq[6][i].in[1] <== 103; - eq[7][i] = IsEqual(); - eq[7][i].in[0] <== in[i]; - eq[7][i].in[1] <== 104; - eq[8][i] = IsEqual(); - eq[8][i].in[0] <== in[i]; - eq[8][i].in[1] <== 105; - eq[9][i] = IsEqual(); - eq[9][i].in[0] <== in[i]; - eq[9][i].in[1] <== 106; - eq[10][i] = IsEqual(); - eq[10][i].in[0] <== in[i]; - eq[10][i].in[1] <== 107; - eq[11][i] = IsEqual(); - eq[11][i].in[0] <== in[i]; - eq[11][i].in[1] <== 108; - eq[12][i] = IsEqual(); - eq[12][i].in[0] <== in[i]; - eq[12][i].in[1] <== 109; - eq[13][i] = IsEqual(); - eq[13][i].in[0] <== in[i]; - eq[13][i].in[1] <== 110; - eq[14][i] = IsEqual(); - eq[14][i].in[0] <== in[i]; - eq[14][i].in[1] <== 111; - eq[15][i] = IsEqual(); - eq[15][i].in[0] <== in[i]; - eq[15][i].in[1] <== 112; - eq[16][i] = IsEqual(); - eq[16][i].in[0] <== in[i]; - eq[16][i].in[1] <== 113; - eq[17][i] = IsEqual(); - eq[17][i].in[0] <== in[i]; - eq[17][i].in[1] <== 114; - eq[18][i] = IsEqual(); - eq[18][i].in[0] <== in[i]; - eq[18][i].in[1] <== 115; - eq[19][i] = IsEqual(); - eq[19][i].in[0] <== in[i]; - eq[19][i].in[1] <== 117; - eq[20][i] = IsEqual(); - eq[20][i].in[0] <== in[i]; - eq[20][i].in[1] <== 118; - eq[21][i] = IsEqual(); - eq[21][i].in[0] <== in[i]; - eq[21][i].in[1] <== 119; - eq[22][i] = IsEqual(); - eq[22][i].in[0] <== in[i]; - eq[22][i].in[1] <== 120; - eq[23][i] = IsEqual(); - eq[23][i].in[0] <== in[i]; - eq[23][i].in[1] <== 121; - eq[24][i] = IsEqual(); - eq[24][i].in[0] <== in[i]; - eq[24][i].in[1] <== 122; - and[2][i] = AND(); - and[2][i].a <== states[i][14]; - multi_or[0][i] = MultiOR(25); - multi_or[0][i].in[0] <== eq[0][i].out; - multi_or[0][i].in[1] <== eq[1][i].out; - multi_or[0][i].in[2] <== eq[2][i].out; - multi_or[0][i].in[3] <== eq[3][i].out; - multi_or[0][i].in[4] <== eq[4][i].out; - multi_or[0][i].in[5] <== eq[5][i].out; - multi_or[0][i].in[6] <== eq[6][i].out; - multi_or[0][i].in[7] <== eq[7][i].out; - multi_or[0][i].in[8] <== eq[8][i].out; - multi_or[0][i].in[9] <== eq[9][i].out; - multi_or[0][i].in[10] <== eq[10][i].out; - multi_or[0][i].in[11] <== eq[11][i].out; - multi_or[0][i].in[12] <== eq[12][i].out; - multi_or[0][i].in[13] <== eq[13][i].out; - multi_or[0][i].in[14] <== eq[14][i].out; - multi_or[0][i].in[15] <== eq[15][i].out; - multi_or[0][i].in[16] <== eq[16][i].out; - multi_or[0][i].in[17] <== eq[17][i].out; - multi_or[0][i].in[18] <== eq[18][i].out; - multi_or[0][i].in[19] <== eq[19][i].out; - multi_or[0][i].in[20] <== eq[20][i].out; - multi_or[0][i].in[21] <== eq[21][i].out; - multi_or[0][i].in[22] <== eq[22][i].out; - multi_or[0][i].in[23] <== eq[23][i].out; - multi_or[0][i].in[24] <== eq[24][i].out; - and[2][i].b <== multi_or[0][i].out; - lt[2][i] = LessThan(8); - lt[2][i].in[0] <== 96; - lt[2][i].in[1] <== in[i]; - lt[3][i] = LessThan(8); - lt[3][i].in[0] <== in[i]; - lt[3][i].in[1] <== 123; - and[3][i] = AND(); - and[3][i].a <== lt[2][i].out; - and[3][i].b <== lt[3][i].out; - and[4][i] = AND(); - and[4][i].a <== states[i][16]; - and[4][i].b <== and[3][i].out; - lt[4][i] = LessThan(8); - lt[4][i].in[0] <== 96; - lt[4][i].in[1] <== in[i]; - lt[5][i] = LessThan(8); - lt[5][i].in[0] <== in[i]; - lt[5][i].in[1] <== 123; - and[5][i] = AND(); - and[5][i].a <== lt[4][i].out; - and[5][i].b <== lt[5][i].out; - and[6][i] = AND(); - and[6][i].a <== states[i][22]; - and[6][i].b <== and[5][i].out; - multi_or[1][i] = MultiOR(4); - multi_or[1][i].in[0] <== and[1][i].out; - multi_or[1][i].in[1] <== and[2][i].out; - multi_or[1][i].in[2] <== and[4][i].out; - multi_or[1][i].in[3] <== and[6][i].out; - states[i+1][1] <== multi_or[1][i].out; - state_changed[i].in[0] <== states[i+1][1]; - eq[25][i] = IsEqual(); - eq[25][i].in[0] <== in[i]; - eq[25][i].in[1] <== 61; - and[7][i] = AND(); - and[7][i].a <== states[i][1]; - and[7][i].b <== eq[25][i].out; - states[i+1][2] <== and[7][i].out; - state_changed[i].in[1] <== states[i+1][2]; - eq[26][i] = IsEqual(); - eq[26][i].in[0] <== in[i]; - eq[26][i].in[1] <== 100; - and[8][i] = AND(); - and[8][i].a <== states[i][0]; - and[8][i].b <== eq[26][i].out; - states[i+1][3] <== and[8][i].out; - state_changed[i].in[2] <== states[i+1][3]; - lt[6][i] = LessThan(8); - lt[6][i].in[0] <== 47; - lt[6][i].in[1] <== in[i]; - lt[7][i] = LessThan(8); - lt[7][i].in[0] <== in[i]; - lt[7][i].in[1] <== 58; - and[9][i] = AND(); - and[9][i].a <== lt[6][i].out; - and[9][i].b <== lt[7][i].out; - lt[8][i] = LessThan(8); - lt[8][i].in[0] <== 64; - lt[8][i].in[1] <== in[i]; - lt[9][i] = LessThan(8); - lt[9][i].in[0] <== in[i]; - lt[9][i].in[1] <== 127; - and[10][i] = AND(); - and[10][i].a <== lt[8][i].out; - and[10][i].b <== lt[9][i].out; - eq[27][i] = IsEqual(); - eq[27][i].in[0] <== in[i]; - eq[27][i].in[1] <== 9; - eq[28][i] = IsEqual(); - eq[28][i].in[0] <== in[i]; - eq[28][i].in[1] <== 10; - eq[29][i] = IsEqual(); - eq[29][i].in[0] <== in[i]; - eq[29][i].in[1] <== 13; - eq[30][i] = IsEqual(); - eq[30][i].in[0] <== in[i]; - eq[30][i].in[1] <== 32; - eq[31][i] = IsEqual(); - eq[31][i].in[0] <== in[i]; - eq[31][i].in[1] <== 33; - eq[32][i] = IsEqual(); - eq[32][i].in[0] <== in[i]; - eq[32][i].in[1] <== 34; - eq[33][i] = IsEqual(); - eq[33][i].in[0] <== in[i]; - eq[33][i].in[1] <== 35; - eq[34][i] = IsEqual(); - eq[34][i].in[0] <== in[i]; - eq[34][i].in[1] <== 36; - eq[35][i] = IsEqual(); - eq[35][i].in[0] <== in[i]; - eq[35][i].in[1] <== 37; - eq[36][i] = IsEqual(); - eq[36][i].in[0] <== in[i]; - eq[36][i].in[1] <== 38; - eq[37][i] = IsEqual(); - eq[37][i].in[0] <== in[i]; - eq[37][i].in[1] <== 39; - eq[38][i] = IsEqual(); - eq[38][i].in[0] <== in[i]; - eq[38][i].in[1] <== 40; - eq[39][i] = IsEqual(); - eq[39][i].in[0] <== in[i]; - eq[39][i].in[1] <== 41; - eq[40][i] = IsEqual(); - eq[40][i].in[0] <== in[i]; - eq[40][i].in[1] <== 42; - eq[41][i] = IsEqual(); - eq[41][i].in[0] <== in[i]; - eq[41][i].in[1] <== 43; - eq[42][i] = IsEqual(); - eq[42][i].in[0] <== in[i]; - eq[42][i].in[1] <== 44; - eq[43][i] = IsEqual(); - eq[43][i].in[0] <== in[i]; - eq[43][i].in[1] <== 45; - eq[44][i] = IsEqual(); - eq[44][i].in[0] <== in[i]; - eq[44][i].in[1] <== 46; - eq[45][i] = IsEqual(); - eq[45][i].in[0] <== in[i]; - eq[45][i].in[1] <== 47; - eq[46][i] = IsEqual(); - eq[46][i].in[0] <== in[i]; - eq[46][i].in[1] <== 58; - eq[47][i] = IsEqual(); - eq[47][i].in[0] <== in[i]; - eq[47][i].in[1] <== 60; - eq[48][i] = IsEqual(); - eq[48][i].in[0] <== in[i]; - eq[48][i].in[1] <== 61; - eq[49][i] = IsEqual(); - eq[49][i].in[0] <== in[i]; - eq[49][i].in[1] <== 62; - eq[50][i] = IsEqual(); - eq[50][i].in[0] <== in[i]; - eq[50][i].in[1] <== 63; - eq[51][i] = IsEqual(); - eq[51][i].in[0] <== in[i]; - eq[51][i].in[1] <== 64; - and[11][i] = AND(); - and[11][i].a <== states[i][2]; - multi_or[2][i] = MultiOR(27); - multi_or[2][i].in[0] <== and[9][i].out; - multi_or[2][i].in[1] <== and[10][i].out; - multi_or[2][i].in[2] <== eq[27][i].out; - multi_or[2][i].in[3] <== eq[28][i].out; - multi_or[2][i].in[4] <== eq[29][i].out; - multi_or[2][i].in[5] <== eq[30][i].out; - multi_or[2][i].in[6] <== eq[31][i].out; - multi_or[2][i].in[7] <== eq[32][i].out; - multi_or[2][i].in[8] <== eq[33][i].out; - multi_or[2][i].in[9] <== eq[34][i].out; - multi_or[2][i].in[10] <== eq[35][i].out; - multi_or[2][i].in[11] <== eq[36][i].out; - multi_or[2][i].in[12] <== eq[37][i].out; - multi_or[2][i].in[13] <== eq[38][i].out; - multi_or[2][i].in[14] <== eq[39][i].out; - multi_or[2][i].in[15] <== eq[40][i].out; - multi_or[2][i].in[16] <== eq[41][i].out; - multi_or[2][i].in[17] <== eq[42][i].out; - multi_or[2][i].in[18] <== eq[43][i].out; - multi_or[2][i].in[19] <== eq[44][i].out; - multi_or[2][i].in[20] <== eq[45][i].out; - multi_or[2][i].in[21] <== eq[46][i].out; - multi_or[2][i].in[22] <== eq[47][i].out; - multi_or[2][i].in[23] <== eq[48][i].out; - multi_or[2][i].in[24] <== eq[49][i].out; - multi_or[2][i].in[25] <== eq[50][i].out; - multi_or[2][i].in[26] <== eq[51][i].out; - and[11][i].b <== multi_or[2][i].out; - lt[10][i] = LessThan(8); - lt[10][i].in[0] <== 47; - lt[10][i].in[1] <== in[i]; - lt[11][i] = LessThan(8); - lt[11][i].in[0] <== in[i]; - lt[11][i].in[1] <== 58; - and[12][i] = AND(); - and[12][i].a <== lt[10][i].out; - and[12][i].b <== lt[11][i].out; - lt[12][i] = LessThan(8); - lt[12][i].in[0] <== 64; - lt[12][i].in[1] <== in[i]; - lt[13][i] = LessThan(8); - lt[13][i].in[0] <== in[i]; - lt[13][i].in[1] <== 127; - and[13][i] = AND(); - and[13][i].a <== lt[12][i].out; - and[13][i].b <== lt[13][i].out; - eq[52][i] = IsEqual(); - eq[52][i].in[0] <== in[i]; - eq[52][i].in[1] <== 9; - eq[53][i] = IsEqual(); - eq[53][i].in[0] <== in[i]; - eq[53][i].in[1] <== 10; - eq[54][i] = IsEqual(); - eq[54][i].in[0] <== in[i]; - eq[54][i].in[1] <== 13; - eq[55][i] = IsEqual(); - eq[55][i].in[0] <== in[i]; - eq[55][i].in[1] <== 32; - eq[56][i] = IsEqual(); - eq[56][i].in[0] <== in[i]; - eq[56][i].in[1] <== 33; - eq[57][i] = IsEqual(); - eq[57][i].in[0] <== in[i]; - eq[57][i].in[1] <== 34; - eq[58][i] = IsEqual(); - eq[58][i].in[0] <== in[i]; - eq[58][i].in[1] <== 35; - eq[59][i] = IsEqual(); - eq[59][i].in[0] <== in[i]; - eq[59][i].in[1] <== 36; - eq[60][i] = IsEqual(); - eq[60][i].in[0] <== in[i]; - eq[60][i].in[1] <== 37; - eq[61][i] = IsEqual(); - eq[61][i].in[0] <== in[i]; - eq[61][i].in[1] <== 38; - eq[62][i] = IsEqual(); - eq[62][i].in[0] <== in[i]; - eq[62][i].in[1] <== 39; - eq[63][i] = IsEqual(); - eq[63][i].in[0] <== in[i]; - eq[63][i].in[1] <== 40; - eq[64][i] = IsEqual(); - eq[64][i].in[0] <== in[i]; - eq[64][i].in[1] <== 41; - eq[65][i] = IsEqual(); - eq[65][i].in[0] <== in[i]; - eq[65][i].in[1] <== 42; - eq[66][i] = IsEqual(); - eq[66][i].in[0] <== in[i]; - eq[66][i].in[1] <== 43; - eq[67][i] = IsEqual(); - eq[67][i].in[0] <== in[i]; - eq[67][i].in[1] <== 44; - eq[68][i] = IsEqual(); - eq[68][i].in[0] <== in[i]; - eq[68][i].in[1] <== 45; - eq[69][i] = IsEqual(); - eq[69][i].in[0] <== in[i]; - eq[69][i].in[1] <== 46; - eq[70][i] = IsEqual(); - eq[70][i].in[0] <== in[i]; - eq[70][i].in[1] <== 47; - eq[71][i] = IsEqual(); - eq[71][i].in[0] <== in[i]; - eq[71][i].in[1] <== 58; - eq[72][i] = IsEqual(); - eq[72][i].in[0] <== in[i]; - eq[72][i].in[1] <== 60; - eq[73][i] = IsEqual(); - eq[73][i].in[0] <== in[i]; - eq[73][i].in[1] <== 61; - eq[74][i] = IsEqual(); - eq[74][i].in[0] <== in[i]; - eq[74][i].in[1] <== 62; - eq[75][i] = IsEqual(); - eq[75][i].in[0] <== in[i]; - eq[75][i].in[1] <== 63; - eq[76][i] = IsEqual(); - eq[76][i].in[0] <== in[i]; - eq[76][i].in[1] <== 64; - and[14][i] = AND(); - and[14][i].a <== states[i][4]; - multi_or[3][i] = MultiOR(27); - multi_or[3][i].in[0] <== and[12][i].out; - multi_or[3][i].in[1] <== and[13][i].out; - multi_or[3][i].in[2] <== eq[52][i].out; - multi_or[3][i].in[3] <== eq[53][i].out; - multi_or[3][i].in[4] <== eq[54][i].out; - multi_or[3][i].in[5] <== eq[55][i].out; - multi_or[3][i].in[6] <== eq[56][i].out; - multi_or[3][i].in[7] <== eq[57][i].out; - multi_or[3][i].in[8] <== eq[58][i].out; - multi_or[3][i].in[9] <== eq[59][i].out; - multi_or[3][i].in[10] <== eq[60][i].out; - multi_or[3][i].in[11] <== eq[61][i].out; - multi_or[3][i].in[12] <== eq[62][i].out; - multi_or[3][i].in[13] <== eq[63][i].out; - multi_or[3][i].in[14] <== eq[64][i].out; - multi_or[3][i].in[15] <== eq[65][i].out; - multi_or[3][i].in[16] <== eq[66][i].out; - multi_or[3][i].in[17] <== eq[67][i].out; - multi_or[3][i].in[18] <== eq[68][i].out; - multi_or[3][i].in[19] <== eq[69][i].out; - multi_or[3][i].in[20] <== eq[70][i].out; - multi_or[3][i].in[21] <== eq[71][i].out; - multi_or[3][i].in[22] <== eq[72][i].out; - multi_or[3][i].in[23] <== eq[73][i].out; - multi_or[3][i].in[24] <== eq[74][i].out; - multi_or[3][i].in[25] <== eq[75][i].out; - multi_or[3][i].in[26] <== eq[76][i].out; - and[14][i].b <== multi_or[3][i].out; - lt[14][i] = LessThan(8); - lt[14][i].in[0] <== 64; - lt[14][i].in[1] <== in[i]; - lt[15][i] = LessThan(8); - lt[15][i].in[0] <== in[i]; - lt[15][i].in[1] <== 127; - and[15][i] = AND(); - and[15][i].a <== lt[14][i].out; - and[15][i].b <== lt[15][i].out; - eq[77][i] = IsEqual(); - eq[77][i].in[0] <== in[i]; - eq[77][i].in[1] <== 9; - eq[78][i] = IsEqual(); - eq[78][i].in[0] <== in[i]; - eq[78][i].in[1] <== 10; - eq[79][i] = IsEqual(); - eq[79][i].in[0] <== in[i]; - eq[79][i].in[1] <== 13; - eq[80][i] = IsEqual(); - eq[80][i].in[0] <== in[i]; - eq[80][i].in[1] <== 32; - eq[81][i] = IsEqual(); - eq[81][i].in[0] <== in[i]; - eq[81][i].in[1] <== 33; - eq[82][i] = IsEqual(); - eq[82][i].in[0] <== in[i]; - eq[82][i].in[1] <== 34; - eq[83][i] = IsEqual(); - eq[83][i].in[0] <== in[i]; - eq[83][i].in[1] <== 35; - eq[84][i] = IsEqual(); - eq[84][i].in[0] <== in[i]; - eq[84][i].in[1] <== 36; - eq[85][i] = IsEqual(); - eq[85][i].in[0] <== in[i]; - eq[85][i].in[1] <== 37; - eq[86][i] = IsEqual(); - eq[86][i].in[0] <== in[i]; - eq[86][i].in[1] <== 38; - eq[87][i] = IsEqual(); - eq[87][i].in[0] <== in[i]; - eq[87][i].in[1] <== 39; - eq[88][i] = IsEqual(); - eq[88][i].in[0] <== in[i]; - eq[88][i].in[1] <== 40; - eq[89][i] = IsEqual(); - eq[89][i].in[0] <== in[i]; - eq[89][i].in[1] <== 41; - eq[90][i] = IsEqual(); - eq[90][i].in[0] <== in[i]; - eq[90][i].in[1] <== 42; - eq[91][i] = IsEqual(); - eq[91][i].in[0] <== in[i]; - eq[91][i].in[1] <== 43; - eq[92][i] = IsEqual(); - eq[92][i].in[0] <== in[i]; - eq[92][i].in[1] <== 44; - eq[93][i] = IsEqual(); - eq[93][i].in[0] <== in[i]; - eq[93][i].in[1] <== 45; - eq[94][i] = IsEqual(); - eq[94][i].in[0] <== in[i]; - eq[94][i].in[1] <== 46; - eq[95][i] = IsEqual(); - eq[95][i].in[0] <== in[i]; - eq[95][i].in[1] <== 47; - eq[96][i] = IsEqual(); - eq[96][i].in[0] <== in[i]; - eq[96][i].in[1] <== 58; - eq[97][i] = IsEqual(); - eq[97][i].in[0] <== in[i]; - eq[97][i].in[1] <== 60; - eq[98][i] = IsEqual(); - eq[98][i].in[0] <== in[i]; - eq[98][i].in[1] <== 61; - eq[99][i] = IsEqual(); - eq[99][i].in[0] <== in[i]; - eq[99][i].in[1] <== 62; - eq[100][i] = IsEqual(); - eq[100][i].in[0] <== in[i]; - eq[100][i].in[1] <== 63; - eq[101][i] = IsEqual(); - eq[101][i].in[0] <== in[i]; - eq[101][i].in[1] <== 64; - and[16][i] = AND(); - and[16][i].a <== states[i][18]; - multi_or[4][i] = MultiOR(26); - multi_or[4][i].in[0] <== and[15][i].out; - multi_or[4][i].in[1] <== eq[77][i].out; - multi_or[4][i].in[2] <== eq[78][i].out; - multi_or[4][i].in[3] <== eq[79][i].out; - multi_or[4][i].in[4] <== eq[80][i].out; - multi_or[4][i].in[5] <== eq[81][i].out; - multi_or[4][i].in[6] <== eq[82][i].out; - multi_or[4][i].in[7] <== eq[83][i].out; - multi_or[4][i].in[8] <== eq[84][i].out; - multi_or[4][i].in[9] <== eq[85][i].out; - multi_or[4][i].in[10] <== eq[86][i].out; - multi_or[4][i].in[11] <== eq[87][i].out; - multi_or[4][i].in[12] <== eq[88][i].out; - multi_or[4][i].in[13] <== eq[89][i].out; - multi_or[4][i].in[14] <== eq[90][i].out; - multi_or[4][i].in[15] <== eq[91][i].out; - multi_or[4][i].in[16] <== eq[92][i].out; - multi_or[4][i].in[17] <== eq[93][i].out; - multi_or[4][i].in[18] <== eq[94][i].out; - multi_or[4][i].in[19] <== eq[95][i].out; - multi_or[4][i].in[20] <== eq[96][i].out; - multi_or[4][i].in[21] <== eq[97][i].out; - multi_or[4][i].in[22] <== eq[98][i].out; - multi_or[4][i].in[23] <== eq[99][i].out; - multi_or[4][i].in[24] <== eq[100][i].out; - multi_or[4][i].in[25] <== eq[101][i].out; - and[16][i].b <== multi_or[4][i].out; - lt[16][i] = LessThan(8); - lt[16][i].in[0] <== 64; - lt[16][i].in[1] <== in[i]; - lt[17][i] = LessThan(8); - lt[17][i].in[0] <== in[i]; - lt[17][i].in[1] <== 127; - and[17][i] = AND(); - and[17][i].a <== lt[16][i].out; - and[17][i].b <== lt[17][i].out; - eq[102][i] = IsEqual(); - eq[102][i].in[0] <== in[i]; - eq[102][i].in[1] <== 9; - eq[103][i] = IsEqual(); - eq[103][i].in[0] <== in[i]; - eq[103][i].in[1] <== 10; - eq[104][i] = IsEqual(); - eq[104][i].in[0] <== in[i]; - eq[104][i].in[1] <== 13; - eq[105][i] = IsEqual(); - eq[105][i].in[0] <== in[i]; - eq[105][i].in[1] <== 32; - eq[106][i] = IsEqual(); - eq[106][i].in[0] <== in[i]; - eq[106][i].in[1] <== 33; - eq[107][i] = IsEqual(); - eq[107][i].in[0] <== in[i]; - eq[107][i].in[1] <== 34; - eq[108][i] = IsEqual(); - eq[108][i].in[0] <== in[i]; - eq[108][i].in[1] <== 35; - eq[109][i] = IsEqual(); - eq[109][i].in[0] <== in[i]; - eq[109][i].in[1] <== 36; - eq[110][i] = IsEqual(); - eq[110][i].in[0] <== in[i]; - eq[110][i].in[1] <== 37; - eq[111][i] = IsEqual(); - eq[111][i].in[0] <== in[i]; - eq[111][i].in[1] <== 38; - eq[112][i] = IsEqual(); - eq[112][i].in[0] <== in[i]; - eq[112][i].in[1] <== 39; - eq[113][i] = IsEqual(); - eq[113][i].in[0] <== in[i]; - eq[113][i].in[1] <== 40; - eq[114][i] = IsEqual(); - eq[114][i].in[0] <== in[i]; - eq[114][i].in[1] <== 41; - eq[115][i] = IsEqual(); - eq[115][i].in[0] <== in[i]; - eq[115][i].in[1] <== 42; - eq[116][i] = IsEqual(); - eq[116][i].in[0] <== in[i]; - eq[116][i].in[1] <== 43; - eq[117][i] = IsEqual(); - eq[117][i].in[0] <== in[i]; - eq[117][i].in[1] <== 44; - eq[118][i] = IsEqual(); - eq[118][i].in[0] <== in[i]; - eq[118][i].in[1] <== 45; - eq[119][i] = IsEqual(); - eq[119][i].in[0] <== in[i]; - eq[119][i].in[1] <== 46; - eq[120][i] = IsEqual(); - eq[120][i].in[0] <== in[i]; - eq[120][i].in[1] <== 47; - eq[121][i] = IsEqual(); - eq[121][i].in[0] <== in[i]; - eq[121][i].in[1] <== 58; - eq[122][i] = IsEqual(); - eq[122][i].in[0] <== in[i]; - eq[122][i].in[1] <== 60; - eq[123][i] = IsEqual(); - eq[123][i].in[0] <== in[i]; - eq[123][i].in[1] <== 61; - eq[124][i] = IsEqual(); - eq[124][i].in[0] <== in[i]; - eq[124][i].in[1] <== 62; - eq[125][i] = IsEqual(); - eq[125][i].in[0] <== in[i]; - eq[125][i].in[1] <== 63; - eq[126][i] = IsEqual(); - eq[126][i].in[0] <== in[i]; - eq[126][i].in[1] <== 64; - and[18][i] = AND(); - and[18][i].a <== states[i][23]; - multi_or[5][i] = MultiOR(26); - multi_or[5][i].in[0] <== and[17][i].out; - multi_or[5][i].in[1] <== eq[102][i].out; - multi_or[5][i].in[2] <== eq[103][i].out; - multi_or[5][i].in[3] <== eq[104][i].out; - multi_or[5][i].in[4] <== eq[105][i].out; - multi_or[5][i].in[5] <== eq[106][i].out; - multi_or[5][i].in[6] <== eq[107][i].out; - multi_or[5][i].in[7] <== eq[108][i].out; - multi_or[5][i].in[8] <== eq[109][i].out; - multi_or[5][i].in[9] <== eq[110][i].out; - multi_or[5][i].in[10] <== eq[111][i].out; - multi_or[5][i].in[11] <== eq[112][i].out; - multi_or[5][i].in[12] <== eq[113][i].out; - multi_or[5][i].in[13] <== eq[114][i].out; - multi_or[5][i].in[14] <== eq[115][i].out; - multi_or[5][i].in[15] <== eq[116][i].out; - multi_or[5][i].in[16] <== eq[117][i].out; - multi_or[5][i].in[17] <== eq[118][i].out; - multi_or[5][i].in[18] <== eq[119][i].out; - multi_or[5][i].in[19] <== eq[120][i].out; - multi_or[5][i].in[20] <== eq[121][i].out; - multi_or[5][i].in[21] <== eq[122][i].out; - multi_or[5][i].in[22] <== eq[123][i].out; - multi_or[5][i].in[23] <== eq[124][i].out; - multi_or[5][i].in[24] <== eq[125][i].out; - multi_or[5][i].in[25] <== eq[126][i].out; - and[18][i].b <== multi_or[5][i].out; - multi_or[6][i] = MultiOR(4); - multi_or[6][i].in[0] <== and[11][i].out; - multi_or[6][i].in[1] <== and[14][i].out; - multi_or[6][i].in[2] <== and[16][i].out; - multi_or[6][i].in[3] <== and[18][i].out; - states[i+1][4] <== multi_or[6][i].out; - state_changed[i].in[3] <== states[i+1][4]; - eq[127][i] = IsEqual(); - eq[127][i].in[0] <== in[i]; - eq[127][i].in[1] <== 107; - and[19][i] = AND(); - and[19][i].a <== states[i][3]; - and[19][i].b <== eq[127][i].out; - states[i+1][5] <== and[19][i].out; - state_changed[i].in[4] <== states[i+1][5]; - eq[128][i] = IsEqual(); - eq[128][i].in[0] <== in[i]; - eq[128][i].in[1] <== 105; - and[20][i] = AND(); - and[20][i].a <== states[i][5]; - and[20][i].b <== eq[128][i].out; - states[i+1][6] <== and[20][i].out; - state_changed[i].in[5] <== states[i+1][6]; - eq[129][i] = IsEqual(); - eq[129][i].in[0] <== in[i]; - eq[129][i].in[1] <== 109; - and[21][i] = AND(); - and[21][i].a <== states[i][6]; - and[21][i].b <== eq[129][i].out; - states[i+1][7] <== and[21][i].out; - state_changed[i].in[6] <== states[i+1][7]; - eq[130][i] = IsEqual(); - eq[130][i].in[0] <== in[i]; - eq[130][i].in[1] <== 45; - and[22][i] = AND(); - and[22][i].a <== states[i][7]; - and[22][i].b <== eq[130][i].out; - states[i+1][8] <== and[22][i].out; - state_changed[i].in[7] <== states[i+1][8]; - eq[131][i] = IsEqual(); - eq[131][i].in[0] <== in[i]; - eq[131][i].in[1] <== 115; - and[23][i] = AND(); - and[23][i].a <== states[i][8]; - and[23][i].b <== eq[131][i].out; - states[i+1][9] <== and[23][i].out; - state_changed[i].in[8] <== states[i+1][9]; - eq[132][i] = IsEqual(); - eq[132][i].in[0] <== in[i]; - eq[132][i].in[1] <== 59; - and[24][i] = AND(); - and[24][i].a <== states[i][4]; - and[24][i].b <== eq[132][i].out; - states[i+1][10] <== and[24][i].out; - state_changed[i].in[9] <== states[i+1][10]; - eq[133][i] = IsEqual(); - eq[133][i].in[0] <== in[i]; - eq[133][i].in[1] <== 105; - and[25][i] = AND(); - and[25][i].a <== states[i][9]; - and[25][i].b <== eq[133][i].out; - states[i+1][11] <== and[25][i].out; - state_changed[i].in[10] <== states[i+1][11]; - eq[134][i] = IsEqual(); - eq[134][i].in[0] <== in[i]; - eq[134][i].in[1] <== 103; - and[26][i] = AND(); - and[26][i].a <== states[i][11]; - and[26][i].b <== eq[134][i].out; - states[i+1][12] <== and[26][i].out; - state_changed[i].in[11] <== states[i+1][12]; - eq[135][i] = IsEqual(); - eq[135][i].in[0] <== in[i]; - eq[135][i].in[1] <== 110; - and[27][i] = AND(); - and[27][i].a <== states[i][12]; - and[27][i].b <== eq[135][i].out; - states[i+1][13] <== and[27][i].out; - state_changed[i].in[12] <== states[i+1][13]; - eq[136][i] = IsEqual(); - eq[136][i].in[0] <== in[i]; - eq[136][i].in[1] <== 32; - and[28][i] = AND(); - and[28][i].a <== states[i][10]; - and[28][i].b <== eq[136][i].out; - eq[137][i] = IsEqual(); - eq[137][i].in[0] <== in[i]; - eq[137][i].in[1] <== 32; - and[29][i] = AND(); - and[29][i].a <== states[i][24]; - and[29][i].b <== eq[137][i].out; - multi_or[7][i] = MultiOR(2); - multi_or[7][i].in[0] <== and[28][i].out; - multi_or[7][i].in[1] <== and[29][i].out; - states[i+1][14] <== multi_or[7][i].out; - state_changed[i].in[13] <== states[i+1][14]; - eq[138][i] = IsEqual(); - eq[138][i].in[0] <== in[i]; - eq[138][i].in[1] <== 97; - and[30][i] = AND(); - and[30][i].a <== states[i][13]; - and[30][i].b <== eq[138][i].out; - states[i+1][15] <== and[30][i].out; - state_changed[i].in[14] <== states[i+1][15]; - eq[139][i] = IsEqual(); - eq[139][i].in[0] <== in[i]; - eq[139][i].in[1] <== 116; - and[31][i] = AND(); - and[31][i].a <== states[i][14]; - and[31][i].b <== eq[139][i].out; - states[i+1][16] <== and[31][i].out; - state_changed[i].in[15] <== states[i+1][16]; - eq[140][i] = IsEqual(); - eq[140][i].in[0] <== in[i]; - eq[140][i].in[1] <== 116; - and[32][i] = AND(); - and[32][i].a <== states[i][15]; - and[32][i].b <== eq[140][i].out; - states[i+1][17] <== and[32][i].out; - state_changed[i].in[16] <== states[i+1][17]; - eq[141][i] = IsEqual(); - eq[141][i].in[0] <== in[i]; - eq[141][i].in[1] <== 61; - and[33][i] = AND(); - and[33][i].a <== states[i][16]; - and[33][i].b <== eq[141][i].out; - states[i+1][18] <== and[33][i].out; - state_changed[i].in[17] <== states[i+1][18]; - eq[142][i] = IsEqual(); - eq[142][i].in[0] <== in[i]; - eq[142][i].in[1] <== 117; - and[34][i] = AND(); - and[34][i].a <== states[i][17]; - and[34][i].b <== eq[142][i].out; - states[i+1][19] <== and[34][i].out; - state_changed[i].in[18] <== states[i+1][19]; - eq[143][i] = IsEqual(); - eq[143][i].in[0] <== in[i]; - eq[143][i].in[1] <== 114; - and[35][i] = AND(); - and[35][i].a <== states[i][19]; - and[35][i].b <== eq[143][i].out; - states[i+1][20] <== and[35][i].out; - state_changed[i].in[19] <== states[i+1][20]; - eq[144][i] = IsEqual(); - eq[144][i].in[0] <== in[i]; - eq[144][i].in[1] <== 101; - and[36][i] = AND(); - and[36][i].a <== states[i][20]; - and[36][i].b <== eq[144][i].out; - states[i+1][21] <== and[36][i].out; - state_changed[i].in[20] <== states[i+1][21]; - eq[145][i] = IsEqual(); - eq[145][i].in[0] <== in[i]; - eq[145][i].in[1] <== 58; - and[37][i] = AND(); - and[37][i].a <== states[i][21]; - and[37][i].b <== eq[145][i].out; - states[i+1][22] <== and[37][i].out; - state_changed[i].in[21] <== states[i+1][22]; - lt[18][i] = LessThan(8); - lt[18][i].in[0] <== 47; - lt[18][i].in[1] <== in[i]; - lt[19][i] = LessThan(8); - lt[19][i].in[0] <== in[i]; - lt[19][i].in[1] <== 58; - and[38][i] = AND(); - and[38][i].a <== lt[18][i].out; - and[38][i].b <== lt[19][i].out; - and[39][i] = AND(); - and[39][i].a <== states[i][18]; - and[39][i].b <== and[38][i].out; - lt[20][i] = LessThan(8); - lt[20][i].in[0] <== 47; - lt[20][i].in[1] <== in[i]; - lt[21][i] = LessThan(8); - lt[21][i].in[0] <== in[i]; - lt[21][i].in[1] <== 58; - and[40][i] = AND(); - and[40][i].a <== lt[20][i].out; - and[40][i].b <== lt[21][i].out; - and[41][i] = AND(); - and[41][i].a <== states[i][23]; - and[41][i].b <== and[40][i].out; - multi_or[8][i] = MultiOR(2); - multi_or[8][i].in[0] <== and[39][i].out; - multi_or[8][i].in[1] <== and[41][i].out; - states[i+1][23] <== multi_or[8][i].out; - state_changed[i].in[22] <== states[i+1][23]; - eq[146][i] = IsEqual(); - eq[146][i].in[0] <== in[i]; - eq[146][i].in[1] <== 59; - and[42][i] = AND(); - and[42][i].a <== states[i][23]; - and[42][i].b <== eq[146][i].out; - states[i+1][24] <== and[42][i].out; - state_changed[i].in[23] <== states[i+1][24]; - states[i+1][0] <== 1 - state_changed[i].out; - } - - component final_state_result = MultiOR(num_bytes+1); - for (var i = 0; i <= num_bytes; i++) { - final_state_result.in[i] <== states[i][24]; - } - out <== final_state_result.out; - - signal is_consecutive[msg_bytes+1][2]; - is_consecutive[msg_bytes][1] <== 1; - for (var i = 0; i < msg_bytes; i++) { - is_consecutive[msg_bytes-1-i][0] <== states[num_bytes-i][24] * (1 - is_consecutive[msg_bytes-i][1]) + is_consecutive[msg_bytes-i][1]; - is_consecutive[msg_bytes-1-i][1] <== state_changed[msg_bytes-i].out * is_consecutive[msg_bytes-1-i][0]; - } - signal is_substr0[msg_bytes][3]; - signal is_reveal0[msg_bytes]; - signal output reveal0[msg_bytes]; - for (var i = 0; i < msg_bytes; i++) { - is_substr0[i][0] <== 0; - is_substr0[i][1] <== is_substr0[i][0] + states[i+1][23] * states[i+2][23]; - is_substr0[i][2] <== is_substr0[i][1] + states[i+1][18] * states[i+2][23]; - is_reveal0[i] <== is_substr0[i][2] * is_consecutive[i][1]; - reveal0[i] <== in[i+1] * is_reveal0[i]; - } -} \ No newline at end of file diff --git a/packages/circom/circuits/common/timestamp.json b/packages/circom/circuits/common/timestamp.json index de7d1bd..252cc10 100644 --- a/packages/circom/circuits/common/timestamp.json +++ b/packages/circom/circuits/common/timestamp.json @@ -8,8 +8,8 @@ }, { "is_public": false, - "regex_def": "((a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)+=(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|!|\"|#|$|%|&|\\'|\\(|\\)|\\*|\\+|,|-|\\.|\\/|:|<|=|>|\\?|@|\\[|\\\\|\\]|\\^|_|`|{|\\||}|~| |\t|\n|\r|\\x0b|\\x0c)+; )+t=", - "max_size": 128 + "regex_def": "((a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)+=[^;]+; )+t=", + "max_size": 1024 }, { "is_public": true, diff --git a/packages/circom/tests/circuits/negate1.json b/packages/circom/tests/circuits/negate1.json new file mode 100644 index 0000000..551b70b --- /dev/null +++ b/packages/circom/tests/circuits/negate1.json @@ -0,0 +1,20 @@ +{ + "max_byte_size": 64, + "parts": [ + { + "is_public": false, + "regex_def": "a:", + "max_size": 2 + }, + { + "is_public": true, + "regex_def": "[^abcdefghijklmnopqrstuvwxyz]+", + "max_size": 128 + }, + { + "is_public": false, + "regex_def": ".", + "max_size": 1 + } + ] +} \ No newline at end of file diff --git a/packages/circom/tests/circuits/negate1_regex.circom b/packages/circom/tests/circuits/negate1_regex.circom new file mode 100644 index 0000000..da56d7e --- /dev/null +++ b/packages/circom/tests/circuits/negate1_regex.circom @@ -0,0 +1,114 @@ +pragma circom 2.1.5; + +include "zk-regex-circom/circuits/regex_helpers.circom"; + +template Negate1Regex(msg_bytes) { + signal input msg[msg_bytes]; + signal output out; + + var num_bytes = msg_bytes+1; + signal in[num_bytes]; + in[0]<==128; + for (var i = 0; i < msg_bytes; i++) { + in[i+1] <== msg[i]; + } + + component eq[4][num_bytes]; + component lt[4][num_bytes]; + component and[7][num_bytes]; + component multi_or[2][num_bytes]; + signal states[num_bytes+1][5]; + component state_changed[num_bytes]; + + states[0][0] <== 1; + for (var i = 1; i < 5; i++) { + states[0][i] <== 0; + } + + for (var i = 0; i < num_bytes; i++) { + state_changed[i] = MultiOR(4); + lt[0][i] = LessThan(8); + lt[0][i].in[0] <== 96; + lt[0][i].in[1] <== in[i]; + lt[1][i] = LessThan(8); + lt[1][i].in[0] <== in[i]; + lt[1][i].in[1] <== 123; + and[0][i] = AND(); + and[0][i].a <== lt[0][i].out; + and[0][i].b <== lt[1][i].out; + eq[0][i] = IsEqual(); + eq[0][i].in[0] <== in[i]; + eq[0][i].in[1] <== 46; + and[1][i] = AND(); + and[1][i].a <== states[i][1]; + multi_or[0][i] = MultiOR(2); + multi_or[0][i].in[0] <== and[0][i].out; + multi_or[0][i].in[1] <== eq[0][i].out; + and[1][i].b <== 1 - multi_or[0][i].out; + lt[2][i] = LessThan(8); + lt[2][i].in[0] <== 96; + lt[2][i].in[1] <== in[i]; + lt[3][i] = LessThan(8); + lt[3][i].in[0] <== in[i]; + lt[3][i].in[1] <== 123; + and[2][i] = AND(); + and[2][i].a <== lt[2][i].out; + and[2][i].b <== lt[3][i].out; + and[3][i] = AND(); + and[3][i].a <== states[i][4]; + and[3][i].b <== 1 - and[2][i].out; + multi_or[1][i] = MultiOR(2); + multi_or[1][i].in[0] <== and[1][i].out; + multi_or[1][i].in[1] <== and[3][i].out; + states[i+1][1] <== multi_or[1][i].out; + state_changed[i].in[0] <== states[i+1][1]; + eq[1][i] = IsEqual(); + eq[1][i].in[0] <== in[i]; + eq[1][i].in[1] <== 46; + and[4][i] = AND(); + and[4][i].a <== states[i][1]; + and[4][i].b <== eq[1][i].out; + states[i+1][2] <== and[4][i].out; + state_changed[i].in[1] <== states[i+1][2]; + eq[2][i] = IsEqual(); + eq[2][i].in[0] <== in[i]; + eq[2][i].in[1] <== 97; + and[5][i] = AND(); + and[5][i].a <== states[i][0]; + and[5][i].b <== eq[2][i].out; + states[i+1][3] <== and[5][i].out; + state_changed[i].in[2] <== states[i+1][3]; + eq[3][i] = IsEqual(); + eq[3][i].in[0] <== in[i]; + eq[3][i].in[1] <== 58; + and[6][i] = AND(); + and[6][i].a <== states[i][3]; + and[6][i].b <== eq[3][i].out; + states[i+1][4] <== and[6][i].out; + state_changed[i].in[3] <== states[i+1][4]; + states[i+1][0] <== 1 - state_changed[i].out; + } + + component final_state_result = MultiOR(num_bytes+1); + for (var i = 0; i <= num_bytes; i++) { + final_state_result.in[i] <== states[i][2]; + } + out <== final_state_result.out; + + signal is_consecutive[msg_bytes+1][2]; + is_consecutive[msg_bytes][1] <== 1; + for (var i = 0; i < msg_bytes; i++) { + is_consecutive[msg_bytes-1-i][0] <== states[num_bytes-i][2] * (1 - is_consecutive[msg_bytes-i][1]) + is_consecutive[msg_bytes-i][1]; + is_consecutive[msg_bytes-1-i][1] <== state_changed[msg_bytes-i].out * is_consecutive[msg_bytes-1-i][0]; + } + signal is_substr0[msg_bytes][3]; + signal is_reveal0[msg_bytes]; + signal output reveal0[msg_bytes]; + for (var i = 0; i < msg_bytes; i++) { + is_substr0[i][0] <== 0; + is_substr0[i][1] <== is_substr0[i][0] + states[i+1][4] * states[i+2][1]; + is_substr0[i][2] <== is_substr0[i][1] + states[i+1][1] * states[i+2][1]; + is_reveal0[i] <== is_substr0[i][2] * is_consecutive[i][1]; + reveal0[i] <== in[i+1] * is_reveal0[i]; + } +} \ No newline at end of file diff --git a/packages/circom/tests/circuits/test_negate1_regex.circom b/packages/circom/tests/circuits/test_negate1_regex.circom new file mode 100644 index 0000000..79cb526 --- /dev/null +++ b/packages/circom/tests/circuits/test_negate1_regex.circom @@ -0,0 +1,2 @@ +include "./negate1_regex.circom"; +component main = Negate1Regex(64); \ No newline at end of file diff --git a/packages/circom/tests/negate_regex.test.ts b/packages/circom/tests/negate_regex.test.ts new file mode 100644 index 0000000..556c57b --- /dev/null +++ b/packages/circom/tests/negate_regex.test.ts @@ -0,0 +1,110 @@ +const ff = require('ffjavascript'); +const stringifyBigInts = ff.utils.stringifyBigInts; +const circom_tester = require("circom_tester"); +const wasm_tester = circom_tester.wasm; +import * as path from "path"; +const p = "21888242871839275222246405745257275088548364400416034343698204186575808495617"; +const field = new ff.F1Field(p); +const apis = require("../../apis"); +const option = { + include: path.join(__dirname, "../../../node_modules") +}; + +jest.setTimeout(120000); +describe("Negate Regex", () => { + it("case 1 with regex 1", async () => { + const input = "a: ABCDEFG XYZ."; + const paddedStr = apis.padString(input, 64); + const circuitInputs = { + msg: paddedStr, + }; + const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); + const witness = await circuit.calculateWitness(circuitInputs); + await circuit.checkConstraints(witness); + // console.log(witness); + expect(1n).toEqual(witness[1]); + const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]]; + for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { + for (let idx = 0; idx < 64; ++idx) { + if (revealedIdx[substr_idx].includes(idx)) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); + } else { + expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); + } + } + } + }); + + it("case 2 with regex 1", async () => { + // Spanish character "í" has 2 bytes. + const input = "a: CRIPTOGRAFíA."; + const paddedStr = apis.padString(input, 64); + const circuitInputs = { + msg: paddedStr, + }; + const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); + const witness = await circuit.calculateWitness(circuitInputs); + await circuit.checkConstraints(witness); + // console.log(witness); + expect(1n).toEqual(witness[1]); + const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]; + for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { + for (let idx = 0; idx < 64; ++idx) { + if (revealedIdx[substr_idx].includes(idx)) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); + } else { + expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); + } + } + } + }); + + it("case 3 with regex 1", async () => { + /// Each Japanese character has 3 bytes. + const input = "a: あいう."; + const paddedStr = apis.padString(input, 64); + const circuitInputs = { + msg: paddedStr, + }; + const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); + const witness = await circuit.calculateWitness(circuitInputs); + await circuit.checkConstraints(witness); + // console.log(witness); + expect(1n).toEqual(witness[1]); + const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]; + for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { + for (let idx = 0; idx < 64; ++idx) { + if (revealedIdx[substr_idx].includes(idx)) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); + } else { + expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); + } + } + } + }); + + it("case 4 with regex 1", async () => { + /// Arabian character "التشفير" has 14 bytes. + const input = "a: التشفير."; + const paddedStr = apis.padString(input, 64); + const circuitInputs = { + msg: paddedStr, + }; + const circuit = await wasm_tester(path.join(__dirname, "./circuits/test_negate1_regex.circom"), option); + const witness = await circuit.calculateWitness(circuitInputs); + await circuit.checkConstraints(witness); + console.log(witness); + console.log(paddedStr); + expect(1n).toEqual(witness[1]); + const revealedIdx = [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]; + for (let substr_idx = 0; substr_idx < 1; ++substr_idx) { + for (let idx = 0; idx < 64; ++idx) { + if (revealedIdx[substr_idx].includes(idx)) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + 64 * substr_idx + idx]); + } else { + expect(0n).toEqual(witness[2 + 64 * substr_idx + idx]); + } + } + } + }); +}); \ No newline at end of file diff --git a/packages/compiler/src/gen_circom.js b/packages/compiler/src/gen_circom.js index 55d2c49..57f28a5 100644 --- a/packages/compiler/src/gen_circom.js +++ b/packages/compiler/src/gen_circom.js @@ -68,22 +68,20 @@ function genCircomAllstr(graph_json, template_name) { lines.push(`\t\tstate_changed[i] = MultiOR(${N - 1});`); for (let i = 1; i < N; i++) { const outputs = []; - let is_negates = []; + // let is_negates = []; for (let prev_i of Object.keys(rev_graph[i])) { const k = rev_graph[i][prev_i]; const eq_outputs = []; let vals = new Set(k); - // let is_negate = false; + let is_negate = false; if (vals.has(0xff)) { vals.delete(0xff); - is_negates.push(true); - } else { - is_negates.push(false); + is_negate = true; } if (vals.size === 0) { continue; } - if (is_negates[is_negates.length - 1] === true) { + if (is_negate === true) { for (let another_i = 1; another_i < N; another_i++) { if (i === another_i) { continue; @@ -156,13 +154,21 @@ function genCircomAllstr(graph_json, template_name) { lines.push(`\t\tand[${and_i}][i] = AND();`); lines.push(`\t\tand[${and_i}][i].a <== states[i][${prev_i}];`); if (eq_outputs.length === 1) { - lines.push(`\t\tand[${and_i}][i].b <== ${eq_outputs[0][0]}[${eq_outputs[0][1]}][i].out;`); + if (is_negate) { + lines.push(`\t\tand[${and_i}][i].b <== 1 - ${eq_outputs[0][0]}[${eq_outputs[0][1]}][i].out;`); + } else { + lines.push(`\t\tand[${and_i}][i].b <== ${eq_outputs[0][0]}[${eq_outputs[0][1]}][i].out;`); + } } else if (eq_outputs.length > 1) { lines.push(`\t\tmulti_or[${multi_or_i}][i] = MultiOR(${eq_outputs.length});`); for (let output_i = 0; output_i < eq_outputs.length; output_i++) { lines.push(`\t\tmulti_or[${multi_or_i}][i].in[${output_i}] <== ${eq_outputs[output_i][0]}[${eq_outputs[output_i][1]}][i].out;`); } - lines.push(`\t\tand[${and_i}][i].b <== multi_or[${multi_or_i}][i].out;`); + if (is_negate) { + lines.push(`\t\tand[${and_i}][i].b <== 1 - multi_or[${multi_or_i}][i].out;`); + } else { + lines.push(`\t\tand[${and_i}][i].b <== multi_or[${multi_or_i}][i].out;`); + } multi_or_i += 1 } @@ -171,19 +177,11 @@ function genCircomAllstr(graph_json, template_name) { } if (outputs.length === 1) { - if (is_negates[0]) { - lines.push(`\t\tstates[i+1][${i}] <== 1 - and[${outputs[0]}][i].out;`); - } else { - lines.push(`\t\tstates[i+1][${i}] <== and[${outputs[0]}][i].out;`); - } + lines.push(`\t\tstates[i+1][${i}] <== and[${outputs[0]}][i].out;`); } else if (outputs.length > 1) { lines.push(`\t\tmulti_or[${multi_or_i}][i] = MultiOR(${outputs.length});`); for (let output_i = 0; output_i < outputs.length; output_i++) { - if (is_negates[output_i]) { - lines.push(`\t\tmulti_or[${multi_or_i}][i].in[${output_i}] <== 1 - and[${outputs[output_i]}][i].out;`); - } else { - lines.push(`\t\tmulti_or[${multi_or_i}][i].in[${output_i}] <== and[${outputs[output_i]}][i].out;`); - } + lines.push(`\t\tmulti_or[${multi_or_i}][i].in[${output_i}] <== and[${outputs[output_i]}][i].out;`); } lines.push(`\t\tstates[i+1][${i}] <== multi_or[${multi_or_i}][i].out;`); multi_or_i += 1 diff --git a/packages/compiler/src/lib.rs b/packages/compiler/src/lib.rs index afdf692..24e1b90 100644 --- a/packages/compiler/src/lib.rs +++ b/packages/compiler/src/lib.rs @@ -144,10 +144,7 @@ impl DecomposedRegexConfig { if config.is_public { public_config_indexes.push(idx); } - let this_regex = config - .regex_def - .replace("^", "\\^") - .replace("[^", "[\u{ff}"); + let this_regex = config.regex_def.replace("^", "\\^").replace("[^", "["); if idx == 0 { part_regexes.push(Regex::new(&this_regex)?); } else { @@ -450,13 +447,21 @@ pub(crate) fn add_graph_nodes( continue; } } - let key_list: Vec = serde_json::from_str(&key)?; + let key_list: Vec = serde_json::from_str::>(&key)? + .iter() + .filter(|s| s.as_str() != "\u{ff}") + .cloned() + .collect_vec(); // let mut key_str = String::new(); // for key_char in key_list.iter() { // // println!("key_char {}", key_char); // assert!(key_char.len() == 1); // // key_str += key_char; // } + // println!("key_list {:?}", key_list); + if key_list.len() == 0 { + continue; + } assert_eq!(key_list[0].as_bytes().len(), 1); graph.add_edge( NodeIndex::from(next_node),