From c3130f2783579c4366207d0efc4183651e106583 Mon Sep 17 00:00:00 2001 From: Yuval Goldberg Date: Fri, 28 Feb 2025 00:20:48 +0200 Subject: [PATCH] Limited padding of segments to >=16 --- CHANGELOG.md | 2 ++ vm/src/vm/runners/builtin_runner/mod.rs | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 740194f41d..fde5e93d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Limited padding of builtin segments to >=16 [#1981](https://github.com/lambdaclass/cairo-vm/pull/1981) + #### [2.0.0] - 2025-02-26 * fix: Check overflow in cairo pie address calculation [#1945](https://github.com/lambdaclass/cairo-vm/pull/1945) diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 9e2fa53d54..b4870ba587 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -56,6 +56,8 @@ pub use signature::SignatureBuiltinRunner; use super::cairo_pie::BuiltinAdditionalData; +const MIN_N_INSTANCES_IN_BUILTIN_SEGMENT: usize = 0; + /* NB: this enum is no accident: we may need (and cairo-vm-py *does* need) * structs containing this to be `Send`. The only two ways to achieve that * are either storing a `dyn Trait` inside an `Arc>` or @@ -535,10 +537,15 @@ impl BuiltinRunner { let used_cells = self.get_used_cells(&vm.segments)?; if vm.disable_trace_padding { // If trace padding is disabled, we pad the used cells to still ensure that the - // number of instances is a power of 2. + // number of instances is a power of 2, and at least + // MIN_N_INSTANCES_IN_BUILTIN_SEGMENT. let num_instances = self.get_used_instances(&vm.segments)?; let padded_used_cells = if num_instances > 0 { - num_instances.next_power_of_two() * self.cells_per_instance() as usize + let padded_num_instances = std::cmp::max( + MIN_N_INSTANCES_IN_BUILTIN_SEGMENT, + num_instances.next_power_of_two(), + ); + padded_num_instances * self.cells_per_instance() as usize } else { 0 }; @@ -971,6 +978,15 @@ mod tests { assert!( n_allocated_instances_true.is_power_of_two() || n_allocated_instances_true == 0 ); + // Assert the builtin segment is padded to at least + // `MIN_N_INSTANCES_IN_BUILTIN_SEGMENT`. + // Pedersen proof has exactly one pedersen builtin, so this indeed tests the padding + // to at least `MIN_N_INSTANCES_IN_BUILTIN_SEGMENT`. + assert!( + n_allocated_instances_true >= MIN_N_INSTANCES_IN_BUILTIN_SEGMENT + || n_allocated_instances_true == 0 + ); + // Checks that the number of allocated instances is different when trace padding is // enabled/disabled. Holds for this specific program, not always (that is, in other // programs, padding may be of size 0, or the same).