use super::{
trusted_setup_points::{G1_POINTS, G2_POINTS},
KzgSettings,
};
use core::hash::{Hash, Hasher};
use once_cell::race::OnceBox;
use std::{boxed::Box, sync::Arc};
#[derive(Debug, Clone, Default, Eq)]
pub enum EnvKzgSettings {
#[default]
Default,
Custom(Arc<c_kzg::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 {
pub fn get(&self) -> &KzgSettings {
match self {
Self::Default => {
static DEFAULT: OnceBox<KzgSettings> = OnceBox::new();
DEFAULT.get_or_init(|| {
let settings =
KzgSettings::load_trusted_setup(G1_POINTS.as_ref(), G2_POINTS.as_ref())
.expect("failed to load default trusted setup");
Box::new(settings)
})
}
Self::Custom(settings) => settings,
}
}
}