Skip to content

Commit

Permalink
refactor: remove VecFpVar struct
Browse files Browse the repository at this point in the history
  • Loading branch information
dmpierre committed Jan 10, 2024
1 parent b939374 commit 5c48096
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 49 deletions.
45 changes: 3 additions & 42 deletions src/folding/circuits/utils.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
use ark_ff::PrimeField;
use ark_r1cs_std::{
alloc::{AllocVar, AllocationMode},
fields::{fp::FpVar, FieldVar},
};
use ark_relations::r1cs::{Namespace, SynthesisError};
use std::{borrow::Borrow, marker::PhantomData};

/// `VecFpVar` is a wrapper around a vector of `FpVar`s.
/// It implements the `IntoIterator` trait and can hence be iterated over
pub struct VecFpVar<F: PrimeField> {
pub vec: Vec<FpVar<F>>,
}

impl<F: PrimeField> IntoIterator for VecFpVar<F> {
type Item = FpVar<F>;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}

impl<F: PrimeField> AllocVar<Vec<F>, F> for VecFpVar<F> {
fn new_variable<T: Borrow<Vec<F>>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
mode: AllocationMode,
) -> Result<Self, SynthesisError> {
let cs = cs.into();
f().and_then(|c| {
let vec = c
.borrow()
.iter()
.map(|element| {
FpVar::new_variable(cs.clone(), || Ok(element), mode)
.map_err(|_| SynthesisError::Unsatisfiable)
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Self { vec })
})
}
}
use ark_r1cs_std::fields::{fp::FpVar, FieldVar};
use ark_relations::r1cs::SynthesisError;
use std::marker::PhantomData;

/// EqEval is a gadget for computing $\tilde{eq}(a, b) = \Pi_{i=1}^{l}(a_i \cdot b_i + (1 - a_i)(1 - b_i))$
/// :warning: This is not the ark_r1cs_std::eq::EqGadget
Expand Down
11 changes: 4 additions & 7 deletions src/folding/hypernova/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// hypernova nimfs verifier circuit
// see section 5 in https://eprint.iacr.org/2023/573.pdf

use crate::folding::circuits::utils::VecFpVar;
use ark_ff::PrimeField;
use ark_r1cs_std::{
fields::{fp::FpVar, FieldVar},
Expand All @@ -28,7 +27,7 @@ impl<F: PrimeField> SumMulsGammaPowsEqSigmaGadget<F> {
/// # Notes
/// In the context of multifolding, `j` corresponds to `ccs.t` in `compute_c_from_sigmas_and_thetas`
pub fn sum_muls_gamma_pows_eq_sigma(
sigmas: VecFpVar<F>,
sigmas: Vec<FpVar<F>>,
eq_eval: FpVar<F>,
gamma: FpVar<F>,
j: FpVar<F>,
Expand All @@ -51,10 +50,7 @@ mod tests {
tests::{get_test_ccs, get_test_z},
CCS,
},
folding::{
circuits::utils::VecFpVar,
hypernova::utils::{compute_sigmas_and_thetas, sum_muls_gamma_pows_eq_sigma},
},
folding::hypernova::utils::{compute_sigmas_and_thetas, sum_muls_gamma_pows_eq_sigma},
pedersen::Pedersen,
utils::virtual_polynomial::eq_eval,
};
Expand Down Expand Up @@ -91,7 +87,8 @@ mod tests {
for (i, sigmas) in sigmas_thetas.0.iter().enumerate() {
let expected =
sum_muls_gamma_pows_eq_sigma(gamma, e_lcccs[i], sigmas, (i * ccs.t) as u64);
let sigmas_var = VecFpVar::<Fr>::new_witness(cs.clone(), || Ok(sigmas)).unwrap();
let sigmas_var =
Vec::<FpVar<Fr>>::new_witness(cs.clone(), || Ok(sigmas.clone())).unwrap();
let eq_var = FpVar::<Fr>::new_witness(cs.clone(), || Ok(e_lcccs[i])).unwrap();
let pow =
FpVar::<Fr>::new_witness(cs.clone(), || Ok(Fr::from((i * ccs.t) as u64))).unwrap();
Expand Down

0 comments on commit 5c48096

Please sign in to comment.