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