diff --git a/.idea/dynamic-data-experiments.iml b/.idea/dynamic-data-experiments.iml index cf84ae4..abc053b 100644 --- a/.idea/dynamic-data-experiments.iml +++ b/.idea/dynamic-data-experiments.iml @@ -2,6 +2,7 @@ + diff --git a/Cargo.lock b/Cargo.lock index 672824f..86eaf1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,12 +25,27 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstream" version = "0.6.19" @@ -352,12 +367,24 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.2.27" @@ -373,6 +400,33 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "clap" version = "4.5.40" @@ -428,6 +482,42 @@ dependencies = [ "libc", ] +[[package]] +name = "criterion" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +dependencies = [ + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "is-terminal", + "itertools 0.10.5", + "num-traits", + "once_cell", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools 0.10.5", +] + [[package]] name = "crossbeam-deque" version = "0.8.6" @@ -453,6 +543,12 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + [[package]] name = "crypto-common" version = "0.1.6" @@ -499,6 +595,7 @@ dependencies = [ "ark-poly-commit", "ark-std", "clap", + "criterion", "itertools 0.14.0", "rand 0.9.1", "reed-solomon-erasure", @@ -581,6 +678,16 @@ dependencies = [ "wasi 0.14.2+wasi-0.2.4", ] +[[package]] +name = "half" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "459196ed295495a68f7d7fe1d84f6c4b7ff0e21fe3017b2f283c6fac3ad803c9" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -614,6 +721,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + [[package]] name = "instant" version = "0.1.13" @@ -623,12 +736,32 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "is-terminal" +version = "0.4.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.13.0" @@ -647,6 +780,22 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "keccak" version = "0.1.5" @@ -678,6 +827,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + [[package]] name = "lru" version = "0.7.8" @@ -687,6 +842,12 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "memchr" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" + [[package]] name = "merlin" version = "3.0.0" @@ -740,6 +901,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + [[package]] name = "parking_lot" version = "0.11.2" @@ -777,6 +944,34 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -913,12 +1108,94 @@ dependencies = [ "spin", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "rustversion" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.103", +] + +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + [[package]] name = "sha2" version = "0.10.9" @@ -982,6 +1259,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tracing" version = "0.1.41" @@ -1041,6 +1328,16 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -1056,6 +1353,74 @@ dependencies = [ "wit-bindgen-rt", ] +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn 2.0.103", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.103", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1072,6 +1437,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 194691e..24c9ec5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,9 @@ ark-poly = "0.5.0" ark-poly-commit = "0.5.0" anyhow = "1.0.95" reed-solomon-erasure = { version = "6.0.0", features = [ "simd-accel" ] } -ark-crypto-primitives = "0.5.0" \ No newline at end of file +ark-crypto-primitives = "0.5.0" +criterion = "0.5.1" + +[[bench]] +name = "encoder" +harness = false \ No newline at end of file diff --git a/src/byte_data.rs b/src/byte_data.rs index 51bf904..5bd6acd 100644 --- a/src/byte_data.rs +++ b/src/byte_data.rs @@ -19,6 +19,30 @@ pub struct Data{ pub matrix: Vec>, } +impl Data{ + pub fn get_row(&self, idx: usize) -> &Vec{ + &self.matrix[idx] + } + + pub fn get_row_mut(&mut self, idx: usize) -> &mut Vec{ + &mut self.matrix[idx] + } + + pub fn get_col(&self, idx: usize) -> Vec<&T> { + self.matrix + .iter() + .map(|row| &row[idx]) + .collect() + } + + pub fn get_col_mut(&mut self, idx: usize) -> Vec<&mut T> { + self.matrix + .iter_mut() + .map(|row| &mut row[idx]) + .collect() + } +} + impl DataMatrix for Data { type Params = Params; diff --git a/src/encoder.rs b/src/encoder.rs index e86b32d..36dc838 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -1,8 +1,16 @@ use std::marker::PhantomData; use anyhow::{anyhow, Result}; +use ark_bls12_381::Bls12_381; +use ark_ec::pairing::Pairing; +use ark_ff::PrimeField; +use ark_poly::{DenseUVPolynomial, GeneralEvaluationDomain, Polynomial}; +use ark_poly::univariate::DensePolynomial; use reed_solomon_erasure::galois_8::ReedSolomon; use crate::byte_data::{Data, Params}; use crate::traits::Encoder; +use ark_poly::domain::EvaluationDomain; +use crate::field_matrix::Matrix; +// ------------- RS Encoder ------------ pub struct RSEncoder{ phantom_data: PhantomData @@ -95,3 +103,92 @@ impl Encoder for RSEncoder { Ok(()) } } + +// ---------------- Bls12_381 Encoder ----------------- + +pub type E = Bls12_381; +pub type F = ::ScalarField; +pub type UniPoly381 = DensePolynomial; + +pub struct BLSEncoder{ + phantom_data: PhantomData +} + +impl BLSEncoder{ + pub fn new() -> Self{ + Self{ + phantom_data: PhantomData::default() + } + } +} + +impl Encoder for BLSEncoder { + type Params = Params; + type DataMatrix = Data; + + fn encode(data: &mut Self::DataMatrix) -> Result<()> { + for i in 0..data.params.m { + let _col = Self::encode_col(data, i)?; + } + Ok(()) + } + + fn encode_col(data: &mut Self::DataMatrix, c: usize) -> Result> { + let n = data.params.n.clone(); + let k = data.params.k.clone(); + let mut col = data.get_col_mut(c); + let col_f: Vec = col.iter().map(|i| ::from_le_bytes_mod_order(&i.clone().to_le_bytes())).collect(); + let poly_poly = UniPoly381::from_coefficients_slice(&col_f); + let domain: GeneralEvaluationDomain = EvaluationDomain::::new(n).unwrap(); + + let mut new_col: Vec = vec![]; + for i in k..n{ + let eval = poly_poly.evaluate(&domain.element(i)); + new_col.push(eval.0.0[0] as u8); + *col[i] = eval.0.0[0] as u8; + } + + Ok(new_col) + } + + fn reconstruct(_params: Params, _matrix_opts: &mut Vec>>) -> Result<()> { + todo!() + } +} + +// --------- BLS Encoder over FieldMatrix ---------------- + +impl Encoder for BLSEncoder { + type Params = Params; + type DataMatrix = Matrix; + + fn encode(data: &mut Matrix) -> Result<()> { + for i in 0..data.params.m { + let _col = Self::encode_col(data, i)?; + } + Ok(()) + } + + fn encode_col(data: &mut Matrix, c: usize) -> Result> { + // let n = data.params.n.clone(); + // let k = data.params.k.clone(); + // let mut col = data.get_col_mut(c); + // let col_f: Vec = col.iter().map(|i| ::from_le_bytes_mod_order(&i.clone().to_le_bytes())).collect(); + // let poly_poly = UniPoly381::from_coefficients_slice(&col_f); + // let domain: GeneralEvaluationDomain = EvaluationDomain::::new(n).unwrap(); + // + // let mut new_col: Vec = vec![]; + // for i in k..n{ + // let eval = poly_poly.evaluate(&domain.element(i)); + // new_col.push(eval.0.0[0] as u8); + // *col[i] = eval.0.0[0] as u8; + // } + // + // Ok(new_col) + todo!() + } + + fn reconstruct(_params: Params, _matrix_opts: &mut Vec>>) -> Result<()> { + todo!() + } +} \ No newline at end of file diff --git a/src/field_matrix.rs b/src/field_matrix.rs index 2ae8cde..5cb0d45 100644 --- a/src/field_matrix.rs +++ b/src/field_matrix.rs @@ -1,32 +1,33 @@ use ark_ff::Field; use ark_std::rand::Rng; use ark_std::UniformRand; -use crate::byte_data::Data; +use crate::byte_data::{Data, Params}; /// a Field matrix with `row` number of rows and `cols` number of columns -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug)] pub struct Matrix { - pub rows: usize, - pub cols: usize, - pub data: Vec>, + pub params: Params, + pub elms: Vec>, } impl Matrix { /// Creates a new matrix from given field data. - pub fn new(rows: usize, cols: usize, data: Vec>) -> Self { - assert!(data.len() == rows, "number of rows must match"); - for row in &data { - assert!(row.len() == cols, "each row must have `cols` elements"); + pub fn new(params: Params, elms: Vec>) -> Self { + assert!(elms.len() == params.n, "number of rows must match"); + for row in &elms { + assert!(row.len() == params.m, "each row must have `m` elements"); } - Matrix { rows, cols, data } + Matrix { params, elms } } /// Generates a random matrix with given dimensions, uses given rng for randomness. - pub fn new_random(rows: usize, cols: usize, rng: &mut R) -> Self + pub fn new_random(params: Params, rng: &mut R) -> Self where F: UniformRand, { + let rows = params.n; + let cols = params.m; let mut data = Vec::with_capacity(rows); for _ in 0..rows { let mut row = Vec::with_capacity(cols); @@ -35,7 +36,7 @@ impl Matrix { } data.push(row); } - Matrix { rows, cols, data } + Matrix { params, elms: data } } /// Creates a new matrix from given data struct @@ -51,24 +52,38 @@ impl Matrix { } field_data.push(row); } - Matrix { rows, cols, data:field_data } + Matrix { params: data.params.clone(), elms:field_data } } /// get the row at 0 Vec{ - assert!(idx < self.rows, "Row index out of bounds"); - self.data[idx].to_vec() + assert!(idx < self.params.n, "Row index out of bounds"); + self.elms[idx].to_vec() } /// get mut the row at 0 &mut Vec{ - assert!(idx < self.rows, "Row index out of bounds"); - &mut self.data[idx] + assert!(idx < self.params.n, "Row index out of bounds"); + &mut self.elms[idx] + } + + pub fn get_col(&self, idx: usize) -> Vec<&F> { + self.elms + .iter() + .map(|row| &row[idx]) + .collect() + } + + pub fn get_col_mut(&mut self, idx: usize) -> Vec<&mut F> { + self.elms + .iter_mut() + .map(|row| &mut row[idx]) + .collect() } /// Print matrix pub fn pretty_print(&self) { - for (i, shard) in self.data.iter().enumerate() { + for (i, shard) in self.elms.iter().enumerate() { print!("row {:>2}: ", i); for &b in shard { print!("{:>3} ", b); diff --git a/src/kzg.rs b/src/kzg.rs index 3a0df25..3d43fa1 100644 --- a/src/kzg.rs +++ b/src/kzg.rs @@ -66,7 +66,7 @@ impl PolynomialCommitmentScheme for KZGPolyComm { let (ck, _vk) = PCS::trim(&srs.pp, degree, degree, Some(&[degree]))?; let mut row_polynomials = vec![]; let timer = start_timer!(|| format!("Poly evaluations and interpolation for {} rows", degree)); - for i in 0..matrix.rows{ + for i in 0..matrix.params.n{ let poly_evals = Evaluations::from_vec_and_domain(matrix.row(i), srs.ploycommit_domain.clone()); let row_poly = poly_evals.interpolate(); let label = String::from(format!("row_poly_{}", i)); diff --git a/src/test.rs b/src/test.rs index dd9e715..d130d30 100644 --- a/src/test.rs +++ b/src/test.rs @@ -2,11 +2,10 @@ mod tests { use crate::byte_data::{Data, Params}; use ark_poly::{EvaluationDomain}; - use reed_solomon_erasure::galois_8::ReedSolomon; use crate::kzg::{F, KZGPolyComm}; use crate::field_matrix::Matrix; use ark_poly_commit::{Polynomial}; - use crate::encoder::RSEncoder; + use crate::encoder::{BLSEncoder, RSEncoder}; use crate::traits::{DataMatrix, Encoder, PolynomialCommitmentScheme}; #[test] @@ -54,6 +53,51 @@ mod tests { } } + #[test] + fn test_bls_encodeer() { + // test parameters + let k = 4; + let p = 4; + let n = k + p; + let m = 8; + + // generate Data with random content + let params = Params { + k, + n, + m, + }; + let mut data = Data::new_random(params); + println!("data #row ={}", data.matrix.len()); + println!("data #col ={}", data.matrix[0].len()); + println!("data before encoding:"); + data.pretty_print(); + // original data matrix + let original: Vec> = data.matrix[..k].to_vec(); + + // encode + BLSEncoder::encode(&mut data).expect("encode failed"); + println!("data after encoding:"); + data.pretty_print(); + + // verify data matrix unchanged + assert_eq!(data.matrix[..k], original[..]); + + // simulate loss of one data and one parity rows + let mut matrix_opts: Vec<_> = data.matrix.iter().cloned().map(Some).collect(); + matrix_opts[1] = None; + matrix_opts[k] = None; + + // TODO: reconstruct missing rows + // BLSEncoder::reconstruct(data.params.clone(), &mut matrix_opts).expect("reconstruction should succeed"); + + // verify reconstruction for data shards + // for i in 0..k { + // let recovered = matrix_opts[i].clone().unwrap(); + // assert_eq!(recovered, &original[i][..]); + // } + } + #[test] fn test_commit_rows() { // dimensions: 8 rows (4 parity), 8 columns