Skip to content

Commit 8ed1b16

Browse files
nemnem7rakita
andauthoredMar 19, 2025··
feat: Return Fatal error on bls precompiles if in no_std (#2249)
* nem/Return Fatal error on bls precompiles if in no_std * feat: Return Fatal error on bls precompiles if in no_std * feat: Return Fatal error on bls precompiles if in no_std * updated addresses * Update crates/precompile/src/lib.rs --------- Co-authored-by: rakita <rakita@users.noreply.github.com>
1 parent 82e5aef commit 8ed1b16

File tree

11 files changed

+95
-54
lines changed

11 files changed

+95
-54
lines changed
 

‎crates/optimism/src/evm.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ mod tests {
265265
fn test_halted_tx_call_bls12_381_g1_add_out_of_gas() {
266266
let ctx = Context::op()
267267
.modify_tx_chained(|tx| {
268-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G1_ADD_ADDRESS));
268+
tx.base.kind = TxKind::Call(bls12_381_const::G1_ADD_ADDRESS);
269269
tx.base.gas_limit = 21_000 + bls12_381_const::G1_ADD_BASE_GAS_FEE - 1;
270270
})
271271
.modify_chain_chained(|l1_block| {
@@ -293,7 +293,7 @@ mod tests {
293293
fn test_halted_tx_call_bls12_381_g1_add_input_wrong_size() {
294294
let ctx = Context::op()
295295
.modify_tx_chained(|tx| {
296-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G1_ADD_ADDRESS));
296+
tx.base.kind = TxKind::Call(bls12_381_const::G1_ADD_ADDRESS);
297297
tx.base.gas_limit = 21_000 + bls12_381_const::G1_ADD_BASE_GAS_FEE;
298298
})
299299
.modify_chain_chained(|l1_block| {
@@ -321,7 +321,7 @@ mod tests {
321321
fn test_halted_tx_call_bls12_381_g1_msm_input_wrong_size() {
322322
let ctx = Context::op()
323323
.modify_tx_chained(|tx| {
324-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G1_MSM_ADDRESS));
324+
tx.base.kind = TxKind::Call(bls12_381_const::G1_MSM_ADDRESS);
325325
tx.base.data = Bytes::from([1; bls12_381_const::G1_MSM_INPUT_LENGTH - 1]);
326326
})
327327
.modify_chain_chained(|l1_block| {
@@ -355,7 +355,7 @@ mod tests {
355355

356356
let ctx = Context::op()
357357
.modify_tx_chained(|tx| {
358-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G1_MSM_ADDRESS));
358+
tx.base.kind = TxKind::Call(bls12_381_const::G1_MSM_ADDRESS);
359359
tx.base.data = Bytes::from([1; bls12_381_const::G1_MSM_INPUT_LENGTH]);
360360
tx.base.gas_limit = 23_560 //initial gas for input
361361
+ gs1_msm_gas
@@ -392,7 +392,7 @@ mod tests {
392392

393393
let ctx = Context::op()
394394
.modify_tx_chained(|tx| {
395-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G1_MSM_ADDRESS));
395+
tx.base.kind = TxKind::Call(bls12_381_const::G1_MSM_ADDRESS);
396396
tx.base.data = Bytes::from([1; bls12_381_const::G1_MSM_INPUT_LENGTH]);
397397
tx.base.gas_limit = 23_560 //initial gas for input
398398
+ gs1_msm_gas;
@@ -422,7 +422,7 @@ mod tests {
422422
fn test_halted_tx_call_bls12_381_g2_msm_input_wrong_size() {
423423
let ctx = Context::op()
424424
.modify_tx_chained(|tx| {
425-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G2_MSM_ADDRESS));
425+
tx.base.kind = TxKind::Call(bls12_381_const::G2_MSM_ADDRESS);
426426
tx.base.data = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH - 1]);
427427
})
428428
.modify_chain_chained(|l1_block| {
@@ -456,7 +456,7 @@ mod tests {
456456

457457
let ctx = Context::op()
458458
.modify_tx_chained(|tx| {
459-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G2_MSM_ADDRESS));
459+
tx.base.kind = TxKind::Call(bls12_381_const::G2_MSM_ADDRESS);
460460
tx.base.data = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH]);
461461
tx.base.gas_limit = 25_608 //initial gas for input
462462
+ gs2_msm_gas
@@ -493,7 +493,7 @@ mod tests {
493493

494494
let ctx = Context::op()
495495
.modify_tx_chained(|tx| {
496-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G2_MSM_ADDRESS));
496+
tx.base.kind = TxKind::Call(bls12_381_const::G2_MSM_ADDRESS);
497497
tx.base.data = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH]);
498498
tx.base.gas_limit = 25_608 //initial gas for input
499499
+ gs2_msm_gas;
@@ -523,7 +523,7 @@ mod tests {
523523
fn test_halted_tx_call_bls12_381_pairing_input_wrong_size() {
524524
let ctx = Context::op()
525525
.modify_tx_chained(|tx| {
526-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::G2_MSM_ADDRESS));
526+
tx.base.kind = TxKind::Call(bls12_381_const::G2_MSM_ADDRESS);
527527
tx.base.data = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH - 1]);
528528
})
529529
.modify_chain_chained(|l1_block| {
@@ -554,7 +554,7 @@ mod tests {
554554

555555
let ctx = Context::op()
556556
.modify_tx_chained(|tx| {
557-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::PAIRING_ADDRESS));
557+
tx.base.kind = TxKind::Call(bls12_381_const::PAIRING_ADDRESS);
558558
tx.base.data = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
559559
tx.base.gas_limit = 27_144 //initial gas for input
560560
+ pairing_gas
@@ -588,7 +588,7 @@ mod tests {
588588

589589
let ctx = Context::op()
590590
.modify_tx_chained(|tx| {
591-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::PAIRING_ADDRESS));
591+
tx.base.kind = TxKind::Call(bls12_381_const::PAIRING_ADDRESS);
592592
tx.base.data = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
593593
tx.base.gas_limit = 27_144 //initial gas for input
594594
+ pairing_gas;
@@ -618,7 +618,7 @@ mod tests {
618618
fn test_halted_tx_call_bls12_381_map_fp_to_g1_out_of_gas() {
619619
let ctx = Context::op()
620620
.modify_tx_chained(|tx| {
621-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP_TO_G1_ADDRESS));
621+
tx.base.kind = TxKind::Call(bls12_381_const::MAP_FP_TO_G1_ADDRESS);
622622
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP_TO_G1_BASE_GAS_FEE - 1;
623623
})
624624
.modify_chain_chained(|l1_block| {
@@ -646,7 +646,7 @@ mod tests {
646646
fn test_halted_tx_call_bls12_381_map_fp_to_g1_input_wrong_size() {
647647
let ctx = Context::op()
648648
.modify_tx_chained(|tx| {
649-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP_TO_G1_ADDRESS));
649+
tx.base.kind = TxKind::Call(bls12_381_const::MAP_FP_TO_G1_ADDRESS);
650650
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP_TO_G1_BASE_GAS_FEE;
651651
})
652652
.modify_chain_chained(|l1_block| {
@@ -674,7 +674,7 @@ mod tests {
674674
fn test_halted_tx_call_bls12_381_map_fp2_to_g2_out_of_gas() {
675675
let ctx = Context::op()
676676
.modify_tx_chained(|tx| {
677-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP2_TO_G2_ADDRESS));
677+
tx.base.kind = TxKind::Call(bls12_381_const::MAP_FP2_TO_G2_ADDRESS);
678678
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP2_TO_G2_BASE_GAS_FEE - 1;
679679
})
680680
.modify_chain_chained(|l1_block| {
@@ -702,7 +702,7 @@ mod tests {
702702
fn test_halted_tx_call_bls12_381_map_fp2_to_g2_input_wrong_size() {
703703
let ctx = Context::op()
704704
.modify_tx_chained(|tx| {
705-
tx.base.kind = TxKind::Call(u64_to_address(bls12_381_const::MAP_FP2_TO_G2_ADDRESS));
705+
tx.base.kind = TxKind::Call(bls12_381_const::MAP_FP2_TO_G2_ADDRESS);
706706
tx.base.gas_limit = 21_000 + bls12_381_const::MAP_FP2_TO_G2_BASE_GAS_FEE;
707707
})
708708
.modify_chain_chained(|l1_block| {

‎crates/precompile/src/bls12_381/g1_add.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ use super::g1::{encode_g1_point, extract_g1_input_no_subgroup_check};
33
use crate::bls12_381_const::{
44
G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, PADDED_G1_LENGTH,
55
};
6-
use crate::{u64_to_address, PrecompileWithAddress};
6+
use crate::PrecompileWithAddress;
77
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
88
use primitives::Bytes;
99

1010
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile.
11-
pub const PRECOMPILE: PrecompileWithAddress =
12-
PrecompileWithAddress(u64_to_address(G1_ADD_ADDRESS), g1_add);
11+
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G1_ADD_ADDRESS, g1_add);
1312

1413
/// G1 addition call expects `256` bytes as an input that is interpreted as byte
1514
/// concatenation of two G1 points (`128` bytes each).

‎crates/precompile/src/bls12_381/g1_msm.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use crate::bls12_381_const::{
88
PADDED_G1_LENGTH, SCALAR_LENGTH,
99
};
1010
use crate::bls12_381_utils::msm_required_gas;
11-
use crate::{u64_to_address, PrecompileWithAddress};
11+
use crate::PrecompileWithAddress;
1212
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
1313
use blst::blst_p1_affine;
1414
use primitives::Bytes;
1515

1616
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile.
17-
pub const PRECOMPILE: PrecompileWithAddress =
18-
PrecompileWithAddress(u64_to_address(G1_MSM_ADDRESS), g1_msm);
17+
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G1_MSM_ADDRESS, g1_msm);
1918

2019
/// Implements EIP-2537 G1MSM precompile.
2120
/// G1 multi-scalar-multiplication call expects `160*k` bytes as an input that is interpreted

‎crates/precompile/src/bls12_381/g2_add.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ use super::g2::{encode_g2_point, extract_g2_input_no_subgroup_check};
33
use crate::bls12_381_const::{
44
G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, PADDED_G2_LENGTH,
55
};
6-
use crate::{u64_to_address, PrecompileWithAddress};
6+
use crate::PrecompileWithAddress;
77
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
88
use primitives::Bytes;
99

1010
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile.
11-
pub const PRECOMPILE: PrecompileWithAddress =
12-
PrecompileWithAddress(u64_to_address(G2_ADD_ADDRESS), g2_add);
11+
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G2_ADD_ADDRESS, g2_add);
1312

1413
/// G2 addition call expects `512` bytes as an input that is interpreted as byte
1514
/// concatenation of two G2 points (`256` bytes each).

‎crates/precompile/src/bls12_381/g2_msm.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use crate::bls12_381_const::{
88
PADDED_G2_LENGTH, SCALAR_LENGTH,
99
};
1010
use crate::bls12_381_utils::msm_required_gas;
11-
use crate::{u64_to_address, PrecompileWithAddress};
11+
use crate::PrecompileWithAddress;
1212
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
1313
use blst::blst_p2_affine;
1414
use primitives::Bytes;
1515

1616
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile.
17-
pub const PRECOMPILE: PrecompileWithAddress =
18-
PrecompileWithAddress(u64_to_address(G2_MSM_ADDRESS), g2_msm);
17+
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G2_MSM_ADDRESS, g2_msm);
1918

2019
/// Implements EIP-2537 G2MSM precompile.
2120
/// G2 multi-scalar-multiplication call expects `288*k` bytes as an input that is interpreted

‎crates/precompile/src/bls12_381/map_fp2_to_g2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use super::{
55
use crate::bls12_381_const::{
66
MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH,
77
};
8-
use crate::{u64_to_address, PrecompileWithAddress};
8+
use crate::PrecompileWithAddress;
99
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
1010
use primitives::Bytes;
1111

1212
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile.
1313
pub const PRECOMPILE: PrecompileWithAddress =
14-
PrecompileWithAddress(u64_to_address(MAP_FP2_TO_G2_ADDRESS), map_fp2_to_g2);
14+
PrecompileWithAddress(MAP_FP2_TO_G2_ADDRESS, map_fp2_to_g2);
1515

1616
/// Field-to-curve call expects 128 bytes as an input that is interpreted as
1717
/// an element of Fp2. Output of this call is 256 bytes and is an encoded G2

‎crates/precompile/src/bls12_381/map_fp_to_g1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use super::{
44
utils::{fp_from_bendian, remove_padding},
55
};
66
use crate::bls12_381_const::{MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, PADDED_FP_LENGTH};
7-
use crate::{u64_to_address, PrecompileWithAddress};
7+
use crate::PrecompileWithAddress;
88
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
99
use primitives::Bytes;
1010

1111
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile.
1212
pub const PRECOMPILE: PrecompileWithAddress =
13-
PrecompileWithAddress(u64_to_address(MAP_FP_TO_G1_ADDRESS), map_fp_to_g1);
13+
PrecompileWithAddress(MAP_FP_TO_G1_ADDRESS, map_fp_to_g1);
1414

1515
/// Field-to-curve call expects 64 bytes as an input that is interpreted as an
1616
/// element of Fp. Output of this call is 128 bytes and is an encoded G1 point.

‎crates/precompile/src/bls12_381/pairing.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ use crate::bls12_381_const::{
33
PADDED_G1_LENGTH, PADDED_G2_LENGTH, PAIRING_ADDRESS, PAIRING_INPUT_LENGTH,
44
PAIRING_MULTIPLIER_BASE, PAIRING_OFFSET_BASE,
55
};
6-
use crate::{
7-
u64_to_address, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress,
8-
};
6+
use crate::{PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
97
use primitives::{Bytes, B256};
108

119
/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile.
12-
pub const PRECOMPILE: PrecompileWithAddress =
13-
PrecompileWithAddress(u64_to_address(PAIRING_ADDRESS), pairing);
10+
pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(PAIRING_ADDRESS, pairing);
1411

1512
/// Pairing call expects 384*k (k being a positive integer) bytes as an inputs
1613
/// that is interpreted as byte concatenation of k slices. Each slice has the

‎crates/precompile/src/bls12_381_const.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
use crate::u64_to_address;
2+
use primitives::Address;
3+
14
// Constants specifying the precompile addresses for each precompile
25
// in EIP-2537
3-
pub const G1_ADD_ADDRESS: u64 = 0x0b;
4-
pub const G1_MSM_ADDRESS: u64 = 0x0c;
5-
pub const G2_ADD_ADDRESS: u64 = 0x0d;
6-
pub const G2_MSM_ADDRESS: u64 = 0x0e;
7-
pub const PAIRING_ADDRESS: u64 = 0x0f;
8-
pub const MAP_FP_TO_G1_ADDRESS: u64 = 0x10;
9-
pub const MAP_FP2_TO_G2_ADDRESS: u64 = 0x11;
6+
pub const G1_ADD_ADDRESS: Address = u64_to_address(0x0b);
7+
pub const G1_MSM_ADDRESS: Address = u64_to_address(0x0c);
8+
pub const G2_ADD_ADDRESS: Address = u64_to_address(0x0d);
9+
pub const G2_MSM_ADDRESS: Address = u64_to_address(0x0e);
10+
pub const PAIRING_ADDRESS: Address = u64_to_address(0x0f);
11+
pub const MAP_FP_TO_G1_ADDRESS: Address = u64_to_address(0x10);
12+
pub const MAP_FP2_TO_G2_ADDRESS: Address = u64_to_address(0x11);
1013

1114
/// G1_ADD_BASE_GAS_FEE specifies the amount of gas needed
1215
/// to perform the G1_ADD precompile.

‎crates/precompile/src/bls12_381_utils.rs

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use crate::bls12_381_const::MSM_MULTIPLIER;
2-
1+
use crate::bls12_381_const::{
2+
G1_ADD_ADDRESS, G1_MSM_ADDRESS, G2_ADD_ADDRESS, G2_MSM_ADDRESS, MAP_FP2_TO_G2_ADDRESS,
3+
MAP_FP_TO_G1_ADDRESS, MSM_MULTIPLIER, PAIRING_ADDRESS,
4+
};
5+
use crate::Vec;
6+
use crate::{PrecompileError, PrecompileWithAddress};
37
/// Implements the gas schedule for G1/G2 Multiscalar-multiplication assuming 30
48
/// MGas/second, see also: <https://eips.ethereum.org/EIPS/eip-2537#g1g2-multiexponentiation>
59
#[inline]
@@ -13,3 +17,43 @@ pub fn msm_required_gas(k: usize, discount_table: &[u16], multiplication_cost: u
1317

1418
(k as u64 * discount * multiplication_cost) / MSM_MULTIPLIER
1519
}
20+
21+
pub fn bls12_381_precompiles_not_supported() -> Vec<PrecompileWithAddress> {
22+
vec![
23+
PrecompileWithAddress(G1_ADD_ADDRESS, |_, _| {
24+
Err(PrecompileError::Fatal(
25+
"no_std is not supported for BLS12-381 precompiles".into(),
26+
))
27+
}),
28+
PrecompileWithAddress(G1_MSM_ADDRESS, |_, _| {
29+
Err(PrecompileError::Fatal(
30+
"no_std is not supported for BLS12-381 precompiles".into(),
31+
))
32+
}),
33+
PrecompileWithAddress(G2_ADD_ADDRESS, |_, _| {
34+
Err(PrecompileError::Fatal(
35+
"no_std is not supported for BLS12-381 precompiles".into(),
36+
))
37+
}),
38+
PrecompileWithAddress(G2_MSM_ADDRESS, |_, _| {
39+
Err(PrecompileError::Fatal(
40+
"no_std is not supported for BLS12-381 precompiles".into(),
41+
))
42+
}),
43+
PrecompileWithAddress(PAIRING_ADDRESS, |_, _| {
44+
Err(PrecompileError::Fatal(
45+
"no_std is not supported for BLS12-381 precompiles".into(),
46+
))
47+
}),
48+
PrecompileWithAddress(MAP_FP_TO_G1_ADDRESS, |_, _| {
49+
Err(PrecompileError::Fatal(
50+
"no_std is not supported for BLS12-381 precompiles".into(),
51+
))
52+
}),
53+
PrecompileWithAddress(MAP_FP2_TO_G2_ADDRESS, |_, _| {
54+
Err(PrecompileError::Fatal(
55+
"no_std is not supported for BLS12-381 precompiles".into(),
56+
))
57+
}),
58+
]
59+
}

‎crates/precompile/src/lib.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,16 @@ impl Precompiles {
161161
pub fn prague() -> &'static Self {
162162
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
163163
INSTANCE.get_or_init(|| {
164-
let precompiles = Self::cancun().clone();
165-
166-
// Don't include BLS12-381 precompiles in no_std builds.
167-
#[cfg(feature = "blst")]
168-
let precompiles = {
169-
let mut precompiles = precompiles;
170-
precompiles.extend(bls12_381::precompiles());
171-
precompiles
172-
};
164+
let mut precompiles = Self::cancun().clone();
165+
166+
cfg_if! {
167+
if #[cfg(feature = "blst")] { // if blst is enabled
168+
let bls = bls12_381::precompiles();
169+
} else {
170+
let bls = bls12_381_utils:: bls12_381_precompiles_not_supported();
171+
}
172+
}
173+
precompiles.extend(bls);
173174

174175
Box::new(precompiles)
175176
})

0 commit comments

Comments
 (0)
Please sign in to comment.