Skip to content

Commit 8a25053

Browse files
authored
test(op-precompiles): Reuse tests for bls12-381 msm tests for pairing (#2239)
* Reuse tests for bls12-381 msm tests for pairing * Fix lint
1 parent f473029 commit 8a25053

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

crates/optimism/src/evm.rs

+95
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,99 @@ mod tests {
517517
}
518518
));
519519
}
520+
521+
#[test]
522+
#[cfg(feature = "blst")]
523+
fn test_halted_tx_call_bls12_381_pairing_input_wrong_size() {
524+
let ctx = Context::op()
525+
.modify_tx_chained(|tx| {
526+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G2_MSM_ADDRESS));
527+
tx.base.data = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH - 1]);
528+
})
529+
.modify_chain_chained(|l1_block| {
530+
l1_block.operator_fee_constant = Some(U256::ZERO);
531+
l1_block.operator_fee_scalar = Some(U256::ZERO)
532+
})
533+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
534+
535+
let mut evm = ctx.build_op();
536+
537+
let output = evm.replay().unwrap();
538+
539+
// assert fails pre gas check, because input is wrong size
540+
assert!(matches!(
541+
output.result,
542+
ExecutionResult::Halt {
543+
reason: OpHaltReason::Base(HaltReason::PrecompileError),
544+
..
545+
}
546+
));
547+
}
548+
549+
#[test]
550+
#[cfg(feature = "blst")]
551+
fn test_halted_tx_call_bls12_381_pairing_out_of_gas() {
552+
let pairing_gas: u64 = bls12_381_const::PAIRING_PAIRING_MULTIPLIER_BASE
553+
+ bls12_381_const::PAIRING_PAIRING_OFFSET_BASE;
554+
555+
let ctx = Context::op()
556+
.modify_tx_chained(|tx| {
557+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::PAIRING_ADDRESS));
558+
tx.base.data = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
559+
tx.base.gas_limit = 27_144 //initial gas for input
560+
+ pairing_gas
561+
- 1; // 1 gas low
562+
})
563+
.modify_chain_chained(|l1_block| {
564+
l1_block.operator_fee_constant = Some(U256::ZERO);
565+
l1_block.operator_fee_scalar = Some(U256::ZERO)
566+
})
567+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
568+
569+
let mut evm = ctx.build_op();
570+
571+
let output = evm.replay().unwrap();
572+
573+
// assert out of gas
574+
assert!(matches!(
575+
output.result,
576+
ExecutionResult::Halt {
577+
reason: OpHaltReason::Base(HaltReason::OutOfGas(OutOfGasError::Precompile)),
578+
..
579+
}
580+
));
581+
}
582+
583+
#[test]
584+
#[cfg(feature = "blst")]
585+
fn test_tx_call_bls12_381_pairing_wrong_input_layout() {
586+
let pairing_gas: u64 = bls12_381_const::PAIRING_PAIRING_MULTIPLIER_BASE
587+
+ bls12_381_const::PAIRING_PAIRING_OFFSET_BASE;
588+
589+
let ctx = Context::op()
590+
.modify_tx_chained(|tx| {
591+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::PAIRING_ADDRESS));
592+
tx.base.data = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
593+
tx.base.gas_limit = 27_144 //initial gas for input
594+
+ pairing_gas;
595+
})
596+
.modify_chain_chained(|l1_block| {
597+
l1_block.operator_fee_constant = Some(U256::ZERO);
598+
l1_block.operator_fee_scalar = Some(U256::ZERO)
599+
})
600+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
601+
602+
let mut evm = ctx.build_op();
603+
604+
let output = evm.replay().unwrap();
605+
606+
// assert fails post gas check, because input is wrong layout
607+
assert!(matches!(
608+
output.result,
609+
ExecutionResult::Halt {
610+
reason: OpHaltReason::Base(HaltReason::PrecompileError),
611+
..
612+
}
613+
));
614+
}
520615
}

0 commit comments

Comments
 (0)