From bd29ebdb057768234104846453b4545cb3e41a60 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Fri, 9 Jul 2021 12:38:38 -0600 Subject: [PATCH] Use ranges to clarify bounds on cell assignment. --- src/dev.rs | 16 ++++++++-------- src/plonk/keygen.rs | 18 +++++++++--------- src/plonk/prover.rs | 11 +++++++---- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/dev.rs b/src/dev.rs index 18837935..5ac64cd7 100644 --- a/src/dev.rs +++ b/src/dev.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::fmt; use std::iter; +use std::ops::RangeTo; use ff::Field; @@ -244,9 +245,8 @@ pub struct MockProver { permutation: permutation::keygen::Assembly, - // All rows including and above this one are off - // limits due to blinding factors. - upper_bound_cell_index: usize, + // A range of available rows for assignment and copies. + usable_rows: RangeTo, } impl Assignment for MockProver { @@ -278,7 +278,7 @@ impl Assignment for MockProver { A: FnOnce() -> AR, AR: Into, { - if row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&row) { return Err(Error::BoundsFailure); } @@ -319,7 +319,7 @@ impl Assignment for MockProver { A: FnOnce() -> AR, AR: Into, { - if row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&row) { return Err(Error::BoundsFailure); } @@ -350,7 +350,7 @@ impl Assignment for MockProver { A: FnOnce() -> AR, AR: Into, { - if row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&row) { return Err(Error::BoundsFailure); } @@ -375,7 +375,7 @@ impl Assignment for MockProver { right_column: Column, right_row: usize, ) -> Result<(), crate::plonk::Error> { - if left_row >= self.upper_bound_cell_index || right_row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&left_row) || !self.usable_rows.contains(&right_row) { return Err(Error::BoundsFailure); } @@ -428,7 +428,7 @@ impl MockProver { advice, instance, permutation, - upper_bound_cell_index: n - (blinding_factors + 1), + usable_rows: ..n - (blinding_factors + 1), }; ConcreteCircuit::FloorPlanner::synthesize(&mut prover, circuit, config)?; diff --git a/src/plonk/keygen.rs b/src/plonk/keygen.rs index 6995be35..4be0af08 100644 --- a/src/plonk/keygen.rs +++ b/src/plonk/keygen.rs @@ -1,5 +1,7 @@ #![allow(clippy::int_plus_one)] +use std::ops::RangeTo; + use ff::Field; use group::Curve; @@ -42,9 +44,8 @@ where struct Assembly { fixed: Vec, LagrangeCoeff>>, permutation: permutation::keygen::Assembly, - // All rows including and above this one are off - // limits due to blinding factors. - upper_bound_cell_index: usize, + // A range of available rows for assignment and copies. + usable_rows: RangeTo, _marker: std::marker::PhantomData, } @@ -71,7 +72,7 @@ impl Assignment for Assembly { A: FnOnce() -> AR, AR: Into, { - if row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&row) { return Err(Error::BoundsFailure); } // Selectors are just fixed columns. @@ -117,7 +118,7 @@ impl Assignment for Assembly { A: FnOnce() -> AR, AR: Into, { - if row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&row) { return Err(Error::BoundsFailure); } @@ -137,8 +138,7 @@ impl Assignment for Assembly { right_column: Column, right_row: usize, ) -> Result<(), Error> { - // Check bounds first - if left_row >= self.upper_bound_cell_index || right_row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&left_row) || !self.usable_rows.contains(&right_row) { return Err(Error::BoundsFailure); } @@ -177,7 +177,7 @@ where let mut assembly: Assembly = Assembly { fixed: vec![domain.empty_lagrange_assigned(); cs.num_fixed_columns], permutation: permutation::keygen::Assembly::new(params.n as usize, &cs.permutation), - upper_bound_cell_index: params.n as usize - (cs.blinding_factors() + 1), + usable_rows: ..params.n as usize - (cs.blinding_factors() + 1), _marker: std::marker::PhantomData, }; @@ -225,7 +225,7 @@ where let mut assembly: Assembly = Assembly { fixed: vec![vk.domain.empty_lagrange_assigned(); vk.cs.num_fixed_columns], permutation: permutation::keygen::Assembly::new(params.n as usize, &vk.cs.permutation), - upper_bound_cell_index: params.n as usize - (vk.cs.blinding_factors() + 1), + usable_rows: ..params.n as usize - (vk.cs.blinding_factors() + 1), _marker: std::marker::PhantomData, }; diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 9eff2907..82d54750 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -1,6 +1,7 @@ use ff::Field; use group::Curve; use std::iter; +use std::ops::RangeTo; use super::{ circuit::{ @@ -128,7 +129,7 @@ pub fn create_proof< struct WitnessCollection<'a, F: Field> { pub advice: Vec, LagrangeCoeff>>, instances: &'a [&'a [F]], - upper_bound_cell_index: usize, + usable_rows: RangeTo, _marker: std::marker::PhantomData, } @@ -185,7 +186,7 @@ pub fn create_proof< A: FnOnce() -> AR, AR: Into, { - if row >= self.upper_bound_cell_index { + if !self.usable_rows.contains(&row) { return Err(Error::BoundsFailure); } @@ -241,6 +242,8 @@ pub fn create_proof< } } + let unusable_rows_start = params.n as usize - (meta.blinding_factors() + 1); + let mut witness = WitnessCollection { advice: vec![domain.empty_lagrange_assigned(); meta.num_advice_columns], instances, @@ -248,7 +251,7 @@ pub fn create_proof< // cells that exist within inactive rows, which include some // number of blinding factors and an extra row for use in the // permutation argument. - upper_bound_cell_index: params.n as usize - (meta.blinding_factors() + 1), + usable_rows: ..unusable_rows_start, _marker: std::marker::PhantomData, }; @@ -259,7 +262,7 @@ pub fn create_proof< // Add blinding factors to advice columns for advice in &mut advice { - for cell in &mut advice[witness.upper_bound_cell_index..] { + for cell in &mut advice[unusable_rows_start..] { *cell = C::Scalar::rand(); } }