@@ -41,6 +41,8 @@ pub(crate) struct ErrorOOGMemoryCopyGadget<F> {
4141 external_address : Word < F > ,
4242
4343 addr_expansion_gadget : MemoryAddrExpandGadget < F > ,
44+ // mcopy expansion
45+ memory_expansion_mcopy : MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > ,
4446 // other kind(CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY) expansion
4547 memory_expansion_normal : MemoryExpansionGadget < F , 1 , N_BYTES_MEMORY_WORD_SIZE > ,
4648 memory_copier_gas : MemoryCopierGasGadget < F , { GasCost :: COPY } > ,
@@ -91,12 +93,16 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
9193 cb. stack_pop ( external_address. expr ( ) ) ;
9294 } ) ;
9395
94- let addr_expansion_gadget = MemoryAddrExpandGadget :: construct ( cb, is_mcopy . expr ( ) ) ;
96+ let addr_expansion_gadget = MemoryAddrExpandGadget :: construct ( cb) ;
9597
9698 cb. stack_pop ( addr_expansion_gadget. dst_memory_addr . offset_rlc ( ) ) ;
9799 cb. stack_pop ( addr_expansion_gadget. src_memory_addr . offset_rlc ( ) ) ;
98100 cb. stack_pop ( addr_expansion_gadget. dst_memory_addr . length_rlc ( ) ) ;
99101
102+ // for mcopy
103+ let memory_expansion_mcopy =
104+ addr_expansion_gadget. build_memory_expansion_mcopy ( cb, is_mcopy. expr ( ) ) ;
105+
100106 // for others (CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY)
101107 let memory_expansion_normal = cb. condition ( not:: expr ( is_mcopy. expr ( ) ) , |cb| {
102108 MemoryExpansionGadget :: construct (
@@ -107,7 +113,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
107113
108114 let memory_expansion_cost = select:: expr (
109115 is_mcopy. expr ( ) ,
110- addr_expansion_gadget . memory_expansion_mcopy . gas_cost ( ) ,
116+ memory_expansion_mcopy. gas_cost ( ) ,
111117 memory_expansion_normal. gas_cost ( ) ,
112118 ) ;
113119 let memory_copier_gas = MemoryCopierGasGadget :: construct (
@@ -160,6 +166,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
160166 tx_id,
161167 external_address,
162168 addr_expansion_gadget,
169+ memory_expansion_mcopy,
163170 memory_expansion_normal,
164171 memory_copier_gas,
165172 insufficient_gas,
@@ -227,13 +234,12 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
227234 ) ?;
228235
229236 // assign memory_expansion_mcopy
230- let ( _, memory_expansion_cost_mcopy) =
231- self . addr_expansion_gadget . memory_expansion_mcopy . assign (
232- region,
233- offset,
234- step. memory_word_size ( ) ,
235- [ src_memory_addr, dst_memory_addr] ,
236- ) ?;
237+ let ( _, memory_expansion_cost_mcopy) = self . memory_expansion_mcopy . assign (
238+ region,
239+ offset,
240+ step. memory_word_size ( ) ,
241+ [ src_memory_addr, dst_memory_addr] ,
242+ ) ?;
237243
238244 let memory_copier_gas = self . memory_copier_gas . assign (
239245 region,
@@ -291,33 +297,38 @@ pub(crate) struct MemoryAddrExpandGadget<F> {
291297 src_memory_addr : MemoryExpandedAddressGadget < F > ,
292298 /// Destination offset and size to copy
293299 dst_memory_addr : MemoryExpandedAddressGadget < F > ,
294- // mcopy expansion
295- memory_expansion_mcopy : MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > ,
296300}
297301
298302// construct src_memory_addr, dst_memory_addr and memory_expansion_mcopy.
299303impl < F : Field > MemoryAddrExpandGadget < F > {
300- fn construct ( cb : & mut EVMConstraintBuilder < F > , is_mcopy : Expression < F > ) -> Self {
304+ fn construct ( cb : & mut EVMConstraintBuilder < F > ) -> Self {
301305 let dst_memory_addr = MemoryExpandedAddressGadget :: construct_self ( cb) ;
302306 // src can also be possible to overflow for mcopy.
303307 let src_memory_addr = MemoryExpandedAddressGadget :: construct_self ( cb) ;
304- // for mcopy
305- let memory_expansion_mcopy = cb. condition ( is_mcopy. expr ( ) , |cb| {
308+ Self {
309+ src_memory_addr,
310+ dst_memory_addr,
311+ }
312+ }
313+ fn build_memory_expansion_mcopy (
314+ & self ,
315+ cb : & mut EVMConstraintBuilder < F > ,
316+ is_mcopy : Expression < F > ,
317+ ) -> MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > {
318+ cb. condition ( is_mcopy. expr ( ) , |cb| {
306319 cb. require_equal (
307320 "mcopy src_address length == dst_address length" ,
308- src_memory_addr. length_rlc ( ) ,
309- dst_memory_addr. length_rlc ( ) ,
321+ self . src_memory_addr . length_rlc ( ) ,
322+ self . dst_memory_addr . length_rlc ( ) ,
310323 ) ;
311324 MemoryExpansionGadget :: construct (
312325 cb,
313- [ src_memory_addr. end_offset ( ) , dst_memory_addr. end_offset ( ) ] ,
326+ [
327+ self . src_memory_addr . end_offset ( ) ,
328+ self . dst_memory_addr . end_offset ( ) ,
329+ ] ,
314330 )
315- } ) ;
316- Self {
317- src_memory_addr,
318- dst_memory_addr,
319- memory_expansion_mcopy,
320- }
331+ } )
321332 }
322333}
323334
@@ -707,16 +718,21 @@ mod tests {
707718 #[ derive( Clone ) ]
708719 struct ErrOOGMemoryCopyGadgetTestContainer < F > {
709720 gadget : MemoryAddrExpandGadget < F > ,
721+ memory_expansion_mcopy : MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > ,
710722 is_mcopy : Cell < F > ,
711723 }
712724
713725 impl < F : Field > MathGadgetContainer < F > for ErrOOGMemoryCopyGadgetTestContainer < F > {
714726 fn configure_gadget_container ( cb : & mut EVMConstraintBuilder < F > ) -> Self {
715727 let is_mcopy = cb. query_cell ( ) ;
716728 cb. require_boolean ( "is_mcopy is bool" , is_mcopy. expr ( ) ) ;
717- let gadget = MemoryAddrExpandGadget :: < F > :: construct ( cb, is_mcopy. expr ( ) ) ;
718-
719- ErrOOGMemoryCopyGadgetTestContainer { gadget, is_mcopy }
729+ let gadget = MemoryAddrExpandGadget :: < F > :: construct ( cb) ;
730+ let memory_expansion_mcopy = gadget. build_memory_expansion_mcopy ( cb, is_mcopy. expr ( ) ) ;
731+ ErrOOGMemoryCopyGadgetTestContainer {
732+ gadget,
733+ memory_expansion_mcopy,
734+ is_mcopy,
735+ }
720736 }
721737
722738 fn assign_gadget_container (
@@ -743,12 +759,8 @@ mod tests {
743759 copy_size. into ( ) ,
744760 ) ?;
745761 // assign memory_expansion_mcopy
746- self . gadget . memory_expansion_mcopy . assign (
747- region,
748- 0 ,
749- 0 ,
750- [ src_memory_addr, dst_memory_addr] ,
751- ) ?;
762+ self . memory_expansion_mcopy
763+ . assign ( region, 0 , 0 , [ src_memory_addr, dst_memory_addr] ) ?;
752764 Ok ( ( ) )
753765 }
754766 }
0 commit comments