Skip to content

fix: place values at sorted key positions in create - #22

Merged
TomAFrench merged 2 commits into
masterfrom
fix/sorted-values-permutation
Jul 29, 2026
Merged

fix: place values at sorted key positions in create#22
TomAFrench merged 2 commits into
masterfrom
fix/sorted-values-permutation

Conversation

@asterite

@asterite asterite commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Problem Resolved

Resolves https://github.com/noir-lang/noir-library-claude/issues/6

Summary of Changes

The use of sorted_keys.sort_indices was 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_advanced function because it's not immediately clear what it returns: noir-lang/noir_sort#28

PR Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

asterite and others added 2 commits July 24, 2026 16:21
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>
@asterite asterite changed the title Fix/sorted values permutation fix: place values at sorted key positions in create Jul 24, 2026
@asterite
asterite requested a review from TomAFrench July 27, 2026 12:28
@TomAFrench
TomAFrench merged commit 9da193f into master Jul 29, 2026
12 of 13 checks passed
@TomAFrench
TomAFrench deleted the fix/sorted-values-permutation branch July 29, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants