Skip to content

Commit 4c740f7

Browse files
authored
Some optimizations in constraint synthesis. (#407)
* Switch cmp * New `Variable` formatting to save 64 bits per `Variable`, and introduce `LcMap` * Remove redundant comments * Format * Optimize new `Variable` * Fix * Add interning * Add comments * Rename * Switch to correct ordering for variables * Clippy comments * More clippy comments * More clippy comments * Fix assertion * Fix comment
1 parent 9b991e0 commit 4c740f7

File tree

17 files changed

+1143
-297
lines changed

17 files changed

+1143
-297
lines changed

relations/examples/bench-no-finalize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ impl ConstraintSynthesizer<Fr> for BenchCircuit {
2929
variables.reserve(3 * self.num_constraints);
3030
let mut lcs = Vec::with_capacity(self.num_constraints);
3131

32-
let mut rng_a = ark_std::rand::rngs::StdRng::seed_from_u64(0 as u64);
32+
let mut rng_a = ark_std::rand::rngs::StdRng::seed_from_u64(0_u64);
3333
let mut rng_b = ark_std::rand::rngs::StdRng::seed_from_u64(rng_a.gen::<u64>());
3434
let mut rng_c = ark_std::rand::rngs::StdRng::seed_from_u64(rng_a.gen::<u64>());
3535

3636
for i in 0..self.num_constraints {
3737
let cur_num_vars = ark_std::cmp::min(variables.len(), 10);
38-
let lower = variables.len().checked_sub(cur_num_vars).unwrap_or(0);
38+
let lower = variables.len().saturating_sub(cur_num_vars);
3939
let upper = variables.len();
4040

4141
let a_i_lc_size = rng_a.gen_range(1..=NUM_COEFFS_IN_LC);

relations/examples/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl ConstraintSynthesizer<Fr> for BenchCircuit {
3434

3535
for i in 0..self.num_constraints {
3636
let cur_num_vars = ark_std::cmp::min(variables.len(), 10);
37-
let lower = variables.len().checked_sub(cur_num_vars).unwrap_or(0);
37+
let lower = variables.len().saturating_sub(cur_num_vars);
3838
let upper = variables.len();
3939

4040
let a_i_lc_size = rng_a.gen_range(1..=NUM_COEFFS_IN_LC);

relations/src/gr1cs/assignment.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use ark_ff::Field;
2+
use ark_std::vec::Vec;
3+
4+
use 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)]
11+
pub 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+
23+
impl<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+
}

0 commit comments

Comments
 (0)