Skip to content

Commit c1a94c7

Browse files
committed
fix
1 parent 318ff8e commit c1a94c7

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

src/lib.nr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct MutSparseArray<let N: u32, T> {
4848
pub struct SparseArray<let N: u32, T> {
4949
keys: [u32; N + 2],
5050
values: [T; N + 3],
51-
maximum: u32, // can be up to 2^32
51+
maximum: u32, // can be up to 2^32 - 1
5252
}
5353
impl<let N: u32, T> SparseArray<N, T>
5454
where
@@ -59,6 +59,7 @@ where
5959
* @brief construct a SparseArray
6060
**/
6161
pub(crate) fn create(_keys: [u32; N], _values: [T; N], size: u32) -> Self {
62+
assert(size >= 1);
6263
let _maximum = size - 1;
6364
let mut r: Self =
6465
SparseArray { keys: [0; N + 2], values: [T::default(); N + 3], maximum: _maximum };
@@ -150,12 +151,8 @@ where
150151
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
151152
let lhs = self.keys[found_index];
152153
let rhs = self.keys[found_index + 1 - found];
153-
//let lhs_condition = idx - lhs - 1 + found;
154154
assert(lhs + 1 - found <= idx);
155155
assert(idx <= rhs + found - 1);
156-
//let rhs_condition = rhs - 1 + found - idx;
157-
//lhs_condition.assert_max_bit_size::<32>();
158-
//rhs_condition.assert_max_bit_size::<32>();
159156

160157
// self.keys[i] maps to self.values[i+1]
161158
// however...if we did not find a non-sparse entry, we want to return self.values[0] (the default value)

src/mut_sparse_array.nr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ where
6363

6464
pub(crate) fn create<let M: u32>(_keys: [u32; M], _values: [T; M], size: u32) -> Self {
6565
assert(M <= N);
66+
assert(size >= 1);
6667
let _maximum: u32 = size - 1;
6768
let mut r: Self = MutSparseArrayBase {
6869
keys: [0; N + 2],
@@ -117,8 +118,6 @@ where
117118
// because `self.keys` is sorted, we can simply validate that
118119
// sorted_keys.sorted[0] < 2^32
119120
// sorted_keys.sorted[N-1] < maximum
120-
121-
let val = sorted_keys.sorted[M - 1];
122121
assert(_maximum >= sorted_keys.sorted[M - 1]);
123122
r.tail_ptr = M + 2;
124123
r
@@ -218,13 +217,8 @@ where
218217
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
219218
let lhs = self.keys[lhs_index];
220219
let rhs = self.keys[rhs_index];
221-
let lhs_condition = idx + found - lhs - 1;
222-
let rhs_condition = rhs + found - 1 - idx;
223-
//ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
224-
//ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
225-
226-
// lhs_condition.assert_max_bit_size::<32>();
227-
// rhs_condition.assert_max_bit_size::<32>();
220+
assert(lhs + 1 - found <= idx);
221+
assert(idx <= rhs + found - 1);
228222
let value_index = (lhs_index + 1) * found;
229223
self.values[value_index]
230224
}

0 commit comments

Comments
 (0)