11use backend:: * ;
22use lean_vm:: * ;
33use std:: { array, collections:: BTreeMap , iter:: repeat_n} ;
4- use utils:: { ToUsize , transposed_par_iter_mut} ;
4+ use utils:: { ToUsize , get_poseidon_16_of_zero , transposed_par_iter_mut} ;
55
66#[ derive( Debug ) ]
77pub struct ExecutionTrace {
@@ -92,8 +92,13 @@ pub fn get_execution_trace(bytecode: &Bytecode, execution_result: ExecutionResul
9292 } ) ;
9393
9494 let mut memory_padded = memory. 0 . par_iter ( ) . map ( |& v| v. unwrap_or ( F :: ZERO ) ) . collect :: < Vec < F > > ( ) ;
95+
96+ // Write poseidon(0) at the end of used memory for poseidon table padding rows
97+ let null_poseidon_16_hash_ptr = memory_padded. len ( ) ;
98+ memory_padded. extend_from_slice ( get_poseidon_16_of_zero ( ) ) ;
99+
95100 // IMPORTANT: memory size should always be >= number of VM cycles
96- let padded_memory_len = ( memory . 0 . len ( ) . max ( n_cycles) . max ( 1 << MIN_LOG_N_ROWS_PER_TABLE ) ) . next_power_of_two ( ) ;
101+ let padded_memory_len = ( memory_padded . len ( ) . max ( n_cycles) . max ( 1 << MIN_LOG_N_ROWS_PER_TABLE ) ) . next_power_of_two ( ) ;
97102 memory_padded. resize ( padded_memory_len, F :: ZERO ) ;
98103
99104 let ExecutionResult { mut traces, .. } = execution_result;
@@ -113,7 +118,7 @@ pub fn get_execution_trace(bytecode: &Bytecode, execution_result: ExecutionResul
113118 } ,
114119 ) ;
115120 for table in traces. keys ( ) . copied ( ) . collect :: < Vec < _ > > ( ) {
116- pad_table ( & table, & mut traces) ;
121+ pad_table ( & table, & mut traces, null_poseidon_16_hash_ptr ) ;
117122 }
118123
119124 ExecutionTrace {
@@ -124,7 +129,7 @@ pub fn get_execution_trace(bytecode: &Bytecode, execution_result: ExecutionResul
124129 }
125130}
126131
127- fn pad_table ( table : & Table , traces : & mut BTreeMap < Table , TableTrace > ) {
132+ fn pad_table ( table : & Table , traces : & mut BTreeMap < Table , TableTrace > , null_hash_ptr : usize ) {
128133 let trace = traces. get_mut ( table) . unwrap ( ) ;
129134 let h = trace. base [ 0 ] . len ( ) ;
130135 trace
@@ -136,7 +141,11 @@ fn pad_table(table: &Table, traces: &mut BTreeMap<Table, TableTrace>) {
136141 trace. non_padded_n_rows = h;
137142 trace. log_n_rows = log2_ceil_usize ( h + 1 ) . max ( MIN_LOG_N_ROWS_PER_TABLE ) ;
138143 let padding_len = ( 1 << trace. log_n_rows ) - h;
139- let padding_row = table. padding_row ( ) ;
144+ let padding_row = if * table == Table :: poseidon16 ( ) {
145+ default_poseidon_row ( null_hash_ptr)
146+ } else {
147+ table. padding_row ( )
148+ } ;
140149 trace. base . par_iter_mut ( ) . enumerate ( ) . for_each ( |( i, col) | {
141150 col. extend ( repeat_n ( padding_row[ i] , padding_len) ) ;
142151 } ) ;
0 commit comments