Skip to content

Commit

Permalink
noteinterface transformer and fieldselectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunkar committed Mar 11, 2024
1 parent 395b342 commit 4049d99
Show file tree
Hide file tree
Showing 60 changed files with 2,655 additions and 2,168 deletions.
1 change: 1 addition & 0 deletions avm-transpiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 0 additions & 27 deletions noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ struct AddressNote {
}

impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
fn serialize_content(self) -> [Field; ADDRESS_NOTE_LEN]{
[self.address.to_field(), self.owner.to_field(), self.randomness]
}

fn deserialize_content(serialized_note: [Field; ADDRESS_NOTE_LEN]) -> Self {
AddressNote {
address: AztecAddress::from_field(serialized_note[0]),
owner: AztecAddress::from_field(serialized_note[1]),
randomness: serialized_note[2],
header: NoteHeader::empty(),
}
}

fn compute_note_content_hash(self) -> Field {
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash(self.serialize_content(), 0)
Expand Down Expand Up @@ -60,14 +47,6 @@ impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
],0)
}

fn set_header(&mut self, header: NoteHeader) {
self.header = header;
}

fn get_header(note: Self) -> NoteHeader {
note.header
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field) {
let encryption_pub_key = get_public_key(self.owner);
Expand All @@ -82,12 +61,6 @@ impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
);
// docs:end:encrypted
}

fn get_note_type_id() -> Field {
// TODO(#4519): autogenerate
// python -c "print(int(''.join(str(ord(c)) for c in 'AddressNote')))"
6510010011410111511578111116101
}
}

impl AddressNote {
Expand Down
46 changes: 35 additions & 11 deletions noir-projects/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ fn check_note_header<Note, N>(context: PrivateContext, storage_slot: Field, note
assert(header.storage_slot == storage_slot);
}

fn check_note_fields<N>(fields: [Field; N], selects: BoundedVec<Option<Select>, N>) {
fn check_note_fields<N>(serialized_note: [Field; N], selects: BoundedVec<Option<Select>, N>) {
for i in 0..selects.len {
let select = selects.get_unchecked(i).unwrap_unchecked();

let value = serialized_note[select.field_selector.index].to_be_bytes(32);
let offset = select.field_selector.offset;
let length = select.field_selector.length;
let mut value_field = 0 as Field;
let mut acc: Field = 1;
for i in 0..32 {
if i < length {
value_field += value[31 + offset - i] as Field * acc;
acc = acc * 256;
}
}

// Values are computed ahead of time because circuits evaluate all branches
let isEqual = fields[select.field_index] == select.value;
let isLt = fields[select.field_index].lt(select.value);
let isEqual = value_field == select.value.to_field();
let isLt = value_field.lt(select.value.to_field());

if (select.comparator == Comparator.EQ) {
assert(isEqual, "Mismatch return note field.");
Expand Down Expand Up @@ -120,6 +132,8 @@ unconstrained fn get_note_internal<Note, N>(storage_slot: Field) -> Note where N
[],
[],
[],
[],
[],
1, // limit
0, // offset
NoteStatus.ACTIVE,
Expand All @@ -133,14 +147,16 @@ unconstrained fn get_notes_internal<Note, N, FILTER_ARGS>(
storage_slot: Field,
options: NoteGetterOptions<Note, N, FILTER_ARGS>
) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] where Note: NoteInterface<N> {
let (num_selects, select_by, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let (num_selects, select_by_indexes, select_by_offsets, select_by_lengths, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let placeholder_opt_notes = [Option::none(); MAX_NOTE_HASH_READ_REQUESTS_PER_CALL];
let placeholder_fields = [0; GET_NOTES_ORACLE_RETURN_LENGTH];
let placeholder_note_length = [0; N];
let opt_notes = oracle::notes::get_notes(
storage_slot,
num_selects,
select_by,
select_by_indexes,
select_by_offsets,
select_by_lengths,
select_values,
select_comparators,
sort_by,
Expand All @@ -162,14 +178,16 @@ unconstrained pub fn view_notes<Note, N>(
storage_slot: Field,
options: NoteViewerOptions<Note, N>
) -> [Option<Note>; MAX_NOTES_PER_PAGE] where Note: NoteInterface<N> {
let (num_selects, select_by, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let (num_selects, select_by_indexes, select_by_offsets, select_by_lengths, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let placeholder_opt_notes = [Option::none(); MAX_NOTES_PER_PAGE];
let placeholder_fields = [0; VIEW_NOTE_ORACLE_RETURN_LENGTH];
let placeholder_note_length = [0; N];
oracle::notes::get_notes(
storage_slot,
num_selects,
select_by,
select_by_indexes,
select_by_offsets,
select_by_lengths,
select_values,
select_comparators,
sort_by,
Expand All @@ -186,16 +204,20 @@ 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], [u8; N], [u8; N], [u8; N]) {
) -> (u8, [u8; N], [u8; N], [u8; N], [Field; N], [u8; N], [u8; N], [u8; N]) {
let mut num_selects = 0;
let mut select_by = [0; N];
let mut select_by_indexes = [0; N];
let mut select_by_offsets = [0; N];
let mut select_by_lengths = [0; N];
let mut select_values = [0; N];
let mut select_comparators = [0; N];

for i in 0..selects.len {
let select = selects.get(i);
if select.is_some() {
select_by[num_selects] = select.unwrap_unchecked().field_index;
select_by_indexes[num_selects] = select.unwrap_unchecked().field_selector.index;
select_by_offsets[num_selects] = select.unwrap_unchecked().field_selector.offset;
select_by_lengths[num_selects] = select.unwrap_unchecked().field_selector.length;
select_values[num_selects] = select.unwrap_unchecked().value;
select_comparators[num_selects] = select.unwrap_unchecked().comparator;
num_selects += 1;
Expand All @@ -212,5 +234,7 @@ unconstrained fn flatten_options<Note, N>(
};
}

(num_selects, select_by, select_values, select_comparators, sort_by, sort_order)
(
num_selects, select_by_indexes, select_by_offsets, select_by_lengths, select_values, select_comparators, sort_by, sort_order
)
}
34 changes: 27 additions & 7 deletions noir-projects/aztec-nr/aztec/src/note/note_getter_options.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use dep::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL};
use dep::std::option::Option;
use dep::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::ToField};
use crate::note::note_interface::NoteInterface;

struct FieldSelector {
index: u8,
offset: u8,
length: u8,
}

struct ComparatorEnum {
EQ: u8,
NEQ: u8,
Expand All @@ -20,14 +27,14 @@ global Comparator = ComparatorEnum {
};

struct Select {
field_index: u8,
field_selector: FieldSelector,
value: Field,
comparator: u8,
}

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

Expand Down Expand Up @@ -118,11 +125,24 @@ impl<Note, N, FILTER_ARGS> NoteGetterOptions<Note, N, FILTER_ARGS> {
}

// This method adds a `Select` criterion to the options.
// It takes a field_index indicating which field to select,
// It takes a field_selector 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<u8>) -> Self {
self.selects.push(Option::some(Select::new(field_index, value, comparator.unwrap_or(Comparator.EQ))));
pub fn select<T>(
&mut self,
field_selector: FieldSelector,
value: T,
comparator: Option<u8>
) -> Self where T: ToField {
self.selects.push(
Option::some(
Select::new(
field_selector,
value.to_field(),
comparator.unwrap_or(Comparator.EQ)
)
)
);
*self
}

Expand Down
22 changes: 18 additions & 4 deletions noir-projects/aztec-nr/aztec/src/note/note_viewer_options.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::note::note_getter_options::{Select, Sort, Comparator, NoteStatus};
use dep::protocol_types::{constants::MAX_NOTES_PER_PAGE};
use dep::std::option::Option;
use crate::note::note_getter_options::{FieldSelector, Select, Sort, Comparator, NoteStatus};
use dep::protocol_types::{constants::MAX_NOTES_PER_PAGE, traits::ToField};
use crate::note::note_interface::NoteInterface;

// docs:start:NoteViewerOptions
Expand Down Expand Up @@ -27,8 +28,21 @@ impl<Note, N> NoteViewerOptions<Note, N> {
// 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<u8>) -> Self {
self.selects.push(Option::some(Select::new(field_index, value, comparator.unwrap_or(Comparator.EQ))));
pub fn select<T>(
&mut self,
field_selector: FieldSelector,
value: T,
comparator: Option<u8>
) -> Self where T: ToField {
self.selects.push(
Option::some(
Select::new(
field_selector,
value.to_field(),
comparator.unwrap_or(Comparator.EQ)
)
)
);
*self
}

Expand Down
20 changes: 15 additions & 5 deletions noir-projects/aztec-nr/aztec/src/oracle/notes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ unconstrained pub fn notify_nullified_note<N>(nullifier: Field, inner_note_hash:
fn get_notes_oracle<N, S>(
_storage_slot: Field,
_num_selects: u8,
_select_by: [u8; N],
_select_by_indexes: [u8; N],
_select_by_offsets: [u8; N],
_select_by_lengths: [u8; N],
_select_values: [Field; N],
_select_comparators: [u8; N],
_sort_by: [u8; N],
Expand All @@ -45,7 +47,9 @@ fn get_notes_oracle<N, S>(
unconstrained fn get_notes_oracle_wrapper<N, S>(
storage_slot: Field,
num_selects: u8,
select_by: [u8; N],
select_by_indexes: [u8; N],
select_by_offsets: [u8; N],
select_by_lengths: [u8; N],
select_values: [Field; N],
select_comparators: [u8; N],
sort_by: [u8; N],
Expand All @@ -59,7 +63,9 @@ unconstrained fn get_notes_oracle_wrapper<N, S>(
get_notes_oracle(
storage_slot,
num_selects,
select_by,
select_by_indexes,
select_by_offsets,
select_by_lengths,
select_values,
select_comparators,
sort_by,
Expand All @@ -75,7 +81,9 @@ unconstrained fn get_notes_oracle_wrapper<N, S>(
unconstrained pub fn get_notes<Note, N, M, S, NS>(
storage_slot: Field,
num_selects: u8,
select_by: [u8; M],
select_by_indexes: [u8; M],
select_by_offsets: [u8; M],
select_by_lengths: [u8; M],
select_values: [Field; M],
select_comparators: [u8; M],
sort_by: [u8; M],
Expand All @@ -90,7 +98,9 @@ unconstrained pub fn get_notes<Note, N, M, S, NS>(
let fields = get_notes_oracle_wrapper(
storage_slot,
num_selects,
select_by,
select_by_indexes,
select_by_offsets,
select_by_lengths,
select_values,
select_comparators,
sort_by,
Expand Down
25 changes: 0 additions & 25 deletions noir-projects/aztec-nr/field-note/src/field_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ struct FieldNote {
}

impl NoteInterface<FIELD_NOTE_LEN> for FieldNote {
fn serialize_content(self) -> [Field; FIELD_NOTE_LEN]{
[self.value]
}

fn deserialize_content(serialized_note: [Field; FIELD_NOTE_LEN]) -> Self {
FieldNote {
value: serialized_note[0],
header: NoteHeader::empty(),
}
}

fn compute_note_content_hash(self) -> Field {
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash(self.serialize_content(), 0)
Expand All @@ -40,25 +29,11 @@ impl NoteInterface<FIELD_NOTE_LEN> for FieldNote {
0
}

fn set_header(&mut self, header: NoteHeader) {
self.header = header;
}

fn get_header(self) -> NoteHeader {
self.header
}

fn broadcast(self, context: &mut PrivateContext, slot: Field) {
assert(
false, "FieldNote does not support broadcast. Add it to PXE directly using the `.addNote` function."
);
}

fn get_note_type_id() -> Field {
// TODO(#4519): autogenerate
// python -c "print(int(''.join(str(ord(c)) for c in 'FieldNote')))"
7010510110810078111116101
}
}

impl FieldNote {
Expand Down
Loading

0 comments on commit 4049d99

Please sign in to comment.