c_kzg/ethereum_kzg_settings/
mod.rs

1use crate::KzgSettings;
2use alloc::{boxed::Box, sync::Arc};
3use once_cell::race::OnceBox;
4
5/// Default G1 Lagrange bytes.
6const ETH_G1_MONOMIAL_POINTS: &[u8] = include_bytes!("./g1_monomial_bytes.bin");
7/// Default G1 Lagrange bytes.
8const ETH_G1_LAGRANGE_POINTS: &[u8] = include_bytes!("./g1_lagrange_bytes.bin");
9/// Default G2 monomial bytes.
10const ETH_G2_MONOMIAL_POINTS: &[u8] = include_bytes!("./g2_monomial_bytes.bin");
11
12/// Returns default Ethereum mainnet KZG settings.
13///
14/// If you need a cloneable settings use `ethereum_kzg_settings_arc` instead.
15pub fn ethereum_kzg_settings(precompute: u64) -> &'static KzgSettings {
16    ethereum_kzg_settings_inner(precompute).as_ref()
17}
18
19/// Returns default Ethereum mainnet KZG settings as an `Arc`.
20///
21/// It is useful for sharing the settings in multiple places.
22pub fn ethereum_kzg_settings_arc(precompute: u64) -> Arc<KzgSettings> {
23    ethereum_kzg_settings_inner(precompute).clone()
24}
25
26fn ethereum_kzg_settings_inner(precompute: u64) -> &'static Arc<KzgSettings> {
27    static DEFAULT: OnceBox<Arc<KzgSettings>> = OnceBox::new();
28    DEFAULT.get_or_init(|| {
29        let settings = KzgSettings::load_trusted_setup(
30            ETH_G1_MONOMIAL_POINTS,
31            ETH_G1_LAGRANGE_POINTS,
32            ETH_G2_MONOMIAL_POINTS,
33            precompute,
34        )
35        .expect("failed to load default trusted setup");
36        Box::new(Arc::new(settings))
37    })
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::{bindings::BYTES_PER_BLOB, Blob, KzgSettings};
44    use std::path::Path;
45
46    #[test]
47    pub fn compare_default_with_file() {
48        let precompute = 0;
49        let ts_settings =
50            KzgSettings::load_trusted_setup_file(Path::new("src/trusted_setup.txt"), precompute)
51                .unwrap();
52        let eth_settings = ethereum_kzg_settings(precompute);
53        let blob = Blob::new([1u8; BYTES_PER_BLOB]);
54
55        // generate commitment
56        let ts_commitment = ts_settings
57            .blob_to_kzg_commitment(&blob)
58            .unwrap()
59            .to_bytes();
60        let eth_commitment = eth_settings
61            .blob_to_kzg_commitment(&blob)
62            .unwrap()
63            .to_bytes();
64        assert_eq!(ts_commitment, eth_commitment);
65
66        // generate proof
67        let ts_proof = ts_settings
68            .compute_blob_kzg_proof(&blob, &ts_commitment)
69            .unwrap()
70            .to_bytes();
71        let eth_proof = eth_settings
72            .compute_blob_kzg_proof(&blob, &eth_commitment)
73            .unwrap()
74            .to_bytes();
75        assert_eq!(ts_proof, eth_proof);
76    }
77}