Skip to content

Commit

Permalink
Remove old API in favour of legacy wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Jan 5, 2024
1 parent ca9d052 commit 8813ef7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1,007 deletions.
68 changes: 0 additions & 68 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use ff::Field;
use sealed::SealedPhase;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::Debug;
use std::iter::{Product, Sum};
use std::{
Expand Down Expand Up @@ -1784,67 +1783,13 @@ impl QueriesMap {
}
}

/*
/// This is a description of the circuit environment, such as the gate, column and
/// permutation arrangements.
#[derive(Debug, Clone)]
pub struct ConstraintSystemV2BackendQueries<F: Field> {
pub(crate) num_fixed_columns: usize,
pub(crate) num_advice_columns: usize,
pub(crate) num_instance_columns: usize,
// pub(crate) num_selectors: usize,
pub(crate) num_challenges: usize,
/// Contains the index of each advice column that is left unblinded.
pub(crate) unblinded_advice_columns: Vec<usize>,
/// Contains the phase for each advice column. Should have same length as num_advice_columns.
pub(crate) advice_column_phase: Vec<u8>,
/// Contains the phase for each challenge. Should have same length as num_challenges.
pub(crate) challenge_phase: Vec<u8>,
/// This is a cached vector that maps virtual selectors to the concrete
/// fixed column that they were compressed into. This is just used by dev
/// tooling right now.
// pub(crate) selector_map: Vec<Column<Fixed>>,
pub(crate) gates: Vec<GateV2Backend<F>>,
pub(crate) advice_queries: Vec<(Column<Advice>, Rotation)>,
// Contains an integer for each advice column
// identifying how many distinct queries it has
// so far; should be same length as num_advice_columns.
pub(crate) num_advice_queries: Vec<usize>,
pub(crate) instance_queries: Vec<(Column<Instance>, Rotation)>,
pub(crate) fixed_queries: Vec<(Column<Fixed>, Rotation)>,
// Permutation argument for performing equality constraints
pub(crate) permutation: permutation::Argument,
// Vector of lookup arguments, where each corresponds to a sequence of
// input expressions and a sequence of table expressions involved in the lookup.
pub(crate) lookups: Vec<lookup::Argument<F>>,
// Vector of shuffle arguments, where each corresponds to a sequence of
// input expressions and a sequence of shuffle expressions involved in the shuffle.
pub(crate) shuffles: Vec<shuffle::Argument<F>>,
// List of indexes of Fixed columns which are associated to a circuit-general Column tied to their annotation.
pub(crate) general_column_annotations: HashMap<metadata::Column, String>,
// Vector of fixed columns, which can be used to store constant values
// that are copied into advice columns.
// pub(crate) constants: Vec<Column<Fixed>>,
// pub(crate) minimum_degree: Option<usize>,
}
*/

/// This is a description of the circuit environment, such as the gate, column and
/// permutation arrangements.
#[derive(Debug, Clone)]
pub struct ConstraintSystemV2Backend<F: Field> {
pub(crate) num_fixed_columns: usize,
pub(crate) num_advice_columns: usize,
pub(crate) num_instance_columns: usize,
// pub(crate) num_selectors: usize,
pub(crate) num_challenges: usize,

/// Contains the index of each advice column that is left unblinded.
Expand All @@ -1855,14 +1800,7 @@ pub struct ConstraintSystemV2Backend<F: Field> {
/// Contains the phase for each challenge. Should have same length as num_challenges.
pub(crate) challenge_phase: Vec<u8>,

/// This is a cached vector that maps virtual selectors to the concrete
/// fixed column that they were compressed into. This is just used by dev
/// tooling right now.
// pub(crate) selector_map: Vec<Column<Fixed>>,
pub(crate) gates: Vec<GateV2Backend<F>>,
// pub(crate) advice_queries: Vec<(Column<Advice>, Rotation)>,
// pub(crate) instance_queries: Vec<(Column<Instance>, Rotation)>,
// pub(crate) fixed_queries: Vec<(Column<Fixed>, Rotation)>,

// Permutation argument for performing equality constraints
pub(crate) permutation: permutation::Argument,
Expand All @@ -1877,11 +1815,6 @@ pub struct ConstraintSystemV2Backend<F: Field> {

// List of indexes of Fixed columns which are associated to a circuit-general Column tied to their annotation.
pub(crate) general_column_annotations: HashMap<metadata::Column, String>,
// Vector of fixed columns, which can be used to store constant values
// that are copied into advice columns.
// pub(crate) constants: Vec<Column<Fixed>>,

// pub(crate) minimum_degree: Option<usize>,
}

/// Witness calculator. Frontend function
Expand Down Expand Up @@ -1942,7 +1875,6 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, Concret
k: self.k,
current_phase,
advice: vec![Polynomial::new_empty(self.n, F::ZERO.into()); self.cs.num_advice_columns],
unblinded_advice: HashSet::from_iter(self.cs.unblinded_advice_columns.clone()),
instances: self.instances,
challenges,
// The prover will not be allowed to assign values to advice
Expand Down
227 changes: 5 additions & 222 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use group::Curve;
use super::{
circuit::{
compile_circuit, Advice, Any, Assignment, Circuit, Column, CompiledCircuitV2,
ConstraintSystem, Fixed, FloorPlanner, Instance, Selector,
ConstraintSystem, Fixed, Instance, Selector,
},
evaluation::Evaluator,
permutation, Assigned, Challenge, Error, LagrangeCoeff, Polynomial, ProvingKey, VerifyingKey,
Expand All @@ -17,7 +17,6 @@ use crate::{
arithmetic::{parallelize, CurveAffine},
circuit::Value,
poly::{
batch_invert_assigned,
commitment::{Blind, Params},
EvaluationDomain,
},
Expand Down Expand Up @@ -247,7 +246,7 @@ where

/// Generate a `VerifyingKey` from an instance of `Circuit`.
/// By default, selector compression is turned **off**.
pub fn keygen_vk_legacy<'params, C, P, ConcreteCircuit>(
pub fn keygen_vk<'params, C, P, ConcreteCircuit>(
params: &P,
circuit: &ConcreteCircuit,
) -> Result<VerifyingKey<C>, Error>
Expand All @@ -257,13 +256,13 @@ where
ConcreteCircuit: Circuit<C::Scalar>,
C::Scalar: FromUniformBytes<64>,
{
keygen_vk_custom_legacy(params, circuit, true)
keygen_vk_custom(params, circuit, true)
}

/// Generate a `VerifyingKey` from an instance of `Circuit`.
///
/// The selector compression optimization is turned on only if `compress_selectors` is `true`.
pub fn keygen_vk_custom_legacy<'params, C, P, ConcreteCircuit>(
pub fn keygen_vk_custom<'params, C, P, ConcreteCircuit>(
params: &P,
circuit: &ConcreteCircuit,
compress_selectors: bool,
Expand All @@ -280,97 +279,6 @@ where
Ok(vk)
}

// TODO: Remove
/// Generate a `VerifyingKey` from an instance of `Circuit`.
/// By default, selector compression is turned **off**.
pub fn keygen_vk<'params, C, P, ConcreteCircuit>(
params: &P,
circuit: &ConcreteCircuit,
) -> Result<VerifyingKey<C>, Error>
where
C: CurveAffine,
P: Params<'params, C>,
ConcreteCircuit: Circuit<C::Scalar>,
C::Scalar: FromUniformBytes<64>,
{
keygen_vk_custom(params, circuit, true)
}

// TODO: Remove
/// Generate a `VerifyingKey` from an instance of `Circuit`.
///
/// The selector compression optimization is turned on only if `compress_selectors` is `true`.
pub fn keygen_vk_custom<'params, C, P, ConcreteCircuit>(
params: &P,
circuit: &ConcreteCircuit,
compress_selectors: bool,
) -> Result<VerifyingKey<C>, Error>
where
C: CurveAffine,
P: Params<'params, C>,
ConcreteCircuit: Circuit<C::Scalar>,
C::Scalar: FromUniformBytes<64>,
{
let (domain, cs, config) = create_domain::<C, ConcreteCircuit>(
params.k(),
#[cfg(feature = "circuit-params")]
circuit.params(),
);

if (params.n() as usize) < cs.minimum_rows() {
return Err(Error::not_enough_rows_available(params.k()));
}

let mut assembly: Assembly<C::Scalar> = Assembly {
k: params.k(),
fixed: vec![domain.empty_lagrange_assigned(); cs.num_fixed_columns],
permutation: permutation::keygen::Assembly::new(params.n() as usize, &cs.permutation),
selectors: vec![vec![false; params.n() as usize]; cs.num_selectors],
usable_rows: 0..params.n() as usize - (cs.blinding_factors() + 1),
_marker: std::marker::PhantomData,
};

// Synthesize the circuit to obtain URS
ConcreteCircuit::FloorPlanner::synthesize(
&mut assembly,
circuit,
config,
cs.constants.clone(),
)?;

let mut fixed = batch_invert_assigned(assembly.fixed);
let (cs, selector_polys) = if compress_selectors {
cs.compress_selectors(assembly.selectors.clone())
} else {
// After this, the ConstraintSystem should not have any selectors: `verify` does not need them, and `keygen_pk` regenerates `cs` from scratch anyways.
let selectors = std::mem::take(&mut assembly.selectors);
cs.directly_convert_selectors_to_fixed(selectors)
};
fixed.extend(
selector_polys
.into_iter()
.map(|poly| domain.lagrange_from_vec(poly)),
);

let permutation_vk = assembly
.permutation
.build_vk(params, &domain, &cs.permutation);

let fixed_commitments = fixed
.iter()
.map(|poly| params.commit_lagrange(poly, Blind::default()).to_affine())
.collect();

Ok(VerifyingKey::from_parts(
domain,
fixed_commitments,
permutation_vk,
cs,
assembly.selectors,
compress_selectors,
))
}

/// Generate a `ProvingKey` from a `VerifyingKey` and an instance of `CompiledCircuit`.
pub fn keygen_pk_v2<'params, C, P>(
params: &P,
Expand Down Expand Up @@ -456,7 +364,7 @@ where
}

/// Generate a `ProvingKey` from a `VerifyingKey` and an instance of `Circuit`.
pub fn keygen_pk_legacy<'params, C, P, ConcreteCircuit>(
pub fn keygen_pk<'params, C, P, ConcreteCircuit>(
params: &P,
vk: VerifyingKey<C>,
circuit: &ConcreteCircuit,
Expand All @@ -469,128 +377,3 @@ where
let (compiled_circuit, _, _) = compile_circuit(params.k(), circuit, vk.compress_selectors)?;
keygen_pk_v2(params, vk, &compiled_circuit)
}

// TODO: Remove
/// Generate a `ProvingKey` from a `VerifyingKey` and an instance of `Circuit`.
pub fn keygen_pk<'params, C, P, ConcreteCircuit>(
params: &P,
vk: VerifyingKey<C>,
circuit: &ConcreteCircuit,
) -> Result<ProvingKey<C>, Error>
where
C: CurveAffine,
P: Params<'params, C>,
ConcreteCircuit: Circuit<C::Scalar>,
{
let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params());
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);

let cs = cs;

if (params.n() as usize) < cs.minimum_rows() {
return Err(Error::not_enough_rows_available(params.k()));
}

let mut assembly: Assembly<C::Scalar> = Assembly {
k: params.k(),
fixed: vec![vk.domain.empty_lagrange_assigned(); cs.num_fixed_columns],
permutation: permutation::keygen::Assembly::new(params.n() as usize, &cs.permutation),
selectors: vec![vec![false; params.n() as usize]; cs.num_selectors],
usable_rows: 0..params.n() as usize - (cs.blinding_factors() + 1),
_marker: std::marker::PhantomData,
};

// Synthesize the circuit to obtain URS
ConcreteCircuit::FloorPlanner::synthesize(
&mut assembly,
circuit,
config,
cs.constants.clone(),
)?;

let mut fixed = batch_invert_assigned(assembly.fixed);
let (cs, selector_polys) = if vk.compress_selectors {
cs.compress_selectors(assembly.selectors)
} else {
cs.directly_convert_selectors_to_fixed(assembly.selectors)
};
// println!(
// "DBG configure queries:\n{:#?}",
// (
// &cs.advice_queries,
// &cs.instance_queries,
// &cs.fixed_queries,
// &cs.num_advice_queries
// )
// );
fixed.extend(
selector_polys
.into_iter()
.map(|poly| vk.domain.lagrange_from_vec(poly)),
);

let fixed_polys: Vec<_> = fixed
.iter()
.map(|poly| vk.domain.lagrange_to_coeff(poly.clone()))
.collect();

let fixed_cosets = fixed_polys
.iter()
.map(|poly| vk.domain.coeff_to_extended(poly.clone()))
.collect();

let permutation_pk = assembly
.permutation
.build_pk(params, &vk.domain, &cs.permutation);

// Compute l_0(X)
// TODO: this can be done more efficiently
let mut l0 = vk.domain.empty_lagrange();
l0[0] = C::Scalar::ONE;
let l0 = vk.domain.lagrange_to_coeff(l0);
let l0 = vk.domain.coeff_to_extended(l0);

// Compute l_blind(X) which evaluates to 1 for each blinding factor row
// and 0 otherwise over the domain.
let mut l_blind = vk.domain.empty_lagrange();
for evaluation in l_blind[..].iter_mut().rev().take(cs.blinding_factors()) {
*evaluation = C::Scalar::ONE;
}
let l_blind = vk.domain.lagrange_to_coeff(l_blind);
let l_blind = vk.domain.coeff_to_extended(l_blind);

// Compute l_last(X) which evaluates to 1 on the first inactive row (just
// before the blinding factors) and 0 otherwise over the domain
let mut l_last = vk.domain.empty_lagrange();
l_last[params.n() as usize - cs.blinding_factors() - 1] = C::Scalar::ONE;
let l_last = vk.domain.lagrange_to_coeff(l_last);
let l_last = vk.domain.coeff_to_extended(l_last);

// Compute l_active_row(X)
let one = C::Scalar::ONE;
let mut l_active_row = vk.domain.empty_extended();
parallelize(&mut l_active_row, |values, start| {
for (i, value) in values.iter_mut().enumerate() {
let idx = i + start;
*value = one - (l_last[idx] + l_blind[idx]);
}
});

// Compute the optimized evaluation data structure
let ev = Evaluator::new(&vk.cs);

Ok(ProvingKey {
vk,
l0,
l_last,
l_active_row,
fixed_values: fixed,
fixed_polys,
fixed_cosets,
permutation: permutation_pk,
ev,
})
}
Loading

0 comments on commit 8813ef7

Please sign in to comment.