Skip to content

Commit

Permalink
readability fixes and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Silur committed Jan 17, 2024
1 parent 933d999 commit 647c725
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/folding/protogalaxy/folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where

// F(X)
let F_X: SparsePolynomial<C::ScalarField> =
calc_f_from_btree(&f_w, &instance.betas, &deltas);
calc_f_from_btree(&f_w, &instance.betas, &deltas).expect("Error calculating F[x]");
let F_X_dense = DensePolynomial::from(F_X.clone());
transcript.absorb_vec(&F_X_dense.coeffs);

Expand Down Expand Up @@ -304,9 +304,29 @@ fn pow_i<F: PrimeField>(i: usize, betas: &Vec<F>) -> F {
r
}

fn calc_f_from_btree<F: PrimeField>(fw: &[F], betas: &[F], deltas: &[F]) -> SparsePolynomial<F> {
assert_eq!(fw.len() & (fw.len() - 1), 0);
assert_eq!(betas.len(), deltas.len());
/// calculates F[x] using the optimized binary-tree technique
/// described in Claim 4.4
/// of [Protogalaxy](https://eprint.iacr.org/2023/1106.pdf)
fn calc_f_from_btree<F: PrimeField>(
fw: &[F],
betas: &[F],
deltas: &[F],
) -> Result<SparsePolynomial<F>, Error> {
let fw_len = fw.len();
let betas_len = betas.len();
let deltas_len = deltas.len();

// ensure our binary tree is full
if !fw_len.is_power_of_two() {
return Err(Error::ProtoGalaxy(ProtoGalaxyError::BTreeNotFull(fw_len)));
}

if betas_len != deltas_len {
return Err(Error::ProtoGalaxy(ProtoGalaxyError::WrongLenBetas(
betas_len, deltas_len,
)));
}

let mut layers: Vec<Vec<SparsePolynomial<F>>> = Vec::new();
let leaves: Vec<SparsePolynomial<F>> = fw
.iter()
Expand All @@ -331,8 +351,8 @@ fn calc_f_from_btree<F: PrimeField>(fw: &[F], betas: &[F], deltas: &[F]) -> Spar
}
let left = ni.clone();
let right = SparsePolynomial::<F>::from_coefficients_vec(vec![
(0, betas[layers.len() - 2]), // FIXME
(1, deltas[layers.len() - 2]), // FIXME
(0, betas[layers.len() - 2]),
(1, deltas[layers.len() - 2]),
])
.mul(&currentNodes[i + 1]);

Expand All @@ -341,7 +361,7 @@ fn calc_f_from_btree<F: PrimeField>(fw: &[F], betas: &[F], deltas: &[F]) -> Spar
currentNodes = layers[index].clone();
}
let root_index = layers.len() - 1;
layers[root_index][0].clone()
Ok(layers[root_index][0].clone())
}

// lagrange_polys method from caulk: https://github.com/caulk-crypto/caulk/tree/8210b51fb8a9eef4335505d1695c44ddc7bf8170/src/multi/setup.rs#L300
Expand Down Expand Up @@ -417,7 +437,6 @@ mod tests {
}
}


#[test]
fn test_eval_f() {
let r1cs = get_test_r1cs::<Fr>();
Expand Down
4 changes: 4 additions & 0 deletions src/folding/protogalaxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ pub enum ProtoGalaxyError {
CouldNotDivideByVanishing,
#[error("The number of incoming instances + 1 should be a power of two, current number of instances: {0}")]
WrongNumInstances(usize),
#[error("The number of incoming items should be a power of two, current number of coefficients: {0}")]
BTreeNotFull(usize),
#[error("The lengths of β and δ do not equal: |β| = {0}, |δ|={0}")]
WrongLenBetas(usize, usize),
}

0 comments on commit 647c725

Please sign in to comment.