2020-11-13 00:08:08 +00:00
|
|
|
use ff::Field;
|
|
|
|
|
|
2020-09-29 14:25:04 +00:00
|
|
|
use super::{
|
2020-11-06 03:25:50 +00:00
|
|
|
circuit::{Advice, Assignment, Circuit, Column, ConstraintSystem, Fixed},
|
2020-12-01 02:09:03 +00:00
|
|
|
permutation, Error, ProvingKey, VerifyingKey,
|
2020-09-29 14:25:04 +00:00
|
|
|
};
|
2020-12-01 02:09:03 +00:00
|
|
|
use crate::arithmetic::{Curve, CurveAffine};
|
2020-09-29 14:25:04 +00:00
|
|
|
use crate::poly::{
|
|
|
|
|
commitment::{Blind, Params},
|
|
|
|
|
EvaluationDomain, LagrangeCoeff, Polynomial, Rotation,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Generate a `ProvingKey` from an instance of `Circuit`.
|
|
|
|
|
pub fn keygen<C, ConcreteCircuit>(
|
|
|
|
|
params: &Params<C>,
|
|
|
|
|
circuit: &ConcreteCircuit,
|
|
|
|
|
) -> Result<ProvingKey<C>, Error>
|
|
|
|
|
where
|
|
|
|
|
C: CurveAffine,
|
|
|
|
|
ConcreteCircuit: Circuit<C::Scalar>,
|
|
|
|
|
{
|
|
|
|
|
struct Assembly<F: Field> {
|
|
|
|
|
fixed: Vec<Polynomial<F, LagrangeCoeff>>,
|
2020-12-01 02:09:03 +00:00
|
|
|
permutations: permutation::keygen::Assembly,
|
2020-09-29 14:25:04 +00:00
|
|
|
_marker: std::marker::PhantomData<F>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F: Field> Assignment<F> for Assembly<F> {
|
|
|
|
|
fn assign_advice(
|
|
|
|
|
&mut self,
|
2020-11-06 03:25:50 +00:00
|
|
|
_: Column<Advice>,
|
2020-09-29 14:25:04 +00:00
|
|
|
_: usize,
|
|
|
|
|
_: impl FnOnce() -> Result<F, Error>,
|
|
|
|
|
) -> Result<(), Error> {
|
2020-11-06 03:13:54 +00:00
|
|
|
// We only care about fixed columns here
|
2020-09-29 14:25:04 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn assign_fixed(
|
|
|
|
|
&mut self,
|
2020-11-06 03:25:50 +00:00
|
|
|
column: Column<Fixed>,
|
2020-09-29 14:25:04 +00:00
|
|
|
row: usize,
|
|
|
|
|
to: impl FnOnce() -> Result<F, Error>,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
*self
|
|
|
|
|
.fixed
|
2020-11-09 16:45:52 +00:00
|
|
|
.get_mut(column.index())
|
2020-09-29 14:25:04 +00:00
|
|
|
.and_then(|v| v.get_mut(row))
|
|
|
|
|
.ok_or(Error::BoundsFailure)? = to()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn copy(
|
|
|
|
|
&mut self,
|
|
|
|
|
permutation: usize,
|
2020-11-06 03:13:54 +00:00
|
|
|
left_column: usize,
|
2020-09-29 14:25:04 +00:00
|
|
|
left_row: usize,
|
2020-11-06 03:13:54 +00:00
|
|
|
right_column: usize,
|
2020-09-29 14:25:04 +00:00
|
|
|
right_row: usize,
|
|
|
|
|
) -> Result<(), Error> {
|
2020-12-01 02:09:03 +00:00
|
|
|
self.permutations
|
|
|
|
|
.copy(permutation, left_column, left_row, right_column, right_row)
|
2020-09-29 14:25:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut cs = ConstraintSystem::default();
|
|
|
|
|
let config = ConcreteCircuit::configure(&mut cs);
|
|
|
|
|
|
|
|
|
|
// The permutation argument will serve alongside the gates, so must be
|
|
|
|
|
// accounted for.
|
2020-12-01 02:09:03 +00:00
|
|
|
let mut degree = cs
|
|
|
|
|
.permutations
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|p| p.required_degree())
|
|
|
|
|
.max()
|
|
|
|
|
.unwrap_or(1);
|
2020-09-29 14:25:04 +00:00
|
|
|
|
|
|
|
|
// Account for each gate to ensure our quotient polynomial is the
|
|
|
|
|
// correct degree and that our extended domain is the right size.
|
|
|
|
|
for poly in cs.gates.iter() {
|
|
|
|
|
degree = std::cmp::max(degree, poly.degree());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let domain = EvaluationDomain::new(degree as u32, params.k);
|
|
|
|
|
|
|
|
|
|
let mut assembly: Assembly<C::Scalar> = Assembly {
|
2020-11-06 03:13:54 +00:00
|
|
|
fixed: vec![domain.empty_lagrange(); cs.num_fixed_columns],
|
2020-12-01 02:09:03 +00:00
|
|
|
permutations: permutation::keygen::Assembly::new(params, &cs),
|
2020-09-29 14:25:04 +00:00
|
|
|
_marker: std::marker::PhantomData,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Synthesize the circuit to obtain SRS
|
|
|
|
|
circuit.synthesize(&mut assembly, config)?;
|
|
|
|
|
|
2020-12-01 02:09:03 +00:00
|
|
|
let (permutation_pks, permutation_vks) = assembly.permutations.build_keys(params, &cs, &domain);
|
2020-09-29 14:25:04 +00:00
|
|
|
|
|
|
|
|
let fixed_commitments = assembly
|
|
|
|
|
.fixed
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|poly| params.commit_lagrange(poly, Blind::default()).to_affine())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let fixed_polys: Vec<_> = assembly
|
|
|
|
|
.fixed
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|poly| domain.lagrange_to_coeff(poly))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let fixed_cosets = cs
|
|
|
|
|
.fixed_queries
|
|
|
|
|
.iter()
|
2020-11-06 03:13:54 +00:00
|
|
|
.map(|&(column, at)| {
|
2020-11-09 16:45:52 +00:00
|
|
|
let poly = fixed_polys[column.index()].clone();
|
2020-09-29 14:25:04 +00:00
|
|
|
domain.coeff_to_extended(poly, at)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// Compute l_0(X)
|
|
|
|
|
// TODO: this can be done more efficiently
|
|
|
|
|
let mut l0 = domain.empty_lagrange();
|
|
|
|
|
l0[0] = C::Scalar::one();
|
|
|
|
|
let l0 = domain.lagrange_to_coeff(l0);
|
|
|
|
|
let l0 = domain.coeff_to_extended(l0, Rotation::default());
|
|
|
|
|
|
|
|
|
|
Ok(ProvingKey {
|
|
|
|
|
vk: VerifyingKey {
|
|
|
|
|
domain,
|
|
|
|
|
fixed_commitments,
|
2020-12-01 02:09:03 +00:00
|
|
|
permutations: permutation_vks,
|
2020-09-29 14:25:04 +00:00
|
|
|
cs,
|
|
|
|
|
},
|
|
|
|
|
l0,
|
|
|
|
|
fixed_polys,
|
|
|
|
|
fixed_cosets,
|
2020-12-01 02:09:03 +00:00
|
|
|
permutations: permutation_pks,
|
2020-09-29 14:25:04 +00:00
|
|
|
})
|
|
|
|
|
}
|