-
Notifications
You must be signed in to change notification settings - Fork 593
feat: dedup arrays before reset #20473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LeilaWang
wants to merge
2
commits into
next
Choose a base branch
from
lw/dedup_before_reset
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,7 @@ | |
| "dbanks", | ||
| "DCMAKE", | ||
| "decrementation", | ||
| "dedup", | ||
| "deduped", | ||
| "defi", | ||
| "deinit", | ||
|
|
||
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
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
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
18 changes: 18 additions & 0 deletions
18
...cuits/crates/private-kernel-lib/src/components/reset_output_validator/check_duplicates.nr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use types::{ | ||
| abis::validation_requests::KeyValidationRequestAndGenerator, | ||
| side_effect::{Counted, Scoped}, | ||
| }; | ||
|
|
||
| /// Two read requests are considered duplicates if they read the same value from the same contract. | ||
| /// The counter (when the read happened) is irrelevant for dedup purposes. | ||
| pub fn are_duplicate_read_requests(a: Scoped<Counted<Field>>, b: Scoped<Counted<Field>>) -> bool { | ||
| (a.inner.inner == b.inner.inner) & (a.contract_address == b.contract_address) | ||
| } | ||
|
|
||
| /// Two key validation requests are considered duplicates if they are exactly the same. | ||
| pub fn are_duplicate_key_validation_requests( | ||
| a: Scoped<KeyValidationRequestAndGenerator>, | ||
| b: Scoped<KeyValidationRequestAndGenerator>, | ||
| ) -> bool { | ||
| a == b | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
...s/noir-protocol-circuits/crates/private-kernel-lib/src/reset/deduped_array/dedup_array.nr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| use types::{traits::Empty, utils::arrays::ClaimedLengthArray}; | ||
|
|
||
| /// Unconstrained function to generate a deduplicated array and index hints. | ||
| /// | ||
| /// For each item in `original`, checks if an equal item (under `eq_fn`) already exists in the | ||
| /// deduped output. If so, records the existing index in `dedup_hints`. If not, appends the item | ||
| /// to the deduped output and records the new index. | ||
| /// | ||
| /// Returns: (deduped_array, dedup_hints) where dedup_hints[i] is the index in deduped_array | ||
| /// that original[i] maps to. | ||
| pub unconstrained fn dedup_array<T, let N: u32, Env>( | ||
| original: ClaimedLengthArray<T, N>, | ||
| eq_fn: fn[Env](T, T) -> bool, | ||
| ) -> (ClaimedLengthArray<T, N>, [u32; N]) | ||
| where | ||
| T: Empty, | ||
| { | ||
| let mut deduped: ClaimedLengthArray<T, N> = ClaimedLengthArray::empty(); | ||
| let mut hints = [0 as u32; N]; | ||
|
|
||
| for i in 0..original.length { | ||
| let item = original.array[i]; | ||
|
|
||
| // Search for an existing match in the deduped array. | ||
| let mut found = false; | ||
| let mut found_index = 0; | ||
| for j in 0..deduped.length { | ||
| if eq_fn(item, deduped.array[j]) { | ||
| found = true; | ||
| found_index = j; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if found { | ||
| hints[i] = found_index; | ||
| } else { | ||
| hints[i] = deduped.length; | ||
| deduped.push(item); | ||
| } | ||
| } | ||
|
|
||
| (deduped, hints) | ||
| } | ||
|
|
||
| mod tests { | ||
| use super::dedup_array; | ||
| use types::utils::arrays::ClaimedLengthArray; | ||
|
|
||
| fn field_eq(a: Field, b: Field) -> bool { | ||
| a == b | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn all_unique() { | ||
| let original = ClaimedLengthArray { array: [11, 22, 33, 0], length: 3 }; | ||
| let (deduped, hints) = dedup_array(original, field_eq); | ||
| assert_eq(deduped.length, 3); | ||
| assert_eq(deduped.array[0], 11); | ||
| assert_eq(deduped.array[1], 22); | ||
| assert_eq(deduped.array[2], 33); | ||
| assert_eq(hints[0], 0); | ||
| assert_eq(hints[1], 1); | ||
| assert_eq(hints[2], 2); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn all_duplicates() { | ||
| let original = ClaimedLengthArray { array: [11, 11, 11, 0], length: 3 }; | ||
| let (deduped, hints) = dedup_array(original, field_eq); | ||
| assert_eq(deduped.length, 1); | ||
| assert_eq(deduped.array[0], 11); | ||
| assert_eq(hints[0], 0); | ||
| assert_eq(hints[1], 0); | ||
| assert_eq(hints[2], 0); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn mix_of_unique_and_duplicate() { | ||
| let original = ClaimedLengthArray { array: [11, 22, 11, 33, 22, 0, 0], length: 5 }; | ||
| let (deduped, hints) = dedup_array(original, field_eq); | ||
| assert_eq(deduped.length, 3); | ||
| assert_eq(deduped.array[0], 11); | ||
| assert_eq(deduped.array[1], 22); | ||
| assert_eq(deduped.array[2], 33); | ||
| assert_eq(hints[0], 0); | ||
| assert_eq(hints[1], 1); | ||
| assert_eq(hints[2], 0); | ||
| assert_eq(hints[3], 2); | ||
| assert_eq(hints[4], 1); | ||
| assert_eq(hints[5], 0); | ||
| assert_eq(hints[6], 0); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn empty_array() { | ||
| let original = ClaimedLengthArray { array: [0, 0, 0, 0], length: 0 }; | ||
| let (deduped, _hints) = dedup_array(original, field_eq); | ||
| assert_eq(deduped.length, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn single_item() { | ||
| let original = ClaimedLengthArray { array: [42, 0, 0, 0], length: 1 }; | ||
| let (deduped, hints) = dedup_array(original, field_eq); | ||
| assert_eq(deduped.length, 1); | ||
| assert_eq(deduped.array[0], 42); | ||
| assert_eq(hints[0], 0); | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
...-projects/noir-protocol-circuits/crates/private-kernel-lib/src/reset/deduped_array/mod.nr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pub mod dedup_array; | ||
| pub mod validate_deduped_array; |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still amazed that Noir makes this the same constraint cost as: