Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
471 changes: 426 additions & 45 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[workspace]
resolver = "2"
members = ["core", "kate/recovery", "kate"]
members = ["core", "kate/recovery", "kate", "fri"]

[profile.release]
lto = "thin"

[workspace.dependencies]
# Parity
Expand Down Expand Up @@ -50,6 +53,13 @@ sha3 = { version = "0.10.0", default-features = false }
poly-multiproof = { git = "https://github.com/availproject/poly-multiproof", rev="c2794e32ca040e6b2544abde18b7187af3e66feb", default-features = false, features = ["ark-bls12-381", "blst"]}
hash-db = { version = "0.16.0", default-features = false }

# fri
binius-prover = { git = "https://github.com/binius-zk/binius64.git", rev = "41cda4a3eeb3fcb57bcd324e20a0ffe0b653f896" }
binius-verifier = { git = "https://github.com/binius-zk/binius64.git", rev = "41cda4a3eeb3fcb57bcd324e20a0ffe0b653f896" }
binius-math = { git = "https://github.com/binius-zk/binius64.git", rev = "41cda4a3eeb3fcb57bcd324e20a0ffe0b653f896" }
binius-field = { git = "https://github.com/binius-zk/binius64.git", rev = "41cda4a3eeb3fcb57bcd324e20a0ffe0b653f896" }
binius-transcript = { git = "https://github.com/binius-zk/binius64.git", rev = "41cda4a3eeb3fcb57bcd324e20a0ffe0b653f896" }

# Others
rayon = "1.5.2"
once_cell = "1.8.0"
Expand Down
11 changes: 11 additions & 0 deletions build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ cargo check --no-default-features --features "std"
cargo check --no-default-features --features "std, serde"
cargo check --target wasm32-unknown-unknown --no-default-features
cargo check --target wasm32-unknown-unknown --no-default-features --features "serde"


# Fri
cd ../fri
cargo check
cargo check --no-default-features
cargo check --no-default-features --features "serde"
cargo check --no-default-features --features "std"
cargo check --no-default-features --features "std, serde"
cargo check --target wasm32-unknown-unknown --no-default-features
cargo check --target wasm32-unknown-unknown --no-default-features --features "serde"
1 change: 0 additions & 1 deletion core/src/asdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ mod tests {
}
}


type Ex = AppUncheckedExtrinsic<TestAccountId, TestCall, TestSig, TestExtra>;
type CEx = CheckedExtrinsic<TestAccountId, TestCall, TestExtra>;

Expand Down
41 changes: 41 additions & 0 deletions core/src/fri_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use codec::{Decode, Encode};
use scale_info::TypeInfo;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "runtime")]
use {sp_debug_derive::RuntimeDebug, sp_runtime_interface::pass_by::PassByCodec};
/// Parameters that Avail config / node code will set.
#[derive(Clone, Copy, Debug)]
pub struct FriParamsConfig {
/// log2(1 / Reed–Solomon code rate).
pub log_inv_rate: usize,
/// Number of FRI test queries (soundness parameter).
pub num_test_queries: usize,
/// log2(number of “shares” / repetitions).
pub log_num_shares: usize,
/// Number of multilinear variables (depends on data size).
pub n_vars: usize,
}

/// Version of Fri/Binius parameters used to interpret size_bytes into
/// codeword length and sampling domain.
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Default, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(PassByCodec, RuntimeDebug))]
pub struct FriParamsVersion(pub u8);

impl FriParamsVersion {
/// Map this version to a FriParamsConfig, given `n_vars`
pub fn to_config(self, n_vars: usize) -> FriParamsConfig {
match self.0 {
0 => FriParamsConfig {
log_inv_rate: 1,
num_test_queries: 128,
log_num_shares: 80,
n_vars,
},
_ => panic!("Unsupported FriParamsVersion {}", self.0),
}
}
}
60 changes: 60 additions & 0 deletions core/src/header/extension/fri_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::FriParamsVersion;
use codec::{Decode, Encode};
use primitive_types::H256;
use scale_info::TypeInfo;
use sp_std::vec::Vec;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "runtime")]
use sp_debug_derive::RuntimeDebug;

/// Metadata needed for DA sampling + PCS verification of one blob.
#[derive(Clone, PartialEq, Eq, Encode, Decode, Default, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(RuntimeDebug))]
pub struct FriBlobCommitment {
/// Original blob size in bytes.
pub size_bytes: u64,

/// Fri PCS commitment (Merkle root of the blob codeword).
pub commitment: Vec<u8>,
}

/// DA commitment extension — input to LC sampling & verification.
/// Replaces KZG’s KateCommitment format.
#[derive(Clone, PartialEq, Eq, Encode, Decode, Default, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "runtime", derive(RuntimeDebug))]
pub struct HeaderExtension {
/// All blob commitments in canonical block order.
pub blobs: Vec<FriBlobCommitment>,

/// Dataroot to be used for bridge & blob inclusion proofs
pub data_root: H256,

/// Parameter set identifier to decode sampling domain / FRI params.
pub params_version: FriParamsVersion,
}

impl HeaderExtension {
pub fn data_root(&self) -> H256 {
self.data_root
}

pub fn get_empty_header(data_root: H256) -> Self {
HeaderExtension {
data_root,
..Default::default()
}
}

pub fn get_faulty_header(data_root: H256) -> Self {
// TODO: Differentiate b/w empty & faulty_header
HeaderExtension {
data_root,
..Default::default()
}
}
}
200 changes: 140 additions & 60 deletions core/src/header/extension/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::{DataLookup, HeaderVersion};
use codec::{Decode, Encode};
use primitive_types::H256;
use scale_info::TypeInfo;
Expand All @@ -8,95 +7,176 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "runtime")]
use {sp_debug_derive::RuntimeDebug, sp_runtime_interface::pass_by::PassByCodec};

pub mod v3;
pub mod fri_v1;
// basically only supported kzg header currently
pub mod v4;

/// Header extension data.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(PassByCodec, RuntimeDebug))]
#[repr(u8)]
pub enum HeaderExtension {
V3(v3::HeaderExtension) = 2,
V4(v4::HeaderExtension) = 3,
}
pub mod kzg {
use super::*;

/// Versioning for KZG header formats.
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(PassByCodec, RuntimeDebug))]
pub enum KzgHeaderVersion {
V4,
}

/// It forwards the call to the inner version of the header. Any invalid version will return the
/// default value or execute an empty block.
macro_rules! forward_to_version {
($self:ident, $function:ident) => {{
match $self {
HeaderExtension::V3(ext) => ext.$function(),
HeaderExtension::V4(ext) => ext.$function(),
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(RuntimeDebug))]
#[cfg_attr(not(feature = "runtime"), derive(Debug))]
pub enum KzgHeader {
V4(v4::HeaderExtension),
}

impl KzgHeader {
pub fn data_root(&self) -> H256 {
match self {
KzgHeader::V4(ext) => ext.data_root(),
}
}
}};

($self:ident, $function:ident, $arg:expr) => {{
match $self {
HeaderExtension::V3(ext) => ext.$function($arg),
HeaderExtension::V4(ext) => ext.$function($arg),
pub fn version(&self) -> KzgHeaderVersion {
match self {
KzgHeader::V4(_) => KzgHeaderVersion::V4,
}
}
}};
}

impl HeaderExtension {
pub fn data_root(&self) -> H256 {
forward_to_version!(self, data_root)
pub fn get_empty_header(data_root: H256, version: KzgHeaderVersion) -> Self {
match version {
KzgHeaderVersion::V4 => v4::HeaderExtension::get_empty_header(data_root).into(),
}
}

pub fn get_faulty_header(data_root: H256, version: KzgHeaderVersion) -> Self {
match version {
KzgHeaderVersion::V4 => v4::HeaderExtension::get_faulty_header(data_root).into(),
}
}
}

pub fn app_lookup(&self) -> DataLookup {
match self {
HeaderExtension::V3(ext) => DataLookup::from(&ext.app_lookup),
HeaderExtension::V4(ext) => ext.app_lookup.clone(),
impl From<v4::HeaderExtension> for KzgHeader {
#[inline]
fn from(ext: v4::HeaderExtension) -> Self {
KzgHeader::V4(ext)
}
}
}

pub mod fri {
use super::*;

pub fn rows(&self) -> u16 {
forward_to_version!(self, rows)
/// Versioning for Fri/Binius header formats.
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(PassByCodec, RuntimeDebug))]
pub enum FriHeaderVersion {
V1,
}

pub fn cols(&self) -> u16 {
forward_to_version!(self, cols)
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(RuntimeDebug))]
#[cfg_attr(not(feature = "runtime"), derive(Debug))]
pub enum FriHeader {
V1(fri_v1::HeaderExtension),
}

pub fn get_empty_header(data_root: H256, version: HeaderVersion) -> HeaderExtension {
match version {
HeaderVersion::V3 => v3::HeaderExtension::get_empty_header(data_root).into(),
HeaderVersion::V4 => v4::HeaderExtension::get_empty_header(data_root).into(),
impl FriHeader {
pub fn data_root(&self) -> H256 {
match self {
FriHeader::V1(ext) => ext.data_root(),
}
}

pub fn version(&self) -> FriHeaderVersion {
match self {
FriHeader::V1(_) => FriHeaderVersion::V1,
}
}

pub fn get_empty_header(data_root: H256, version: FriHeaderVersion) -> Self {
match version {
FriHeaderVersion::V1 => fri_v1::HeaderExtension::get_empty_header(data_root).into(),
}
}

pub fn get_faulty_header(data_root: H256, version: FriHeaderVersion) -> Self {
match version {
FriHeaderVersion::V1 => {
fri_v1::HeaderExtension::get_faulty_header(data_root).into()
},
}
}
}

pub fn get_faulty_header(data_root: H256, version: HeaderVersion) -> HeaderExtension {
match version {
HeaderVersion::V3 => v3::HeaderExtension::get_faulty_header(data_root).into(),
HeaderVersion::V4 => v4::HeaderExtension::get_faulty_header(data_root).into(),
impl From<fri_v1::HeaderExtension> for FriHeader {
#[inline]
fn from(ext: fri_v1::HeaderExtension) -> Self {
FriHeader::V1(ext)
}
}
}

#[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)]
pub enum CommitmentScheme {
Kzg,
Fri,
}

pub fn get_header_version(&self) -> HeaderVersion {
/// header extension: *which PCS + which version inside*.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "runtime", derive(PassByCodec, RuntimeDebug))]
#[cfg_attr(not(feature = "runtime"), derive(Debug))]
pub enum HeaderExtension {
Kzg(kzg::KzgHeader),
Fri(fri::FriHeader),
}

impl HeaderExtension {
pub fn data_root(&self) -> H256 {
match self {
HeaderExtension::V3(_) => HeaderVersion::V3,
HeaderExtension::V4(_) => HeaderVersion::V4,
HeaderExtension::Kzg(h) => h.data_root(),
HeaderExtension::Fri(h) => h.data_root(),
}
}
}

impl Default for HeaderExtension {
fn default() -> Self {
v3::HeaderExtension::default().into()
pub fn is_kzg(&self) -> bool {
matches!(self, HeaderExtension::Kzg(_))
}

pub fn is_fri(&self) -> bool {
matches!(self, HeaderExtension::Fri(_))
}

pub fn commitment_scheme(&self) -> CommitmentScheme {
match self {
HeaderExtension::Fri(_) => CommitmentScheme::Fri,
HeaderExtension::Kzg(_) => CommitmentScheme::Kzg,
}
}

pub fn get_empty_kzg(data_root: H256, version: kzg::KzgHeaderVersion) -> Self {
HeaderExtension::Kzg(kzg::KzgHeader::get_empty_header(data_root, version))
}
}

impl From<v3::HeaderExtension> for HeaderExtension {
#[inline]
fn from(ext: v3::HeaderExtension) -> Self {
Self::V3(ext)
pub fn get_empty_fri(data_root: H256, version: fri::FriHeaderVersion) -> Self {
HeaderExtension::Fri(fri::FriHeader::get_empty_header(data_root, version))
}

pub fn get_faulty_kzg(data_root: H256, version: kzg::KzgHeaderVersion) -> Self {
HeaderExtension::Kzg(kzg::KzgHeader::get_faulty_header(data_root, version))
}

pub fn get_faulty_fri(data_root: H256, version: fri::FriHeaderVersion) -> Self {
HeaderExtension::Fri(fri::FriHeader::get_faulty_header(data_root, version))
}
}

impl From<v4::HeaderExtension> for HeaderExtension {
#[inline]
fn from(ext: v4::HeaderExtension) -> Self {
Self::V4(ext)
impl Default for HeaderExtension {
fn default() -> Self {
HeaderExtension::Fri(fri::FriHeader::V1(fri_v1::HeaderExtension::default()))
}
}
Loading