2020-08-22 20:15:39 +00:00
|
|
|
use super::{
|
2020-08-31 05:58:00 +00:00
|
|
|
circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit, Variable},
|
2020-08-22 20:15:39 +00:00
|
|
|
domain::EvaluationDomain,
|
2020-08-27 16:10:55 +00:00
|
|
|
Error, SRS,
|
2020-08-22 20:15:39 +00:00
|
|
|
};
|
|
|
|
|
use crate::arithmetic::{Curve, CurveAffine, Field};
|
|
|
|
|
use crate::polycommit::Params;
|
|
|
|
|
|
|
|
|
|
impl<C: CurveAffine> SRS<C> {
|
|
|
|
|
/// This generates a structured reference string for the provided `circuit`
|
|
|
|
|
/// and `params`.
|
|
|
|
|
pub fn generate<ConcreteCircuit: Circuit<C::Scalar>>(
|
|
|
|
|
params: &Params<C>,
|
|
|
|
|
circuit: &ConcreteCircuit,
|
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
|
struct Assembly<F: Field> {
|
2020-08-22 22:10:27 +00:00
|
|
|
fixed: Vec<Vec<F>>,
|
2020-08-31 05:58:00 +00:00
|
|
|
copy: Vec<Vec<Variable>>,
|
2020-08-22 20:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F: Field> ConstraintSystem<F> for Assembly<F> {
|
2020-08-23 19:26:04 +00:00
|
|
|
fn assign_advice(
|
2020-08-22 22:10:27 +00:00
|
|
|
&mut self,
|
2020-08-23 19:26:04 +00:00
|
|
|
_: AdviceWire,
|
|
|
|
|
_: usize,
|
|
|
|
|
_: impl FnOnce() -> Result<F, Error>,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
// We only care about fixed wires here
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn assign_fixed(
|
|
|
|
|
&mut self,
|
|
|
|
|
wire: FixedWire,
|
|
|
|
|
row: usize,
|
2020-08-22 22:10:27 +00:00
|
|
|
to: impl FnOnce() -> Result<F, Error>,
|
|
|
|
|
) -> Result<(), Error> {
|
2020-08-23 19:26:04 +00:00
|
|
|
*self
|
|
|
|
|
.fixed
|
|
|
|
|
.get_mut(wire.0)
|
|
|
|
|
.and_then(|v| v.get_mut(row))
|
|
|
|
|
.ok_or(Error::BoundsFailure)? = to()?;
|
|
|
|
|
|
2020-08-22 22:10:27 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-08-31 05:58:00 +00:00
|
|
|
|
|
|
|
|
fn assign_copy(&mut self, left: Variable, right: Variable) -> Result<(), Error> {
|
|
|
|
|
*self
|
|
|
|
|
.copy
|
|
|
|
|
.get_mut((left.0).0)
|
|
|
|
|
.and_then(|v| v.get_mut(left.1))
|
|
|
|
|
.ok_or(Error::BoundsFailure)? = right;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-08-22 20:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 22:10:27 +00:00
|
|
|
let mut meta = MetaCircuit::default();
|
|
|
|
|
let config = ConcreteCircuit::configure(&mut meta);
|
|
|
|
|
|
2020-08-22 20:15:39 +00:00
|
|
|
let mut assembly: Assembly<C::Scalar> = Assembly {
|
2020-08-22 22:10:27 +00:00
|
|
|
fixed: vec![vec![C::Scalar::zero(); params.n as usize]; meta.num_fixed_wires],
|
2020-08-31 05:58:00 +00:00
|
|
|
copy: vec![
|
|
|
|
|
vec![Variable::new(AdviceWire(0), 0); params.n as usize];
|
|
|
|
|
meta.num_advice_wires
|
|
|
|
|
],
|
2020-08-22 20:15:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Synthesize the circuit to obtain SRS
|
2020-08-22 21:09:47 +00:00
|
|
|
circuit.synthesize(&mut assembly, config)?;
|
2020-08-22 20:15:39 +00:00
|
|
|
|
2020-08-22 22:10:27 +00:00
|
|
|
let fixed_commitments = assembly
|
|
|
|
|
.fixed
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|poly| params.commit_lagrange(poly, C::Scalar::one()).to_affine())
|
|
|
|
|
.collect();
|
|
|
|
|
|
2020-08-27 16:10:55 +00:00
|
|
|
let mut degree = 1;
|
|
|
|
|
for poly in meta.gates.iter() {
|
|
|
|
|
degree = std::cmp::max(degree, poly.degree());
|
|
|
|
|
}
|
2020-08-22 20:15:39 +00:00
|
|
|
|
2020-08-27 16:10:55 +00:00
|
|
|
let domain = EvaluationDomain::new(degree as u32, params.k);
|
2020-08-22 20:15:39 +00:00
|
|
|
|
2020-08-27 16:10:55 +00:00
|
|
|
let fixed_polys: Vec<_> = assembly
|
2020-08-22 22:10:27 +00:00
|
|
|
.fixed
|
|
|
|
|
.into_iter()
|
2020-08-27 16:10:55 +00:00
|
|
|
.map(|poly| domain.obtain_poly(poly))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let fixed_cosets = meta
|
|
|
|
|
.fixed_queries
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&(wire, at)| {
|
|
|
|
|
let poly = fixed_polys[wire.0].clone();
|
|
|
|
|
domain.obtain_coset(poly, at)
|
2020-08-24 19:50:52 +00:00
|
|
|
})
|
2020-08-22 22:10:27 +00:00
|
|
|
.collect();
|
|
|
|
|
|
2020-08-22 20:15:39 +00:00
|
|
|
Ok(SRS {
|
|
|
|
|
domain,
|
2020-08-22 22:10:27 +00:00
|
|
|
fixed_commitments,
|
|
|
|
|
fixed_polys,
|
2020-08-27 16:10:55 +00:00
|
|
|
fixed_cosets,
|
2020-08-22 22:10:27 +00:00
|
|
|
meta,
|
2020-08-22 20:15:39 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|