Skip to content

Commit

Permalink
feat: Completion of core functionality and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Jul 26, 2023
1 parent 13ea5d6 commit fc25554
Show file tree
Hide file tree
Showing 9 changed files with 1,083 additions and 504 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./crates/melo-erasure-coding/Cargo.toml"
]
}
46 changes: 27 additions & 19 deletions crates/melo-erasure-coding/src/erasure_coding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#![cfg_attr(not(feature = "std"), no_std)]
use kzg::{FFTFr, Fr, PolyRecover, DAS, FFTG1, G1};
use melo_core_primitives::kzg::Blob;
use melo_core_primitives::kzg::BlsScalar;
use melo_core_primitives::kzg::Polynomial;
use melo_core_primitives::kzg::ReprConvert;
Expand All @@ -24,12 +23,6 @@ use rust_kzg_blst::{
utils::reverse_bit_order,
};

pub fn blob_to_poly(fs: &FsFFTSettings, blob: &Blob) -> Result<Polynomial, String> {
// self.new_fft_settings(data.len())?;
let poly = FsPoly { coeffs: fs.fft_fr(&blob.0, true)? };
Ok(Polynomial::from(poly))
}

pub fn extend(fs: &FsFFTSettings, source: &[BlsScalar]) -> Result<Vec<BlsScalar>, String> {
fs.das_fft_extension(BlsScalar::slice_to_repr(source))
.map(BlsScalar::vec_from_repr)
Expand All @@ -53,24 +46,39 @@ pub fn extend_fs_g1<T: ReprConvert<FsG1>>(
}

pub fn recover(fs: &FsFFTSettings, shards: &[Option<BlsScalar>]) -> Result<Vec<BlsScalar>, String> {
let poly = FsPoly::recover_poly_from_samples(BlsScalar::slice_option_to_repr(shards), &fs)?;

Ok(BlsScalar::vec_from_repr(poly.coeffs))
match shards.contains(&None) {
true => {
let poly = FsPoly::recover_poly_from_samples(BlsScalar::slice_option_to_repr(shards), &fs)?;
Ok(BlsScalar::vec_from_repr(poly.coeffs))
},
false => {
// shards 中不包含 None ,它是安全的
Ok(shards.iter().map(|s| s.unwrap()).collect::<Vec<BlsScalar>>())
},
}
}

pub fn recover_poly(
fs: &FsFFTSettings,
shards: &[Option<BlsScalar>],
) -> Result<Polynomial, String> {
// let poly = FsPoly::recover_poly_coeffs_from_samples(BlsScalar::slice_option_to_repr(shards), &fs)?;
// Ok(Polynomial::from(poly))
// 检查 shards 前半部分是否没有 none ,如果没有,则使用快速傅里叶直接计算
// 同时也是防止整个shares 没有 none 的情况下,引起错误

let mut poly = match shards.contains(&None) {
true => {
let poly = FsPoly::recover_poly_coeffs_from_samples(BlsScalar::slice_option_to_repr(shards), &fs)?;
Polynomial::from(poly)
},
false => {
// shards 中不包含 None ,它是安全的
let data = shards.iter().map(|s| s.unwrap()).collect::<Vec<BlsScalar>>();
let coeffs = fs.fft_fr(BlsScalar::slice_to_repr(&data), true).expect("");
Polynomial::from_coeffs(&coeffs)
},
};

let mut poly = Polynomial::from(FsPoly::recover_poly_coeffs_from_samples(
BlsScalar::slice_option_to_repr(shards),
&fs,
)?);

poly.normalize();
poly.left();

Ok(poly)
}
}
41 changes: 17 additions & 24 deletions crates/melo-erasure-coding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@

#![cfg_attr(not(feature = "std"), no_std)]

use erasure_coding::blob_to_poly;
use itertools::Itertools;
use melo_core_primitives::{
config,
kzg::{embedded_kzg_settings, Blob, KZG},
segment::Segment,
};
use segment::poly_to_segment_vec;
use melo_core_primitives::{blob::Blob, kzg::SCALAR_SAFE_BYTES};

#[cfg(test)]
mod tests;
Expand All @@ -33,32 +27,31 @@ pub mod extend_col;
pub mod recovery;
pub mod segment;

pub fn bytes_vec_to_blobs(bytes_vec: &Vec<Vec<u8>>) -> Result<Vec<Blob>, String> {
pub fn bytes_vec_to_blobs(
bytes_vec: &Vec<Vec<u8>>,
field_elements_per_blob: usize,
) -> Result<Vec<Blob>, String> {
if bytes_vec.iter().any(|bytes| bytes.is_empty()) {
return Err("bytes_vec should not contain empty bytes; qed".to_string());
}
if !field_elements_per_blob.is_power_of_two() {
return Err("field_elements_per_blob should be a power of 2; qed".to_string());
}
if field_elements_per_blob == 0 {
return Err("field_elements_per_blob should be greater than 0; qed".to_string());
}
let bytes_per_blob = SCALAR_SAFE_BYTES * field_elements_per_blob;
let blobs = bytes_vec
.iter()
.flat_map(|bytes| {
bytes
.chunks(config::BYTES_PER_BLOB)
.chunks(bytes_per_blob)
.map(|chunk| {
Blob::try_from_bytes_pad(chunk)
Blob::try_from_bytes_pad(chunk, bytes_per_blob)
.expect("Failed to convert bytes to Blob; qed")
})
.collect_vec()
})
.collect_vec();
Ok(blobs)
}

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,chunk_size)
.expect("Failed to convert poly to segment vector; qed")
})
.collect_vec();
Ok(matrix)
}
3 changes: 1 addition & 2 deletions crates/melo-erasure-coding/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ pub fn recovery_row_from_segments(
let poly = recover_poly(kzg.get_fs(), &row)?;

let recovery_row = extend_poly(kzg.get_fs(), &poly)?;
// let mut recovery_row = recover(kzg.get_fs(), &row)?;
// reverse_bit_order(&mut recovery_row);

order_segments
.iter()
.enumerate()
Expand Down
Loading

0 comments on commit fc25554

Please sign in to comment.