Skip to content

Commit 6af5943

Browse files
authored
chore: remove omit param from serialize derivation (#13703)
While working on #13684 I noticed we no longer use this param at all, so I simply got rid of it. The current partial note approach (the feature that used to use `omit`) also does not rely on it.
1 parent 36fb0a1 commit 6af5943

File tree

4 files changed

+97
-118
lines changed

4 files changed

+97
-118
lines changed

noir-projects/aztec-nr/aztec/src/macros/events.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use protocol_types::meta::generate_serialize_to_fields;
44
comptime fn generate_event_interface(s: TypeDefinition) -> Quoted {
55
let name = s.name();
66
let typ = s.as_type();
7-
let (serialization_fields, _) = generate_serialize_to_fields(quote { self }, typ, &[], false);
7+
let (serialization_fields, _) = generate_serialize_to_fields(quote { self }, typ, false);
88
let content_len = serialization_fields.len();
99

1010
let event_type_id = compute_event_selector(s);

noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub(crate) comptime fn transform_public(f: FunctionDefinition) -> Quoted {
196196
let original_params = f.parameters();
197197
let args_len = original_params
198198
.map(|(name, typ): (Quoted, Type)| {
199-
generate_serialize_to_fields(name, typ, &[], false).0.len()
199+
generate_serialize_to_fields(name, typ, false).0.len()
200200
})
201201
.fold(0, |acc: u32, val: u32| acc + val);
202202

noir-projects/aztec-nr/aztec/src/macros/notes.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ comptime fn index_note_fields(
282282
} else {
283283
indexed_nullable_fields = indexed_nullable_fields.push_back((name, typ, counter));
284284
}
285-
let (serialization_fields, _) = generate_serialize_to_fields(name, typ, &[], true);
285+
let (serialization_fields, _) = generate_serialize_to_fields(name, typ, true);
286286
// Each struct member can occupy multiple fields so we need to increment the counter accordingly
287287
counter += serialization_fields.len();
288288
}

noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr

Lines changed: 94 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ pub comptime fn generate_deserialize_from_fields(
227227
/// # Parameters
228228
/// - `name`: The base identifier (e.g., `self`, `some_var`).
229229
/// - `typ`: The type being serialized (e.g., a custom struct, array, or primitive type).
230-
/// - `omit`: A list of field names (as `Quoted`) to be excluded from the serialized output.
231230
/// - `should_pack`: A boolean indicating whether the type should be packed.
232231
///
233232
/// # Returns
@@ -248,7 +247,7 @@ pub comptime fn generate_deserialize_from_fields(
248247
///
249248
/// Serializing the struct:
250249
/// ```rust
251-
/// generate_serialize_to_fields(quote { my_mock_struct }, MockStruct, &[], false)
250+
/// generate_serialize_to_fields(quote { my_mock_struct }, MockStruct, false)
252251
/// // Returns:
253252
/// // ([`my_mock_struct.a`, `my_mock_struct.b`], [])
254253
/// ```
@@ -264,29 +263,29 @@ pub comptime fn generate_deserialize_from_fields(
264263
///
265264
/// Serialization output:
266265
/// ```rust
267-
/// generate_serialize_to_fields(quote { self }, NestedStruct, &[], false)
266+
/// generate_serialize_to_fields(quote { self }, NestedStruct, false)
268267
/// // Returns:
269268
/// // ([`self.m1.a`, `self.m1.b`, `self.m2.a`, `self.m2.b`], [])
270269
/// ```
271270
///
272271
/// ## Array
273272
/// For an array type:
274273
/// ```rust
275-
/// generate_serialize_to_fields(quote { my_array }, [Field; 3], &[], false)
274+
/// generate_serialize_to_fields(quote { my_array }, [Field; 3], false)
276275
/// // Returns:
277276
/// // ([`my_array[0]`, `my_array[1]`, `my_array[2]`], [])
278277
/// ```
279278
///
280279
/// ## String
281280
/// For a string field, where each character is serialized as a `Field`:
282281
/// ```rust
283-
/// generate_serialize_to_fields(quote { my_string }, StringType, &[], false)
282+
/// generate_serialize_to_fields(quote { my_string }, StringType, false)
284283
/// // Returns:
285284
/// // ([`my_string_as_bytes[0] as Field`, `my_string_as_bytes[1] as Field`, ...],
286285
/// // [`let my_string_as_bytes = my_string.as_bytes()`])
287286
/// ```
288287
///
289-
/// ## Nested Struct with Omitted Field and packing enabled
288+
/// ## Nested Struct with packing enabled
290289
/// - u128 has a `Packable` implementation hence it will be packed.
291290
///
292291
/// For a more complex struct:
@@ -297,128 +296,109 @@ pub comptime fn generate_deserialize_from_fields(
297296
/// }
298297
/// ```
299298
///
300-
/// Serializing while omitting `value2`:
301-
/// ```rust
302-
/// generate_serialize_to_fields(quote { self }, MyStruct, &[quote { self.value2 }], true)
303-
/// // Returns:
304-
/// // ([`value_packed[0]`], [`let value_packed = self.value.pack()`])
305-
/// ```
306-
///
307299
/// # Panics
308300
/// - If the type is unsupported for serialization.
309301
/// - If the provided `typ` contains invalid constants or incompatible structures.
310302
pub comptime fn generate_serialize_to_fields(
311303
name: Quoted,
312304
typ: Type,
313-
omit: [Quoted],
314305
should_pack: bool,
315306
) -> ([Quoted], [Quoted]) {
316307
let mut fields = &[];
317308
let mut aux_vars = &[];
318309

319-
// Proceed if none of the omit rules omits this name
320-
if !omit.any(|to_omit| to_omit == name) {
321-
// If the type implements `Packable`, its length will be assigned to the `maybe_packed_len_typ` variable.
322-
let maybe_packed_len_typ = std::meta::typ::fresh_type_variable();
323-
let packable_constraint =
324-
quote { crate::traits::Packable<$maybe_packed_len_typ> }.as_trait_constraint();
325-
326-
if (should_pack & typ.implements(packable_constraint)) {
327-
// Packing is enabled and the given type implements the `Packable` trait so we call the `pack()`
328-
// method, add the resulting field array to `aux_vars` and each field to `fields`.
329-
let packed_len = maybe_packed_len_typ.as_constant().unwrap();
330-
331-
// We collapse the name to a one that gets tokenized as a single token (e.g. "self.value" -> "self_value").
332-
let name_at_one_token = collapse_to_one_token(name);
333-
let packed_struct_name = f"{name_at_one_token}_aux_var".quoted_contents();
334-
335-
// We add the individual fields to the fields array
336-
let pack_method = get_trait_impl_method(
337-
typ,
338-
quote { crate::traits::Packable<$packed_len> },
339-
quote { pack },
340-
);
341-
let packed_struct = quote { let $packed_struct_name = $pack_method($name) };
342-
for i in 0..packed_len {
343-
fields = fields.push_back(quote { $packed_struct_name[$i] });
344-
}
310+
// If the type implements `Packable`, its length will be assigned to the `maybe_packed_len_typ` variable.
311+
let maybe_packed_len_typ = std::meta::typ::fresh_type_variable();
312+
let packable_constraint =
313+
quote { crate::traits::Packable<$maybe_packed_len_typ> }.as_trait_constraint();
345314

346-
// We add the new auxiliary variable to the aux_vars array
347-
aux_vars = aux_vars.push_back(packed_struct);
348-
} else if typ.is_field() {
349-
// For field we just add the value to fields
350-
fields = fields.push_back(name);
351-
} else if typ.as_integer().is_some() | typ.is_bool() {
352-
// For integer and bool we just cast to Field and add the value to fields
353-
fields = fields.push_back(quote { $name as Field });
354-
} else if typ.as_data_type().is_some() {
355-
// For struct we pref
356-
let nested_struct = typ.as_data_type().unwrap();
357-
let params = nested_struct.0.fields(nested_struct.1);
358-
let struct_flattened = params.map(|(param_name, param_type): (Quoted, Type)| {
359-
let maybe_prefixed_name = if name == quote {} {
360-
// Triggered when the param name is of a value available in the current scope (e.g. a function
361-
// argument) --> then we don't prefix the name with anything.
362-
param_name
363-
} else {
364-
// Triggered when we want to prefix the param name with the `name` from function input. This
365-
// can typically be `self` when implementing a method on a struct.
366-
quote { $name.$param_name }
367-
};
368-
generate_serialize_to_fields(
369-
quote {$maybe_prefixed_name},
370-
param_type,
371-
omit,
372-
should_pack,
373-
)
374-
});
375-
let struct_flattened_fields = struct_flattened.fold(
376-
&[],
377-
|acc: [Quoted], (fields, _): (_, [Quoted])| acc.append(fields),
378-
);
379-
let struct_flattened_aux_vars = struct_flattened.fold(
380-
&[],
381-
|acc: [Quoted], (_, aux_vars): ([Quoted], _)| acc.append(aux_vars),
382-
);
383-
fields = fields.append(struct_flattened_fields);
384-
aux_vars = aux_vars.append(struct_flattened_aux_vars);
385-
} else if typ.as_array().is_some() {
386-
// For array we recursively call `generate_serialize_to_fields(...)` for each element
387-
let (element_type, array_len) = typ.as_array().unwrap();
388-
let array_len = array_len.as_constant().unwrap();
389-
for i in 0..array_len {
390-
let (element_fields, element_aux_vars) = generate_serialize_to_fields(
391-
quote { $name[$i] },
392-
element_type,
393-
omit,
394-
should_pack,
395-
);
396-
fields = fields.append(element_fields);
397-
aux_vars = aux_vars.append(element_aux_vars);
398-
}
399-
} else if typ.as_str().is_some() {
400-
// For string we convert the value to bytes, we store the `as_bytes` in an auxiliary variables and
401-
// then we add each byte to fields as a Field
402-
let length_type = typ.as_str().unwrap();
403-
let str_len = length_type.as_constant().unwrap();
404-
let as_member = name.as_expr().unwrap().as_member_access();
405-
let var_name = if as_member.is_some() {
406-
as_member.unwrap().1
315+
if (should_pack & typ.implements(packable_constraint)) {
316+
// Packing is enabled and the given type implements the `Packable` trait so we call the `pack()`
317+
// method, add the resulting field array to `aux_vars` and each field to `fields`.
318+
let packed_len = maybe_packed_len_typ.as_constant().unwrap();
319+
320+
// We collapse the name to a one that gets tokenized as a single token (e.g. "self.value" -> "self_value").
321+
let name_at_one_token = collapse_to_one_token(name);
322+
let packed_struct_name = f"{name_at_one_token}_aux_var".quoted_contents();
323+
324+
// We add the individual fields to the fields array
325+
let pack_method = get_trait_impl_method(
326+
typ,
327+
quote { crate::traits::Packable<$packed_len> },
328+
quote { pack },
329+
);
330+
let packed_struct = quote { let $packed_struct_name = $pack_method($name) };
331+
for i in 0..packed_len {
332+
fields = fields.push_back(quote { $packed_struct_name[$i] });
333+
}
334+
335+
// We add the new auxiliary variable to the aux_vars array
336+
aux_vars = aux_vars.push_back(packed_struct);
337+
} else if typ.is_field() {
338+
// For field we just add the value to fields
339+
fields = fields.push_back(name);
340+
} else if typ.as_integer().is_some() | typ.is_bool() {
341+
// For integer and bool we just cast to Field and add the value to fields
342+
fields = fields.push_back(quote { $name as Field });
343+
} else if typ.as_data_type().is_some() {
344+
// For struct we pref
345+
let nested_struct = typ.as_data_type().unwrap();
346+
let params = nested_struct.0.fields(nested_struct.1);
347+
let struct_flattened = params.map(|(param_name, param_type): (Quoted, Type)| {
348+
let maybe_prefixed_name = if name == quote {} {
349+
// Triggered when the param name is of a value available in the current scope (e.g. a function
350+
// argument) --> then we don't prefix the name with anything.
351+
param_name
407352
} else {
408-
name
353+
// Triggered when we want to prefix the param name with the `name` from function input. This
354+
// can typically be `self` when implementing a method on a struct.
355+
quote { $name.$param_name }
409356
};
410-
let as_bytes_name = f"{var_name}_as_bytes".quoted_contents();
411-
let as_bytes = quote { let $as_bytes_name = $name.as_bytes() };
412-
for i in 0..str_len {
413-
fields = fields.push_back(quote { $as_bytes_name[$i] as Field });
414-
}
415-
aux_vars = aux_vars.push_back(as_bytes);
357+
generate_serialize_to_fields(quote {$maybe_prefixed_name}, param_type, should_pack)
358+
});
359+
let struct_flattened_fields = struct_flattened.fold(
360+
&[],
361+
|acc: [Quoted], (fields, _): (_, [Quoted])| acc.append(fields),
362+
);
363+
let struct_flattened_aux_vars = struct_flattened.fold(
364+
&[],
365+
|acc: [Quoted], (_, aux_vars): ([Quoted], _)| acc.append(aux_vars),
366+
);
367+
fields = fields.append(struct_flattened_fields);
368+
aux_vars = aux_vars.append(struct_flattened_aux_vars);
369+
} else if typ.as_array().is_some() {
370+
// For array we recursively call `generate_serialize_to_fields(...)` for each element
371+
let (element_type, array_len) = typ.as_array().unwrap();
372+
let array_len = array_len.as_constant().unwrap();
373+
for i in 0..array_len {
374+
let (element_fields, element_aux_vars) =
375+
generate_serialize_to_fields(quote { $name[$i] }, element_type, should_pack);
376+
fields = fields.append(element_fields);
377+
aux_vars = aux_vars.append(element_aux_vars);
378+
}
379+
} else if typ.as_str().is_some() {
380+
// For string we convert the value to bytes, we store the `as_bytes` in an auxiliary variables and
381+
// then we add each byte to fields as a Field
382+
let length_type = typ.as_str().unwrap();
383+
let str_len = length_type.as_constant().unwrap();
384+
let as_member = name.as_expr().unwrap().as_member_access();
385+
let var_name = if as_member.is_some() {
386+
as_member.unwrap().1
416387
} else {
417-
panic(
418-
f"Unsupported type for serialization of argument {name} and type {typ}",
419-
)
388+
name
389+
};
390+
let as_bytes_name = f"{var_name}_as_bytes".quoted_contents();
391+
let as_bytes = quote { let $as_bytes_name = $name.as_bytes() };
392+
for i in 0..str_len {
393+
fields = fields.push_back(quote { $as_bytes_name[$i] as Field });
420394
}
395+
aux_vars = aux_vars.push_back(as_bytes);
396+
} else {
397+
panic(
398+
f"Unsupported type for serialization of argument {name} and type {typ}",
399+
)
421400
}
401+
422402
(fields, aux_vars)
423403
}
424404

@@ -441,7 +421,7 @@ comptime fn collapse_to_one_token(q: Quoted) -> Quoted {
441421

442422
pub(crate) comptime fn derive_serialize(s: TypeDefinition) -> Quoted {
443423
let typ = s.as_type();
444-
let (fields, aux_vars) = generate_serialize_to_fields(quote { self }, typ, &[], false);
424+
let (fields, aux_vars) = generate_serialize_to_fields(quote { self }, typ, false);
445425
let aux_vars_for_serialization = if aux_vars.len() > 0 {
446426
let joint = aux_vars.join(quote {;});
447427
quote { $joint; }
@@ -463,7 +443,7 @@ pub(crate) comptime fn derive_serialize(s: TypeDefinition) -> Quoted {
463443

464444
pub(crate) comptime fn derive_deserialize(s: TypeDefinition) -> Quoted {
465445
let typ = s.as_type();
466-
let (fields, _) = generate_serialize_to_fields(quote { self }, typ, &[], false);
446+
let (fields, _) = generate_serialize_to_fields(quote { self }, typ, false);
467447
let serialized_len = fields.len();
468448
let (deserialized, _) =
469449
generate_deserialize_from_fields(quote { self }, typ, quote { serialized }, 0, false);
@@ -485,8 +465,7 @@ pub comptime fn derive_packable_and_get_packed_len(s: TypeDefinition) -> (Quoted
485465
let packing_enabled = true;
486466

487467
let typ = s.as_type();
488-
let (fields, aux_vars) =
489-
generate_serialize_to_fields(quote { self }, typ, &[], packing_enabled);
468+
let (fields, aux_vars) = generate_serialize_to_fields(quote { self }, typ, packing_enabled);
490469
let aux_vars_for_packing = if aux_vars.len() > 0 {
491470
let joint = aux_vars.join(quote {;});
492471
quote { $joint; }

0 commit comments

Comments
 (0)