Skip to content

Commit

Permalink
Limited padding of segments to >=16
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalsw committed Mar 3, 2025
1 parent 3de653d commit c3130f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 18 additions & 2 deletions vm/src/vm/runners/builtin_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<&dyn Trait>>` or
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit c3130f2

Please sign in to comment.