use alloc::sync::Arc;
use core::hash::{Hash, Hasher};
pub use c_kzg::KzgSettings;
const PRECOMPUTE: u64 = 8;
#[derive(Clone, Debug, Default, Eq)]
pub enum EnvKzgSettings {
#[default]
Default,
Custom(Arc<KzgSettings>),
}
impl PartialEq for EnvKzgSettings {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Default, Self::Default) => true,
(Self::Custom(a), Self::Custom(b)) => Arc::ptr_eq(a, b),
_ => false,
}
}
}
impl Hash for EnvKzgSettings {
fn hash<H: Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
match self {
Self::Default => {}
Self::Custom(settings) => Arc::as_ptr(settings).hash(state),
}
}
}
impl EnvKzgSettings {
#[inline]
pub fn get(&self) -> &KzgSettings {
match self {
Self::Default => c_kzg::ethereum_kzg_settings(PRECOMPUTE),
Self::Custom(settings) => settings,
}
}
#[cfg(feature = "std")]
pub fn load_from_trusted_setup_file(
trusted_setup_file: &std::path::Path,
) -> Result<Self, c_kzg::Error> {
let settings = KzgSettings::load_trusted_setup_file(trusted_setup_file, PRECOMPUTE)?;
Ok(Self::Custom(Arc::new(settings)))
}
}