1
1
//! Contains methods used to generate tables in `json_tables.nr`. These table generation methods shouldn't be used inside of actual circuits.
2
2
use crate::enums::CaptureMode::STRING_CAPTURE ;
3
- use crate::enums::Layer:: {ARRAY_LAYER , OBJECT_LAYER , SINGLE_VALUE_LAYER };
3
+ use crate::enums::Layer:: {ARRAY_LAYER , OBJECT_LAYER };
4
4
use crate::enums::Token:: {
5
5
BEGIN_ARRAY_TOKEN , BEGIN_OBJECT_TOKEN , END_ARRAY_TOKEN , END_OBJECT_TOKEN , KEY_SEPARATOR_TOKEN ,
6
6
KEY_TOKEN , LITERAL_TOKEN , NO_TOKEN , NUM_TOKENS , NUMERIC_TOKEN , STRING_TOKEN ,
@@ -60,32 +60,32 @@ unconstrained fn make_ascii_to_token_table() -> [Field; 1024] {
60
60
result
61
61
}
62
62
63
- unconstrained fn make_token_validation_table () -> [Field ; NUM_TOKENS * NUM_TOKENS * 3 ] {
63
+ unconstrained fn make_token_validation_table () -> [Field ; NUM_TOKENS * NUM_TOKENS * 2 ] {
64
64
// index = layer type, current token and next token
65
65
// output is layer type
66
- // 11 tokens , 3 layers = 11 * 11 * 3 = 121 * 3 = 343
66
+ // 11 tokens , 2 layers = 11 * 11 * 2 = 121 * 2 = 242
67
67
// object contexts
68
- let no_change = ValidationFlags { push_layer : 0 , push_layer_type_of_root : 0 , pop_layer : 0 };
69
- let error_flags =
70
- ValidationFlags { push_layer : 0x1000000 , push_layer_type_of_root : 0 , pop_layer : 0 } ;
68
+ let no_change =
69
+ ValidationFlags { push_layer : false , push_layer_type_of_root : false , pop_layer : false };
70
+ let error_flags_field = 0x1000000 ;
71
71
let begin_new_object_flags = ValidationFlags {
72
- push_layer : 1 ,
73
- push_layer_type_of_root : OBJECT_LAYER as Field ,
74
- pop_layer : 0 ,
72
+ push_layer : true ,
73
+ push_layer_type_of_root : OBJECT_LAYER != 0 ,
74
+ pop_layer : false ,
75
75
};
76
76
let begin_new_array_flags = ValidationFlags {
77
- push_layer : 1 ,
78
- push_layer_type_of_root : ARRAY_LAYER as Field ,
79
- pop_layer : 0 ,
77
+ push_layer : true ,
78
+ push_layer_type_of_root : ARRAY_LAYER != 0 ,
79
+ pop_layer : false ,
80
80
};
81
81
let end_object_or_array_flags : ValidationFlags =
82
- ValidationFlags { push_layer : 0 , push_layer_type_of_root : 0 , pop_layer : 1 };
82
+ ValidationFlags { push_layer : false , push_layer_type_of_root : false , pop_layer : true };
83
83
84
84
let token_ids : [u32 ; NUM_TOKENS ] = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
85
85
86
- let error_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|_ | error_flags . to_field () );
86
+ let error_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|_ | error_flags_field );
87
87
let object_layer_begin_object_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
88
- let mut result = error_flags . to_field () ;
88
+ let mut result = error_flags_field ;
89
89
if (token == KEY_TOKEN ) {
90
90
result = no_change .to_field ();
91
91
}
@@ -98,13 +98,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
98
98
let object_layer_key_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
99
99
let mut result = no_change .to_field ();
100
100
if (token != KEY_SEPARATOR_TOKEN ) {
101
- result = error_flags . to_field () ;
101
+ result = error_flags_field ;
102
102
}
103
103
result
104
104
});
105
105
106
106
let object_layer_key_separator_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
107
- let mut result = error_flags . to_field () ;
107
+ let mut result = error_flags_field ;
108
108
if (token == STRING_TOKEN ) | (token == LITERAL_TOKEN ) | (token == NUMERIC_TOKEN ) {
109
109
result = no_change .to_field ();
110
110
}
@@ -118,7 +118,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
118
118
});
119
119
120
120
let object_layer_value_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
121
- let mut result = error_flags . to_field () ;
121
+ let mut result = error_flags_field ;
122
122
if (token == VALUE_SEPARATOR_TOKEN ) {
123
123
result = no_change .to_field ();
124
124
}
@@ -129,7 +129,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
129
129
});
130
130
131
131
let object_layer_end_object_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
132
- let mut result = error_flags . to_field () ;
132
+ let mut result = error_flags_field ;
133
133
if (token == VALUE_SEPARATOR_TOKEN ) {
134
134
result = no_change .to_field ();
135
135
}
@@ -144,7 +144,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
144
144
});
145
145
146
146
let object_layer_value_separator_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
147
- let mut result = error_flags . to_field () ;
147
+ let mut result = error_flags_field ;
148
148
if (token == KEY_TOKEN ) {
149
149
result = no_change .to_field ();
150
150
}
@@ -153,11 +153,8 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
153
153
154
154
let mut object_layer_flags : [[Field ; NUM_TOKENS ]; NUM_TOKENS ] = [[0 ; NUM_TOKENS ]; NUM_TOKENS ];
155
155
let mut array_layer_flags : [[Field ; NUM_TOKENS ]; NUM_TOKENS ] = [[0 ; NUM_TOKENS ]; NUM_TOKENS ];
156
- let mut single_value_layer_flags : [[Field ; NUM_TOKENS ]; NUM_TOKENS ] =
157
- [[0 ; NUM_TOKENS ]; NUM_TOKENS ];
158
-
159
156
let no_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
160
- let mut result = error_flags . to_field () ;
157
+ let mut result = error_flags_field ;
161
158
if (token == NO_TOKEN ) {
162
159
result = no_change .to_field ();
163
160
}
@@ -177,7 +174,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
177
174
object_layer_flags [KEY_TOKEN ] = object_layer_key_token_outcomes ;
178
175
179
176
let array_layer_begin_array_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token : u32 | {
180
- let mut result = error_flags . to_field () ;
177
+ let mut result = error_flags_field ;
181
178
if (token == STRING_TOKEN ) | (token == LITERAL_TOKEN ) | (token == NUMERIC_TOKEN ) {
182
179
result = no_change .to_field ();
183
180
}
@@ -194,7 +191,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
194
191
});
195
192
196
193
let array_layer_value_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
197
- let mut result = error_flags . to_field () ;
194
+ let mut result = error_flags_field ;
198
195
if (token == VALUE_SEPARATOR_TOKEN ) {
199
196
result = no_change .to_field ();
200
197
}
@@ -205,7 +202,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
205
202
});
206
203
207
204
let array_layer_value_separator_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
208
- let mut result = error_flags . to_field () ;
205
+ let mut result = error_flags_field ;
209
206
if (token == STRING_TOKEN ) | (token == LITERAL_TOKEN ) | (token == NUMERIC_TOKEN ) {
210
207
result = no_change .to_field ();
211
208
}
@@ -219,7 +216,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
219
216
});
220
217
221
218
let array_layer_value_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
222
- let mut result = error_flags . to_field () ;
219
+ let mut result = error_flags_field ;
223
220
if (token == VALUE_SEPARATOR_TOKEN ) {
224
221
result = no_change .to_field ();
225
222
}
@@ -229,7 +226,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
229
226
result
230
227
});
231
228
let array_layer_end_array_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
232
- let mut result = error_flags . to_field () ;
229
+ let mut result = error_flags_field ;
233
230
if (token == VALUE_SEPARATOR_TOKEN ) {
234
231
result = no_change .to_field ();
235
232
}
@@ -243,7 +240,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
243
240
result
244
241
});
245
242
let array_layer_end_object_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
246
- let mut result = error_flags . to_field () ;
243
+ let mut result = error_flags_field ;
247
244
if (token == VALUE_SEPARATOR_TOKEN ) {
248
245
result = no_change .to_field ();
249
246
}
@@ -264,36 +261,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
264
261
array_layer_flags [NUMERIC_TOKEN ] = array_layer_value_token_outcomes ;
265
262
array_layer_flags [LITERAL_TOKEN ] = array_layer_value_token_outcomes ;
266
263
array_layer_flags [KEY_TOKEN ] = error_token_outcomes ;
267
-
268
- let single_value_layer_value_token_outcomes : [Field ; NUM_TOKENS ] = token_ids .map (|token | {
269
- let mut result = error_flags .to_field ();
270
- // we have reached the end of json
271
- if (token == NO_TOKEN ) {
272
- result = no_change .to_field ();
273
- }
274
- result
275
- });
276
- single_value_layer_flags [NO_TOKEN ] = no_token_outcomes ;
277
- single_value_layer_flags [BEGIN_OBJECT_TOKEN ] = error_token_outcomes ;
278
- single_value_layer_flags [END_OBJECT_TOKEN ] = single_value_layer_value_token_outcomes ;
279
- single_value_layer_flags [BEGIN_ARRAY_TOKEN ] = error_token_outcomes ;
280
- single_value_layer_flags [END_ARRAY_TOKEN ] = single_value_layer_value_token_outcomes ;
281
- single_value_layer_flags [KEY_SEPARATOR_TOKEN ] = object_layer_key_separator_token_outcomes ;
282
- single_value_layer_flags [VALUE_SEPARATOR_TOKEN ] = error_token_outcomes ;
283
- single_value_layer_flags [STRING_TOKEN ] = single_value_layer_value_token_outcomes ;
284
- single_value_layer_flags [NUMERIC_TOKEN ] = single_value_layer_value_token_outcomes ;
285
- single_value_layer_flags [LITERAL_TOKEN ] = single_value_layer_value_token_outcomes ;
286
- single_value_layer_flags [KEY_TOKEN ] = no_token_outcomes ;
287
-
288
- let mut flattened_flags : [Field ; NUM_TOKENS * NUM_TOKENS * 3 ] =
289
- [0 ; NUM_TOKENS * NUM_TOKENS * 3 ];
264
+ let mut flattened_flags : [Field ; NUM_TOKENS * NUM_TOKENS * 2 ] =
265
+ [0 ; NUM_TOKENS * NUM_TOKENS * 2 ];
290
266
let NN = (NUM_TOKENS * NUM_TOKENS );
291
267
for j in 0 ..NUM_TOKENS {
292
268
for k in 0 ..NUM_TOKENS {
293
269
flattened_flags [OBJECT_LAYER * NN + j * NUM_TOKENS + k ] = object_layer_flags [j ][k ];
294
270
flattened_flags [ARRAY_LAYER * NN + j * NUM_TOKENS + k ] = array_layer_flags [j ][k ];
295
- flattened_flags [SINGLE_VALUE_LAYER * NN + j * NUM_TOKENS + k ] =
296
- single_value_layer_flags [j ][k ];
297
271
}
298
272
}
299
273
flattened_flags
0 commit comments