1
1
use crate::_comparison_tools::lt::assert_lte_240_bit ;
2
- use crate::_comparison_tools::lt::lt_field_16_bit ;
3
2
use crate::_comparison_tools::lt::lte_field_240_bit ;
4
3
use crate::json::JSON ;
5
4
use crate::json_entry:: {JSONEntry , JSONEntryPacked };
@@ -62,6 +61,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
62
61
let KeyIndexData { json_index , json_length , parent_id , array_index } =
63
62
KeyIndexData ::from_field (self .key_data [i ]);
64
63
let hash = hasher .get_keyhash (self .json_packed , json_index , json_length );
64
+ //ensures hash:0-199 bits, array_index:200-215 bits, parent_id: 216-239 bits
65
65
hashlist [i ] = hash + array_index * two_pow_200 + parent_id * two_pow_216 ;
66
66
}
67
67
@@ -72,69 +72,60 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
72
72
73
73
let mut sorted_entries : [JSONEntryPacked ; MaxNumValues ] =
74
74
[JSONEntryPacked ::default (); MaxNumValues ];
75
+
75
76
for i in 0 ..MaxNumValues {
76
77
sorted_entries [sort_result .sort_indices [i ]] = self .json_entries_packed [i ];
77
78
}
78
79
79
- let mut ids : [Field ; MaxNumValues ] = [0 ; MaxNumValues ];
80
- let mut parent_indices : [Field ; MaxNumValues ] = [0 ; MaxNumValues ];
81
- let mut entry_types : [Field ; MaxNumValues ] = [0 ; MaxNumValues ];
80
+ let mut parent_indices : [u32 ; MaxNumValues ] = [0 ; MaxNumValues ];
82
81
82
+ let mut identity_to_json_map : [u32 ; MaxNumValues ] = [0 ; MaxNumValues ];
83
83
for i in 0 ..MaxNumValues {
84
84
// 11.75 + 3.5 = 15.25 gates per iteration
85
85
let (id , parent_index , entry_type ) = JSONEntry ::extract_entry_type_id_and_parent_index_from_field (
86
86
sorted_entries [i ].value ,
87
87
);
88
- ids [i ] = id ;
89
- parent_indices [i ] = parent_index ;
90
- entry_types [i ] = entry_type ;
91
- }
92
-
93
- let mut identity_to_json_map : [Field ; MaxNumValues ] = [0 ; MaxNumValues ];
94
- // 6.5 gates per iteration
95
- for i in 0 ..MaxNumValues {
96
- let id = ids [i ];
97
- let entry_type = entry_types [i ];
88
+ parent_indices [i ] = cast_num_to_u32 (parent_index );
98
89
// 2 gates
90
+ // update is 1 for end of object/array, 0 for other
99
91
let update = TOKEN_ENDS_OBJECT_OR_ARRAY [cast_num_to_u32 (entry_type )];
100
92
// NOTE THIS RELIES ON MaxNumValues ACTUALLY DESCRIBING NUMMaxNumValues + 1
101
93
let index = if update {
102
- id
94
+ cast_num_to_u32 ( id )
103
95
} else {
104
- ( MaxNumValues as Field - 1 )
96
+ MaxNumValues - 1
105
97
};
106
98
// 3.5 gates
107
- identity_to_json_map [cast_num_to_u32 ( index ) ] = i as Field ;
99
+ identity_to_json_map [index ] = i ;
108
100
}
109
-
110
101
// 13.5 gates per iteration
111
102
let mut parent_identity_pre = parent_indices [0 ];
112
103
for i in 1 ..MaxNumValues {
113
104
let parent_identity_post = parent_indices [i ];
114
105
// if the parent identity changes,
115
106
// 3.5 gate
116
- // the list is sorted according to parent_ideneity ,
117
- // n.b. parent_identity_post - parent_identity_pre is not neccessarily 0 or 1 (can be larger)
107
+ // the list is sorted according to parent_identity ,
108
+ // n.b. parent_identity_post - parent_identity_pre is not necessarily 0 or 1 (can be larger)
118
109
// due to empty objects and arrays increasing identity value without creating associated child json entries
119
- let new_parent = lt_field_16_bit (parent_identity_pre , parent_identity_post ) as Field ;
120
- // let new_parent = (parent_identity_post as u32 > parent_identity_pre as u32) as Field;
110
+ let new_parent = parent_identity_pre < parent_identity_post ;
121
111
// 3.5 gates
122
- let index_of_parent = identity_to_json_map [cast_num_to_u32 ( parent_identity_post ) ];
112
+ let index_of_parent = identity_to_json_map [parent_identity_post ];
123
113
// 1 gate + 3.5 gates
124
114
let updated = JSONEntry ::add_child_pointer_into_field (
125
- sorted_entries [cast_num_to_u32 ( index_of_parent ) ].value ,
115
+ sorted_entries [index_of_parent ].value ,
126
116
i as Field ,
127
117
);
128
118
129
119
// RELIES ON THE SMALLEST ENTRY IN THE SORTED LIST BEING EMPTY
130
120
// 1 gate
131
- let index = ( index_of_parent * new_parent ) ;
121
+ let index = if new_parent { index_of_parent } else { 0 } ;
132
122
// 3.5 gates
133
- sorted_entries [cast_num_to_u32 (index )] = JSONEntryPacked { value : updated };
123
+ //index is just 0 if new_parent is false, so sorted_entries[0] is useless info
124
+ sorted_entries [index ] = JSONEntryPacked { value : updated };
134
125
135
126
parent_identity_pre = parent_identity_post ;
136
127
}
137
- sorted_entries [0 ] = JSONEntryPacked ::default (); // TODO document why we want to always make 0 a dead entry
128
+ sorted_entries [0 ] = JSONEntryPacked ::default ();
138
129
self .unsorted_json_entries_packed = self .json_entries_packed ;
139
130
self .json_entries_packed = sorted_entries ;
140
131
self .key_hashes = sort_result .sorted ;
0 commit comments