@@ -56,6 +56,11 @@ pub use signature::SignatureBuiltinRunner;
56
56
57
57
use super :: cairo_pie:: BuiltinAdditionalData ;
58
58
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
+
59
64
/* NB: this enum is no accident: we may need (and cairo-vm-py *does* need)
60
65
* structs containing this to be `Send`. The only two ways to achieve that
61
66
* are either storing a `dyn Trait` inside an `Arc<Mutex<&dyn Trait>>` or
@@ -535,10 +540,15 @@ impl BuiltinRunner {
535
540
let used_cells = self . get_used_cells ( & vm. segments ) ?;
536
541
if vm. disable_trace_padding {
537
542
// 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.
539
545
let num_instances = self . get_used_instances ( & vm. segments ) ?;
540
546
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
542
552
} else {
543
553
0
544
554
} ;
@@ -971,6 +981,15 @@ mod tests {
971
981
assert ! (
972
982
n_allocated_instances_true. is_power_of_two( ) || n_allocated_instances_true == 0
973
983
) ;
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
+
974
993
// Checks that the number of allocated instances is different when trace padding is
975
994
// enabled/disabled. Holds for this specific program, not always (that is, in other
976
995
// programs, padding may be of size 0, or the same).
0 commit comments