Skip to content

Commit

Permalink
Fix expression bugs. (#925)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alon-Ti authored Dec 8, 2024
1 parent a201e77 commit 2798020
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
50 changes: 33 additions & 17 deletions crates/prover/src/constraint_framework/expr.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::collections::{HashMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::ops::{Add, AddAssign, Index, Mul, MulAssign, Neg, Sub};

use itertools::sorted;
use num_traits::{One, Zero};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

use super::preprocessed_columns::PreprocessedColumn;
use super::{AssertEvaluator, EvalAtRow, Relation, RelationEntry, INTERACTION_TRACE_IDX};
Expand Down Expand Up @@ -251,7 +250,7 @@ impl BaseExpr {
}

pub fn random_eval(&self) -> BaseField {
let assignment = self.collect_variables().random_assignment();
let assignment = self.collect_variables().random_assignment(0);
assert!(assignment.2.is_empty());
self.eval_expr::<AssertEvaluator<'_>, _, _>(&assignment.0, &assignment.1)
}
Expand Down Expand Up @@ -384,7 +383,7 @@ impl ExtExpr {
}

pub fn random_eval(&self) -> SecureField {
let assignment = self.collect_variables().random_assignment();
let assignment = self.collect_variables().random_assignment(0);
self.eval_expr::<AssertEvaluator<'_>, _, _, _>(&assignment.0, &assignment.1, &assignment.2)
}
}
Expand Down Expand Up @@ -433,21 +432,37 @@ impl ExprVariables {
}

/// Generates a random assignment to the variables.
/// Note that the assignment is deterministic in the sets of variables (disregarding their
/// order), and this is required.
pub fn random_assignment(&self) -> ExprVarAssignment {
let mut rng = SmallRng::seed_from_u64(0);

/// Note that the assignment is deterministically dependent on every variable and that this is
/// required.
pub fn random_assignment(&self, salt: usize) -> ExprVarAssignment {
let cols = sorted(self.cols.iter())
.map(|col| ((col.interaction, col.idx, col.offset), rng.gen()))
.map(|col| {
((col.interaction, col.idx, col.offset), {
let mut hasher = DefaultHasher::new();
(salt, col).hash(&mut hasher);
(hasher.finish() as u32).into()
})
})
.collect();

let params = sorted(self.params.iter())
.map(|param| (param.clone(), rng.gen()))
.map(|param| {
(param.clone(), {
let mut hasher = DefaultHasher::new();
(salt, param).hash(&mut hasher);
(hasher.finish() as u32).into()
})
})
.collect();

let ext_params = sorted(self.ext_params.iter())
.map(|param| (param.clone(), rng.gen()))
.map(|param| {
(param.clone(), {
let mut hasher = DefaultHasher::new();
(salt, param).hash(&mut hasher);
(hasher.finish() as u32).into()
})
})
.collect();

(cols, params, ext_params)
Expand Down Expand Up @@ -849,11 +864,12 @@ impl EvalAtRow for ExprEvaluator {
interaction: usize,
offsets: [isize; N],
) -> [Self::F; N] {
std::array::from_fn(|i| {
let res = std::array::from_fn(|i| {
let col = ColumnExpr::from((interaction, self.cur_var_index, offsets[i]));
self.cur_var_index += 1;
BaseExpr::Col(col)
})
});
self.cur_var_index += 1;
res
}

fn add_constraint<G>(&mut self, constraint: G)
Expand Down Expand Up @@ -1094,8 +1110,8 @@ mod tests {
let constraint_0 = ((col_1_0[0]) * (intermediate0)) * (1 / (col_1_0[0] + col_1_1[0]));
\
let constraint_1 = (SecureCol(col_2_3[0], col_2_5[0], col_2_7[0], col_2_9[0]) \
- (SecureCol(col_2_4[-1], col_2_6[-1], col_2_8[-1], col_2_10[-1]) \
let constraint_1 = (SecureCol(col_2_3[0], col_2_4[0], col_2_5[0], col_2_6[0]) \
- (SecureCol(col_2_3[-1], col_2_4[-1], col_2_5[-1], col_2_6[-1]) \
- ((total_sum) * (preprocessed.is_first)))) \
* (intermediate1) \
- (1);"
Expand Down
10 changes: 5 additions & 5 deletions crates/prover/src/examples/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,16 @@ mod tests {
\
let constraint_0 = (SecureCol(\
col_2_2[claimed_sum_offset], \
col_2_3[claimed_sum_offset], \
col_2_4[claimed_sum_offset], \
col_2_7[claimed_sum_offset], \
col_2_10[claimed_sum_offset], \
col_2_13[claimed_sum_offset]\
col_2_5[claimed_sum_offset]\
) - (claimed_sum)) \
* (preprocessed.is_first);
\
let constraint_1 = (SecureCol(col_2_2[0], col_2_5[0], col_2_8[0], col_2_11[0]) \
- (SecureCol(col_2_3[-1], col_2_6[-1], col_2_9[-1], col_2_12[-1]) \
let constraint_1 = (SecureCol(col_2_2[0], col_2_3[0], col_2_4[0], col_2_5[0]) \
- (SecureCol(col_2_2[-1], col_2_3[-1], col_2_4[-1], col_2_5[-1]) \
- ((total_sum) * (preprocessed.is_first)))\
) \
* ((intermediate0) * (intermediate1)) \
Expand Down

0 comments on commit 2798020

Please sign in to comment.