Skip to content

Commit

Permalink
chore: move some nr utils around (#10553)
Browse files Browse the repository at this point in the history
This PR improves bunch of small things. I noticed we were using small
utils exported from protocol-circuits, notably `arr_copy_slice`, which
seems like a bad idea as it we then unnecessarily couple those
codebases. Also, said utility was quite awful: the name is bad (since
'slice' already has meaning inside of noir), and it mutated an argument
which it also returned. I replaced this for `subarray`, which I think is
much nicer.

I also took the liberty of replacing usage of this in the protocol
circuits, removing `conditional_assign` (which was unused) and improving
some array management in which the array lengths were unnecessarily
being imported and forced in situations were the compiler could already
infer them.

Finally I moved `collapse` into its own file as it was before in order
to better structure our submodules with each having their own test etc.
This helps avoid unnecessary exports of private pieces in order to test
them.

---------

Co-authored-by: Jan Beneš <[email protected]>
  • Loading branch information
nventuro and benesjan authored Dec 10, 2024
1 parent 8c064d4 commit d132f83
Show file tree
Hide file tree
Showing 21 changed files with 375 additions and 350 deletions.
17 changes: 9 additions & 8 deletions noir-projects/aztec-nr/aztec/src/keys/point_to_symmetric_key.nr
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use crate::utils::point::point_to_bytes;
use dep::protocol_types::{
constants::GENERATOR_INDEX__SYMMETRIC_KEY, point::Point, scalar::Scalar, utils::arr_copy_slice,
};
use dep::protocol_types::{constants::GENERATOR_INDEX__SYMMETRIC_KEY, point::Point, scalar::Scalar};
use std::{embedded_curve_ops::multi_scalar_mul, hash::sha256};

// TODO(#5726): This function is called deriveAESSecret in TS. I don't like point_to_symmetric_key name much since
// point is not the only input of the function. Unify naming with TS once we have a better name.
pub fn point_to_symmetric_key(secret: Scalar, point: Point) -> [u8; 32] {
let shared_secret: Point = multi_scalar_mul([point], [secret]);
let shared_secret = point_to_bytes(shared_secret);
let mut shared_secret_bytes_with_separator = [0 as u8; 33];
shared_secret_bytes_with_separator =
arr_copy_slice(shared_secret, shared_secret_bytes_with_separator, 0);
let shared_secret = point_to_bytes(multi_scalar_mul([point], [secret]));

let mut shared_secret_bytes_with_separator: [u8; 33] = std::mem::zeroed();
for i in 0..shared_secret.len() {
shared_secret_bytes_with_separator[i] = shared_secret[i];
}

shared_secret_bytes_with_separator[32] = GENERATOR_INDEX__SYMMETRIC_KEY;

sha256(shared_secret_bytes_with_separator)
}

Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/note/note_getter/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
let filter_args = options.filter_args;
let filtered_notes = filter_fn(opt_notes, filter_args);

let notes = crate::utils::collapse_array(filtered_notes);
let notes = crate::utils::array::collapse(filtered_notes);
let mut note_hashes: BoundedVec<Field, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> =
BoundedVec::new();

Expand Down
13 changes: 5 additions & 8 deletions noir-projects/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use crate::{
context::PrivateContext,
note::{note_header::NoteHeader, note_interface::{NoteInterface, NullifiableNote}},
utils::array,
};

use dep::protocol_types::{
hash::{
compute_siloed_note_hash as compute_siloed_note_hash,
compute_siloed_nullifier as compute_siloed_nullifier_from_preimage,
compute_unique_note_hash,
},
utils::arr_copy_slice,
use dep::protocol_types::hash::{
compute_siloed_note_hash as compute_siloed_note_hash,
compute_siloed_nullifier as compute_siloed_nullifier_from_preimage, compute_unique_note_hash,
};

pub fn compute_siloed_nullifier<Note, let N: u32>(
Expand Down Expand Up @@ -128,7 +125,7 @@ pub unconstrained fn compute_note_hash_and_optionally_a_nullifier<T, let N: u32,
where
T: NoteInterface<N> + NullifiableNote,
{
let mut note = deserialize_content(arr_copy_slice(serialized_note, [0; N], 0));
let mut note = deserialize_content(array::subarray(serialized_note, 0));
note.set_header(note_header);

let note_hash = note.compute_note_hash();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use dep::protocol_types::{
address::AztecAddress, constants::L1_TO_L2_MSG_TREE_HEIGHT, utils::arr_copy_slice,
};
use crate::utils::array;
use dep::protocol_types::{address::AztecAddress, constants::L1_TO_L2_MSG_TREE_HEIGHT};

/// Returns the leaf index and sibling path of an entry in the L1 to L2 messaging tree, which can then be used to prove
/// its existence.
Expand All @@ -12,7 +11,7 @@ pub unconstrained fn get_l1_to_l2_membership_witness(
let returned_message =
get_l1_to_l2_membership_witness_oracle(contract_address, message_hash, secret);
let leaf_index = returned_message[0];
let sibling_path = arr_copy_slice(returned_message, [0; L1_TO_L2_MSG_TREE_HEIGHT], 1);
let sibling_path = array::subarray(returned_message, 1);

(leaf_index, sibling_path)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use dep::protocol_types::{
constants::{ARCHIVE_HEIGHT, NOTE_HASH_TREE_HEIGHT},
utils::arr_copy_slice,
};
use crate::utils::array;
use dep::protocol_types::constants::{ARCHIVE_HEIGHT, NOTE_HASH_TREE_HEIGHT};

global NOTE_HASH_TREE_ID: Field = 1;
global ARCHIVE_TREE_ID: Field = 4;
Expand Down Expand Up @@ -31,7 +29,7 @@ pub unconstrained fn get_membership_witness<let N: u32, let M: u32>(
leaf_value: Field,
) -> MembershipWitness<N, M> {
let fields: [Field; M] = get_membership_witness_oracle(block_number, tree_id, leaf_value);
MembershipWitness { index: fields[0], path: arr_copy_slice(fields, [0; N], 1) }
MembershipWitness { index: fields[0], path: array::subarray(fields, 1) }
}

// Note: get_nullifier_membership_witness function is implemented in get_nullifier_membership_witness.nr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::utils::array;
use dep::protocol_types::{
abis::nullifier_leaf_preimage::{NULLIFIER_LEAF_PREIMAGE_LENGTH, NullifierLeafPreimage},
constants::NULLIFIER_TREE_HEIGHT,
utils::arr_copy_slice,
abis::nullifier_leaf_preimage::NullifierLeafPreimage, constants::NULLIFIER_TREE_HEIGHT,
};

// INDEX_LENGTH + NULLIFIER_LEAF_PREIMAGE_LENGTH + NULLIFIER_TREE_HEIGHT
Expand All @@ -15,15 +14,12 @@ pub struct NullifierMembershipWitness {

impl NullifierMembershipWitness {
pub fn deserialize(fields: [Field; NULLIFIER_MEMBERSHIP_WITNESS]) -> Self {
let leaf_preimage_fields = arr_copy_slice(fields, [0; NULLIFIER_LEAF_PREIMAGE_LENGTH], 1);
let serialized_leaf_preimage = array::subarray(fields, 1);

Self {
index: fields[0],
leaf_preimage: NullifierLeafPreimage::deserialize(leaf_preimage_fields),
path: arr_copy_slice(
fields,
[0; NULLIFIER_TREE_HEIGHT],
1 + NULLIFIER_LEAF_PREIMAGE_LENGTH,
),
leaf_preimage: NullifierLeafPreimage::deserialize(serialized_leaf_preimage),
path: array::subarray(fields, 1 + serialized_leaf_preimage.len()),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use dep::protocol_types::{
constants::PUBLIC_DATA_TREE_HEIGHT, data::PublicDataTreeLeafPreimage, utils::arr_copy_slice,
};
use crate::utils::array;
use dep::protocol_types::{constants::PUBLIC_DATA_TREE_HEIGHT, data::PublicDataTreeLeafPreimage};

global LEAF_PREIMAGE_LENGTH: u32 = 4;
global PUBLIC_DATA_WITNESS: u32 = 45;
Expand Down Expand Up @@ -30,10 +29,6 @@ pub unconstrained fn get_public_data_witness(
next_index: fields[3] as u32,
next_slot: fields[4],
},
path: arr_copy_slice(
fields,
[0; PUBLIC_DATA_TREE_HEIGHT],
1 + LEAF_PREIMAGE_LENGTH,
),
path: array::subarray(fields, 1 + LEAF_PREIMAGE_LENGTH),
}
}
13 changes: 7 additions & 6 deletions noir-projects/aztec-nr/aztec/src/oracle/notes.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::note::{note_header::NoteHeader, note_interface::NoteInterface};
use crate::{note::{note_header::NoteHeader, note_interface::NoteInterface}, utils::array};

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

/// Notifies the simulator that a note has been created, so that it can be returned in future read requests in the same
Expand Down Expand Up @@ -181,12 +180,14 @@ where
let return_header_length: u32 = 2; // num_notes & contract_address.
let extra_preimage_length: u32 = 2; // nonce & note_hash_counter.
let read_offset: u32 = return_header_length + i * (N + extra_preimage_length);

let nonce = fields[read_offset];
let note_hash_counter = fields[read_offset + 1] as u32;
let header = NoteHeader { contract_address, nonce, storage_slot, note_hash_counter };
let serialized_note = arr_copy_slice(fields, [0; N], read_offset + 2);
let mut note = Note::deserialize_content(serialized_note);
note.set_header(header);
let note_content = array::subarray(fields, read_offset + 2);

let mut note = Note::deserialize_content(note_content);
note.set_header(NoteHeader { contract_address, nonce, storage_slot, note_hash_counter });

placeholder_opt_notes[i] = Option::some(note);
};
}
Expand Down
Loading

0 comments on commit d132f83

Please sign in to comment.