Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create codec mod #22

Merged
merged 11 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/host/src/eigenda_fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ where
let cert_blob_info = BlobInfo::decode(&mut &item_slice[4..]).unwrap();

// TODO ensure data_length is always power of 2. Proxy made mistake
// Proxy should return a cert whose data_length measured in symbol (i.e. 32 Bytes)
// Currently, it returns number of bytes. We need to fix proxy and here later.
samlaf marked this conversation as resolved.
Show resolved Hide resolved
let data_size = cert_blob_info.blob_header.data_length as u64;
let blob_length: u64 = data_size / 32;
samlaf marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
96 changes: 0 additions & 96 deletions crates/eigenda/src/codec.rs

This file was deleted.

108 changes: 100 additions & 8 deletions crates/eigenda/src/eigenda_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::codec;
use crate::BLOB_ENCODING_VERSION_0;
use alloc::vec;
use alloy_primitives::Bytes;
use bytes::buf::Buf;
use kona_derive::errors::BlobDecodingError;

use rust_kzg_bn254::helpers;

#[derive(Default, Clone, Debug)]
Expand All @@ -12,14 +13,70 @@ pub struct EigenDABlobData {
}

impl EigenDABlobData {
/// Decodes the blob into raw byte data.
/// Decodes the blob into raw byte data. Reverse of the encode function below
/// Returns a [BlobDecodingError] if the blob is invalid.
pub(crate) fn decode(&self) -> Result<Bytes, BlobDecodingError> {
let rollup_blob = codec::decode_eigenda_blob(&self.blob)?;
// might insert a FFT here,
pub fn decode(&self) -> Result<Bytes, BlobDecodingError> {
let blob = &self.blob;
if blob.len() < 32 {
return Err(BlobDecodingError::InvalidLength);
}

info!(target: "eigenda-datasource", "padded_eigenda_blob {:?}", blob);

// see https://github.com/Layr-Labs/eigenda/blob/f8b0d31d65b29e60172507074922668f4ca89420/api/clients/codecs/default_blob_codec.go#L44
let content_size = blob.slice(2..6).get_u32();
info!(target: "eigenda-datasource", "content_size {:?}", content_size);

// the first 32 Bytes are reserved as the header field element
let codec_data = blob.slice(32..);

// rust kzg bn254 impl already
let blob_content =
helpers::remove_empty_byte_from_padded_bytes_unchecked(codec_data.as_ref());
let blob_content: Bytes = blob_content.into();

if blob_content.len() < content_size as usize {
return Err(BlobDecodingError::InvalidLength);
}
Ok(blob_content.slice(..content_size as usize))
}

/// The encode function accepts an input of opaque rollup data array into an EigenDABlobData.
/// EigenDABlobData contains a header of 32 bytes and a transformation of input data
/// The 0 index byte of header is always 0, to comply to bn254 field element constraint
/// The 1 index byte of header is proxy encoding version.
/// The 2-4 indices of header are storing the length of the input rollup data in big endien
/// The payload is prepared by padding an empty byte for every 31 bytes from the rollup data
/// This matches exactly the eigenda proxy implementation, whose logic is in
/// https://github.com/Layr-Labs/eigenda/blob/master/encoding/utils/codec/codec.go#L12
///
/// The length of (header + payload) by the encode function is always power of 2
/// The eigenda proxy does not take such constraint.
/// TODO it is possible to remove such power of 2 constraint, such that the client is not
/// relying on the data_length from eigenda cert. It might save some comm rounds between
/// host and client.
pub fn encode(rollup_data: &[u8]) -> Self {
let rollup_data_size = rollup_data.len() as u32;

// encode to become raw blob
let codec_rollup_data = helpers::convert_by_padding_empty_byte(rollup_data.as_ref());

let blob_payload_size = codec_rollup_data.len();

let blob_size = blob_payload_size + 32;
let blob_size = blob_size.next_power_of_two();

let mut raw_blob = vec![0u8; blob_size as usize];

raw_blob[1] = BLOB_ENCODING_VERSION_0;
raw_blob[2..6].copy_from_slice(&rollup_data_size.to_be_bytes());

// encode length as uint32
raw_blob[32..(32 + blob_payload_size as usize)].copy_from_slice(&codec_rollup_data);

// take data
Ok(rollup_blob)
Self {
blob: Bytes::from(raw_blob),
}
}
}

Expand Down Expand Up @@ -69,4 +126,39 @@ mod tests {
assert!(result.is_err());
assert_eq!(result.unwrap_err(), BlobDecodingError::InvalidLength);
}

#[test]
fn test_encode_and_decode_success() {
let rollup_data = vec![1, 2, 3, 4];
let eigenda_blob = EigenDABlobData::encode(&rollup_data);
let data_len = eigenda_blob.blob.len();
assert!(data_len.is_power_of_two());

samlaf marked this conversation as resolved.
Show resolved Hide resolved
let result = eigenda_blob.decode();
assert!(result.is_ok());
assert_eq!(result.unwrap(), Bytes::from(rollup_data));
}

#[test]
fn test_encode_and_decode_success_empty() {
let rollup_data = vec![];
let eigenda_blob = EigenDABlobData::encode(&rollup_data);
let data_len = eigenda_blob.blob.len();
// 32 is eigenda blob header size
assert!(data_len == 32);

let result = eigenda_blob.decode();
assert!(result.is_ok());
assert_eq!(result.unwrap(), Bytes::from(rollup_data));
}

#[test]
fn test_encode_and_decode_error_invalid_length() {
let rollup_data = vec![1, 2, 3, 4];
let mut eigenda_blob = EigenDABlobData::encode(&rollup_data);
eigenda_blob.blob.truncate(33);
let result = eigenda_blob.decode();
assert!(result.is_err());
assert_eq!(result.unwrap_err(), BlobDecodingError::InvalidLength);
}
}
4 changes: 0 additions & 4 deletions crates/eigenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ pub use eigenda_data::EigenDABlobData;
mod certificate;
pub use certificate::BlobInfo;

pub mod codec;
pub use codec::decode_eigenda_blob;
pub use codec::encode_eigenda_blob;

mod constant;
pub use constant::BLOB_ENCODING_VERSION_0;
pub use constant::STALE_GAP;
Loading