From 8ee34c5a98c56abd4c994a46447d16fc2e93911b Mon Sep 17 00:00:00 2001 From: sifnoc Date: Thu, 18 Jul 2024 11:20:59 +0000 Subject: [PATCH] fix: added assertions for N_CURRENCIES in the gate --- prover/src/circuits/config/circuit_config.rs | 87 +++++++++----------- prover/src/circuits/summa_circuit.rs | 12 +-- 2 files changed, 45 insertions(+), 54 deletions(-) diff --git a/prover/src/circuits/config/circuit_config.rs b/prover/src/circuits/config/circuit_config.rs index 8357f357..8fbcbf94 100644 --- a/prover/src/circuits/config/circuit_config.rs +++ b/prover/src/circuits/config/circuit_config.rs @@ -43,61 +43,56 @@ pub trait CircuitConfig: Clone let range_check_chips = self.initialize_range_check_chips(); for (i, entry) in entries.iter().enumerate() { - let last_decompositions: Vec> = layouter - .assign_region( - || format!("assign entry {} to the table", i), - |mut region| { - region.assign_advice( - || "username", - self.get_username(), + let last_decompositions = layouter.assign_region( + || format!("assign entry {} to the table", i), + |mut region| { + region.assign_advice( + || "username", + self.get_username(), + 0, + || Value::known(big_uint_to_fp::(entry.username_as_big_uint())), + )?; + + region.assign_advice( + || "concatenated balance", + self.get_concatenated_balance(), + 0, + || { + Value::known(big_uint_to_fp::( + &entry.concatenated_balance().unwrap(), + )) + }, + )?; + + // Decompose the balances + let mut assigned_balances = Vec::new(); + + for (j, balance) in entry.balances().iter().enumerate() { + let assigned_balance = region.assign_advice( + || format!("balance {}", j), + self.get_balances()[j], 0, - || Value::known(big_uint_to_fp::(entry.username_as_big_uint())), + || Value::known(big_uint_to_fp(balance)), )?; - region.assign_advice( - || "concatenated balance", - self.get_concatenated_balance(), - 0, - || { - Value::known(big_uint_to_fp::( - &entry.concatenated_balance().unwrap(), - )) - }, - )?; + assigned_balances.push(assigned_balance); + } - // Decompose the balances - let mut assigned_balances = Vec::new(); + let mut last_decompositions = vec![]; - for (j, balance) in entry.balances().iter().enumerate() { - let assigned_balance = region.assign_advice( - || format!("balance {}", j), - self.get_balances()[j], - 0, - || Value::known(big_uint_to_fp(balance)), - )?; - - assigned_balances.push(assigned_balance); - } + for (j, assigned_balance) in assigned_balances.iter().enumerate() { + let mut zs = Vec::with_capacity(4); - let mut last_decompositions = vec![]; + if !range_check_chips.is_empty() { + range_check_chips[j].assign(&mut region, &mut zs, assigned_balance)?; - for (j, assigned_balance) in assigned_balances.iter().enumerate() { - let mut zs = Vec::with_capacity(4); - - if !range_check_chips.is_empty() { - range_check_chips[j].assign( - &mut region, - &mut zs, - assigned_balance, - )?; - - last_decompositions.push(zs[3].clone()); - } + last_decompositions.push(zs[3].clone()); } + } - Ok(last_decompositions) - }, - )?; + Ok(last_decompositions) + }, + )?; self.constrain_decompositions(last_decompositions, &mut layouter)?; } diff --git a/prover/src/circuits/summa_circuit.rs b/prover/src/circuits/summa_circuit.rs index 0d7ca988..199809bd 100644 --- a/prover/src/circuits/summa_circuit.rs +++ b/prover/src/circuits/summa_circuit.rs @@ -111,17 +111,13 @@ impl< let mut current_shift = Expression::Constant(base_shift); - // The number of currencies is limited to 3 because the range check bits are 64 for each currency. + // The number of currencies is limited 1 or 3 because the range check chip logic. // In other words, more than 3 currencies would exceed the maximum bit count of 254, which is number of bits in Bn254. match N_CURRENCIES { 1 => { // No need to add any shift for the only balance - } - 2 => { - let balance = meta.query_advice(balances[0], Rotation::cur()); - let shifted_balance = balance * current_shift.clone(); - balances_expr = balances_expr + shifted_balance; - } + println!("For a better performance for single currency, check out V3c. More details at: https://github.com/summa-dev/summa-solvency/tree/v3c"); + }, 3 => { for i in (0..N_CURRENCIES - 1).rev() { let balance = meta.query_advice(balances[i], Rotation::cur()); @@ -134,7 +130,7 @@ impl< } } _ => panic!( - "Unsupported number of currencies, Only 1, 2, and 3 currencies are supported" + "Unsupported number of currencies, Only 1 and 3 currencies are supported" ), }