Skip to content

Commit 913800e

Browse files
authored
fix modexp input (#1099)
1 parent d0a7cb3 commit 913800e

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

bus-mapping/src/precompile.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,11 @@ impl ModExpAuxData {
287287
/// Create a new instance of modexp auxiliary data.
288288
pub fn new(input: &[u8], output: &[u8], return_bytes: &[u8]) -> Self {
289289
let mut resized_input = input.to_vec();
290+
if resized_input.len() < 96 {
291+
resized_input.resize(96, 0);
292+
}
290293

291-
let (input_valid, [base_len, exp_len, modulus_len]) = Self::check_input(input);
294+
let (input_valid, [base_len, exp_len, modulus_len]) = Self::check_input(&resized_input);
292295

293296
let base_mem_len = if input_valid { base_len.as_usize() } else { 0 };
294297
let exp_mem_len = if input_valid { exp_len.as_usize() } else { 0 };

zkevm-circuits/src/evm_circuit/execution/begin_tx.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,27 @@ mod test {
16221622
CircuitTestBuilder::new_from_test_ctx(ctx).run();
16231623
}
16241624

1625+
/// from failed testtool case
1626+
#[test]
1627+
fn begin_tx_precompile_fail_modexp() {
1628+
let ctx = TestContext::<1, 1>::new(
1629+
None,
1630+
|accs| {
1631+
accs[0].address(MOCK_ACCOUNTS[0]).balance(eth(20));
1632+
},
1633+
|mut txs, accs| {
1634+
txs[0]
1635+
.from(accs[0].address)
1636+
.to(address!("0x0000000000000000000000000000000000000005"))
1637+
.input(Bytes::from_str("0x00000000008000000000000000000000000000000000000000000000000000000000000400000000000000000000000a").unwrap());
1638+
},
1639+
|block, _tx| block.number(0xcafeu64),
1640+
)
1641+
.unwrap();
1642+
1643+
CircuitTestBuilder::new_from_test_ctx(ctx).run();
1644+
}
1645+
16251646
/// testool case EmptyTransaction3_d0_g0_v0
16261647
#[test]
16271648
fn begin_tx_create_empty_tx() {

zkevm-circuits/src/evm_circuit/execution/precompiles/modexp.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ impl<F: Field> ExecutionGadget<F> for ModExpGadget<F> {
825825
cb.curr.state.gas_left.expr(),
826826
);
827827

828-
let required_input_len = 192.expr();
828+
let required_input_len = MODEXP_INPUT_LIMIT.expr();
829829
let pad_right = LtGadget::construct(cb, call_data_length.expr(), required_input_len.expr());
830830
let padding = cb.condition(pad_right.expr(), |cb| {
831831
PaddingGadget::construct(
@@ -1535,6 +1535,26 @@ mod test {
15351535
gas: 1000.into(),
15361536
..Default::default()
15371537
},
1538+
PrecompileCallArgs {
1539+
name: "from test tool: zero padding and invalid size",
1540+
setup_code: bytecode! {
1541+
// Base size
1542+
PUSH32(word!("0x0000000000800000000000000000000000000000000000000000000000000000"))
1543+
PUSH1(0x00)
1544+
MSTORE
1545+
// Esize
1546+
PUSH32(word!("0x000000400000000000000000000000a000000000000000000000000000000000"))
1547+
PUSH1(0x20)
1548+
MSTORE
1549+
},
1550+
call_data_offset: 0x0.into(),
1551+
call_data_length: 0x30.into(),
1552+
ret_offset: 0xe0.into(),
1553+
ret_size: 0x20.into(),
1554+
address: PrecompileCalls::Modexp.address().to_word(),
1555+
gas: 100000.into(),
1556+
..Default::default()
1557+
},
15381558
]
15391559
});
15401560

0 commit comments

Comments
 (0)