Skip to content

Commit

Permalink
fix tob-3-10 and tob-3-11 (scroll-tech#979)
Browse files Browse the repository at this point in the history
* fix tob-3-10 and tob-3-11

* Fix lint.

---------

Co-authored-by: Zhang Zhuo <[email protected]>
Co-authored-by: Steven Gu <[email protected]>
  • Loading branch information
3 people authored Sep 23, 2023
1 parent f0a10fe commit 138b7a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
5 changes: 2 additions & 3 deletions aggregator/src/aggregation/rlc/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl RlcConfig {
cond: &AssignedCell<Fr, Fr>,
offset: &mut usize,
) -> Result<AssignedCell<Fr, Fr>, Error> {
// (cond - 1) * b + cond * a
// (1 - cond) * b + cond * a
let cond_not = self.not(region, cond, offset)?;
let tmp = self.mul(region, a, cond, offset)?;
self.mul_add(region, b, &cond_not, &tmp, offset)
Expand Down Expand Up @@ -360,8 +360,7 @@ impl RlcConfig {
offset: &mut usize,
) -> Result<AssignedCell<Fr, Fr>, Error> {
assert!(flags.len() == inputs.len());

let mut acc = inputs[0].clone();
let mut acc = self.mul(region, &inputs[0], &flags[0], offset)?;
for (input, flag) in inputs.iter().zip(flags.iter()).skip(1) {
let tmp = self.mul_add(region, &acc, challenge, input, offset)?;
acc = self.select(region, &tmp, &acc, flag, offset)?;
Expand Down
47 changes: 37 additions & 10 deletions aggregator/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,15 @@ pub(crate) fn conditional_constraints(
let chunk_is_valid_cells = chunks_are_valid
.iter()
.map(|chunk_is_valid| -> Result<_, halo2_proofs::plonk::Error> {
let cell = rlc_config.load_private(
rlc_config.load_private(
&mut region,
&Fr::from(*chunk_is_valid as u64),
&mut offset,
)?;
rlc_config.enforce_binary(&mut region, &cell, &mut offset)?;
Ok(cell)
)
})
.collect::<Result<Vec<_>, halo2_proofs::plonk::Error>>()?;
let num_valid_snarks =
num_valid_snarks(rlc_config, &mut region, &chunk_is_valid_cells, &mut offset)?;
constrain_flags(rlc_config, &mut region, &chunk_is_valid_cells, &mut offset)?;

log::trace!("number of valid chunks: {:?}", num_valid_snarks.value());
//
Expand Down Expand Up @@ -1047,17 +1045,46 @@ pub(crate) fn conditional_constraints(
Ok(())
}

// Input a list of flags whether the snark is valid
// Return a cell for number of valid snarks
fn num_valid_snarks(
/// Input a list of flags whether the snark is valid
///
/// Assert the following relations on the flags:
/// - all elements are binary
/// - the first element is 1
/// - for the next elements, if the element is 1, the previous element must also be 1
///
/// Return a cell for number of valid snarks
fn constrain_flags(
rlc_config: &RlcConfig,
region: &mut Region<Fr>,
chunk_are_valid: &[AssignedCell<Fr, Fr>],
offset: &mut usize,
) -> Result<AssignedCell<Fr, Fr>, halo2_proofs::plonk::Error> {
assert!(!chunk_are_valid.is_empty());

let one = {
let one = rlc_config.load_private(region, &Fr::one(), offset)?;
let one_cell = rlc_config.one_cell(chunk_are_valid[0].cell().region_index);
region.constrain_equal(one.cell(), one_cell)?;
one
};

// the first element is 1
region.constrain_equal(chunk_are_valid[0].cell(), one.cell())?;

let mut res = chunk_are_valid[0].clone();
for e in chunk_are_valid.iter().skip(1) {
res = rlc_config.add(region, &res, e, offset)?;
for (index, cell) in chunk_are_valid.iter().enumerate().skip(1) {
rlc_config.enforce_binary(region, cell, offset)?;

// if the element is 1, the previous element must also be 1
rlc_config.conditional_enforce_equal(
region,
&chunk_are_valid[index - 1],
&one,
cell,
offset,
)?;

res = rlc_config.add(region, &res, cell, offset)?;
}
Ok(res)
}

0 comments on commit 138b7a3

Please sign in to comment.