fix: place values at sorted key positions in create - #22
Merged
Conversation
SparseArray::create and MutSparseArrayBase::create populated value slots with _values[sort_indices[i]], but sort_indices maps input position to sorted position, so this applies the permutation in the wrong direction. For any key ordering whose sort permutation is not its own inverse (e.g. a 3-cycle like [3, 1, 2]), get(key) returned a different key's value. Write each input value to its key's sorted slot instead, and add regression tests using a non-involution key ordering. Fixes noir-lang/noir-library-claude#6 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Extract the duplicated sort-and-populate block (key sorting, start/end point insertion, value placement, boundary values, maximum check) into a single insert_sorted_keys_and_values helper, and drop the duplicated __sort/assert_sorted functions from mut_sparse_array.nr. The wrong-index bug fixed in the previous commit existed identically in both copies; this ensures the logic lives in one place. No circuit cost change: ACIR opcode count is identical before and after (verified with nargo info on a scratch binary exercising create/get/set for both types), since Noir fully inlines constrained functions. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
TomAFrench
approved these changes
Jul 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem Resolved
Resolves https://github.com/noir-lang/noir-library-claude/issues/6
Summary of Changes
The use of
sorted_keys.sort_indiceswas reversed: its value says "x went to y" but it was used like "y got to x". It worked fine on the existing tests because they only needed one permutation of the original array so "x went to y" was the same as "y went to x".Then I checked if it was possible to extract the duplicate logic and, yes, it was possible so that's the second commit.
I also sent a PR to noir_sort to document the
sort_advancedfunction because it's not immediately clear what it returns: noir-lang/noir_sort#28PR Checklist
cargo fmton default settings.