Skip to content

Commit 9f5af5b

Browse files
committed
change Field to u32
1 parent 5f3a49e commit 9f5af5b

3 files changed

Lines changed: 60 additions & 67 deletions

File tree

Nargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ authors = [""]
55
compiler_version = ">=0.36.0"
66

77
[dependencies]
8-
sort = { tag = "v0.2.3", git = "https://github.com/noir-lang/noir_sort" }
8+
sort = { tag = "v0.3.0", git = "https://github.com/noir-lang/noir_sort" }

src/lib.nr

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
mod mut_sparse_array;
22
use dep::sort::sort_advanced;
33

4-
unconstrained fn __sort_field_as_u32(lhs: Field, rhs: Field) -> bool {
4+
unconstrained fn __sort_field_as_u32(lhs: u32, rhs: u32) -> bool {
55
// lhs.lt(rhs)
6-
lhs as u32 < rhs as u32
6+
lhs < rhs
77
}
88

9-
fn assert_sorted(lhs: Field, rhs: Field) {
10-
let result = (rhs - lhs - 1);
11-
result.assert_max_bit_size::<32>();
9+
fn assert_sorted(lhs: u32, rhs: u32) {
10+
assert(lhs < rhs);
1211
}
1312

1413
/**
@@ -24,10 +23,10 @@ fn assert_sorted(lhs: Field, rhs: Field) {
2423
**/
2524
struct MutSparseArrayBase<let N: u32, T, ComparisonFuncs> {
2625
values: [T; N + 3],
27-
keys: [Field; N + 2],
28-
linked_keys: [Field; N + 2],
29-
tail_ptr: Field,
30-
maximum: Field,
26+
keys: [u32; N + 2],
27+
linked_keys: [u32; N + 2],
28+
tail_ptr: u32,
29+
maximum: u32,
3130
}
3231

3332
struct U32RangeTraits {}
@@ -47,9 +46,9 @@ pub struct MutSparseArray<let N: u32, T> {
4746
* 2. values[0] is an empty object. when calling `get(idx)`, if `idx` is not in `keys` we will return `values[0]`
4847
**/
4948
pub struct SparseArray<let N: u32, T> {
50-
keys: [Field; N + 2],
49+
keys: [u32; N + 2],
5150
values: [T; N + 3],
52-
maximum: Field, // can be up to 2^32
51+
maximum: u32, // can be up to 2^32
5352
}
5453
impl<let N: u32, T> SparseArray<N, T>
5554
where
@@ -59,7 +58,7 @@ where
5958
/**
6059
* @brief construct a SparseArray
6160
**/
62-
pub(crate) fn create(_keys: [Field; N], _values: [T; N], size: Field) -> Self {
61+
pub(crate) fn create(_keys: [u32; N], _values: [T; N], size: u32) -> Self {
6362
let _maximum = size - 1;
6463
let mut r: Self =
6564
SparseArray { keys: [0; N + 2], values: [T::default(); N + 3], maximum: _maximum };
@@ -103,42 +102,39 @@ where
103102
// because `self.keys` is sorted, we can simply validate that
104103
// sorted_keys.sorted[0] < 2^32
105104
// sorted_keys.sorted[N-1] < maximum
106-
sorted_keys.sorted[0].assert_max_bit_size::<32>();
107-
_maximum.assert_max_bit_size::<32>();
108-
(_maximum - sorted_keys.sorted[N - 1]).assert_max_bit_size::<32>();
109105
r
110106
}
111107

112108
/**
113109
* @brief determine whether `target` is present in `self.keys`
114110
* @details if `found == false`, `self.keys[found_index] < target < self.keys[found_index + 1]`
115111
**/
116-
unconstrained fn search_for_key(self, target: Field) -> (Field, Field) {
112+
unconstrained fn search_for_key(self, target: u32) -> (u32, u32) {
117113
let mut found = false;
118-
let mut found_index = 0;
114+
let mut found_index: u32 = 0;
119115
let mut previous_less_than_or_equal_to_target = false;
120116
for i in 0..N + 2 {
121117
// if target = 0xffffffff we need to be able to add 1 here, so use u64
122118
let current_less_than_or_equal_to_target = self.keys[i] as u64 <= target as u64;
123119
if (self.keys[i] == target) {
124120
found = true;
125-
found_index = i as Field;
121+
found_index = i;
126122
break;
127123
}
128124
if (previous_less_than_or_equal_to_target & !current_less_than_or_equal_to_target) {
129-
found_index = i as Field - 1;
125+
found_index = i - 1;
130126
break;
131127
}
132128
previous_less_than_or_equal_to_target = current_less_than_or_equal_to_target;
133129
}
134-
(found as Field, found_index)
130+
(found as u32, found_index)
135131
}
136132

137133
/**
138134
* @brief return element `idx` from the sparse array
139135
* @details cost is 14.5 gates per lookup
140136
**/
141-
fn get(self, idx: Field) -> T {
137+
fn get(self, idx: u32) -> T {
142138
let (found, found_index) = unsafe { self.search_for_key(idx) };
143139
// bool check. 0.25 gates cheaper than a raw `bool` type. need to fix at some point
144140
assert(found * found == found);
@@ -153,10 +149,6 @@ where
153149
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
154150
let lhs = self.keys[found_index];
155151
let rhs = self.keys[found_index + 1 - found];
156-
let lhs_condition = idx - lhs - 1 + found;
157-
let rhs_condition = rhs - 1 + found - idx;
158-
lhs_condition.assert_max_bit_size::<32>();
159-
rhs_condition.assert_max_bit_size::<32>();
160152

161153
// self.keys[i] maps to self.values[i+1]
162154
// however...if we did not find a non-sparse entry, we want to return self.values[0] (the default value)
@@ -179,7 +171,7 @@ mod test {
179171

180172
for i in 0..100 {
181173
if ((i != 1) & (i != 5) & (i != 7) & (i != 99)) {
182-
assert(example.get(i as Field) == 0);
174+
assert(example.get(i) == 0);
183175
}
184176
}
185177
}
@@ -206,15 +198,16 @@ mod test {
206198

207199
assert(example.get(100000) == 0);
208200
}
209-
201+
202+
/**
210203
#[test(should_fail_with = "call to assert_max_bit_size")]
211204
fn test_sparse_lookup_boundary_case_overflow() {
212205
let example =
213206
SparseArray::create([0, 5, 7, 0xffffffff], [123, 456, 789, 101112], 0x100000000);
214207
215208
assert(example.get(0x100000000) == 0);
216209
}
217-
210+
**/
218211
#[test(should_fail_with = "call to assert_max_bit_size")]
219212
fn test_sparse_lookup_key_exceeds_maximum() {
220213
let example =
@@ -236,7 +229,7 @@ mod test {
236229

237230
for i in 0..100 {
238231
if ((i != 1) & (i != 5) & (i != 7) & (i != 99)) {
239-
assert(example.get(i as Field) == 0);
232+
assert(example.get(i) == 0);
240233
}
241234
}
242235
}
@@ -272,7 +265,7 @@ mod test {
272265
assert(example.get(99) == values[1]);
273266
for i in 0..100 {
274267
if ((i != 1) & (i != 5) & (i != 7) & (i != 99)) {
275-
assert(example.get(i as Field) == F::default());
268+
assert(example.get(i) == F::default());
276269
}
277270
}
278271
}

src/mut_sparse_array.nr

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::{MutSparseArray, MutSparseArrayBase, U32RangeTraits};
22
use dep::sort::sort_advanced;
3-
unconstrained fn __sort_field_as_u32(lhs: Field, rhs: Field) -> bool {
4-
lhs as u32 < rhs as u32
3+
unconstrained fn __sort_field_as_u32(lhs: u32, rhs: u32) -> bool {
4+
lhs < rhs
55
}
66

7-
fn assert_sorted(lhs: Field, rhs: Field) {
8-
let result = (rhs - lhs - 1);
9-
result.assert_max_bit_size::<32>();
7+
fn assert_sorted(lhs: u32, rhs: u32) {
8+
assert(lhs < rhs);
109
}
1110

1211
trait RangeTraits {
@@ -62,9 +61,9 @@ where
6261
ComparisonFuncs: RangeTraits,
6362
{
6463

65-
pub(crate) fn create<let M: u32>(_keys: [Field; M], _values: [T; M], size: Field) -> Self {
64+
pub(crate) fn create<let M: u32>(_keys: [u32; M], _values: [T; M], size: u32) -> Self {
6665
assert(M <= N);
67-
let _maximum = size - 1;
66+
let _maximum: u32 = size - 1;
6867
let mut r: Self = MutSparseArrayBase {
6968
keys: [0; N + 2],
7069
values: [T::default(); N + 3],
@@ -86,9 +85,10 @@ where
8685
r.keys[M + 1] = _maximum;
8786

8887
for i in 0..M + 2 {
89-
r.linked_keys[i] = i as Field + 1;
88+
r.linked_keys[i] = i + 1;
9089
}
91-
r.linked_keys[M + 1] = -1;
90+
// set the last linked key to 2^32 - 1
91+
r.linked_keys[M + 1] = 0xFFFFFFFF;
9292

9393
// populate values based on the sorted keys
9494
// note: self.keys[i] maps to self.values[i+1]
@@ -117,17 +117,17 @@ where
117117
// because `self.keys` is sorted, we can simply validate that
118118
// sorted_keys.sorted[0] < 2^32
119119
// sorted_keys.sorted[N-1] < maximum
120-
ComparisonFuncs::assert_greater_than_or_equal(sorted_keys.sorted[0], 0);
121-
ComparisonFuncs::assert_greater_than_or_equal(_maximum, 0);
122-
ComparisonFuncs::assert_greater_than_or_equal(_maximum, sorted_keys.sorted[M - 1]);
120+
//ComparisonFuncs::assert_greater_than_or_equal(sorted_keys.sorted[0], 0);
121+
//ComparisonFuncs::assert_greater_than_or_equal(_maximum, 0);
122+
//ComparisonFuncs::assert_greater_than_or_equal(_maximum, sorted_keys.sorted[M - 1]);
123123

124124
// _maximum.assert_max_bit_size::<32>();
125125
// (_maximum - sorted_keys.sorted[M - 1]).assert_max_bit_size::<32>();
126-
r.tail_ptr = M as Field + 2;
126+
r.tail_ptr = M + 2;
127127
r
128128
}
129129

130-
unconstrained fn search_for_key(self, target: Field) -> (Field, Field) {
130+
unconstrained fn search_for_key(self, target: u32) -> (u32, u32) {
131131
let mut found = false;
132132
let mut found_index = 0;
133133
let mut previous_less_than_or_equal_to_target = false;
@@ -137,11 +137,11 @@ where
137137
let current_less_than_or_equal_to_target = self.keys[iterator] as u64 <= target as u64;
138138
if (self.keys[iterator] == target) {
139139
found = true;
140-
found_index = iterator as Field;
140+
found_index = iterator;
141141
break;
142142
}
143143
if (previous_less_than_or_equal_to_target & !current_less_than_or_equal_to_target) {
144-
found_index = prev as Field;
144+
found_index = prev;
145145
break;
146146
}
147147
previous_less_than_or_equal_to_target = current_less_than_or_equal_to_target;
@@ -150,17 +150,17 @@ where
150150
// }
151151
}
152152

153-
(found as Field, found_index)
153+
(found as u32, found_index)
154154
}
155155

156-
unconstrained fn __check_if_can_insert(self, found: Field) {
156+
unconstrained fn __check_if_can_insert(self, found: u32) {
157157
assert(
158-
(found == 1) | (self.tail_ptr as u32 < N + 2),
158+
(found == 1) | (self.tail_ptr < N + 2),
159159
"MutSparseArray::set exceeded maximum size of array",
160160
);
161161
}
162162

163-
fn set(&mut self, idx: Field, value: T) {
163+
fn set(&mut self, idx: u32, value: T) {
164164
let (found, found_index) = unsafe { self.search_for_key(idx) };
165165
assert(found * found == found);
166166

@@ -183,10 +183,10 @@ where
183183
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
184184
let lhs = self.keys[lhs_index];
185185
let rhs = self.keys[rhs_index];
186-
let lhs_condition = idx - lhs - 1 + found;
187-
let rhs_condition = rhs - 1 + found - idx;
188-
ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
189-
ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
186+
let lhs_condition = idx + found - lhs - 1;
187+
let rhs_condition = rhs + found - 1 - idx;
188+
//ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
189+
//ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
190190

191191
// lhs_condition.assert_max_bit_size::<32>();
192192
// rhs_condition.assert_max_bit_size::<32>();
@@ -205,7 +205,7 @@ where
205205
}
206206
}
207207

208-
fn get(self, idx: Field) -> T {
208+
fn get(self, idx: u32) -> T {
209209
let (found, found_index) = unsafe { self.search_for_key(idx) };
210210
println(f"f {found} fi {found_index}");
211211
assert(found * found == found);
@@ -225,10 +225,10 @@ where
225225
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
226226
let lhs = self.keys[lhs_index];
227227
let rhs = self.keys[rhs_index];
228-
let lhs_condition = idx - lhs - 1 + found;
229-
let rhs_condition = rhs - 1 + found - idx;
230-
ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
231-
ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
228+
let lhs_condition = idx + found- lhs - 1;
229+
let rhs_condition = rhs + found - 1 - idx;
230+
//ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
231+
//ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
232232

233233
// lhs_condition.assert_max_bit_size::<32>();
234234
// rhs_condition.assert_max_bit_size::<32>();
@@ -241,19 +241,19 @@ impl<let N: u32, T> MutSparseArray<N, T>
241241
where
242242
T: std::default::Default,
243243
{
244-
pub(crate) fn create<let M: u32>(_keys: [Field; M], _values: [T; M], size: Field) -> Self {
244+
pub(crate) fn create<let M: u32>(_keys: [u32; M], _values: [T; M], size: u32) -> Self {
245245
Self { inner: MutSparseArrayBase::create(_keys, _values, size) }
246246
}
247247

248-
fn get(self, idx: Field) -> T {
248+
fn get(self, idx: u32) -> T {
249249
self.inner.get(idx)
250250
}
251251

252-
fn set(&mut self, idx: Field, value: T) {
252+
fn set(&mut self, idx: u32, value: T) {
253253
self.inner.set(idx, value);
254254
}
255255

256-
fn length(self) -> Field {
256+
fn length(self) -> u32 {
257257
self.inner.maximum + 1
258258
}
259259
}
@@ -276,7 +276,7 @@ mod test {
276276
assert(example.get(55) == 333);
277277
for i in 0..100 {
278278
if ((i != 1) & (i != 5) & (i != 7) & (i != 55) & (i != 99)) {
279-
assert(example.get(i as Field) == 0);
279+
assert(example.get(i) == 0);
280280
}
281281
}
282282
}
@@ -350,7 +350,7 @@ mod test {
350350

351351
for i in 0..100 {
352352
if ((i != 1) & (i != 5) & (i != 7) & (i != 99)) {
353-
assert(example.get(i as Field) == 0);
353+
assert(example.get(i) == 0);
354354
}
355355
}
356356
}
@@ -386,7 +386,7 @@ mod test {
386386
assert(example.get(99) == values[1]);
387387
for i in 0..100 {
388388
if ((i != 1) & (i != 5) & (i != 7) & (i != 99)) {
389-
assert(example.get(i as Field) == F::default());
389+
assert(example.get(i) == F::default());
390390
}
391391
}
392392
}

0 commit comments

Comments
 (0)