Skip to content

Commit 45f6c6e

Browse files
committed
merge
2 parents 1126e06 + 601ec49 commit 45f6c6e

File tree

12 files changed

+411
-629
lines changed

12 files changed

+411
-629
lines changed

src/_string_tools/slice_packed_field.nr

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -594,37 +594,11 @@ fn divmod_31(numerator: u16) -> (u16, u16) {
594594
(quotient, remainder)
595595
}
596596

597-
/**
598-
* @brief converts a 16 bit value into 16 fake bools (Field elements that are 0 or 1)
599-
**/
600-
unconstrained fn decompose(val: Field) -> [Field; 16] {
601-
let mut r: [Field; 16] = [0; 16];
602-
603-
let mut it = val as u32;
604-
for i in 0..16 {
605-
r[i] = (it & 1) as Field;
606-
it >>= 1;
607-
}
608-
r
609-
}
610-
611597
// 5 gates?
612-
pub fn get_last_limb_path<let OutputFields: u32>(last_limb_index: Field) -> [Field; OutputFields] {
598+
pub fn get_last_limb_path<let OutputFields: u32>(last_limb_index: Field) -> [u1; OutputFields] {
613599
// TODO we offset by 1 explain why (0 byte length produces 0 - 1 which = invalid array index. we just add 1 and increase array length by 1 to compensate)
614600
let path = LAST_LIMB_PATH[cast_num_to_u32(last_limb_index + 1)]; // 2
615-
// Safety: check the comments below
616-
let path_valid_bits = unsafe { decompose(path) };
617-
let mut path_valid_sum: Field = 0;
618-
let mut path_valid_output: [Field; OutputFields] = [0; OutputFields];
619-
for i in 0..OutputFields {
620-
// we check that the path valid bits are binary
621-
assert(path_valid_bits[i] * path_valid_bits[i] - path_valid_bits[i] == 0);
622-
path_valid_sum += (path_valid_bits[i] * (1 << i as u8) as Field);
623-
path_valid_output[i] = path_valid_bits[i];
624-
}
625-
// we check that the path valid bits sum to the path
626-
assert(path_valid_sum == path);
627-
path_valid_output
601+
path.to_le_bits::<OutputFields>()
628602
}
629603

630604
/**
@@ -728,13 +702,13 @@ pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
728702
// 1, 70.5
729703
let index_of_output_limb: Field = (num_bytes_div_31 as Field - num_bytes_mod_31_is_0 as Field);
730704
// 5, 75.5
731-
let path_valid_output: [Field; OutputFields] = get_last_limb_path(index_of_output_limb);
705+
let path_valid_output: [u1; OutputFields] = get_last_limb_path(index_of_output_limb);
732706

733707
// 2, 77.5
734708
let tail_shift = BYTE_SHIFT[cast_num_to_u32(num_unused_bytes_in_start_limb)];
735709

736710
// 51, 128.5
737-
let mut result = [0; OutputFields];
711+
let mut result: [Field; OutputFields] = [0; OutputFields];
738712
let mut previous = tail;
739713
for i in 0..(OutputFields - 1) {
740714
// 0
@@ -748,9 +722,9 @@ pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
748722
// 1, 48
749723
let combined = previous * tail_shift + head;
750724
// 1, 49
751-
result[i] = combined * slice_valid;
725+
result[i] = combined * (slice_valid as Field);
752726
// 2, 51
753-
previous = (tail - previous) * slice_valid + previous;
727+
previous = (tail - previous) * (slice_valid as Field) + previous;
754728
}
755729

756730
// 2, 130.5
@@ -787,9 +761,10 @@ pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
787761
std::as_witness(last_limb);
788762

789763
let mut path: [Field; OutputFields] = [0; OutputFields];
790-
path[0] = (1 - path_valid_output[0]);
764+
path[0] = (1 - path_valid_output[0] as Field);
791765
for i in 1..OutputFields {
792-
path[i] = path_valid_output[i] * -path_valid_output[i - 1] + path_valid_output[i - 1];
766+
path[i] = path_valid_output[i - 1] as Field
767+
- path_valid_output[i] as Field * path_valid_output[i - 1] as Field;
793768
}
794769

795770
for i in 0..OutputFields {

src/_table_generation/make_tables.nr

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Contains methods used to generate tables in `json_tables.nr`. These table generation methods shouldn't be used inside of actual circuits.
22
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};
44
use crate::enums::Token::{
55
BEGIN_ARRAY_TOKEN, BEGIN_OBJECT_TOKEN, END_ARRAY_TOKEN, END_OBJECT_TOKEN, KEY_SEPARATOR_TOKEN,
66
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] {
6060
result
6161
}
6262

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] {
6464
// index = layer type, current token and next token
6565
// 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
6767
// 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;
7171
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,
7575
};
7676
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,
8080
};
8181
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 };
8383

8484
let token_ids: [u32; NUM_TOKENS] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
8585

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);
8787
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;
8989
if (token == KEY_TOKEN) {
9090
result = no_change.to_field();
9191
}
@@ -98,13 +98,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
9898
let object_layer_key_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
9999
let mut result = no_change.to_field();
100100
if (token != KEY_SEPARATOR_TOKEN) {
101-
result = error_flags.to_field();
101+
result = error_flags_field;
102102
}
103103
result
104104
});
105105

106106
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;
108108
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
109109
result = no_change.to_field();
110110
}
@@ -118,7 +118,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
118118
});
119119

120120
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;
122122
if (token == VALUE_SEPARATOR_TOKEN) {
123123
result = no_change.to_field();
124124
}
@@ -129,7 +129,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
129129
});
130130

131131
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;
133133
if (token == VALUE_SEPARATOR_TOKEN) {
134134
result = no_change.to_field();
135135
}
@@ -144,7 +144,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
144144
});
145145

146146
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;
148148
if (token == KEY_TOKEN) {
149149
result = no_change.to_field();
150150
}
@@ -153,11 +153,8 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
153153

154154
let mut object_layer_flags: [[Field; NUM_TOKENS]; NUM_TOKENS] = [[0; NUM_TOKENS]; NUM_TOKENS];
155155
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-
159156
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;
161158
if (token == NO_TOKEN) {
162159
result = no_change.to_field();
163160
}
@@ -177,7 +174,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
177174
object_layer_flags[KEY_TOKEN] = object_layer_key_token_outcomes;
178175

179176
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;
181178
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
182179
result = no_change.to_field();
183180
}
@@ -194,7 +191,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
194191
});
195192

196193
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;
198195
if (token == VALUE_SEPARATOR_TOKEN) {
199196
result = no_change.to_field();
200197
}
@@ -205,7 +202,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
205202
});
206203

207204
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;
209206
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
210207
result = no_change.to_field();
211208
}
@@ -219,7 +216,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
219216
});
220217

221218
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;
223220
if (token == VALUE_SEPARATOR_TOKEN) {
224221
result = no_change.to_field();
225222
}
@@ -229,7 +226,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
229226
result
230227
});
231228
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;
233230
if (token == VALUE_SEPARATOR_TOKEN) {
234231
result = no_change.to_field();
235232
}
@@ -243,7 +240,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
243240
result
244241
});
245242
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;
247244
if (token == VALUE_SEPARATOR_TOKEN) {
248245
result = no_change.to_field();
249246
}
@@ -264,36 +261,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
264261
array_layer_flags[NUMERIC_TOKEN] = array_layer_value_token_outcomes;
265262
array_layer_flags[LITERAL_TOKEN] = array_layer_value_token_outcomes;
266263
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];
290266
let NN = (NUM_TOKENS * NUM_TOKENS);
291267
for j in 0..NUM_TOKENS {
292268
for k in 0..NUM_TOKENS {
293269
flattened_flags[OBJECT_LAYER * NN + j * NUM_TOKENS + k] = object_layer_flags[j][k];
294270
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];
297271
}
298272
}
299273
flattened_flags

src/enums.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ pub(crate) mod Token {
2525
pub(crate) mod Layer {
2626
pub(crate) global OBJECT_LAYER: u32 = 0;
2727
pub(crate) global ARRAY_LAYER: u32 = 1;
28-
pub(crate) global SINGLE_VALUE_LAYER: u32 = 2;
2928
}

0 commit comments

Comments
 (0)