Skip to content

Commit

Permalink
Katana: Add state update DA encodings (#2474)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Sep 25, 2024
1 parent 8453b63 commit c11da6c
Show file tree
Hide file tree
Showing 13 changed files with 598 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/katana/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ anyhow.workspace = true
base64.workspace = true
derive_more.workspace = true
lazy_static.workspace = true
num-traits.workspace = true
rand = { workspace = true, features = [ "small_rng" ] }
serde.workspace = true
serde_json.workspace = true
Expand All @@ -23,10 +24,11 @@ thiserror.workspace = true
alloy-primitives.workspace = true
flate2 = { workspace = true, optional = true }
katana-cairo.workspace = true
num-bigint = "0.4.6"

[dev-dependencies]
num-traits.workspace = true
assert_matches.workspace = true
rstest.workspace = true
similar-asserts.workspace = true

[features]
Expand Down
41 changes: 41 additions & 0 deletions crates/katana/primitives/src/da/blob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use num_bigint::BigUint;
use num_traits::Num;

use super::eip4844::{BLOB_LEN, BLS_MODULUS, GENERATOR};
use super::math::{fft, ifft};

/// Recovers the original data from a given blob.
///
/// This function takes a vector of `BigUint` representing the data of a blob and
/// returns the recovered original data as a vector of `BigUint`.
///
/// # Arguments
///
/// * `data` - A vector of `BigUint` representing the blob data.
///
/// # Returns
///
/// A vector of `BigUint` representing the recovered original data.
pub fn recover(data: Vec<BigUint>) -> Vec<BigUint> {
let xs: Vec<BigUint> = (0..BLOB_LEN)
.map(|i| {
let bin = format!("{:012b}", i);
let bin_rev = bin.chars().rev().collect::<String>();
GENERATOR.modpow(&BigUint::from_str_radix(&bin_rev, 2).unwrap(), &BLS_MODULUS)
})
.collect();

ifft(data, xs, &BLS_MODULUS)
}

pub fn transform(data: Vec<BigUint>) -> Vec<BigUint> {
let xs: Vec<BigUint> = (0..BLOB_LEN)
.map(|i| {
let bin = format!("{:012b}", i);
let bin_rev = bin.chars().rev().collect::<String>();
GENERATOR.modpow(&BigUint::from_str_radix(&bin_rev, 2).unwrap(), &BLS_MODULUS)
})
.collect();

fft(data, xs, &BLS_MODULUS)
}
26 changes: 26 additions & 0 deletions crates/katana/primitives/src/da/eip4844.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::str::FromStr;

use lazy_static::lazy_static;
use num_bigint::{BigUint, ToBigUint};

// ****************************************************************************
// * PARAMETERS & CONSTANTS *
// ****************************************************************************
/// Length of the blob.
pub const BLOB_LEN: usize = 4096;

lazy_static! {
/// EIP-4844 BLS12-381 modulus.
///
/// As defined in https://eips.ethereum.org/EIPS/eip-4844
pub static ref BLS_MODULUS: BigUint = BigUint::from_str(
"52435875175126190479447740508185965837690552500527637822603658699938581184513",
)
.unwrap();
/// Generator of the group of evaluation points (EIP-4844 parameter).
pub static ref GENERATOR: BigUint = BigUint::from_str(
"39033254847818212395286706435128746857159659164139250548781411570340225835782",
)
.unwrap();
pub static ref TWO: BigUint = 2u32.to_biguint().unwrap();
}
Loading

0 comments on commit c11da6c

Please sign in to comment.