forked from bluealloy/revm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbls12_381_utils.rs
59 lines (56 loc) · 2.2 KB
/
bls12_381_utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::bls12_381_const::{
G1_ADD_ADDRESS, G1_MSM_ADDRESS, G2_ADD_ADDRESS, G2_MSM_ADDRESS, MAP_FP2_TO_G2_ADDRESS,
MAP_FP_TO_G1_ADDRESS, MSM_MULTIPLIER, PAIRING_ADDRESS,
};
use crate::Vec;
use crate::{PrecompileError, PrecompileWithAddress};
/// Implements the gas schedule for G1/G2 Multiscalar-multiplication assuming 30
/// MGas/second, see also: <https://eips.ethereum.org/EIPS/eip-2537#g1g2-multiexponentiation>
#[inline]
pub fn msm_required_gas(k: usize, discount_table: &[u16], multiplication_cost: u64) -> u64 {
if k == 0 {
return 0;
}
let index = core::cmp::min(k - 1, discount_table.len() - 1);
let discount = discount_table[index] as u64;
(k as u64 * discount * multiplication_cost) / MSM_MULTIPLIER
}
pub fn bls12_381_precompiles_not_supported() -> Vec<PrecompileWithAddress> {
vec![
PrecompileWithAddress(G1_ADD_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
PrecompileWithAddress(G1_MSM_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
PrecompileWithAddress(G2_ADD_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
PrecompileWithAddress(G2_MSM_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
PrecompileWithAddress(PAIRING_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
PrecompileWithAddress(MAP_FP_TO_G1_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
PrecompileWithAddress(MAP_FP2_TO_G2_ADDRESS, |_, _| {
Err(PrecompileError::Fatal(
"no_std is not supported for BLS12-381 precompiles".into(),
))
}),
]
}