@@ -56,6 +56,11 @@ pub use signature::SignatureBuiltinRunner;
5656
5757use 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