@@ -6,18 +6,18 @@ include "hash_machine.circom";
66template JSONExtraction (DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH ) {
77 signal input data[DATA_BYTES];
88 signal input ciphertext_digest;
9- signal input sequence_digest;
9+ signal input sequence_digest; // todo(sambhav): should sequence digest be 0 for first json circuit?
1010 signal input value_digest;
11- signal input state[MAX_STACK_HEIGHT * 4 + 3 ];
11+ signal input state[MAX_STACK_HEIGHT * 4 + 4 ];
1212
1313 signal input step_in[PUBLIC_IO_LENGTH];
1414 signal output step_out[PUBLIC_IO_LENGTH];
1515
1616 // --------------------------------------------------------------------------------------------//
1717
1818 // assertions:
19- step_in[5 ] === 0 ; // HTTP statements matched
20- signal input_state_digest <== PolynomialDigest(MAX_STACK_HEIGHT * 4 + 3 )(state, ciphertext_digest);
19+ // step_in[5] === 0; // HTTP statements matched // TODO: either remove this or send a public io var
20+ signal input_state_digest <== PolynomialDigest(MAX_STACK_HEIGHT * 4 + 4 )(state, ciphertext_digest);
2121 step_in[8 ] === input_state_digest;
2222 signal sequence_digest_hashed <== Poseidon(1 )([sequence_digest]);
2323 step_in[9 ] === sequence_digest_hashed;
@@ -34,18 +34,6 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
3434 signal intermediate_digest[DATA_BYTES][3 * MAX_STACK_HEIGHT];
3535 signal state_digest[DATA_BYTES];
3636
37- // Debugging
38- // for(var i = 0; i<MAX_STACK_HEIGHT; i++) {
39- // log("State[", 0, "].next_stack[", i,"] = [",State[0].next_stack[i][0], "][", State[0].next_stack[i][1],"]" );
40- // }
41- // for(var i = 0; i<MAX_STACK_HEIGHT; i++) {
42- // log("State[", 0, "].next_tree_hash[", i,"] = [",State[0].next_tree_hash[i][0], "][", State[0].next_tree_hash[i][1],"]" );
43- // }
44- // log("State[", 0, "].next_monomial =", State[0].next_monomial);
45- // log("State[", 0, "].next_parsing_string =", State[0].next_parsing_string);
46- // log("State[", 0, "].next_parsing_number =", State[0].next_parsing_number);
47- // log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
48-
4937 var total_matches = 0 ;
5038 signal sequence_is_matched[DATA_BYTES];
5139 signal value_is_matched[DATA_BYTES];
@@ -61,7 +49,8 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
6149 State[0 ].polynomial_input <== ciphertext_digest;
6250 State[0 ].monomial <== state[MAX_STACK_HEIGHT* 4 ];
6351 State[0 ].parsing_string <== state[MAX_STACK_HEIGHT* 4 + 1 ];
64- State[0 ].parsing_number <== state[MAX_STACK_HEIGHT* 4 + 2 ];
52+ State[0 ].parsing_primitive <== state[MAX_STACK_HEIGHT* 4 + 2 ];
53+ State[0 ].escaped <== state[MAX_STACK_HEIGHT* 4 + 3 ];
6554 } else {
6655 State[data_idx] = StateUpdateHasher(MAX_STACK_HEIGHT);
6756 State[data_idx].byte <== data[data_idx];
@@ -70,7 +59,8 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
7059 State[data_idx].tree_hash <== State[data_idx - 1 ].next_tree_hash;
7160 State[data_idx].monomial <== State[data_idx - 1 ].next_monomial;
7261 State[data_idx].parsing_string <== State[data_idx - 1 ].next_parsing_string;
73- State[data_idx].parsing_number <== State[data_idx - 1 ].next_parsing_number;
62+ State[data_idx].parsing_primitive <== State[data_idx - 1 ].next_parsing_primitive;
63+ State[data_idx].escaped <== State[data_idx - 1 ].next_escaped;
7464 }
7565
7666 // Digest the whole stack and key tree hash
@@ -104,14 +94,15 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
10494 // }
10595 // log("State[", data_idx, "].next_monomial =", State[data_idx].next_monomial);
10696 // log("State[", data_idx, "].next_parsing_string =", State[data_idx].next_parsing_string);
107- // log("State[", data_idx, "].next_parsing_number =", State[data_idx].next_parsing_number);
97+ // log("State[", data_idx, "].next_parsing_primitive =", State[data_idx].next_parsing_primitive);
98+ // log("State[", data_idx, "].next_escaped =", State[data_idx].next_escaped);
10899 // log("++++++++++++++++++++++++++++++++++++++++++++++++");
109100 // log("state_digest[", data_idx,"] = ", state_digest[data_idx]);
110101 // log("total_matches = ", total_matches);
111102 // log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
112103 }
113104
114- signal new_state[MAX_STACK_HEIGHT* 4 + 3 ];
105+ signal new_state[MAX_STACK_HEIGHT* 4 + 4 ];
115106 for (var i = 0 ; i < MAX_STACK_HEIGHT; i++ ) {
116107 new_state[i* 2 ] <== State[DATA_BYTES - 1 ].next_stack[i][0 ];
117108 new_state[i* 2 + 1 ] <== State[DATA_BYTES - 1 ].next_stack[i][1 ];
@@ -120,11 +111,12 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
120111 }
121112 new_state[MAX_STACK_HEIGHT* 4 ] <== State[DATA_BYTES - 1 ].next_monomial;
122113 new_state[MAX_STACK_HEIGHT* 4 + 1 ] <== State[DATA_BYTES - 1 ].next_parsing_string;
123- new_state[MAX_STACK_HEIGHT* 4 + 2 ] <== State[DATA_BYTES - 1 ].next_parsing_number;
124- signal new_state_digest <== PolynomialDigest(MAX_STACK_HEIGHT * 4 + 3 )(new_state, ciphertext_digest);
114+ new_state[MAX_STACK_HEIGHT* 4 + 2 ] <== State[DATA_BYTES - 1 ].next_parsing_primitive;
115+ new_state[MAX_STACK_HEIGHT* 4 + 3 ] <== State[DATA_BYTES - 1 ].next_escaped;
116+ signal new_state_digest <== PolynomialDigest(MAX_STACK_HEIGHT * 4 + 4 )(new_state, ciphertext_digest);
125117
126- // for (var i = 0 ; i < MAX_STACK_HEIGHT * 4 + 3 ; i++) {
127- // log("new_state[", i, "] = ", new_state[i]);
118+ // for (var i = 0 ; i < MAX_STACK_HEIGHT * 2 + 2 ; i++) {
119+ // log("new_state[", i, "] = ", new_state[i*2], new_state[i*2 + 1 ]);
128120 // }
129121
130122 // Verify we have now processed all the data properly
@@ -145,10 +137,12 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
145137 // Set the output to the digest of the intended value
146138 step_out[0 ] <== step_in[0 ] - data_digest + value_digest * total_matches;
147139
140+ // value_digest should be non-zero
141+ signal is_value_digest_zero <== IsEqual()([value_digest, 0 ]);
148142 // both should be 0 or 1 together
149143 signal is_new_state_digest_zero <== IsEqual()([new_state_digest, 0 ]);
150144 signal is_step_out_zero_matched <== IsEqual()([step_out[0 ], value_digest]);
151- 0 === is_new_state_digest_zero - is_step_out_zero_matched; // verify final value matches
145+ 0 === ( 1 - is_value_digest_zero) * ( is_new_state_digest_zero - is_step_out_zero_matched) ; // verify final value matches
152146
153147 step_out[1 ] <== step_in[1 ];
154148 step_out[2 ] <== step_in[2 ];
@@ -160,9 +154,8 @@ template JSONExtraction(DATA_BYTES, MAX_STACK_HEIGHT, PUBLIC_IO_LENGTH) {
160154 step_out[8 ] <== new_state_digest;
161155 step_out[9 ] <== step_in[9 ];
162156 step_out[10 ] <== step_in[10 ];
163- for (var i = 11 ; i < PUBLIC_IO_LENGTH ; i++ ) {
164- step_out[i] <== step_in[i];
165- }
157+
158+ step_out[1 ] === step_out[2 ]; // assert http and plaintext parsed same amount
166159
167160 // for (var i = 0 ; i < PUBLIC_IO_LENGTH ; i++) {
168161 // log("step_out[", i, "] = ", step_out[i]);
0 commit comments