Skip to main content

ark_relations/gr1cs/
assignment.rs

1use ark_ff::Field;
2use ark_std::vec::Vec;
3
4use crate::{
5    gr1cs::{field_interner::FieldInterner, lc_map::LcMap, Variable},
6    utils::variable::VarKind,
7};
8
9/// Assignments for a GR1CS constraint system.
10#[derive(Debug, Clone)]
11pub struct Assignments<F> {
12    /// Assignments to the public input variables. This is empty if `self.mode
13    /// == SynthesisMode::Setup`.
14    pub instance_assignment: Vec<F>,
15    /// Assignments to the private input variables. This is empty if `self.mode
16    /// == SynthesisMode::Setup`.
17    pub witness_assignment: Vec<F>,
18    /// A cache for the linear combination assignments. It shows evaluation
19    /// result of each linear combination
20    pub lc_assignment: Vec<F>,
21}
22
23impl<F: Field> Assignments<F> {
24    /// Obtain the assignment corresponding to the `Variable` `v`.
25    #[inline]
26    pub fn assigned_value(&self, v: Variable) -> Option<F> {
27        let idx = v.index();
28        match v.kind() {
29            VarKind::Zero => Some(F::ZERO),
30            VarKind::One => Some(F::ONE),
31            VarKind::Instance => self.instance_assignment.get(idx?).copied(),
32            VarKind::Witness => self.witness_assignment.get(idx?).copied(),
33            VarKind::SymbolicLc => self.lc_assignment.get(idx?).copied(),
34        }
35    }
36
37    /// Evaluate the linear combination `lc` with the assigned values and return
38    /// the result.
39    #[inline]
40    pub(super) fn eval_lc(
41        &self,
42        lc: usize,
43        lc_map: &LcMap<F>,
44        f_interner: &FieldInterner<F>,
45    ) -> Option<F> {
46        lc_map.get(lc).map(|lc| {
47            lc.map(|(&coeff, &var)| {
48                f_interner.value(coeff).unwrap() * self.assigned_value(var).unwrap()
49            })
50            .sum()
51        })
52    }
53}