Skip to content

Commit b9fb58d

Browse files
authored
Limited padding of segments to >=16 (#1981)
1 parent c71b496 commit b9fb58d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-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
* fix: Enforce `disable_trace_padding` used only in `proof_mode` [#1984](https://github.com/lambdaclass/cairo-vm/pull/1984)
68

79
* feat: adding option to simulate builtins [#1956](https://github.com/lambdaclass/cairo-vm/pull/1956)

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

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

5757
use super::cairo_pie::BuiltinAdditionalData;
5858

59+
const MIN_N_INSTANCES_IN_BUILTIN_SEGMENT: usize = 16;
60+
61+
// Assert MIN_N_INSTANCES_IN_BUILTIN_SEGMENT is a power of 2.
62+
const _: () = assert!(MIN_N_INSTANCES_IN_BUILTIN_SEGMENT.is_power_of_two());
63+
5964
/* NB: this enum is no accident: we may need (and cairo-vm-py *does* need)
6065
* structs containing this to be `Send`. The only two ways to achieve that
6166
* are either storing a `dyn Trait` inside an `Arc<Mutex<&dyn Trait>>` or
@@ -535,10 +540,15 @@ impl BuiltinRunner {
535540
let used_cells = self.get_used_cells(&vm.segments)?;
536541
if vm.disable_trace_padding {
537542
// If trace padding is disabled, we pad the used cells to still ensure that the
538-
// number of instances is a power of 2.
543+
// number of instances is a power of 2, and at least
544+
// MIN_N_INSTANCES_IN_BUILTIN_SEGMENT.
539545
let num_instances = self.get_used_instances(&vm.segments)?;
540546
let padded_used_cells = if num_instances > 0 {
541-
num_instances.next_power_of_two() * self.cells_per_instance() as usize
547+
let padded_num_instances = core::cmp::max(
548+
MIN_N_INSTANCES_IN_BUILTIN_SEGMENT,
549+
num_instances.next_power_of_two(),
550+
);
551+
padded_num_instances * self.cells_per_instance() as usize
542552
} else {
543553
0
544554
};
@@ -971,6 +981,15 @@ mod tests {
971981
assert!(
972982
n_allocated_instances_true.is_power_of_two() || n_allocated_instances_true == 0
973983
);
984+
// Assert the builtin segment is padded to at least
985+
// `MIN_N_INSTANCES_IN_BUILTIN_SEGMENT`.
986+
// Pedersen proof has exactly one pedersen builtin, so this indeed tests the padding
987+
// to at least `MIN_N_INSTANCES_IN_BUILTIN_SEGMENT`.
988+
assert!(
989+
n_allocated_instances_true >= MIN_N_INSTANCES_IN_BUILTIN_SEGMENT
990+
|| n_allocated_instances_true == 0
991+
);
992+
974993
// Checks that the number of allocated instances is different when trace padding is
975994
// enabled/disabled. Holds for this specific program, not always (that is, in other
976995
// programs, padding may be of size 0, or the same).

0 commit comments

Comments
 (0)