Skip to content

Commit

Permalink
refactor: define auxiliary functions for the batching of sumcheck cla…
Browse files Browse the repository at this point in the history
…ims (Arecibo backport) (argumentcomputer#273)

* `snark.rs`: factor out batch evaluation Sumcheck (argumentcomputer#106)

* Factor batch eval

* Comments

* refactor: Remove nedless pass-by-value instances, turn on clippy (argumentcomputer#122)

* refactor: Remove nedless pass-by-value instances, turn on the corresponding clippy lint

- Updated function parameters in `ppsnark.rs` and `snark.rs` from `Vec<G::Scalar>` to `&[G::Scalar]` (introduced in argumentcomputer#106),
- Modified the `prove` function in `ppsnark.rs` to also convert `T_row`, `W_row`, `T_col`, and `W_col` from `Vec` to slices (`&[G::Scalar]`).
- Enhanced the `xclippy` alias in `.cargo/config` by adding `-Wclippy::checked_conversions`, `-Wclippy::needless_pass_by_value`, and `-Wclippy::unnecessary_mut_passed` and reorganizing its elements.

* `PolyEval{Instance, Witness}::pad`
Accept `Vec` rather that `&[T]` to avoid copies

Co-authored-by: Francois Garillot <[email protected]>

---------

Co-authored-by: Adrian Hamelink <[email protected]>

---------

Co-authored-by: Adrian Hamelink <[email protected]>
Co-authored-by: Adrian Hamelink <[email protected]>
  • Loading branch information
3 people authored Nov 29, 2023
1 parent d52eb82 commit 9004d70
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 137 deletions.
23 changes: 10 additions & 13 deletions src/spartan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ pub struct PolyEvalWitness<E: Engine> {
}

impl<E: Engine> PolyEvalWitness<E> {
fn pad(W: &[PolyEvalWitness<E>]) -> Vec<PolyEvalWitness<E>> {
fn pad(mut W: Vec<PolyEvalWitness<E>>) -> Vec<PolyEvalWitness<E>> {
// determine the maximum size
if let Some(n) = W.iter().map(|w| w.p.len()).max() {
W.iter()
.map(|w| {
let mut p = vec![E::Scalar::ZERO; n];
p[..w.p.len()].copy_from_slice(&w.p);
PolyEvalWitness { p }
})
.collect()
W.iter_mut().for_each(|w| {
w.p.resize(n, E::Scalar::ZERO);
});
W
} else {
Vec::new()
}
Expand Down Expand Up @@ -94,14 +91,14 @@ pub struct PolyEvalInstance<E: Engine> {
}

impl<E: Engine> PolyEvalInstance<E> {
fn pad(U: &[PolyEvalInstance<E>]) -> Vec<PolyEvalInstance<E>> {
fn pad(U: Vec<PolyEvalInstance<E>>) -> Vec<PolyEvalInstance<E>> {
// determine the maximum size
if let Some(ell) = U.iter().map(|u| u.x.len()).max() {
U.iter()
.map(|u| {
U.into_iter()
.map(|mut u| {
let mut x = vec![E::Scalar::ZERO; ell - u.x.len()];
x.extend(u.x.clone());
PolyEvalInstance { c: u.c, x, e: u.e }
x.append(&mut u.x);
PolyEvalInstance { x, ..u }
})
.collect()
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/spartan/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ impl<E: Engine> MemorySumcheckInstance<E> {
pub fn new(
ck: &CommitmentKey<E>,
r: &E::Scalar,
T_row: Vec<E::Scalar>,
W_row: Vec<E::Scalar>,
T_row: &[E::Scalar],
W_row: &[E::Scalar],
ts_row: Vec<E::Scalar>,
T_col: Vec<E::Scalar>,
W_col: Vec<E::Scalar>,
T_col: &[E::Scalar],
W_col: &[E::Scalar],
ts_col: Vec<E::Scalar>,
transcript: &mut E::TE,
) -> Result<(Self, [Commitment<E>; 4], [Vec<E::Scalar>; 4]), NovaError> {
Expand Down Expand Up @@ -362,8 +362,8 @@ impl<E: Engine> MemorySumcheckInstance<E> {
((t_plus_r_inv_row, w_plus_r_inv_row), (t_plus_r_row, w_plus_r_row)),
((t_plus_r_inv_col, w_plus_r_inv_col), (t_plus_r_col, w_plus_r_col)),
) = rayon::join(
|| helper(&T_row, &W_row, &ts_row, r),
|| helper(&T_col, &W_col, &ts_col, r),
|| helper(T_row, W_row, &ts_row, r),
|| helper(T_col, W_col, &ts_col, r),
);

let t_plus_r_inv_row = t_plus_r_inv_row?;
Expand Down Expand Up @@ -1068,7 +1068,7 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> RelaxedR1CSSNARKTrait<E> for Relax
let u: PolyEvalInstance<E> = PolyEvalInstance::batch(&comm_vec, &tau_coords, &eval_vec, &c);

// we now need to prove three claims
// (1) 0 = \sum_x poly_tau(x) * (poly_Az(x) * poly_Bz(x) - poly_uCz_E(x)), and eval_Az_at_tau + r * eval_Az_at_tau + r^2 * eval_Cz_at_tau = (Az+r*Bz+r^2*Cz)(tau)
// (1) 0 = \sum_x poly_tau(x) * (poly_Az(x) * poly_Bz(x) - poly_uCz_E(x)), and eval_Az_at_tau + r * eval_Bz_at_tau + r^2 * eval_Cz_at_tau = (Az+r*Bz+r^2*Cz)(tau)
// (2) eval_Az_at_tau + c * eval_Bz_at_tau + c^2 * eval_Cz_at_tau = \sum_y L_row(y) * (val_A(y) + c * val_B(y) + c^2 * val_C(y)) * L_col(y)
// (3) L_row(i) = eq(tau, row(i)) and L_col(i) = z(col(i))
let gamma = transcript.squeeze(b"g")?;
Expand Down Expand Up @@ -1139,11 +1139,11 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> RelaxedR1CSSNARKTrait<E> for Relax
MemorySumcheckInstance::new(
ck,
&r,
T_row,
W_row,
&T_row,
&W_row,
pk.S_repr.ts_row.clone(),
T_col,
W_col,
&T_col,
&W_col,
pk.S_repr.ts_col.clone(),
&mut transcript,
)
Expand Down
Loading

0 comments on commit 9004d70

Please sign in to comment.