Skip to content

Commit

Permalink
feat: comptime deriving generators in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 11, 2024
1 parent 4c1163a commit db1ba55
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions noir-projects/aztec-nr/aztec/src/macros/notes/mod.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
meta::{type_of, unquote, typ::fresh_type_variable}, collections::umap::UHashMap,
hash::{BuildHasherDefault, poseidon2::Poseidon2Hasher}
hash::{BuildHasherDefault, poseidon2::Poseidon2Hasher, derive_generators}
};
use protocol_types::meta::{flatten_to_fields, pack_from_fields};
use protocol_types::{meta::{flatten_to_fields, pack_from_fields}, point::Point};
use crate::note::{note_header::NoteHeader, note_getter_options::PropertySelector};

comptime global NOTE_HEADER_TYPE = type_of(NoteHeader::empty());
Expand Down Expand Up @@ -294,15 +294,19 @@ comptime fn generate_multi_scalar_mul(indexed_fields: [(Quoted, Type, u32)]) ->
let mut scalars_list = &[];
let mut args_list = &[];
let mut aux_vars_list = &[];
// TODO(#8648): Generate generators at comptime instead of importing here.
for i in 0..indexed_fields.len() {
let (field_name, typ, index) = indexed_fields[i];
let start_generator_index = index + 1;
let (flattened_field, aux_vars) = flatten_to_fields(field_name, typ, &[]);
for j in 0..flattened_field.len() {
let flattened_as_field = flattened_field[j];
let generator_index = start_generator_index + j;
generators_list = generators_list.push_back(f"aztec::generators::Ga{generator_index}".quoted_contents());

let generators: [Point; 1] = derive_generators("aztec_nr_generators".as_bytes(), generator_index);
let generator_x = generators[0].x;
let generator_y = generators[1].y;

generators_list = generators_list.push_back(f"aztec::protocol_types::point::Point {{ x: {generator_x}, y: {generator_y}, is_infinite: false }}".quoted_contents());
scalars_list = scalars_list.push_back(quote { std::hash::from_field_unsafe($flattened_as_field) });
}
args_list = args_list.push_back(quote { $field_name: $typ });
Expand Down Expand Up @@ -573,7 +577,11 @@ comptime fn generate_finalization_payload(
/// }
/// }
/// ```
comptime fn generate_partial_note_impl(s: StructDefinition, setup_payload_name: Quoted, finalization_payload_name: Quoted) -> Quoted {
comptime fn generate_partial_note_impl(
s: StructDefinition,
setup_payload_name: Quoted,
finalization_payload_name: Quoted
) -> Quoted {
let name = s.name();
quote {
impl aztec::note::note_interface::PartialNote<$setup_payload_name, $finalization_payload_name> for $name {
Expand Down

0 comments on commit db1ba55

Please sign in to comment.