From 9b252880f00d08d5f10b8f6923164f127953075f Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Sun, 1 Nov 2026 10:23:14 +0000 Subject: [PATCH] fix: read boundary sentinel values from the sorted keys create() copied the value of key 0 into the start sentinel only when 0 was the first input key, and the value of `maximum` into the end sentinel only when it was the last input key. Otherwise the sentinel held the default value, so get(0) returned 0 and a prover could choose the sentinel slot to read `maximum` as 0. After sorting, those keys can only be keys[1] and keys[M], so check those instead. --- src/lib.nr | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/lib.nr b/src/lib.nr index f2d5a9a..7b91d55 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -45,15 +45,15 @@ where } // insert values that map to our key start and endpoints - // if input_keys[0] = 0 then values[1] must equal input_values[0], so some - // conditional logic is required (same for input_keys[M-1]) + // if 0 is an input key then it is keys[1], and values[1] must equal values[2], so some + // conditional logic is required (same for `maximum` and keys[M]) let mut initial_value = T::default(); - if (input_keys[0] == 0) { - initial_value = input_values[0]; + if (keys[1] == 0) { + initial_value = values[2]; } let mut final_value = T::default(); - if (input_keys[M - 1] == maximum) { - final_value = input_values[M - 1]; + if (keys[M] == maximum) { + final_value = values[M + 1]; } values[1] = initial_value; values[M + 2] = final_value; @@ -245,6 +245,18 @@ mod test { assert(example.get(3) == 30); } + // 0 and `maximum` are also the start and end sentinels, which must hold the + // same values wherever those keys appear in the input + #[test] + fn test_sparse_lookup_unsorted_boundary_keys() { + let example = SparseArray::create([9, 5, 0], [90, 50, 10], 10); + + assert(example.get(0) == 10); + assert(example.get(9) == 90); + assert(example.values[1] == 10); + assert(example.values[5] == 90); + } + #[test] fn test_sparse_lookup_u32() { let example = SparseArray::create(