Skip to content

Commit

Permalink
fix: added assertions for N_CURRENCIES in the gate
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Jul 18, 2024
1 parent aa47d62 commit 8ee34c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 54 deletions.
87 changes: 41 additions & 46 deletions prover/src/circuits/config/circuit_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,61 +43,56 @@ pub trait CircuitConfig<const N_CURRENCIES: usize, const N_USERS: usize>: Clone
let range_check_chips = self.initialize_range_check_chips();

for (i, entry) in entries.iter().enumerate() {
let last_decompositions: Vec<halo2_proofs::circuit::AssignedCell<Fp, Fp>> = 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::<Fp>(entry.username_as_big_uint())),
)?;

region.assign_advice(
|| "concatenated balance",
self.get_concatenated_balance(),
0,
|| {
Value::known(big_uint_to_fp::<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::<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::<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)?;
}
Expand Down
12 changes: 4 additions & 8 deletions prover/src/circuits/summa_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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"
),
}

Expand Down

0 comments on commit 8ee34c5

Please sign in to comment.