@@ -41,6 +41,8 @@ pub(crate) struct ErrorOOGMemoryCopyGadget<F> {
41
41
external_address : Word < F > ,
42
42
43
43
addr_expansion_gadget : MemoryAddrExpandGadget < F > ,
44
+ // mcopy expansion
45
+ memory_expansion_mcopy : MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > ,
44
46
// other kind(CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY) expansion
45
47
memory_expansion_normal : MemoryExpansionGadget < F , 1 , N_BYTES_MEMORY_WORD_SIZE > ,
46
48
memory_copier_gas : MemoryCopierGasGadget < F , { GasCost :: COPY } > ,
@@ -91,12 +93,16 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
91
93
cb. stack_pop ( external_address. expr ( ) ) ;
92
94
} ) ;
93
95
94
- let addr_expansion_gadget = MemoryAddrExpandGadget :: construct ( cb, is_mcopy . expr ( ) ) ;
96
+ let addr_expansion_gadget = MemoryAddrExpandGadget :: construct ( cb) ;
95
97
96
98
cb. stack_pop ( addr_expansion_gadget. dst_memory_addr . offset_rlc ( ) ) ;
97
99
cb. stack_pop ( addr_expansion_gadget. src_memory_addr . offset_rlc ( ) ) ;
98
100
cb. stack_pop ( addr_expansion_gadget. dst_memory_addr . length_rlc ( ) ) ;
99
101
102
+ // for mcopy
103
+ let memory_expansion_mcopy =
104
+ addr_expansion_gadget. build_memory_expansion_mcopy ( cb, is_mcopy. expr ( ) ) ;
105
+
100
106
// for others (CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY)
101
107
let memory_expansion_normal = cb. condition ( not:: expr ( is_mcopy. expr ( ) ) , |cb| {
102
108
MemoryExpansionGadget :: construct (
@@ -107,7 +113,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
107
113
108
114
let memory_expansion_cost = select:: expr (
109
115
is_mcopy. expr ( ) ,
110
- addr_expansion_gadget . memory_expansion_mcopy . gas_cost ( ) ,
116
+ memory_expansion_mcopy. gas_cost ( ) ,
111
117
memory_expansion_normal. gas_cost ( ) ,
112
118
) ;
113
119
let memory_copier_gas = MemoryCopierGasGadget :: construct (
@@ -160,6 +166,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
160
166
tx_id,
161
167
external_address,
162
168
addr_expansion_gadget,
169
+ memory_expansion_mcopy,
163
170
memory_expansion_normal,
164
171
memory_copier_gas,
165
172
insufficient_gas,
@@ -227,13 +234,12 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
227
234
) ?;
228
235
229
236
// 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
+ ) ?;
237
243
238
244
let memory_copier_gas = self . memory_copier_gas . assign (
239
245
region,
@@ -291,33 +297,38 @@ pub(crate) struct MemoryAddrExpandGadget<F> {
291
297
src_memory_addr : MemoryExpandedAddressGadget < F > ,
292
298
/// Destination offset and size to copy
293
299
dst_memory_addr : MemoryExpandedAddressGadget < F > ,
294
- // mcopy expansion
295
- memory_expansion_mcopy : MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > ,
296
300
}
297
301
298
302
// construct src_memory_addr, dst_memory_addr and memory_expansion_mcopy.
299
303
impl < F : Field > MemoryAddrExpandGadget < F > {
300
- fn construct ( cb : & mut EVMConstraintBuilder < F > , is_mcopy : Expression < F > ) -> Self {
304
+ fn construct ( cb : & mut EVMConstraintBuilder < F > ) -> Self {
301
305
let dst_memory_addr = MemoryExpandedAddressGadget :: construct_self ( cb) ;
302
306
// src can also be possible to overflow for mcopy.
303
307
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| {
306
319
cb. require_equal (
307
320
"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 ( ) ,
310
323
) ;
311
324
MemoryExpansionGadget :: construct (
312
325
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
+ ] ,
314
330
)
315
- } ) ;
316
- Self {
317
- src_memory_addr,
318
- dst_memory_addr,
319
- memory_expansion_mcopy,
320
- }
331
+ } )
321
332
}
322
333
}
323
334
@@ -707,16 +718,21 @@ mod tests {
707
718
#[ derive( Clone ) ]
708
719
struct ErrOOGMemoryCopyGadgetTestContainer < F > {
709
720
gadget : MemoryAddrExpandGadget < F > ,
721
+ memory_expansion_mcopy : MemoryExpansionGadget < F , 2 , N_BYTES_MEMORY_WORD_SIZE > ,
710
722
is_mcopy : Cell < F > ,
711
723
}
712
724
713
725
impl < F : Field > MathGadgetContainer < F > for ErrOOGMemoryCopyGadgetTestContainer < F > {
714
726
fn configure_gadget_container ( cb : & mut EVMConstraintBuilder < F > ) -> Self {
715
727
let is_mcopy = cb. query_cell ( ) ;
716
728
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
+ }
720
736
}
721
737
722
738
fn assign_gadget_container (
@@ -743,12 +759,8 @@ mod tests {
743
759
copy_size. into ( ) ,
744
760
) ?;
745
761
// 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] ) ?;
752
764
Ok ( ( ) )
753
765
}
754
766
}
0 commit comments