Skip to content

Commit

Permalink
fix: Fix bugs caused by chunk_size
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Jul 23, 2023
1 parent fe909c0 commit f9d1669
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/melo-erasure-coding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ pub fn bytes_vec_to_blobs(bytes_vec: &Vec<Vec<u8>>) -> Result<Vec<Blob>, String>
Ok(blobs)
}

pub fn blobs_to_segments(blobs: &Vec<Blob>) -> Result<Vec<Vec<Segment>>, String> {
pub fn blobs_to_segments(blobs: &Vec<Blob>, chunk_size: usize) -> Result<Vec<Vec<Segment>>, String> {
let kzg = KZG::new(embedded_kzg_settings());
let matrix = blobs
.iter()
.enumerate()
.map(|(y, blob)| {
let poly = blob_to_poly(kzg.get_fs(),blob).expect("Failed to convert blob to poly; qed");
poly_to_segment_vec(&poly, &kzg, y)
poly_to_segment_vec(&poly, &kzg, y,chunk_size)
.expect("Failed to convert poly to segment vector; qed")
})
.collect_vec();
Expand Down
4 changes: 2 additions & 2 deletions crates/melo-erasure-coding/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use melo_core_primitives::segment::{Segment, SegmentData};
pub fn recovery_row_from_segments(
segments: &Vec<Segment>,
kzg: &KZG,
segments_size: usize,
) -> Result<Vec<Segment>, String> {
let y = segments[0].position.y;
let segments_size = segments[0].size();
let order_segments = order_segments_row(&segments)?;
let row = segment_datas_to_row(&order_segments);
let row = segment_datas_to_row(&order_segments, segments_size);
let poly = recover_poly(kzg.get_fs(), &row)?;
let recovery_row = poly.to_bls_scalars();
order_segments
Expand Down
12 changes: 6 additions & 6 deletions crates/melo-erasure-coding/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

extern crate alloc;
use kzg::FK20MultiSettings;
use melo_core_primitives::config::{FIELD_ELEMENTS_PER_BLOB, SEGMENT_LENGTH};
use melo_core_primitives::config::FIELD_ELEMENTS_PER_BLOB;
use melo_core_primitives::kzg::{BlsScalar, KZGProof, Polynomial, Position, ReprConvert, KZG};
use melo_core_primitives::segment::{Segment, SegmentData};
use rust_kzg_blst::types::fk20_multi_settings::FsFK20MultiSettings;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn order_segments_col(
Ok(ordered_segments)
}

pub fn segment_datas_to_row(segments: &Vec<Option<SegmentData>>) -> Vec<Option<BlsScalar>> {
pub fn segment_datas_to_row(segments: &Vec<Option<SegmentData>>, chunk_size: usize) -> Vec<Option<BlsScalar>> {
segments
.iter()
.flat_map(|segment_data_option| match segment_data_option {
Expand All @@ -63,7 +63,7 @@ pub fn segment_datas_to_row(segments: &Vec<Option<SegmentData>>) -> Vec<Option<B
.iter()
.map(|scalar| Some(*scalar))
.collect::<Vec<Option<BlsScalar>>>(),
None => vec![None; SEGMENT_LENGTH],
None => vec![None; chunk_size],
})
.collect::<Vec<Option<BlsScalar>>>()
}
Expand All @@ -78,13 +78,13 @@ pub fn segment_datas_to_row(segments: &Vec<Option<SegmentData>>) -> Vec<Option<B
/// # Returns
///
/// A `Result` containing a vector of `Segment` structs or an error message.
pub fn poly_to_segment_vec(poly: &Polynomial, kzg: &KZG, y: usize) -> Result<Vec<Segment>, String> {
pub fn poly_to_segment_vec(poly: &Polynomial, kzg: &KZG, y: usize, chunk_size: usize) -> Result<Vec<Segment>, String> {
let poly_len = poly.0.coeffs.len();
let fk = FsFK20MultiSettings::new(&kzg.ks, 2 * poly_len, SEGMENT_LENGTH).unwrap();
let fk = FsFK20MultiSettings::new(&kzg.ks, 2 * poly_len, chunk_size).unwrap();
let all_proofs = fk.data_availability(&poly.0).unwrap();

let segments = extend_poly(&fk.kzg_settings.fs, &poly)?
.chunks(SEGMENT_LENGTH)
.chunks(chunk_size)
.enumerate()
.map(|(i, chunk)| {
let position = Position { y: y as u32, x: i as u32 };
Expand Down
6 changes: 3 additions & 3 deletions crates/melo-erasure-coding/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn commit_multi_random() {
// Commit to the polynomial
let commitment = kzg.commit(&poly).unwrap();
// Compute the multi proofs
let proofs = kzg.all_proofs(&poly).unwrap();
let proofs = kzg.all_proofs(&poly,chunk_len).unwrap();

let mut extended_coeffs = poly.0.coeffs.clone();

Expand Down Expand Up @@ -134,7 +134,7 @@ fn extend_and_commit_multi_random() {
// Commit to the polynomial
let commitment = kzg.commit(&poly).unwrap();
// Compute the multi proofs
let proofs = kzg.all_proofs(&poly).unwrap();
let proofs = kzg.all_proofs(&poly, chunk_len).unwrap();

reverse_bit_order(&mut data);
let mut ys = vec![FsFr::default(); chunk_len];
Expand Down Expand Up @@ -182,7 +182,7 @@ fn extend_segments_col_random() {
let matrix = polys
.iter()
.enumerate()
.map(|(i, poly)| poly_to_segment_vec(&poly, &kzg, i).unwrap())
.map(|(i, poly)| poly_to_segment_vec(&poly, &kzg, i, chunk_len).unwrap())
.collect::<Vec<_>>();
assert!(matrix[0][0].verify(&kzg, &commitments[0], chunk_count).unwrap());
// Pick a column from the segments
Expand Down
1 change: 0 additions & 1 deletion primitives/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
pub const BYTES_PER_BLOB: usize = BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;
pub const BYTES_PER_FIELD_ELEMENT: usize = 32;
pub const FIELD_ELEMENTS_PER_BLOB: usize = 4;
pub const SEGMENT_LENGTH: usize = 16;
pub const EMBEDDED_KZG_SETTINGS_BYTES: &[u8] = include_bytes!("../../scripts/eth-public-parameters.bin");
21 changes: 12 additions & 9 deletions primitives/src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ use core::hash::{Hash, Hasher};
use core::mem;
use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Into};
use kzg::eip_4844::{BYTES_PER_G1, BYTES_PER_G2};
use kzg::{
FFTSettings, FK20MultiSettings, Fr, KZGSettings, Poly, G1, G2,
};
use kzg::{FFTSettings, FK20MultiSettings, Fr, KZGSettings, Poly, G1, G2};
use parity_scale_codec::{Decode, Encode, EncodeLike, Input, MaxEncodedLen};

use rust_kzg_blst::{
eip_4844::{
blob_to_kzg_commitment_rust, compute_blob_kzg_proof_rust,
verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust,
blob_to_kzg_commitment_rust, compute_blob_kzg_proof_rust, verify_blob_kzg_proof_batch_rust,
verify_blob_kzg_proof_rust,
},
types::{
fft_settings::FsFFTSettings, fk20_multi_settings::FsFK20MultiSettings, fr::FsFr, g1::FsG1,
Expand All @@ -39,7 +37,7 @@ use rust_kzg_blst::{
};
use scale_info::{Type, TypeInfo};

use crate::config::{EMBEDDED_KZG_SETTINGS_BYTES, SEGMENT_LENGTH};
use crate::config::EMBEDDED_KZG_SETTINGS_BYTES;

macro_rules! kzg_type_with_size {
($name:ident, $type:ty, $size:expr) => {
Expand Down Expand Up @@ -387,15 +385,20 @@ impl KZG {
self.ks.get_expanded_roots_of_unity_at(i)
}

pub fn get_kzg_index(&self, chunk_count: usize, chunk_index: usize, chunk_size: usize) -> usize {
pub fn get_kzg_index(
&self,
chunk_count: usize,
chunk_index: usize,
chunk_size: usize,
) -> usize {
let domain_stride = self.max_width() / (2 * chunk_size * chunk_count);
let domain_pos = Self::reverse_bits_limited(chunk_count, chunk_index);
domain_pos * domain_stride
}

pub fn all_proofs(&self, poly: &Polynomial) -> Result<Vec<KZGProof>, String> {
pub fn all_proofs(&self, poly: &Polynomial, chunk_size: usize) -> Result<Vec<KZGProof>, String> {
let poly_len = poly.0.coeffs.len();
let fk = FsFK20MultiSettings::new(&self.ks, 2 * poly_len, SEGMENT_LENGTH).unwrap();
let fk = FsFK20MultiSettings::new(&self.ks, 2 * poly_len, chunk_size).unwrap();
let all_proofs = fk.data_availability(&poly.0).unwrap();
Ok(KZGProof::vec_from_repr(all_proofs))
}
Expand Down

0 comments on commit f9d1669

Please sign in to comment.