Skip to content

Commit 07bc4fb

Browse files
committed
merge
2 parents 3c26548 + 126c8b7 commit 07bc4fb

File tree

9 files changed

+241
-479
lines changed

9 files changed

+241
-479
lines changed

src/_table_generation/make_tables.nr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,27 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
6565
// output is layer type
6666
// 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
}
@@ -154,7 +154,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
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];
156156
let no_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
157-
let mut result = error_flags.to_field();
157+
let mut result = error_flags_field;
158158
if (token == NO_TOKEN) {
159159
result = no_change.to_field();
160160
}
@@ -174,7 +174,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
174174
object_layer_flags[KEY_TOKEN] = object_layer_key_token_outcomes;
175175

176176
let array_layer_begin_array_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token: u32| {
177-
let mut result = error_flags.to_field();
177+
let mut result = error_flags_field;
178178
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
179179
result = no_change.to_field();
180180
}
@@ -191,7 +191,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
191191
});
192192

193193
let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
194-
let mut result = error_flags.to_field();
194+
let mut result = error_flags_field;
195195
if (token == VALUE_SEPARATOR_TOKEN) {
196196
result = no_change.to_field();
197197
}
@@ -202,7 +202,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
202202
});
203203

204204
let array_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
205-
let mut result = error_flags.to_field();
205+
let mut result = error_flags_field;
206206
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
207207
result = no_change.to_field();
208208
}
@@ -216,7 +216,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
216216
});
217217

218218
let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
219-
let mut result = error_flags.to_field();
219+
let mut result = error_flags_field;
220220
if (token == VALUE_SEPARATOR_TOKEN) {
221221
result = no_change.to_field();
222222
}
@@ -226,7 +226,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
226226
result
227227
});
228228
let array_layer_end_array_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
229-
let mut result = error_flags.to_field();
229+
let mut result = error_flags_field;
230230
if (token == VALUE_SEPARATOR_TOKEN) {
231231
result = no_change.to_field();
232232
}
@@ -240,7 +240,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
240240
result
241241
});
242242
let array_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
243-
let mut result = error_flags.to_field();
243+
let mut result = error_flags_field;
244244
if (token == VALUE_SEPARATOR_TOKEN) {
245245
result = no_change.to_field();
246246
}

src/get_array.nr

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,43 @@ use crate::json::JSONValue;
66
use crate::json_entry::JSONEntry;
77
use crate::utils::cast_num_to_u32;
88

9-
/**
10-
* @brief getter methods for extracting array types out of a JSON struct
11-
**/
9+
/// getter methods for extracting array types out of a JSON struct
1210
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
13-
/**
14-
* @brief Updates the JSON object to point to a specific array entry
15-
**/
11+
/// Updates the JSON object to point to a specific array entry
1612
pub(crate) fn update_json_array(mut self, entry: JSONEntry, key_index: u32) -> Self {
17-
self.layer_type_of_root = cast_num_to_u32(entry.parent_index);
1813
self.root_id = entry.id;
1914
self.layer_type_of_root = ARRAY_LAYER;
2015
self.root_index_in_transcript = key_index;
2116
self
2217
}
23-
24-
/**
25-
* @brief if the root JSON is an array, return its length
26-
**/
18+
/// if the root JSON is an array, return its length
2719
pub(crate) fn get_length(self) -> u32 {
2820
assert(self.layer_type_of_root == ARRAY_LAYER, "can only get length of an array type");
2921
let parent_entry: JSONEntry =
3022
self.json_entries_packed[self.root_index_in_transcript].into();
3123
parent_entry.num_children as u32
3224
}
3325

34-
/**
35-
* @brief if the root JSON is an object, extract an array given by `key`
36-
* @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
37-
**/
26+
/// if the root JSON is an object, extract an array given by `key`
27+
///
28+
/// returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
3829
pub(crate) fn get_array<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<Self> {
3930
self.get_array_var(BoundedVec::from_parts_unchecked(key, KeyBytes))
4031
}
4132

42-
/**
43-
* @brief if the root JSON is an object, extract an array given by `key`
44-
* @description will revert if the array does not exist
45-
**/
33+
/// if the root JSON is an object, extract an array given by `key`
34+
///
35+
/// will revert if the array does not exist
4636
pub(crate) fn get_array_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Self {
4737
self.get_array_unchecked_var(BoundedVec::from_parts_unchecked(key, KeyBytes))
4838
}
4939

50-
/**
51-
* @brief same as `get_array` for where the key length may be less than KeyBytes
52-
**/
40+
/// same as `get_array` for where the key length may be less than KeyBytes
5341
pub(crate) fn get_array_var<let KeyBytes: u32>(
5442
self,
5543
key: BoundedVec<u8, KeyBytes>,
5644
) -> Option<Self> {
57-
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
58-
let (exists, key_index) = self.key_exists_impl_var(key);
59-
let entry: JSONEntry = self.json_entries_packed[key_index].into();
60-
// TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
45+
let (exists, entry, key_index) = self.get_json_entry_var(key);
6146
if !exists {
6247
assert_eq(
6348
entry.entry_type,
@@ -75,9 +60,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7560
}
7661
}
7762

78-
/**
79-
* @brief same as `get_array_unchecked` for where the key length may be less than KeyBytes
80-
**/
63+
/// same as `get_array_unchecked` for where the key length may be less than KeyBytes
8164
pub(crate) fn get_array_unchecked_var<let KeyBytes: u32>(
8265
self,
8366
key: BoundedVec<u8, KeyBytes>,
@@ -90,22 +73,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
9073
self.update_json_array(entry, key_index)
9174
}
9275

93-
/**
94-
* @brief if the root JSON is an array, extract an array given by the position of the target in the source array
95-
* @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
96-
**/
76+
/// if the root JSON is an array, extract an array given by the position of the target in the source array
77+
///
78+
/// returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
9779
pub(crate) fn get_array_from_array(self, array_index: Field) -> Option<Self> {
98-
assert(
99-
self.layer_type_of_root == ARRAY_LAYER,
100-
"can only acceess array elements from array",
101-
);
102-
103-
let parent_entry: JSONEntry =
104-
self.json_entries_packed[self.root_index_in_transcript].into();
105-
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
106-
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
107-
108-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
80+
let (entry, valid, entry_index) = self.get_entry_from_array(array_index);
10981
if valid {
11082
assert_eq(
11183
entry.entry_type,
@@ -114,7 +86,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
11486
);
11587
}
11688

117-
let result = self.update_json_array(entry, cast_num_to_u32(entry_index));
89+
let result = self.update_json_array(entry, entry_index);
11890

11991
if valid {
12092
Option::some(result)
@@ -123,33 +95,15 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
12395
}
12496
}
12597

126-
/**
127-
* @brief if the root JSON is an array, extract an array given by the position of the target in the source array
128-
* @description will revert if the array does not exist
129-
**/
98+
/// if the root JSON is an array, extract an array given by the position of the target in the source array
99+
///
100+
/// will revert if the array does not exist
130101
pub(crate) fn get_array_from_array_unchecked(self, array_index: Field) -> Self {
131-
assert(
132-
self.layer_type_of_root == ARRAY_LAYER,
133-
"can only acceess array elements from array",
134-
);
135-
136-
let parent_entry: JSONEntry =
137-
self.json_entries_packed[self.root_index_in_transcript].into();
138-
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
139-
assert(valid, "array overflow");
140-
let entry_index = (parent_entry.child_pointer + array_index);
141-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
142-
assert(
143-
entry.entry_type == END_ARRAY_TOKEN as Field,
144-
"get_array_from_array_unchecked: entry exists but is not an array!",
145-
);
146-
147-
self.update_json_array(entry, cast_num_to_u32(entry_index))
102+
let result = self.get_array_from_array(array_index);
103+
result.expect(f"array index out of bounds")
148104
}
149105

150-
/**
151-
* @brief if the root is an array, map over the array values, applying `fn f` to each value
152-
**/
106+
/// if the root is an array, map over the array values, applying `fn f` to each value
153107
fn map<U, let MaxElements: u32, let MaxElementBytes: u32>(
154108
self,
155109
f: fn(JSONValue<MaxElementBytes>) -> U,

0 commit comments

Comments
 (0)