Skip to content

Commit 187fa4e

Browse files
committed
Limited padding of segments to >=16
1 parent 3de653d commit 187fa4e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* feat: Limited padding of builtin segments to >=16 [#1981](https://github.com/lambdaclass/cairo-vm/pull/1981)
6+
57
#### [2.0.0] - 2025-02-26
68

79
* fix: Check overflow in cairo pie address calculation [#1945](https://github.com/lambdaclass/cairo-vm/pull/1945)

vm/src/vm/runners/builtin_runner/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub use signature::SignatureBuiltinRunner;
5656

5757
use super::cairo_pie::BuiltinAdditionalData;
5858

59+
const MIN_N_INSTANCES_IN_BUILTIN_SEGMENT: usize = 16;
60+
5961
/* NB: this enum is no accident: we may need (and cairo-vm-py *does* need)
6062
* structs containing this to be `Send`. The only two ways to achieve that
6163
* are either storing a `dyn Trait` inside an `Arc<Mutex<&dyn Trait>>` or
@@ -535,10 +537,15 @@ impl BuiltinRunner {
535537
let used_cells = self.get_used_cells(&vm.segments)?;
536538
if vm.disable_trace_padding {
537539
// If trace padding is disabled, we pad the used cells to still ensure that the
538-
// number of instances is a power of 2.
540+
// number of instances is a power of 2, and at least
541+
// MIN_N_INSTANCES_IN_BUILTIN_SEGMENT.
539542
let num_instances = self.get_used_instances(&vm.segments)?;
540543
let padded_used_cells = if num_instances > 0 {
541-
num_instances.next_power_of_two() * self.cells_per_instance() as usize
544+
let padded_num_instances = std::cmp::max(
545+
MIN_N_INSTANCES_IN_BUILTIN_SEGMENT,
546+
num_instances.next_power_of_two(),
547+
);
548+
padded_num_instances * self.cells_per_instance() as usize
542549
} else {
543550
0
544551
};
@@ -971,6 +978,15 @@ mod tests {
971978
assert!(
972979
n_allocated_instances_true.is_power_of_two() || n_allocated_instances_true == 0
973980
);
981+
// Assert the builtin segment is padded to at least
982+
// `MIN_N_INSTANCES_IN_BUILTIN_SEGMENT`.
983+
// Pedersen proof has exactly one pedersen builtin, so this indeed tests the padding
984+
// to at least `MIN_N_INSTANCES_IN_BUILTIN_SEGMENT`.
985+
assert!(
986+
n_allocated_instances_true >= MIN_N_INSTANCES_IN_BUILTIN_SEGMENT
987+
|| n_allocated_instances_true == 0
988+
);
989+
974990
// Checks that the number of allocated instances is different when trace padding is
975991
// enabled/disabled. Holds for this specific program, not always (that is, in other
976992
// programs, padding may be of size 0, or the same).

0 commit comments

Comments
 (0)