Skip to content

Commit

Permalink
chore(noir): extend 4681 bitsize refactor (#4689)
Browse files Browse the repository at this point in the history
Built ontop of #4681

---------

Co-authored-by: sirasistant <sirasistant@gmail.com>
  • Loading branch information
Maddiaa0 and sirasistant committed Feb 27, 2024
1 parent 0702dc6 commit 811d767
Show file tree
Hide file tree
Showing 88 changed files with 1,151 additions and 1,372 deletions.
1 change: 0 additions & 1 deletion docs/docs/developers/contracts/references/storage/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type = "contract"

[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/aztec" }
safe_math = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/safe-math"}
authwit={ git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/authwit"}
compressed_string = {git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/compressed-string"}
```
Expand Down
8 changes: 0 additions & 8 deletions docs/docs/developers/contracts/resources/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ protocol_types = { git="https://github.com/AztecProtocol/aztec-packages/", tag="

This library contains types that are used in the Aztec protocol. Find it on [GitHub](https://github.com/AztecProtocol/aztec-packages/tree/master/noir-projects/noir-protocol-circuits/src/crates/types/src).

## Safe math

```toml
safe_math = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/safe-math" }
```

This is a library for safe arithmetic, similar to OpenZeppelin's safe math library. Find it on [GitHub](https://github.com/AztecProtocol/aztec-packages/tree/master/noir-projects/aztec-nr/safe-math).

## Value note

```toml
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/developers/contracts/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Your folder should look like:

Before writing the contracts, we must add the aztec.nr library. This adds smart contract utility functions for interacting with the Aztec network.

3. Finally, add relevant aztec-nr dependencies that you might use such as `aztec.nr`, `value_note` and `safe_math` libraries.
3. Finally, add relevant aztec-nr dependencies that you might use such as `aztec.nr` and `value_note` libraries.

Open Nargo.toml that is in the `contracts/example_contract` folder, and add the dependency section as follows

Expand All @@ -74,7 +74,6 @@ aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_

# Utility dependencies
value_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/value-note"}
safe_math = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/safe-math"}
```

:::info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Then, open the `contracts/token/Nargo.toml` configuration file, and add the `azt
[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/aztec" }
authwit = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/authwit"}
safe_math = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/safe-math"}
compressed_string = {git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/compressed-string"}
```

Expand Down
1 change: 0 additions & 1 deletion docs/docs/developers/tutorials/writing_token_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type = "contract"

[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/aztec" }
safe_math = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/safe-math"}
authwit={ git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/authwit"}
compressed_string = {git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/compressed-string"}
```
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ members = [
"compressed-string",
"easy-private-state",
"field-note",
"safe-math",
"slow-updates-tree",
"value-note",
]
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ aztec = { git = "https://github.com/AztecProtocol/aztec-nr", tag = "master" , di

# Optional libraries
easy_private_state = { git = "https://github.com/AztecProtocol/aztec-nr", tag = "master" , directory = "easy-private-state" }
safe_math = { git = "https://github.com/AztecProtocol/aztec-nr", tag = "master" , directory = "safe-math" }
value_note = { git = "https://github.com/AztecProtocol/aztec-nr", tag = "master" , directory = "value-note" }
```

Expand Down
18 changes: 7 additions & 11 deletions noir-projects/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use dep::std::option::Option;
use dep::protocol_types::{
constants::{
MAX_READ_REQUESTS_PER_CALL,
GET_NOTE_ORACLE_RETURN_LENGTH,
GET_NOTES_ORACLE_RETURN_LENGTH,
MAX_NOTES_PER_PAGE,
VIEW_NOTE_ORACLE_RETURN_LENGTH,
},
MAX_READ_REQUESTS_PER_CALL, GET_NOTE_ORACLE_RETURN_LENGTH, GET_NOTES_ORACLE_RETURN_LENGTH,
MAX_NOTES_PER_PAGE, VIEW_NOTE_ORACLE_RETURN_LENGTH
}
};
use crate::context::PrivateContext;
use crate::note::{
note_getter_options::{NoteGetterOptions, Select, Sort, SortOrder, Comparator, NoteStatus},
note_interface::NoteInterface,
note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_consumption,
note_interface::NoteInterface, note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_consumption
};
use crate::oracle;

Expand Down Expand Up @@ -60,7 +56,7 @@ fn check_notes_order<N>(
for i in 0..sorts.len {
let sort = sorts.get_unchecked(i).unwrap_unchecked();
let eq = fields_0[sort.field_index] == fields_1[sort.field_index];
let lt = fields_0[sort.field_index] as u120 < fields_1[sort.field_index] as u120;
let lt = fields_0[sort.field_index].lt(fields_1[sort.field_index]);
if sort.order == SortOrder.ASC {
assert(eq | lt, "Return notes not sorted in ascending order.");
} else if !eq {
Expand Down Expand Up @@ -195,7 +191,7 @@ unconstrained pub fn view_notes<Note, N>(
unconstrained fn flatten_options<Note, N>(
selects: BoundedVec<Option<Select>, N>,
sorts: BoundedVec<Option<Sort>, N>
) -> (u8, [u8; N], [Field; N], [u3; N], [u8; N], [u2; N]) {
) -> (u8, [u8; N], [Field; N], [u8; N], [u8; N], [u8; N]) {
let mut num_selects = 0;
let mut select_by = [0; N];
let mut select_values = [0; N];
Expand Down
50 changes: 24 additions & 26 deletions noir-projects/aztec-nr/aztec/src/note/note_getter_options.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use dep::std::option::Option;
use dep::protocol_types::{
constants::MAX_READ_REQUESTS_PER_CALL,
};
use dep::protocol_types::{constants::MAX_READ_REQUESTS_PER_CALL};
use crate::note::note_interface::NoteInterface;

struct ComparatorEnum {
EQ: u3,
NEQ: u3,
LT: u3,
LTE: u3,
GT: u3,
GTE: u3,
EQ: u8,
NEQ: u8,
LT: u8,
LTE: u8,
GT: u8,
GTE: u8,
}

global Comparator = ComparatorEnum {
Expand All @@ -25,18 +23,18 @@ global Comparator = ComparatorEnum {
struct Select {
field_index: u8,
value: Field,
comparator: u3,
comparator: u8,
}

impl Select {
pub fn new(field_index: u8, value: Field, comparator: u3) -> Self {
pub fn new(field_index: u8, value: Field, comparator: u8) -> Self {
Select { field_index, value, comparator }
}
}

struct SortOrderEnum {
DESC: u2,
ASC: u2,
DESC: u8,
ASC: u8,
}

global SortOrder = SortOrderEnum {
Expand All @@ -46,18 +44,18 @@ global SortOrder = SortOrderEnum {

struct Sort {
field_index: u8,
order: u2,
order: u8,
}

impl Sort {
pub fn new(field_index: u8, order: u2) -> Self {
pub fn new(field_index: u8, order: u8) -> Self {
Sort { field_index, order }
}
}

struct NoteStatusEnum {
ACTIVE: u2,
ACTIVE_OR_NULLIFIED: u2,
ACTIVE: u8,
ACTIVE_OR_NULLIFIED: u8,
}

global NoteStatus = NoteStatusEnum {
Expand All @@ -81,7 +79,7 @@ struct NoteGetterOptions<Note, N, FILTER_ARGS> {
offset: u32,
filter: fn ([Option<Note>; MAX_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS,
status: u2,
status: u8,
}
// docs:end:NoteGetterOptions

Expand All @@ -99,39 +97,39 @@ impl<Note, N, FILTER_ARGS> NoteGetterOptions<Note, N, FILTER_ARGS> {
offset: 0,
filter: return_all_notes,
filter_args: 0,
status: NoteStatus.ACTIVE,
status: NoteStatus.ACTIVE
}
}

// This function initializes a NoteGetterOptions with a filter, which takes the notes returned from the database and filter_args as its parameters.
// `filter_args` allows you to provide additional data or context to the custom filter.
pub fn with_filter(
filter: fn ([Option<Note>; MAX_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS,
filter: fn([Option<Note>; MAX_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS
) -> Self where Note: NoteInterface<N> {
NoteGetterOptions {
NoteGetterOptions {
selects: BoundedVec::new(Option::none()),
sorts: BoundedVec::new(Option::none()),
limit: MAX_READ_REQUESTS_PER_CALL as u32,
offset: 0,
filter,
filter_args,
status: NoteStatus.ACTIVE,
status: NoteStatus.ACTIVE
}
}

// This method adds a `Select` criterion to the options.
// It takes a field_index indicating which field to select,
// a value representing the specific value to match in that field, and
// a comparator (For possible values of comparators, please see the Comparator enum above)
pub fn select(&mut self, field_index: u8, value: Field, comparator: Option<u3>) -> Self {
pub fn select(&mut self, field_index: u8, value: Field, comparator: Option<u8>) -> Self {
self.selects.push(Option::some(Select::new(field_index, value, comparator.unwrap_or(Comparator.EQ))));
*self
}

// This method adds a `Sort` criterion to the options.
// It takes a field_index indicating which field to sort by and an order (SortOrder) to determine the sorting direction.
pub fn sort(&mut self, field_index: u8, order: u2) -> Self {
pub fn sort(&mut self, field_index: u8, order: u8) -> Self {
self.sorts.push(Option::some(Sort::new(field_index, order)));
*self
}
Expand All @@ -150,7 +148,7 @@ impl<Note, N, FILTER_ARGS> NoteGetterOptions<Note, N, FILTER_ARGS> {
}

// This method sets the status value, which determines whether to retrieve active or nullified notes.
pub fn set_status(&mut self, status: u2) -> Self {
pub fn set_status(&mut self, status: u8) -> Self {
self.status = status;
*self
}
Expand Down
14 changes: 6 additions & 8 deletions noir-projects/aztec-nr/aztec/src/note/note_viewer_options.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use dep::std::option::Option;
use crate::note::note_getter_options::{Select, Sort, Comparator, NoteStatus};
use dep::protocol_types::{
constants::MAX_NOTES_PER_PAGE,
};
use dep::protocol_types::{constants::MAX_NOTES_PER_PAGE};
use crate::note::note_interface::NoteInterface;

// docs:start:NoteViewerOptions
Expand All @@ -11,7 +9,7 @@ struct NoteViewerOptions<Note, N> {
sorts: BoundedVec<Option<Sort>, N>,
limit: u32,
offset: u32,
status: u2,
status: u8,
}
// docs:end:NoteViewerOptions

Expand All @@ -22,20 +20,20 @@ impl<Note, N> NoteViewerOptions<Note, N> {
sorts: BoundedVec::new(Option::none()),
limit: MAX_NOTES_PER_PAGE as u32,
offset: 0,
status: NoteStatus.ACTIVE,
status: NoteStatus.ACTIVE
}
}

// This method adds a `Select` criterion to the options.
// It takes a field_index indicating which field to select,
// a value representing the specific value to match in that field, and
// a comparator (For possible values of comparators, please see the Comparator enum from note_getter_options)
pub fn select(&mut self, field_index: u8, value: Field, comparator: Option<u3>) -> Self {
pub fn select(&mut self, field_index: u8, value: Field, comparator: Option<u8>) -> Self {
self.selects.push(Option::some(Select::new(field_index, value, comparator.unwrap_or(Comparator.EQ))));
*self
}

pub fn sort(&mut self, field_index: u8, order: u2) -> Self {
pub fn sort(&mut self, field_index: u8, order: u8) -> Self {
self.sorts.push(Option::some(Sort::new(field_index, order)));
*self
}
Expand All @@ -52,7 +50,7 @@ impl<Note, N> NoteViewerOptions<Note, N> {
}

// This method sets the status value, which determines whether to retrieve active or nullified notes.
pub fn set_status(&mut self, status: u2) -> Self {
pub fn set_status(&mut self, status: u8) -> Self {
self.status = status;
*self
}
Expand Down
28 changes: 11 additions & 17 deletions noir-projects/aztec-nr/aztec/src/oracle/notes.nr
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use dep::std::option::Option;
use crate::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
};
use crate::note::{note_header::NoteHeader, note_interface::NoteInterface};

use dep::protocol_types::{
address::AztecAddress,
utils::arr_copy_slice,
};
use dep::protocol_types::{address::AztecAddress, utils::arr_copy_slice};

#[oracle(notifyCreatedNote)]
fn notify_created_note_oracle<N>(
Expand Down Expand Up @@ -39,12 +33,12 @@ fn get_notes_oracle<N, S>(
_num_selects: u8,
_select_by: [u8; N],
_select_values: [Field; N],
_select_comparators: [u3; N],
_select_comparators: [u8; N],
_sort_by: [u8; N],
_sort_order: [u2; N],
_sort_order: [u8; N],
_limit: u32,
_offset: u32,
_status: u2,
_status: u8,
_return_size: u32,
_placeholder_fields: [Field; S]
) -> [Field; S] {}
Expand All @@ -54,12 +48,12 @@ unconstrained fn get_notes_oracle_wrapper<N, S>(
num_selects: u8,
select_by: [u8; N],
select_values: [Field; N],
select_comparators: [u3; N],
select_comparators: [u8; N],
sort_by: [u8; N],
sort_order: [u2; N],
sort_order: [u8; N],
limit: u32,
offset: u32,
status: u2,
status: u8,
mut placeholder_fields: [Field; S]
) -> [Field; S] {
let return_size = placeholder_fields.len() as u32;
Expand All @@ -84,12 +78,12 @@ unconstrained pub fn get_notes<Note, N, M, S, NS>(
num_selects: u8,
select_by: [u8; M],
select_values: [Field; M],
select_comparators: [u3; M],
select_comparators: [u8; M],
sort_by: [u8; M],
sort_order: [u2; M],
sort_order: [u8; M],
limit: u32,
offset: u32,
status: u2,
status: u8,
mut placeholder_opt_notes: [Option<Note>; S], // TODO: Remove it and use `limit` to initialize the note array.
placeholder_fields: [Field; NS], // TODO: Remove it and use `limit` to initialize the note array.
_placeholder_note_length: [Field; N] // Turbofish hack? Compiler breaks calculating read_offset unless we add this parameter
Expand Down
Loading

0 comments on commit 811d767

Please sign in to comment.