sonobe/folding-schemes/src/arith/ccs/circuits.rs
folding-schemes/src/arith/ccs/circuits.rsRaw
1use super::CCS;
2use crate::utils::gadgets::SparseMatrixVar;
3use ark_ff::PrimeField;
4use ark_r1cs_std::{
5 alloc::{AllocVar, AllocationMode},
6 fields::fp::FpVar,
7};
8use ark_relations::r1cs::{Namespace, SynthesisError};
9use ark_std::borrow::Borrow;
10/// CCSMatricesVar contains the matrices 'M' of the CCS without the rest of CCS parameters.
11///
12#[derive(Debug, Clone)]
13pub struct CCSMatricesVar<F: PrimeField> {
14 // we only need native representation, so the constraint field==F
15 pub M: Vec<SparseMatrixVar<FpVar<F>>>,
16}
17
18impl<F: PrimeField> AllocVar<CCS<F>, F> for CCSMatricesVar<F> {
19 fn new_variable<T: Borrow<CCS<F>>>(
20 cs: impl Into<Namespace<F>>,
21 f: impl FnOnce() -> Result<T, SynthesisError>,
22 _mode: AllocationMode,
23 ) -> Result<Self, SynthesisError> {
24 f().and_then(|val| {
25 let cs = cs.into();
26 let M: Vec<SparseMatrixVar<FpVar<F>>> = val
27 .borrow()
28 .M
29 .iter()
30 .map(|M| SparseMatrixVar::<FpVar<F>>::new_constant(cs.clone(), M.clone()))
31 .collect::<Result<_, SynthesisError>>()?;
32 Ok(Self { M })
33 })
34 }
35}