|
| 1 | +//! Implements the Customizable Constraint System (CCS) format. |
| 2 | +//! |
| 3 | +//! A CCS represents arithmetic constraints through a combination of matrices |
| 4 | +//! and multisets, allowing efficient verification of arithmetic computations. |
| 5 | +//! |
| 6 | +//! The system consists of: |
| 7 | +//! - A set of sparse matrices representing linear combinations |
| 8 | +//! - Multisets defining which matrices participate in each constraint |
| 9 | +//! - Constants applied to each constraint term |
| 10 | +
|
| 11 | +use matrix::SparseMatrix; |
| 12 | + |
| 13 | +use super::*; |
| 14 | + |
| 15 | +/// A Customizable Constraint System over a field F. |
| 16 | +#[derive(Debug, Default)] |
| 17 | +pub struct CCS<F: Field> { |
| 18 | + /// Constants for each constraint term |
| 19 | + pub constants: Vec<F>, |
| 20 | + /// Sets of matrix indices for Hadamard products |
| 21 | + pub multisets: Vec<Vec<usize>>, |
| 22 | + /// Constraint matrices |
| 23 | + pub matrices: Vec<SparseMatrix<F>>, |
| 24 | +} |
| 25 | + |
| 26 | +impl<F: Field + std::fmt::Debug> CCS<F> { |
| 27 | + /// Creates a new empty CCS. |
| 28 | + pub fn new() -> Self { |
| 29 | + Self::default() |
| 30 | + } |
| 31 | + |
| 32 | + /// Checks if a witness and public input satisfy the constraint system. |
| 33 | + /// |
| 34 | + /// Forms vector z = (w, 1, x) and verifies that all constraints are satisfied. |
| 35 | + /// |
| 36 | + /// # Arguments |
| 37 | + /// * `w` - The witness vector |
| 38 | + /// * `x` - The public input vector |
| 39 | + /// |
| 40 | + /// # Returns |
| 41 | + /// `true` if all constraints are satisfied, `false` otherwise |
| 42 | + pub fn is_satisfied(&self, w: &[F], x: &[F]) -> bool { |
| 43 | + // Construct z = (w, 1, x) |
| 44 | + let mut z = Vec::with_capacity(w.len() + 1 + x.len()); |
| 45 | + z.extend(w.iter().copied()); |
| 46 | + z.push(F::ONE); |
| 47 | + z.extend(x.iter().copied()); |
| 48 | + |
| 49 | + // Compute all matrix-vector products |
| 50 | + let products: Vec<Vec<F>> = self |
| 51 | + .matrices |
| 52 | + .iter() |
| 53 | + .enumerate() |
| 54 | + .map(|(i, matrix)| { |
| 55 | + let result = matrix * &z; |
| 56 | + println!("M{i} · z = {result:?}"); |
| 57 | + result |
| 58 | + }) |
| 59 | + .collect(); |
| 60 | + |
| 61 | + // For each row in the output... |
| 62 | + let m = if let Some(first) = products.first() { |
| 63 | + first.len() |
| 64 | + } else { |
| 65 | + return true; // No constraints |
| 66 | + }; |
| 67 | + |
| 68 | + // For each output coordinate... |
| 69 | + for row in 0..m { |
| 70 | + let mut sum = F::ZERO; |
| 71 | + |
| 72 | + // For each constraint... |
| 73 | + for (i, multiset) in self.multisets.iter().enumerate() { |
| 74 | + let mut term = products[multiset[0]][row]; |
| 75 | + |
| 76 | + for &idx in multiset.iter().skip(1) { |
| 77 | + term *= products[idx][row]; |
| 78 | + } |
| 79 | + |
| 80 | + let contribution = self.constants[i] * term; |
| 81 | + sum += contribution; |
| 82 | + } |
| 83 | + |
| 84 | + if sum != F::ZERO { |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + true |
| 90 | + } |
| 91 | + |
| 92 | + /// Creates a new CCS configured for constraints up to the given degree. |
| 93 | + /// |
| 94 | + /// # Arguments |
| 95 | + /// * `d` - Maximum degree of constraints |
| 96 | + /// |
| 97 | + /// # Panics |
| 98 | + /// If d < 2 |
| 99 | + pub fn new_degree(d: usize) -> Self { |
| 100 | + assert!(d >= 2, "Degree must be positive"); |
| 101 | + |
| 102 | + let mut ccs = Self { constants: Vec::new(), multisets: Vec::new(), matrices: Vec::new() }; |
| 103 | + |
| 104 | + // We'll create terms starting from highest degree down to degree 1 |
| 105 | + // For a degree d CCS, we need terms of all degrees from d down to 1 |
| 106 | + let mut next_matrix_index = 0; |
| 107 | + |
| 108 | + // Handle each degree from d down to 1 |
| 109 | + for degree in (1..=d).rev() { |
| 110 | + // For a term of degree k, we need k matrices Hadamard multiplied |
| 111 | + let matrix_indices: Vec<usize> = (0..degree).map(|i| next_matrix_index + i).collect(); |
| 112 | + |
| 113 | + // Add this term's multiset and its coefficient |
| 114 | + ccs.multisets.push(matrix_indices); |
| 115 | + ccs.constants.push(F::ONE); |
| 116 | + |
| 117 | + // Update our tracking of matrix indices |
| 118 | + next_matrix_index += degree; |
| 119 | + } |
| 120 | + |
| 121 | + // Calculate total number of matrices needed: |
| 122 | + // For degree d, we need d + (d-1) + ... + 1 matrices |
| 123 | + // This is the triangular number formula: n(n+1)/2 |
| 124 | + let total_matrices = (d * (d + 1)) / 2; |
| 125 | + |
| 126 | + // Initialize empty matrices - their content will be filled during conversion |
| 127 | + for _ in 0..total_matrices { |
| 128 | + ccs.matrices.push(SparseMatrix::new_rows_cols(1, 0)); |
| 129 | + } |
| 130 | + |
| 131 | + ccs |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl<F: Field + Display> Display for CCS<F> { |
| 136 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 137 | + writeln!(f, "Customizable Constraint System:")?; |
| 138 | + |
| 139 | + // First, display all matrices with their indices |
| 140 | + writeln!(f, "\nMatrices:")?; |
| 141 | + for (i, matrix) in self.matrices.iter().enumerate() { |
| 142 | + writeln!(f, "M{i} =")?; |
| 143 | + writeln!(f, "{matrix}")?; |
| 144 | + } |
| 145 | + |
| 146 | + // Show how constraints are formed from multisets and constants |
| 147 | + writeln!(f, "\nConstraints:")?; |
| 148 | + |
| 149 | + // We expect multisets to come in pairs, each pair forming one constraint |
| 150 | + for i in 0..self.multisets.len() { |
| 151 | + // Write the constant for the first multiset |
| 152 | + write!(f, "{}·(", self.constants[i])?; |
| 153 | + |
| 154 | + // Write the Hadamard product for the first multiset |
| 155 | + if let Some(first_idx) = self.multisets[i].first() { |
| 156 | + write!(f, "M{first_idx}")?; |
| 157 | + for &idx in &self.multisets[i][1..] { |
| 158 | + write!(f, "∘M{idx}")?; |
| 159 | + } |
| 160 | + } |
| 161 | + write!(f, ")")?; |
| 162 | + |
| 163 | + // Sum up the expressions to the last one |
| 164 | + if i < self.multisets.len() - 1 { |
| 165 | + write!(f, " + ")?; |
| 166 | + } |
| 167 | + } |
| 168 | + writeln!(f, " = 0")?; |
| 169 | + Ok(()) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +#[cfg(test)] |
| 174 | +mod tests { |
| 175 | + use super::*; |
| 176 | + use crate::mock::F17; |
| 177 | + |
| 178 | + #[test] |
| 179 | + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] |
| 180 | + fn test_ccs_satisfaction() { |
| 181 | + println!("\nSetting up CCS for constraint x * y = z"); |
| 182 | + |
| 183 | + // For z = (y, z, 1, x), create matrices: |
| 184 | + let mut m1 = SparseMatrix::new_rows_cols(1, 4); |
| 185 | + m1.write(0, 3, F17::ONE); // Select x |
| 186 | + let mut m2 = SparseMatrix::new_rows_cols(1, 4); |
| 187 | + m2.write(0, 0, F17::ONE); // Select y |
| 188 | + let mut m3 = SparseMatrix::new_rows_cols(1, 4); |
| 189 | + m3.write(0, 1, F17::ONE); // Select z |
| 190 | + |
| 191 | + println!("Created matrices:"); |
| 192 | + println!("M1 (selects x): {m1:?}"); |
| 193 | + println!("M2 (selects y): {m2:?}"); |
| 194 | + println!("M3 (selects z): {m3:?}"); |
| 195 | + |
| 196 | + let mut ccs = CCS::new(); |
| 197 | + ccs.matrices = vec![m1, m2, m3]; |
| 198 | + // Encode x * y - z = 0 |
| 199 | + ccs.multisets = vec![vec![0, 1], vec![2]]; |
| 200 | + ccs.constants = vec![F17::ONE, F17::from(-1)]; |
| 201 | + |
| 202 | + println!("\nTesting valid case: x=2, y=3, z=6"); |
| 203 | + let x = vec![F17::from(2)]; // public input x = 2 |
| 204 | + let w = vec![F17::from(3), F17::from(6)]; // witness y = 3, z = 6 |
| 205 | + assert!(ccs.is_satisfied(&w, &x)); |
| 206 | + |
| 207 | + println!("\nTesting invalid case: x=2, y=3, z=7"); |
| 208 | + let w_invalid = vec![F17::from(3), F17::from(7)]; // witness y = 3, z = 7 (invalid) |
| 209 | + assert!(!ccs.is_satisfied(&w_invalid, &x)); |
| 210 | + } |
| 211 | +} |
0 commit comments