Skip to content

Commit

Permalink
Clearer assertion failure on duplicate elements given to member() (#196)
Browse files Browse the repository at this point in the history
Before this commit, when `member()` function got a `set` with
duplicate elements, the program would panic with
```
assertion failed: cs.is_sat()
```
with debugging informaiton about failing constraints.

After this commit, the program still panics when encounting
duplicates, but now with a clearer message
```
assertion failed: unique_values(set)
```
  • Loading branch information
yoichi-nexus authored Jun 21, 2024
1 parent 762a1bf commit 45a9602
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion vm/src/circuit/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//! These matrices are meant to be used at compile-time as a
//! source for generating constraints over a target field.

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::ops::Range;

use ark_bn254::FrConfig;
Expand Down Expand Up @@ -324,9 +325,20 @@ impl R1CS {
}
}

fn unique_values<Elm: Eq + Hash>(set: &[Elm]) -> bool {
let mut seen = HashSet::new();
for v in set.iter() {
if !seen.insert(v) {
return false;
}
}
true
}

pub fn member(cs: &mut R1CS, name: &str, k: u32, set: &[u32]) {
debug_assert!(set.len() > 1);
debug_assert!(set.contains(&k));
debug_assert!(unique_values(set));

// Compute constant that comes from evaulating
// (x - s0)(x - s1)...(x - s{n - 1}) / (x - sk)
Expand Down Expand Up @@ -613,6 +625,13 @@ mod test {
test_mem(&[57, 67, 77, 107, 117, 119]);
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "assertion failed: unique_values(set)")]
fn test_member_dup() {
test_mem(&[2, 2]);
}

fn test_sel(n: u32) {
for k in 0..n {
let mut cs = R1CS::default();
Expand Down

0 comments on commit 45a9602

Please sign in to comment.