Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • dragoon/komodo
  • a.stevan/komodo
  • c.heme/komodo
3 results
Show changes
Commits on Source (3)
use std::cmp::max;
use std::ops::{Add, Mul};
use ark_ec::pairing::Pairing;
use ark_ff::PrimeField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{One, Zero};
use ark_std::Zero;
use crate::error::KomodoError;
use crate::field;
use crate::linalg::Matrix;
#[derive(Debug, Default, Clone, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
pub struct LinearCombinationElement<E: Pairing> {
pub index: u32,
pub weight: E::ScalarField,
}
#[derive(Debug, Default, Clone, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
pub struct Shard<E: Pairing> {
pub k: u32,
pub linear_combination: Vec<LinearCombinationElement<E>>,
pub linear_combination: Vec<E::ScalarField>,
pub hash: Vec<u8>,
pub bytes: Vec<u8>,
pub bytes: Vec<E::ScalarField>,
pub size: usize,
}
impl<E: Pairing> Shard<E> {
pub fn mul(&self, alpha: E::ScalarField) -> Self {
let bytes = if alpha.is_zero() {
vec![0u8; self.bytes.len()]
} else if alpha.is_one() {
self.bytes.to_vec()
} else {
let elements = field::split_data_into_field_elements::<E>(&self.bytes, 1, true)
.iter()
.map(|e| e.mul(alpha))
.collect::<Vec<_>>();
field::merge_elements_into_bytes::<E>(&elements, false)
};
Shard {
Self {
k: self.k,
linear_combination: self
.linear_combination
.iter()
.map(|l| LinearCombinationElement {
index: l.index,
weight: l.weight.mul(alpha),
})
.map(|e| e.mul(alpha))
.collect(),
hash: self.hash.clone(),
bytes,
bytes: self.bytes.iter().map(|e| e.mul(alpha)).collect(),
size: self.size,
}
}
......@@ -63,8 +41,8 @@ impl<E: Pairing> Shard<E> {
}
let elements = {
let elements_self = field::split_data_into_field_elements::<E>(&self.bytes, 1, true);
let elements_other = field::split_data_into_field_elements::<E>(&other.bytes, 1, true);
let elements_self = self.bytes.clone();
let elements_other = other.bytes.clone();
elements_self
.iter()
......@@ -73,25 +51,24 @@ impl<E: Pairing> Shard<E> {
.collect::<Vec<_>>()
};
let mut linear_combination = vec![];
for lce in &self.linear_combination {
linear_combination.push(LinearCombinationElement {
index: lce.index,
weight: lce.weight.mul(alpha),
});
let mut linear_combination = Vec::with_capacity(max(
self.linear_combination.len(),
other.linear_combination.len(),
));
linear_combination.fill(E::ScalarField::zero());
for (i, l) in self.linear_combination.iter().enumerate() {
linear_combination[i] = linear_combination[i] + l.mul(alpha);
}
for lce in &other.linear_combination {
linear_combination.push(LinearCombinationElement {
index: lce.index,
weight: lce.weight.mul(beta),
});
for (i, l) in other.linear_combination.iter().enumerate() {
linear_combination[i] = linear_combination[i] + l.mul(beta);
}
Shard {
k: self.k,
linear_combination,
hash: self.hash.clone(),
bytes: field::merge_elements_into_bytes::<E>(&elements, false),
bytes: elements,
size: self.size,
}
}
......@@ -107,19 +84,14 @@ pub fn decode<E: Pairing>(blocks: Vec<Shard<E>>, transpose: bool) -> Result<Vec<
let points: Vec<_> = blocks
.iter()
.take(k as usize)
.map(|b| {
E::ScalarField::from_le_bytes_mod_order(
// TODO: use the real linear combination
&(b.linear_combination[0].index as u64).to_le_bytes(),
)
})
.map(|b| b.linear_combination[0])
.collect();
let shards = Matrix::from_vec_vec(
blocks
.iter()
.take(k as usize)
.map(|b| field::split_data_into_field_elements::<E>(&b.bytes, 1, true))
.map(|b| b.bytes.clone())
.collect(),
)?
.transpose();
......@@ -141,12 +113,12 @@ mod tests {
use ark_bls12_381::Bls12_381;
use ark_ec::pairing::Pairing;
use ark_ff::PrimeField;
use ark_std::One;
use ark_std::{One, Zero};
use rs_merkle::algorithms::Sha256;
use rs_merkle::Hasher;
use crate::{
fec::{decode, LinearCombinationElement, Shard},
fec::{decode, Shard},
field,
linalg::Matrix,
};
......@@ -181,15 +153,11 @@ mod tests {
.transpose()
.elements
.chunks(source_shards.height)
.enumerate()
.map(|(i, s)| Shard {
.map(|s| Shard {
k: k as u32,
linear_combination: vec![LinearCombinationElement {
index: i as u32,
weight: E::ScalarField::one(),
}],
linear_combination: vec![E::ScalarField::one()],
hash: hash.clone(),
bytes: field::merge_elements_into_bytes::<E>(s, false),
bytes: s.to_vec(),
size: data.len(),
})
.collect();
......@@ -214,12 +182,13 @@ mod tests {
}
fn create_fake_shard<E: Pairing>(
linear_combination: &[LinearCombinationElement<E>],
linear_combination: &[E::ScalarField],
bytes: &[u8],
) -> Shard<E> {
let mut bytes = bytes.to_vec();
bytes.resize(32, 0);
//let mut bytes = bytes.to_vec();
//bytes.resize(32, 0);
let bytes =
field::split_data_into_field_elements::<E>(bytes, linear_combination.len(), false);
Shard {
k: 0,
linear_combination: linear_combination.to_vec(),
......@@ -230,30 +199,13 @@ mod tests {
}
fn recoding_template<E: Pairing>() {
let a: Shard<E> = create_fake_shard(
&[LinearCombinationElement {
index: 0,
weight: E::ScalarField::one(),
}],
&[1, 2, 3],
);
let b: Shard<E> = create_fake_shard(
&[LinearCombinationElement {
index: 1,
weight: E::ScalarField::one(),
}],
&[4, 5, 6],
);
let a: Shard<E> = create_fake_shard(&[E::ScalarField::one()], &[1, 2, 3]);
let b: Shard<E> =
create_fake_shard(&[E::ScalarField::zero(), E::ScalarField::one()], &[4, 5, 6]);
assert_eq!(
a.mul(E::ScalarField::from_le_bytes_mod_order(&[2])),
create_fake_shard(
&[LinearCombinationElement {
index: 0,
weight: E::ScalarField::from_le_bytes_mod_order(&[2]),
}],
&[2, 4, 6],
)
create_fake_shard(&[E::ScalarField::from_le_bytes_mod_order(&[2])], &[2, 4, 6],)
);
let c = a.combine(
......@@ -266,14 +218,8 @@ mod tests {
c,
create_fake_shard(
&[
LinearCombinationElement {
index: 0,
weight: E::ScalarField::from_le_bytes_mod_order(&[3]),
},
LinearCombinationElement {
index: 1,
weight: E::ScalarField::from_le_bytes_mod_order(&[5]),
}
E::ScalarField::from_le_bytes_mod_order(&[3]),
E::ScalarField::from_le_bytes_mod_order(&[5]),
],
&[23, 31, 39]
)
......@@ -287,18 +233,8 @@ mod tests {
),
create_fake_shard(
&[
LinearCombinationElement {
index: 0,
weight: E::ScalarField::from_le_bytes_mod_order(&[6]),
},
LinearCombinationElement {
index: 1,
weight: E::ScalarField::from_le_bytes_mod_order(&[10]),
},
LinearCombinationElement {
index: 0,
weight: E::ScalarField::from_le_bytes_mod_order(&[4]),
}
E::ScalarField::from_le_bytes_mod_order(&[10]),
E::ScalarField::from_le_bytes_mod_order(&[10]),
],
&[50, 70, 90],
)
......
......@@ -5,8 +5,7 @@ use ark_ff::{Field, PrimeField};
use ark_poly::DenseUVPolynomial;
use ark_poly_commit::kzg10::{Commitment, Powers, Randomness, KZG10};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::One;
use fec::LinearCombinationElement;
use ark_std::{One, Zero};
use rs_merkle::algorithms::Sha256;
use rs_merkle::Hasher;
use tracing::{debug, info};
......@@ -66,15 +65,16 @@ where
let mut proofs = Vec::new();
for (i, row) in evaluations.iter().enumerate() {
let mut linear_combination = Vec::with_capacity(i + 1);
linear_combination.fill(E::ScalarField::zero());
linear_combination[i] = E::ScalarField::one();
proofs.push(Block {
shard: fec::Shard {
k: k as u32,
linear_combination: vec![LinearCombinationElement {
index: i as u32,
weight: E::ScalarField::one(),
}],
linear_combination,
hash: hash.to_vec(),
bytes: field::merge_elements_into_bytes::<E>(row, false),
bytes: row.clone(),
size: nb_bytes,
},
commit: commits.clone(),
......@@ -151,7 +151,7 @@ where
P: DenseUVPolynomial<E::ScalarField, Point = E::ScalarField>,
for<'a, 'b> &'a P: Div<&'b P, Output = P>,
{
let elements = field::split_data_into_field_elements::<E>(&block.shard.bytes, 1, true);
let elements = block.shard.bytes.clone();
let polynomial = P::from_coefficients_vec(elements);
let (commit, _) = KZG10::<E, P>::commit(verifier_key, &polynomial, None, None)?;
......@@ -159,8 +159,9 @@ where
.shard
.linear_combination
.iter()
.map(|lce| {
let alpha = E::ScalarField::from_le_bytes_mod_order(&[lce.index as u8]);
.enumerate()
.map(|(i, w)| {
let alpha = E::ScalarField::from_le_bytes_mod_order(&[i as u8]);
let f: E::G1 = block
.commit
......@@ -171,7 +172,7 @@ where
commit.mul(alpha.pow([j as u64]))
})
.sum();
f * lce.weight
f * w
})
.sum();
Ok(Into::<E::G1>::into(commit.0) == rhs)
......