kzg_rs/
trusted_setup.rs

1use crate::{enums::KzgError, NUM_G1_POINTS, NUM_ROOTS_OF_UNITY};
2
3use alloc::sync::Arc;
4use bls12_381::{G1Affine, G2Affine, Scalar};
5use core::{
6    hash::{Hash, Hasher},
7    mem::transmute,
8    slice,
9};
10use spin::Once;
11
12pub fn get_roots_of_unity() -> &'static [Scalar] {
13    static ROOTS_OF_UNITY: Once<&'static [Scalar]> = Once::new();
14    ROOTS_OF_UNITY.call_once(|| {
15        let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/roots_of_unity.bin"));
16        unsafe { transmute(slice::from_raw_parts(bytes.as_ptr(), NUM_ROOTS_OF_UNITY)) }
17    })
18}
19
20pub fn get_g1_points() -> &'static [G1Affine] {
21    static G1_POINTS: Once<&'static [G1Affine]> = Once::new();
22    G1_POINTS.call_once(|| {
23        let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/g1.bin"));
24        unsafe { transmute(slice::from_raw_parts(bytes.as_ptr(), NUM_G1_POINTS)) }
25    })
26}
27
28pub fn get_g2_points() -> &'static [G2Affine] {
29    static G2_POINTS: Once<&'static [G2Affine]> = Once::new();
30    G2_POINTS.call_once(|| {
31        let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/g2.bin"));
32        unsafe { transmute(slice::from_raw_parts(bytes.as_ptr(), NUM_G1_POINTS)) }
33    })
34}
35
36pub fn get_kzg_settings() -> KzgSettings {
37    KzgSettings {
38        roots_of_unity: get_roots_of_unity(),
39        g1_points: get_g1_points(),
40        g2_points: get_g2_points(),
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45#[repr(C, align(4))]
46pub struct KzgSettings {
47    pub roots_of_unity: &'static [Scalar],
48    pub g1_points: &'static [G1Affine],
49    pub g2_points: &'static [G2Affine],
50}
51
52#[derive(Debug, Clone, Default, Eq)]
53pub enum EnvKzgSettings {
54    #[default]
55    Default,
56    Custom(Arc<KzgSettings>),
57}
58
59impl PartialEq for EnvKzgSettings {
60    fn eq(&self, other: &Self) -> bool {
61        match (self, other) {
62            (Self::Default, Self::Default) => true,
63            (Self::Custom(a), Self::Custom(b)) => Arc::ptr_eq(a, b),
64            _ => false,
65        }
66    }
67}
68
69impl Hash for EnvKzgSettings {
70    fn hash<H: Hasher>(&self, state: &mut H) {
71        core::mem::discriminant(self).hash(state);
72        match self {
73            Self::Default => {}
74            Self::Custom(settings) => Arc::as_ptr(settings).hash(state),
75        }
76    }
77}
78
79impl EnvKzgSettings {
80    pub fn get(&self) -> &KzgSettings {
81        match self {
82            Self::Default => {
83                static DEFAULT: Once<KzgSettings> = Once::new();
84                DEFAULT.call_once(|| {
85                    KzgSettings::load_trusted_setup_file()
86                        .expect("failed to load default trusted setup")
87                })
88            }
89            Self::Custom(settings) => settings,
90        }
91    }
92}
93
94impl KzgSettings {
95    pub fn load_trusted_setup_file() -> Result<Self, KzgError> {
96        Ok(get_kzg_settings())
97    }
98}