Skip to content

Commit d1a3c32

Browse files
authored
Merge branch 'main' into jl/remove_decompose
2 parents e94e705 + 15df67c commit d1a3c32

File tree

6 files changed

+201
-435
lines changed

6 files changed

+201
-435
lines changed

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,

src/get_literal.nr

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
use crate::_comparison_tools::lt::lt_field_16_bit;
2-
use crate::enums::Layer::ARRAY_LAYER;
31
use crate::enums::Token::LITERAL_TOKEN;
42
use crate::json::JSON;
5-
use crate::json_entry::JSONEntry;
63
use crate::utils::cast_num_to_u32;
74

85
global MAX_LITERAL_LENGTH_AS_STRING: u32 = 5;
96
global LITERAL_OFFSET_SHIFT: [Field; 6] =
107
[0x10000000000, 0x100000000, 0x1000000, 0x10000, 0x100, 1];
118

12-
/**
13-
* @brief a JSON "literal" type has 3 states: "true", "false", "null".
14-
* As such we can't directly convert to a bool and cover all possible cases
15-
**/
9+
/// a JSON "literal" type has 3 states: "true", "false", "null".
10+
/// As such we can't directly convert to a bool and cover all possible cases
1611
pub struct JSONLiteral {
1712
pub(crate) value: Field,
1813
}
@@ -39,9 +34,7 @@ impl JSONLiteral {
3934
}
4035
}
4136

42-
/**
43-
* @brief given an input array, return a JSONLiteral depending on whether the array says "true", "false" or "null"
44-
**/
37+
/// given an input array, return a JSONLiteral depending on whether the array says "true", "false" or "null"
4538
fn extract_literal_from_array(
4639
arr: [u8; MAX_LITERAL_LENGTH_AS_STRING],
4740
json_length: Field,
@@ -64,37 +57,29 @@ fn extract_literal_from_array(
6457
JSONLiteral::new(is_true, is_false, is_null)
6558
}
6659

67-
/**
68-
* @brief getter methods for extracting literal values out of a JSON struct
69-
**/
60+
/// getter methods for extracting literal values out of a JSON struct
7061
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
7162

72-
/**
73-
* @brief if the root JSON is an object, extract a literal value given by `key`
74-
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
75-
**/
63+
/// if the root JSON is an object, extract a literal value given by `key`
64+
///
65+
/// returns an Option<JSONLiteral> which will be null if the literal does not exist
7666
pub(crate) fn get_literal<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<JSONLiteral> {
7767
self.get_literal_var(BoundedVec::from_parts_unchecked(key, KeyBytes))
7868
}
7969

80-
/**
81-
* @brief if the root JSON is an object, extract a literal value given by `key`
82-
* @description will revert if the literal does not exist
83-
**/
70+
/// if the root JSON is an object, extract a literal value given by `key`
71+
///
72+
/// will revert if the literal does not exist
8473
fn get_literal_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> JSONLiteral {
8574
self.get_literal_unchecked_var(BoundedVec::from_parts_unchecked(key, KeyBytes))
8675
}
8776

88-
/**
89-
* @brief same as `get_literal` for where the key length may be less than KeyBytes
90-
**/
77+
/// same as `get_literal` for where the key length may be less than KeyBytes
9178
pub(crate) fn get_literal_var<let KeyBytes: u32>(
9279
self,
9380
key: BoundedVec<u8, KeyBytes>,
9481
) -> Option<JSONLiteral> {
95-
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
96-
97-
let (exists, entry) = self.get_json_entry_var(key);
82+
let (exists, entry, _) = self.get_json_entry_var(key);
9883
assert(
9984
(entry.entry_type - LITERAL_TOKEN as Field) * exists as Field == 0,
10085
"get_literal_var: entry exists but is not a literal!",
@@ -109,15 +94,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
10994
}
11095
}
11196

112-
/**
113-
* @brief same as `get_literal_unchecked` for where the key length may be less than KeyBytes
114-
**/
97+
/// same as `get_literal_unchecked` for where the key length may be less than KeyBytes
11598
pub(crate) fn get_literal_unchecked_var<let KeyBytes: u32>(
11699
self,
117100
key: BoundedVec<u8, KeyBytes>,
118101
) -> JSONLiteral {
119-
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
120-
121102
let entry = self.get_json_entry_unchecked_var(key);
122103
assert(
123104
entry.entry_type == LITERAL_TOKEN as Field,
@@ -129,23 +110,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
129110
extract_literal_from_array(parsed_string, entry.json_length)
130111
}
131112

132-
/**
133-
* @brief if the root JSON is an array, extract a literal given by the position of the target in the source array
134-
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
135-
**/
136-
fn get_literal_from_array(self, array_index: Field) -> Option<JSONLiteral> {
137-
assert(
138-
self.layer_type_of_root == ARRAY_LAYER,
139-
"can only acceess array elements from array",
140-
);
141-
142-
let parent_entry: JSONEntry =
143-
self.json_entries_packed[self.root_index_in_transcript].into();
144-
145-
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
146-
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
147-
148-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
113+
/// if the root JSON is an array, extract a literal given by the position of the target in the source array
114+
///
115+
/// returns an Option<JSONLiteral> which will be null if the literal does not exist
116+
pub fn get_literal_from_array(self, array_index: Field) -> Option<JSONLiteral> {
117+
let (entry, valid, _) = self.get_entry_from_array(array_index);
149118
if valid {
150119
assert_eq(
151120
entry.entry_type,
@@ -165,32 +134,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
165134
}
166135
}
167136

168-
/**
169-
* @brief if the root JSON is an array, extract a literal given by the position of the target in the source array
170-
* @description will revert if the literal does not exist
171-
**/
137+
/// if the root JSON is an array, extract a literal given by the position of the target in the source array
138+
///
139+
/// will revert if the literal does not exist
172140
fn get_literal_from_array_unchecked(self, array_index: Field) -> JSONLiteral {
173-
assert(
174-
self.layer_type_of_root == ARRAY_LAYER,
175-
"can only acceess array elements from array",
176-
);
177-
178-
let parent_entry: JSONEntry =
179-
self.json_entries_packed[self.root_index_in_transcript].into();
180-
181-
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
182-
assert(valid, "array overflow");
183-
let entry_index = (parent_entry.child_pointer + array_index);
184-
185-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
186-
187-
assert(
188-
entry.entry_type == LITERAL_TOKEN as Field,
189-
"get_literal_from_array_unchecked: entry exists but is not a literal!",
190-
);
191-
192-
let mut parsed_string: [u8; MAX_LITERAL_LENGTH_AS_STRING] =
193-
self.extract_string_entry(entry);
194-
extract_literal_from_array(parsed_string, entry.json_length)
141+
let result = self.get_literal_from_array(array_index);
142+
result.expect(f"array index out of bounds")
195143
}
196144
}

0 commit comments

Comments
 (0)