Skip to content

Commit 2fafc07

Browse files
authored
chore: replace root_index_in_transcript with u32 (#74)
1 parent dd909da commit 2fafc07

File tree

8 files changed

+96
-107
lines changed

8 files changed

+96
-107
lines changed

src/get_array.nr

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
1717
pub(crate) fn get_length(self) -> u32 {
1818
assert(self.layer_type_of_root == ARRAY_LAYER, "can only get length of an array type");
1919
let parent_entry: JSONEntry =
20-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
20+
self.json_entries_packed[self.root_index_in_transcript].into();
2121
parent_entry.num_children as u32
2222
}
2323

@@ -38,7 +38,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
3838
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
3939
r.root_id = entry.id;
4040
r.layer_type_of_root = ARRAY_LAYER;
41-
r.root_index_in_transcript = key_index as Field;
41+
r.root_index_in_transcript = key_index;
4242

4343
if exists {
4444
Option::some(r)
@@ -77,16 +77,19 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7777
let (exists, key_index) = self.key_exists_impl_var(key);
7878
let entry: JSONEntry = self.json_entries_packed[key_index].into();
7979
// TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
80-
assert(
81-
(entry.entry_type - END_ARRAY_TOKEN as Field) * exists as Field == 0,
82-
"key does not describe an object",
83-
);
80+
if !exists {
81+
assert_eq(
82+
entry.entry_type,
83+
END_ARRAY_TOKEN as Field,
84+
"key does not describe an object",
85+
);
86+
}
8487

8588
let mut r = self;
8689
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
8790
r.root_id = entry.id;
8891
r.layer_type_of_root = ARRAY_LAYER;
89-
r.root_index_in_transcript = key_index as Field;
92+
r.root_index_in_transcript = key_index;
9093

9194
if exists {
9295
Option::some(r)
@@ -127,21 +130,24 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
127130
);
128131

129132
let parent_entry: JSONEntry =
130-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
133+
self.json_entries_packed[self.root_index_in_transcript].into();
131134
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
132135
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
133136

134137
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
135-
assert(
136-
(entry.entry_type - END_ARRAY_TOKEN as Field) * valid as Field == 0,
137-
"get_object_from_array: entry exists but is not an object!",
138-
);
138+
if valid {
139+
assert_eq(
140+
entry.entry_type,
141+
END_ARRAY_TOKEN as Field,
142+
"get_object_from_array: entry exists but is not an object!",
143+
);
144+
}
139145

140146
let mut r = self;
141147
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
142148
r.root_id = entry.id;
143149
r.layer_type_of_root = ARRAY_LAYER;
144-
r.root_index_in_transcript = entry_index;
150+
r.root_index_in_transcript = cast_num_to_u32(entry_index);
145151

146152
if valid {
147153
Option::some(r)
@@ -161,7 +167,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
161167
);
162168

163169
let parent_entry: JSONEntry =
164-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
170+
self.json_entries_packed[self.root_index_in_transcript].into();
165171
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
166172
assert(valid, "array overflow");
167173
let entry_index = (parent_entry.child_pointer + array_index);
@@ -175,7 +181,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
175181
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
176182
r.root_id = entry.id;
177183
r.layer_type_of_root = ARRAY_LAYER;
178-
r.root_index_in_transcript = entry_index;
184+
r.root_index_in_transcript = cast_num_to_u32(entry_index);
179185
r
180186
}
181187

@@ -191,8 +197,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
191197
{
192198
assert(self.layer_type_of_root == ARRAY_LAYER, "can only call map on an array");
193199

194-
let entry: JSONEntry =
195-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
200+
let entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
196201
let num_children = entry.num_children;
197202
let mut r: [U; MaxElements] = [U::default(); MaxElements];
198203

src/get_literal.nr

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,19 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
164164
);
165165

166166
let parent_entry: JSONEntry =
167-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
167+
self.json_entries_packed[self.root_index_in_transcript].into();
168168

169169
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
170170
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
171171

172172
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
173-
174-
assert(
175-
(entry.entry_type - LITERAL_TOKEN as Field) * valid as Field == 0,
176-
"get_literal_from_array: entry exists but is not a literal!",
177-
);
173+
if valid {
174+
assert_eq(
175+
entry.entry_type,
176+
LITERAL_TOKEN as Field,
177+
"get_literal_from_array: entry exists but is not a literal!",
178+
);
179+
}
178180

179181
let mut parsed_string: [u8; MAX_LITERAL_LENGTH_AS_STRING] =
180182
self.extract_string_entry(entry);
@@ -198,7 +200,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
198200
);
199201

200202
let parent_entry: JSONEntry =
201-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
203+
self.json_entries_packed[self.root_index_in_transcript].into();
202204

203205
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
204206
assert(valid, "array overflow");

src/get_number.nr

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,19 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
127127
);
128128

129129
let parent_entry: JSONEntry =
130-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
130+
self.json_entries_packed[self.root_index_in_transcript].into();
131131

132132
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
133133
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
134134

135135
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
136-
137-
assert(
138-
(entry.entry_type - NUMERIC_TOKEN as Field) * valid as Field == 0,
139-
"get_number: entry exists but is not a number!",
140-
);
136+
if valid {
137+
assert_eq(
138+
entry.entry_type,
139+
NUMERIC_TOKEN as Field,
140+
"get_number: entry exists but is not a number!",
141+
);
142+
}
141143

142144
let mut parsed_string: [u8; U64_LENGTH_AS_BASE10_STRING] = self.extract_string_entry(entry);
143145
let result = extract_number_from_array(parsed_string, entry.json_length);
@@ -160,7 +162,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
160162
);
161163

162164
let parent_entry: JSONEntry =
163-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
165+
self.json_entries_packed[self.root_index_in_transcript].into();
164166

165167
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
166168
assert(valid, "array overflow");

src/get_object.nr

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
2828
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
2929
r.root_id = entry.id;
3030
r.layer_type_of_root = OBJECT_LAYER;
31-
r.root_index_in_transcript = key_index as Field;
31+
r.root_index_in_transcript = key_index;
3232
if exists {
3333
Option::some(r)
3434
} else {
@@ -43,7 +43,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
4343
pub(crate) fn get_object_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Self {
4444
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
4545
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index(key);
46-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
46+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
4747
assert(
4848
entry.entry_type == END_OBJECT_TOKEN as Field,
4949
"get_object: entry exists but is not an object!",
@@ -77,7 +77,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7777
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
7878
r.root_id = entry.id;
7979
r.layer_type_of_root = OBJECT_LAYER;
80-
r.root_index_in_transcript = key_index as Field;
80+
r.root_index_in_transcript = key_index;
8181
if exists {
8282
Option::some(r)
8383
} else {
@@ -94,7 +94,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
9494
) -> Self {
9595
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
9696
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key);
97-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
97+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
9898
assert(
9999
entry.entry_type == END_OBJECT_TOKEN as Field,
100100
"get_object: entry exists but is not an object!",
@@ -119,23 +119,25 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
119119
);
120120

121121
let parent_entry: JSONEntry =
122-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
122+
self.json_entries_packed[self.root_index_in_transcript].into();
123123

124124
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
125125
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
126126

127127
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
128-
129-
assert(
130-
(entry.entry_type - END_OBJECT_TOKEN as Field) * valid as Field == 0,
131-
"get_object_from_array: entry exists but is not an object!",
132-
);
128+
if valid {
129+
assert_eq(
130+
entry.entry_type,
131+
END_OBJECT_TOKEN as Field,
132+
"get_object_from_array: entry exists but is not an object!",
133+
);
134+
}
133135

134136
let mut r = self;
135137
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
136138
r.root_id = entry.id;
137139
r.layer_type_of_root = OBJECT_LAYER;
138-
r.root_index_in_transcript = entry_index;
140+
r.root_index_in_transcript = cast_num_to_u32(entry_index);
139141

140142
if valid {
141143
Option::some(r)
@@ -155,7 +157,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
155157
);
156158

157159
let parent_entry: JSONEntry =
158-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
160+
self.json_entries_packed[self.root_index_in_transcript].into();
159161

160162
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
161163
assert(valid, "array overflow");
@@ -171,7 +173,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
171173
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
172174
r.root_id = entry.id;
173175
r.layer_type_of_root = OBJECT_LAYER;
174-
r.root_index_in_transcript = entry_index;
176+
r.root_index_in_transcript = cast_num_to_u32(entry_index);
175177
r
176178
}
177179
}
@@ -193,13 +195,7 @@ fn test_object() {
193195
assert(B == B_alt.unwrap());
194196

195197
let C = B.get_object_unchecked("bartholomew tony Harrison III".as_bytes());
196-
assert(
197-
JSONEntry::from(
198-
C.json_entries_packed[cast_num_to_u32(C.root_index_in_transcript)],
199-
)
200-
.num_children
201-
== 1,
202-
);
198+
assert_eq(JSONEntry::from(C.json_entries_packed[C.root_index_in_transcript]).num_children, 1);
203199

204200
let not_real = B.get_object("bartholomew tony Harrison IV".as_bytes());
205201
assert(not_real.is_some() == false);
@@ -208,13 +204,7 @@ fn test_object() {
208204
"bartholomew tony Harrison III".as_bytes(),
209205
29,
210206
));
211-
assert(
212-
JSONEntry::from(
213-
C.json_entries_packed[cast_num_to_u32(C.root_index_in_transcript)],
214-
)
215-
.num_children
216-
== 1,
217-
);
207+
assert_eq(JSONEntry::from(C.json_entries_packed[C.root_index_in_transcript]).num_children, 1);
218208

219209
let C = B.get_object_var(BoundedVec::from_parts_unchecked(
220210
"bartholomew tony Harrison III".as_bytes(),

src/get_string.nr

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,19 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
201201
);
202202

203203
let parent_entry: JSONEntry =
204-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
204+
self.json_entries_packed[self.root_index_in_transcript].into();
205205

206206
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
207207
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
208208

209209
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
210-
211-
assert(
212-
(entry.entry_type - STRING_TOKEN as Field) * valid as Field == 0,
213-
"get_array_element_as_string: entry exists but is not a string!",
214-
);
210+
if valid {
211+
assert_eq(
212+
entry.entry_type,
213+
STRING_TOKEN as Field,
214+
"get_array_element_as_string: entry exists but is not a string!",
215+
);
216+
}
215217

216218
let mut parsed_string: [u8; StringBytes] = self.extract_string_entry(entry);
217219
let parsed_string: BoundedVec<u8, StringBytes> = process_escape_sequences(
@@ -239,7 +241,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
239241
);
240242

241243
let parent_entry: JSONEntry =
242-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
244+
self.json_entries_packed[self.root_index_in_transcript].into();
243245

244246
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
245247
assert(valid, "array overflow");
@@ -379,17 +381,19 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
379381
);
380382

381383
let parent_entry: JSONEntry =
382-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
384+
self.json_entries_packed[self.root_index_in_transcript].into();
383385

384386
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
385387
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
386388

387389
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
388-
389-
assert(
390-
(entry.entry_type - STRING_TOKEN as Field) * valid as Field == 0,
391-
"get_array_element_as_string: entry exists but is not a string!",
392-
);
390+
if valid {
391+
assert_eq(
392+
entry.entry_type,
393+
STRING_TOKEN as Field,
394+
"get_array_element_as_string: entry exists but is not a string!",
395+
);
396+
}
393397

394398
let mut parsed_string: [u8; StringBytes] = self.extract_string_entry(entry);
395399

@@ -422,7 +426,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
422426
);
423427

424428
let parent_entry: JSONEntry =
425-
self.json_entries_packed[cast_num_to_u32(self.root_index_in_transcript)].into();
429+
self.json_entries_packed[self.root_index_in_transcript].into();
426430

427431
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
428432
assert(valid, "array overflow");

0 commit comments

Comments
 (0)