alloy_eips/eip4844/
env_settings.rs

1use alloc::sync::Arc;
2use core::hash::{Hash, Hasher};
3
4// Re-export for convenience
5pub use c_kzg::KzgSettings;
6
7/// Precompute value that optimizes computing cell kzg proofs.
8///
9/// Set to 0 as we do not use `compute_cells_and_kzg_proofs` or `recover_cells_and_kzg_proofs`.
10///
11/// Learn more: <https://github.com/ethereum/c-kzg-4844/blob/dffa18ee350aeef38f749ffad24a27c1645fb4f8/README.md?plain=1#L112>
12const PRECOMPUTE: u64 = 0;
13
14/// KZG settings.
15#[derive(Clone, Debug, Default, Eq)]
16pub enum EnvKzgSettings {
17    /// Default mainnet trusted setup.
18    #[default]
19    Default,
20    /// Custom trusted setup.
21    Custom(Arc<KzgSettings>),
22}
23
24// Implement PartialEq and Hash manually because `c_kzg::KzgSettings` does not implement them.
25impl PartialEq for EnvKzgSettings {
26    fn eq(&self, other: &Self) -> bool {
27        match (self, other) {
28            (Self::Default, Self::Default) => true,
29            (Self::Custom(a), Self::Custom(b)) => Arc::ptr_eq(a, b),
30            _ => false,
31        }
32    }
33}
34
35impl Hash for EnvKzgSettings {
36    fn hash<H: Hasher>(&self, state: &mut H) {
37        core::mem::discriminant(self).hash(state);
38        match self {
39            Self::Default => {}
40            Self::Custom(settings) => Arc::as_ptr(settings).hash(state),
41        }
42    }
43}
44
45impl EnvKzgSettings {
46    /// Returns the KZG settings.
47    ///
48    /// This will initialize the default settings if it is not already loaded.
49    #[inline]
50    pub fn get(&self) -> &KzgSettings {
51        match self {
52            Self::Default => c_kzg::ethereum_kzg_settings(PRECOMPUTE),
53            Self::Custom(settings) => settings,
54        }
55    }
56
57    /// Load custom KZG settings from a trusted setup file.
58    #[cfg(feature = "std")]
59    pub fn load_from_trusted_setup_file(
60        trusted_setup_file: &std::path::Path,
61    ) -> Result<Self, c_kzg::Error> {
62        let settings = KzgSettings::load_trusted_setup_file(trusted_setup_file, PRECOMPUTE)?;
63        Ok(Self::Custom(Arc::new(settings)))
64    }
65}