c_kzg/ethereum_kzg_settings/
mod.rs

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