pasta_curves-source/src/plonk/srs.rs

94 lines
2.7 KiB
Rust
Raw Normal View History

2020-08-22 20:15:39 +00:00
use super::{
circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit},
2020-08-22 20:15:39 +00:00
domain::EvaluationDomain,
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> {
fixed: Vec<Vec<F>>,
2020-08-22 20:15:39 +00:00
}
impl<F: Field> ConstraintSystem<F> for Assembly<F> {
fn assign_advice(
&mut self,
_: 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,
to: impl FnOnce() -> Result<F, Error>,
) -> Result<(), Error> {
*self
.fixed
.get_mut(wire.0)
.and_then(|v| v.get_mut(row))
.ok_or(Error::BoundsFailure)? = to()?;
Ok(())
}
2020-08-22 20:15:39 +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 {
fixed: vec![vec![C::Scalar::zero(); params.n as usize]; meta.num_fixed_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
let fixed_commitments = assembly
.fixed
.iter()
.map(|poly| params.commit_lagrange(poly, C::Scalar::one()).to_affine())
.collect();
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
let domain = EvaluationDomain::new(degree as u32, params.k);
2020-08-22 20:15:39 +00:00
let fixed_polys: Vec<_> = assembly
.fixed
.into_iter()
.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)
})
.collect();
2020-08-22 20:15:39 +00:00
Ok(SRS {
domain,
fixed_commitments,
fixed_polys,
fixed_cosets,
meta,
2020-08-22 20:15:39 +00:00
})
}
}