Skip to content

Commit 767c531

Browse files
authored
test(op-precompiles): Add tests for bls12-381 map fp to g (#2241)
* Drive-by, fix constant names * Add tests for map fp to g1 * Add tests for map fp2 to g2
1 parent 2a347d4 commit 767c531

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

crates/optimism/src/evm.rs

+112
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,116 @@ mod tests {
612612
}
613613
));
614614
}
615+
616+
#[test]
617+
#[cfg(feature = "blst")]
618+
fn test_halted_tx_call_bls12_381_map_fp_to_g1_out_of_gas() {
619+
let ctx = Context::op()
620+
.modify_tx_chained(|tx| {
621+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP_TO_G1_ADDRESS));
622+
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP_TO_G1_BASE_GAS_FEE - 1;
623+
})
624+
.modify_chain_chained(|l1_block| {
625+
l1_block.operator_fee_constant = Some(U256::ZERO);
626+
l1_block.operator_fee_scalar = Some(U256::ZERO)
627+
})
628+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
629+
630+
let mut evm = ctx.build_op();
631+
632+
let output = evm.replay().unwrap();
633+
634+
// assert out of gas
635+
assert!(matches!(
636+
output.result,
637+
ExecutionResult::Halt {
638+
reason: OpHaltReason::Base(HaltReason::OutOfGas(OutOfGasError::Precompile)),
639+
..
640+
}
641+
));
642+
}
643+
644+
#[test]
645+
#[cfg(feature = "blst")]
646+
fn test_halted_tx_call_bls12_381_map_fp_to_g1_input_wrong_size() {
647+
let ctx = Context::op()
648+
.modify_tx_chained(|tx| {
649+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP_TO_G1_ADDRESS));
650+
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP_TO_G1_BASE_GAS_FEE;
651+
})
652+
.modify_chain_chained(|l1_block| {
653+
l1_block.operator_fee_constant = Some(U256::ZERO);
654+
l1_block.operator_fee_scalar = Some(U256::ZERO)
655+
})
656+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
657+
658+
let mut evm = ctx.build_op();
659+
660+
let output = evm.replay().unwrap();
661+
662+
// assert fails post gas check, because input is wrong size
663+
assert!(matches!(
664+
output.result,
665+
ExecutionResult::Halt {
666+
reason: OpHaltReason::Base(HaltReason::PrecompileError),
667+
..
668+
}
669+
));
670+
}
671+
672+
#[test]
673+
#[cfg(feature = "blst")]
674+
fn test_halted_tx_call_bls12_381_map_fp2_to_g2_out_of_gas() {
675+
let ctx = Context::op()
676+
.modify_tx_chained(|tx| {
677+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP2_TO_G2_ADDRESS));
678+
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP2_TO_G2_BASE_GAS_FEE - 1;
679+
})
680+
.modify_chain_chained(|l1_block| {
681+
l1_block.operator_fee_constant = Some(U256::ZERO);
682+
l1_block.operator_fee_scalar = Some(U256::ZERO)
683+
})
684+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
685+
686+
let mut evm = ctx.build_op();
687+
688+
let output = evm.replay().unwrap();
689+
690+
// assert out of gas
691+
assert!(matches!(
692+
output.result,
693+
ExecutionResult::Halt {
694+
reason: OpHaltReason::Base(HaltReason::OutOfGas(OutOfGasError::Precompile)),
695+
..
696+
}
697+
));
698+
}
699+
700+
#[test]
701+
#[cfg(feature = "blst")]
702+
fn test_halted_tx_call_bls12_381_map_fp2_to_g2_input_wrong_size() {
703+
let ctx = Context::op()
704+
.modify_tx_chained(|tx| {
705+
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP2_TO_G2_ADDRESS));
706+
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP2_TO_G2_BASE_GAS_FEE;
707+
})
708+
.modify_chain_chained(|l1_block| {
709+
l1_block.operator_fee_constant = Some(U256::ZERO);
710+
l1_block.operator_fee_scalar = Some(U256::ZERO)
711+
})
712+
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
713+
714+
let mut evm = ctx.build_op();
715+
716+
let output = evm.replay().unwrap();
717+
718+
// assert fails post gas check, because input is wrong size
719+
assert!(matches!(
720+
output.result,
721+
ExecutionResult::Halt {
722+
reason: OpHaltReason::Base(HaltReason::PrecompileError),
723+
..
724+
}
725+
));
726+
}
615727
}

0 commit comments

Comments
 (0)