fuel_block_committer_encoding/
blob.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
mod decoder;
mod encoder;
mod header;

pub use decoder::Decoder;
pub use encoder::Encoder;
pub use header::*;

use crate::constants::BYTES_PER_BLOB;

pub type Blob = Box<[u8; BYTES_PER_BLOB]>;

#[cfg(feature = "kzg")]
pub fn generate_sidecar(
    blobs: impl IntoIterator<Item = Blob>,
) -> anyhow::Result<alloy::consensus::BlobTransactionSidecar> {
    let blobs = blobs
        .into_iter()
        .map(|blob| alloy::eips::eip4844::Blob::from(*blob))
        .collect::<Vec<_>>();
    let mut commitments = Vec::with_capacity(blobs.len());
    let mut proofs = Vec::with_capacity(blobs.len());
    let settings = alloy::consensus::EnvKzgSettings::default();

    for blob in &blobs {
        // SAFETY: same size
        let blob =
            unsafe { core::mem::transmute::<&alloy::eips::eip4844::Blob, &c_kzg::Blob>(blob) };
        let commitment = c_kzg::KzgCommitment::blob_to_kzg_commitment(blob, settings.get())?;
        let proof =
            c_kzg::KzgProof::compute_blob_kzg_proof(blob, &commitment.to_bytes(), settings.get())?;

        // SAFETY: same size
        unsafe {
            commitments.push(core::mem::transmute::<
                c_kzg::Bytes48,
                alloy::eips::eip4844::Bytes48,
            >(commitment.to_bytes()));
            proofs.push(core::mem::transmute::<
                c_kzg::Bytes48,
                alloy::eips::eip4844::Bytes48,
            >(proof.to_bytes()));
        }
    }

    Ok(alloy::consensus::BlobTransactionSidecar::new(
        blobs,
        commitments,
        proofs,
    ))
}