Skip to content

Commit

Permalink
feat!: make options.limit a compile-time constant (#7027)
Browse files Browse the repository at this point in the history
This will guarantee that we can do things such as `for i in
0..options.limit` - I changed some of the callsites to iterate over the
notes using it. This will reduce gate count for callers that set a limit
value.
  • Loading branch information
nventuro authored Jun 12, 2024
1 parent bc80e85 commit 78cd640
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Aztec is in full-speed development. Literally every version breaks compatibility

## TBD

### [Aztec.nr] `options.limit` has to be constant

The `limit` parameter in `NoteGetterOptions` and `NoteViewerOptions` is now required to be a compile-time constant. This allows performing loops over this value, which leads to reduced circuit gate counts when setting a `limit` value.

### [Aztec.nr] emit encrypted logs

Emitting or broadcasting encrypted notes are no longer done as part of the note creation, but must explicitly be either emitted or discarded instead.
Expand Down
5 changes: 2 additions & 3 deletions noir-projects/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ fn constrain_get_notes_internal<Note, N, M, FILTER_ARGS>(
num_notes += 1;
};
}
if options.limit != 0 {
assert(num_notes <= options.limit, "Got more notes than limit.");
}

assert(num_notes <= options.limit, "Got more notes than limit.");

assert(num_notes != 0, "Cannot return zero notes");

Expand Down
4 changes: 4 additions & 0 deletions noir-projects/aztec-nr/aztec/src/note/note_getter_options.nr
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ impl<Note, N, M, FILTER_ARGS> NoteGetterOptions<Note, N, M, FILTER_ARGS> {
// This method lets you set a limit for the maximum number of notes to be retrieved in a single query result.
pub fn set_limit(&mut self, limit: u32) -> Self {
assert(limit <= MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32);
// By requesting that the limit is a constant, we guarantee that it will be possible to loop over it, reducing
// gate counts when a limit has been set.
assert_constant(limit);

self.limit = limit;
*self
}
Expand Down
4 changes: 4 additions & 0 deletions noir-projects/aztec-nr/aztec/src/note/note_viewer_options.nr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl<Note, N, M> NoteViewerOptions<Note, N, M> {

pub fn set_limit(&mut self, limit: u32) -> Self {
assert(limit <= MAX_NOTES_PER_PAGE as u32);
// By requesting that the limit is a constant, we guarantee that it will be possible to loop over it, reducing
// gate counts when a limit has been set. This isn't required in unconstrained code, but we still keep this
// requirement here for API consistency.
assert_constant(limit);
self.limit = limit;
*self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<Context> EasyPrivateUint<&mut PrivateContext> {
// docs:end:get_notes

let mut minuend: u64 = 0;
for i in 0..maybe_notes.len() {
for i in 0..options.limit {
if maybe_notes[i].is_some() {
let note = maybe_notes[i].unwrap_unchecked();

Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn decrement_by_at_most(
let opt_notes = balance.get_notes(options);

let mut decremented = 0;
for i in 0..opt_notes.len() {
for i in 0..options.limit {
if opt_notes[i].is_some() {
let note = opt_notes[i].unwrap_unchecked();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Deck<&mut PrivateContext> {
let options = NoteGetterOptions::with_filter(filter_cards, cards);
let maybe_notes = self.set.get_notes(options);
let mut found_cards = [Option::none(); N];
for i in 0..maybe_notes.len() {
for i in 0..options.limit {
if maybe_notes[i].is_some() {
let card_note = CardNote::from_note(maybe_notes[i].unwrap_unchecked());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
// docs:end:get_notes

let mut minuend: U128 = U128::from_integer(0);
for i in 0..maybe_notes.len() {
for i in 0..options.limit {
if maybe_notes[i].is_some() {
let note = maybe_notes[i].unwrap_unchecked();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
// docs:end:get_notes

let mut minuend: U128 = U128::from_integer(0);
for i in 0..maybe_notes.len() {
for i in 0..options.limit {
if maybe_notes[i].is_some() {
let note = maybe_notes[i].unwrap_unchecked();

Expand Down

0 comments on commit 78cd640

Please sign in to comment.