Skip to content

Commit dd909da

Browse files
authored
chore: use u32 for KeySearchResult indices (#70)
1 parent 6cda9b8 commit dd909da

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/get_array.nr

Lines changed: 4 additions & 4 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
pub(crate) fn get_array<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<Self> {
2929
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
3030
let (exists, key_index) = self.key_exists_impl(key);
31-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
31+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
3232
assert(
3333
(entry.entry_type - END_ARRAY_TOKEN as Field) * exists as Field == 0,
3434
"key does not describe an object",
@@ -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;
41+
r.root_index_in_transcript = key_index as Field;
4242

4343
if exists {
4444
Option::some(r)
@@ -75,7 +75,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7575
) -> Option<Self> {
7676
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
7777
let (exists, key_index) = self.key_exists_impl_var(key);
78-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
78+
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
8080
assert(
8181
(entry.entry_type - END_ARRAY_TOKEN as Field) * exists as Field == 0,
@@ -86,7 +86,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
8686
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
8787
r.root_id = entry.id;
8888
r.layer_type_of_root = ARRAY_LAYER;
89-
r.root_index_in_transcript = key_index;
89+
r.root_index_in_transcript = key_index as Field;
9090

9191
if exists {
9292
Option::some(r)

src/get_object.nr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
1818
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
1919

2020
let (exists, key_index) = self.key_exists_impl(key);
21-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
21+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
2222
assert(
2323
(entry.entry_type - END_OBJECT_TOKEN as Field) * exists as Field == 0,
2424
"get_object: entry exists but is not an object!",
@@ -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;
31+
r.root_index_in_transcript = key_index as Field;
3232
if exists {
3333
Option::some(r)
3434
} else {
@@ -67,7 +67,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
6767
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
6868

6969
let (exists, key_index) = self.key_exists_impl_var(key);
70-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
70+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
7171
assert(
7272
(entry.entry_type - END_OBJECT_TOKEN as Field) * exists as Field == 0,
7373
"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;
80+
r.root_index_in_transcript = key_index as Field;
8181
if exists {
8282
Option::some(r)
8383
} else {

src/getters.nr

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub struct KeySearchResult {
1414
found: bool, // does the key exist?
1515
target_lt_smallest_entry: bool, // is the target keyhash smaller than the smallest keyhash in self.key_hashes?
1616
target_gt_largest_entry: bool, // is the target keyhash larger than the largest keyhash in self.key_hashes?
17-
lhs_index: Field, // either the index of the key being searched for, or the index of the keyhash in self.key_hashes that is closest to keyhash (hash > lhs_index_hash)
18-
rhs_index: Field, // either the index of the key being searched for, or the index of the keyhash in self.key_hashes that is closest to keyhash (hash < rhs_index_hash)
17+
lhs_index: u32, // either the index of the key being searched for, or the index of the keyhash in self.key_hashes that is closest to keyhash (hash > lhs_index_hash)
18+
rhs_index: u32, // either the index of the key being searched for, or the index of the keyhash in self.key_hashes that is closest to keyhash (hash < rhs_index_hash)
1919
}
2020

2121
/**
@@ -36,7 +36,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
3636
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
3737

3838
let (exists, key_index) = self.key_exists_impl(key);
39-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
39+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
4040
(exists, entry)
4141
}
4242

@@ -80,7 +80,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
8080
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
8181

8282
let (exists, key_index) = self.key_exists_impl_var(key);
83-
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(key_index)].into();
83+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
8484
(exists, entry)
8585
}
8686

@@ -256,8 +256,8 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
256256
found,
257257
target_lt_smallest_entry,
258258
target_gt_largest_entry,
259-
lhs_index,
260-
rhs_index,
259+
lhs_index: lhs_index as u32,
260+
rhs_index: rhs_index as u32,
261261
}
262262
}
263263

@@ -274,7 +274,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
274274
* Method computes a key hash and checks whether key hash exists in the list of sorted preprocessed key hashes
275275
* If it does *not* exist, we can find two adjacent entries in `key_hashes` where `key_hashes[i]` < target_key_hash < `key_hashes[i+1]`
276276
**/
277-
pub(crate) fn key_exists_impl<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> (bool, Field) {
277+
pub(crate) fn key_exists_impl<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> (bool, u32) {
278278
/*
279279
Option A: key exists
280280
Option B: key does NOT exist
@@ -293,19 +293,21 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
293293

294294
// Safety: The assertion below checks that the keyhash is stored in the the index returned by the unconstrained function
295295
let search_result = unsafe { self.search_for_key_in_map(keyhash) };
296+
if search_result.found {
297+
assert_eq(search_result.lhs_index, search_result.rhs_index);
298+
}
299+
296300
let found = search_result.found as Field;
297301

298302
let target_lt_smallest_entry = search_result.target_lt_smallest_entry as Field;
299303
let target_gt_largest_entry = search_result.target_gt_largest_entry as Field;
300304

301-
assert(((search_result.lhs_index - search_result.rhs_index) * found) == 0);
302-
303305
// only one of "found", "target_lt_smallest_entry", "target_gt_largest_entry" can be true
304306
let exclusion_test = found + target_gt_largest_entry + target_lt_smallest_entry;
305307
assert(exclusion_test * exclusion_test == exclusion_test);
306308

307-
let mut lhs = self.key_hashes[cast_num_to_u32(search_result.lhs_index)];
308-
let mut rhs = self.key_hashes[cast_num_to_u32(search_result.rhs_index)];
309+
let mut lhs = self.key_hashes[search_result.lhs_index];
310+
let mut rhs = self.key_hashes[search_result.rhs_index];
309311

310312
// case where hash < self.key_hashes[0]
311313
// 0 < hash < hashes[0]
@@ -334,7 +336,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
334336
pub(crate) fn key_exists_impl_var<let KeyBytes: u32>(
335337
self,
336338
key: BoundedVec<u8, KeyBytes>,
337-
) -> (bool, Field) {
339+
) -> (bool, u32) {
338340
/*
339341
Option A: key exists
340342
Option B: key does NOT exist
@@ -353,19 +355,20 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
353355

354356
// Safety: the assertion (search_result.lhs_index - search_result.rhs_index) * found == 0 constraints this function
355357
let search_result = unsafe { self.search_for_key_in_map(keyhash) };
356-
let found = search_result.found as Field;
358+
if search_result.found {
359+
assert_eq(search_result.lhs_index, search_result.rhs_index);
360+
}
357361

362+
let found = search_result.found as Field;
358363
let target_lt_smallest_entry = search_result.target_lt_smallest_entry as Field;
359364
let target_gt_largest_entry = search_result.target_gt_largest_entry as Field;
360365

361-
assert(((search_result.lhs_index - search_result.rhs_index) * found) == 0);
362-
363366
// only one of "found", "target_lt_smallest_entry", "target_gt_largest_entry" can be true
364367
let exclusion_test = found + target_gt_largest_entry + target_lt_smallest_entry;
365368
assert(exclusion_test * exclusion_test == exclusion_test);
366369

367-
let mut lhs = self.key_hashes[cast_num_to_u32(search_result.lhs_index)];
368-
let mut rhs = self.key_hashes[cast_num_to_u32(search_result.rhs_index)];
370+
let mut lhs = self.key_hashes[search_result.lhs_index];
371+
let mut rhs = self.key_hashes[search_result.rhs_index];
369372

370373
// case where hash < self.key_hashes[0]
371374
// 0 < hash < hashes[0]
@@ -376,8 +379,8 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
376379
rhs = rhs * (1 - target_gt_largest_entry) + target_gt_largest_entry * HASH_MAXIMUM;
377380

378381
// case where hash == self.key_hashes[found_index]
379-
lhs = lhs - found;
380-
rhs = rhs + found;
382+
lhs -= found;
383+
rhs += found;
381384

382385
assert_gt_240_bit(keyhash, lhs);
383386
assert_lt_240_bit(keyhash, rhs);

0 commit comments

Comments
 (0)