Skip to content

Commit

Permalink
Make progress on testing fe-be split
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Dec 28, 2023
1 parent 013e6d4 commit 50fb46a
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 94 deletions.
84 changes: 83 additions & 1 deletion halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::circuit::layouter::SyncDeps;
use crate::dev::metadata;
use crate::{
circuit::{Layouter, Region, Value},
poly::{LagrangeCoeff, Polynomial, Rotation},
poly::{batch_invert_assigned, LagrangeCoeff, Polynomial, Rotation},
};
use core::cmp::max;
use core::ops::{Add, Mul};
Expand Down Expand Up @@ -1677,6 +1677,88 @@ pub struct ConstraintSystemV2Backend<F: Field> {
// pub(crate) minimum_degree: Option<usize>,
}

/// TODO: Document. Frontend function
pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
k: u32,
circuit: &ConcreteCircuit,
compress_selectors: bool,
) -> Result<CompiledCircuitV2<F>, Error> {
let n = 2usize.pow(k);
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 n < cs.minimum_rows() {
return Err(Error::not_enough_rows_available(k));
}

let mut assembly = crate::plonk::keygen::Assembly {
k,
fixed: vec![Polynomial::new_empty(n, F::ZERO.into()); cs.num_fixed_columns],
permutation: permutation::keygen::Assembly::new(n, &cs.permutation),
selectors: vec![vec![false; n as usize]; cs.num_selectors],

Check warning on line 1702 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> halo2_proofs/src/plonk/circuit.rs:1702:37 | 1702 | selectors: vec![vec![false; n as usize]; cs.num_selectors], | ^^^^^^^^^^ help: try: `n` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `-W clippy::unnecessary-cast` implied by `-W clippy::all`

Check warning on line 1702 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> halo2_proofs/src/plonk/circuit.rs:1702:37 | 1702 | selectors: vec![vec![false; n as usize]; cs.num_selectors], | ^^^^^^^^^^ help: try: `n` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `-W clippy::unnecessary-cast` implied by `-W clippy::all`
usable_rows: 0..n as usize - (cs.blinding_factors() + 1),

Check warning on line 1703 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> halo2_proofs/src/plonk/circuit.rs:1703:25 | 1703 | usable_rows: 0..n as usize - (cs.blinding_factors() + 1), | ^^^^^^^^^^ help: try: `n` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

Check warning on line 1703 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> halo2_proofs/src/plonk/circuit.rs:1703:25 | 1703 | usable_rows: 0..n as usize - (cs.blinding_factors() + 1), | ^^^^^^^^^^ help: try: `n` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
_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| Polynomial::new_lagrange_from_vec(poly)),
);

let cs2 = ConstraintSystemV2Backend {
num_fixed_columns: cs.num_fixed_columns,
num_advice_columns: cs.num_advice_columns,
num_instance_columns: cs.num_instance_columns,
num_challenges: cs.num_challenges,
unblinded_advice_columns: cs.unblinded_advice_columns,
advice_column_phase: cs.advice_column_phase.iter().map(|p| p.0).collect(),
challenge_phase: cs.challenge_phase.iter().map(|p| p.0).collect(),
gates: cs
.gates
.iter()
.map(|g| GateV2Backend {
name: g.name.clone(),
constraint_names: g.constraint_names.clone(),
polys: g.polys.clone(),
})
.collect(),
permutation: cs.permutation,
lookups: cs.lookups,
shuffles: cs.shuffles,
general_column_annotations: cs.general_column_annotations,
};
let preprocessing = PreprocessingV2 {
permutation: assembly.permutation,
fixed,
};

Ok(CompiledCircuitV2 {
cs: cs2,
preprocessing,
})
}

impl<F: Field> ConstraintSystemV2Backend<F> {
/// Compute the degree of the constraint system (the maximum degree of all
/// constraints).
Expand Down
14 changes: 7 additions & 7 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ where

/// Assembly to be used in circuit synthesis.
#[derive(Debug)]
struct Assembly<F: Field> {
k: u32,
fixed: Vec<Polynomial<Assigned<F>, LagrangeCoeff>>,
permutation: permutation::keygen::Assembly,
selectors: Vec<Vec<bool>>,
pub(crate) struct Assembly<F: Field> {
pub(crate) k: u32,
pub(crate) fixed: Vec<Polynomial<Assigned<F>, LagrangeCoeff>>,
pub(crate) permutation: permutation::keygen::Assembly,
pub(crate) selectors: Vec<Vec<bool>>,
// A range of available rows for assignment and copies.
usable_rows: Range<usize>,
_marker: std::marker::PhantomData<F>,
pub(crate) usable_rows: Range<usize>,
pub(crate) _marker: std::marker::PhantomData<F>,
}

impl<F: Field> Assignment<F> for Assembly<F> {
Expand Down
25 changes: 23 additions & 2 deletions halo2_proofs/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,29 @@ impl Basis for ExtendedLagrangeCoeff {}
/// basis.
#[derive(Clone, Debug)]
pub struct Polynomial<F, B> {
values: Vec<F>,
_marker: PhantomData<B>,
pub(crate) values: Vec<F>,
pub(crate) _marker: PhantomData<B>,
}

impl<F: Clone, B> Polynomial<F, B> {
pub(crate) fn new_empty(size: usize, zero: F) -> Self {
Polynomial {
values: vec![zero; size],
_marker: PhantomData,
}
}
}

impl<F: Clone> Polynomial<F, LagrangeCoeff> {
/// Obtains a polynomial in Lagrange form when given a vector of Lagrange
/// coefficients of size `n`; panics if the provided vector is the wrong
/// length.
pub(crate) fn new_lagrange_from_vec(values: Vec<F>) -> Polynomial<F, LagrangeCoeff> {
Polynomial {
values,
_marker: PhantomData,
}
}
}

impl<F, B> Index<usize> for Polynomial<F, B> {
Expand Down
Loading

0 comments on commit 50fb46a

Please sign in to comment.