mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-08 20:40:31 +00:00
Implementation of gate/query API
This commit is contained in:
parent
c20f3fdf1a
commit
36f9e87056
2 changed files with 81 additions and 11 deletions
17
src/plonk.rs
17
src/plonk.rs
|
|
@ -45,7 +45,7 @@ pub struct SRS<C: CurveAffine> {
|
||||||
|
|
||||||
fixed_commitments: Vec<C>,
|
fixed_commitments: Vec<C>,
|
||||||
fixed_polys: Vec<(Vec<C::Scalar>, Vec<C::Scalar>)>,
|
fixed_polys: Vec<(Vec<C::Scalar>, Vec<C::Scalar>)>,
|
||||||
meta: MetaCircuit,
|
meta: MetaCircuit<C::Scalar>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is an object which represents a (Turbo)PLONK proof.
|
/// This is an object which represents a (Turbo)PLONK proof.
|
||||||
|
|
@ -128,7 +128,7 @@ fn test_proving() {
|
||||||
impl<F: Field> Circuit<F> for MyCircuit<F> {
|
impl<F: Field> Circuit<F> for MyCircuit<F> {
|
||||||
type Config = MyConfig;
|
type Config = MyConfig;
|
||||||
|
|
||||||
fn configure(meta: &mut MetaCircuit) -> MyConfig {
|
fn configure(meta: &mut MetaCircuit<F>) -> MyConfig {
|
||||||
let a = meta.advice_wire();
|
let a = meta.advice_wire();
|
||||||
let b = meta.advice_wire();
|
let b = meta.advice_wire();
|
||||||
let c = meta.advice_wire();
|
let c = meta.advice_wire();
|
||||||
|
|
@ -138,6 +138,19 @@ fn test_proving() {
|
||||||
let sc = meta.fixed_wire();
|
let sc = meta.fixed_wire();
|
||||||
let sm = meta.fixed_wire();
|
let sm = meta.fixed_wire();
|
||||||
|
|
||||||
|
meta.create_gate(|meta| {
|
||||||
|
let a = meta.query_advice(a, 0);
|
||||||
|
let b = meta.query_advice(b, 0);
|
||||||
|
let c = meta.query_advice(c, 0);
|
||||||
|
|
||||||
|
let sa = meta.query_fixed(sa, 0);
|
||||||
|
let sb = meta.query_fixed(sb, 0);
|
||||||
|
let sc = meta.query_fixed(sc, 0);
|
||||||
|
let sm = meta.query_fixed(sm, 0);
|
||||||
|
|
||||||
|
a.clone() * sa + b.clone() * sb + a * b * sm + (c * sc * (-F::one()))
|
||||||
|
});
|
||||||
|
|
||||||
MyConfig {
|
MyConfig {
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use core::cmp::max;
|
use core::cmp::max;
|
||||||
use core::ops::{Add, Mul};
|
use core::ops::{Add, Mul};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::Error;
|
use super::Error;
|
||||||
use crate::arithmetic::Field;
|
use crate::arithmetic::Field;
|
||||||
|
|
@ -19,11 +20,11 @@ pub enum Wire {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This represents a wire which has a fixed (permanent) value
|
/// This represents a wire which has a fixed (permanent) value
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct FixedWire(pub usize);
|
pub struct FixedWire(pub usize);
|
||||||
|
|
||||||
/// This represents a wire which has a witness-specific value
|
/// This represents a wire which has a witness-specific value
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct AdviceWire(pub usize);
|
pub struct AdviceWire(pub usize);
|
||||||
|
|
||||||
/// Represents a pointer to a value in the constraint system.
|
/// Represents a pointer to a value in the constraint system.
|
||||||
|
|
@ -94,7 +95,7 @@ pub trait Circuit<F: Field> {
|
||||||
|
|
||||||
/// The circuit is given an opportunity to describe the exact gate
|
/// The circuit is given an opportunity to describe the exact gate
|
||||||
/// arrangement, wire arrangement, etc.
|
/// arrangement, wire arrangement, etc.
|
||||||
fn configure(meta: &mut MetaCircuit) -> Self::Config;
|
fn configure(meta: &mut MetaCircuit<F>) -> Self::Config;
|
||||||
|
|
||||||
/// Given the provided `cs`, synthesize the circuit. The concrete type of
|
/// Given the provided `cs`, synthesize the circuit. The concrete type of
|
||||||
/// the caller will be different depending on the context, and they may or
|
/// the caller will be different depending on the context, and they may or
|
||||||
|
|
@ -121,6 +122,36 @@ pub enum Polynomial<F> {
|
||||||
Scaled(Box<Polynomial<F>>, F),
|
Scaled(Box<Polynomial<F>>, F),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<F: Field> Polynomial<F> {
|
||||||
|
fn evaluate<T>(
|
||||||
|
&self,
|
||||||
|
fixed_wire: &impl Fn(FixedWire, isize) -> T,
|
||||||
|
advice_wire: &impl Fn(AdviceWire, isize) -> T,
|
||||||
|
sum: &impl Fn(T, T) -> T,
|
||||||
|
product: &impl Fn(T, T) -> T,
|
||||||
|
scaled: &impl Fn(T, F) -> T,
|
||||||
|
) -> T {
|
||||||
|
match self {
|
||||||
|
Polynomial::Fixed(a, location) => fixed_wire(*a, *location),
|
||||||
|
Polynomial::Advice(a, location) => advice_wire(*a, *location),
|
||||||
|
Polynomial::Sum(a, b) => {
|
||||||
|
let a = a.evaluate(fixed_wire, advice_wire, sum, product, scaled);
|
||||||
|
let b = b.evaluate(fixed_wire, advice_wire, sum, product, scaled);
|
||||||
|
sum(a, b)
|
||||||
|
}
|
||||||
|
Polynomial::Product(a, b) => {
|
||||||
|
let a = a.evaluate(fixed_wire, advice_wire, sum, product, scaled);
|
||||||
|
let b = b.evaluate(fixed_wire, advice_wire, sum, product, scaled);
|
||||||
|
product(a, b)
|
||||||
|
}
|
||||||
|
Polynomial::Scaled(a, f) => {
|
||||||
|
let a = a.evaluate(fixed_wire, advice_wire, sum, product, scaled);
|
||||||
|
scaled(a, *f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<F: Field> Polynomial<F> {
|
impl<F: Field> Polynomial<F> {
|
||||||
fn degree(&self) -> usize {
|
fn degree(&self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
|
|
@ -157,25 +188,51 @@ impl<F> Mul<F> for Polynomial<F> {
|
||||||
/// This is a description of the circuit environment, such as the gate, wire and
|
/// This is a description of the circuit environment, such as the gate, wire and
|
||||||
/// permutation arrangements.
|
/// permutation arrangements.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MetaCircuit {
|
pub struct MetaCircuit<F> {
|
||||||
pub(crate) num_fixed_wires: usize,
|
pub(crate) num_fixed_wires: usize,
|
||||||
pub(crate) num_advice_wires: usize,
|
pub(crate) num_advice_wires: usize,
|
||||||
// permutations: Vec<Vec<Wire>>,
|
// permutations: Vec<Vec<Wire>>,
|
||||||
// gates: Vec<Polynomial>,
|
gates: Vec<Polynomial<F>>,
|
||||||
// queries: HashSet<(Wire, usize)>,
|
advice_queries: HashMap<(AdviceWire, isize), usize>,
|
||||||
|
fixed_queries: HashMap<(FixedWire, isize), usize>,
|
||||||
// num_queries: usize,
|
// num_queries: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MetaCircuit {
|
impl<F: Field> Default for MetaCircuit<F> {
|
||||||
fn default() -> MetaCircuit {
|
fn default() -> MetaCircuit<F> {
|
||||||
MetaCircuit {
|
MetaCircuit {
|
||||||
num_fixed_wires: 0,
|
num_fixed_wires: 0,
|
||||||
num_advice_wires: 0,
|
num_advice_wires: 0,
|
||||||
|
gates: vec![],
|
||||||
|
fixed_queries: HashMap::new(),
|
||||||
|
advice_queries: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MetaCircuit {
|
impl<F: Field> MetaCircuit<F> {
|
||||||
|
/// Query a fixed wire at a relative position
|
||||||
|
pub fn query_fixed(&mut self, wire: FixedWire, at: isize) -> Polynomial<F> {
|
||||||
|
let len = self.fixed_queries.len();
|
||||||
|
self.fixed_queries.entry((wire, at)).or_insert_with(|| len);
|
||||||
|
|
||||||
|
Polynomial::Fixed(wire, at)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Query an advice wire at a relative position
|
||||||
|
pub fn query_advice(&mut self, wire: AdviceWire, at: isize) -> Polynomial<F> {
|
||||||
|
let len = self.advice_queries.len();
|
||||||
|
self.advice_queries.entry((wire, at)).or_insert_with(|| len);
|
||||||
|
|
||||||
|
Polynomial::Advice(wire, at)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new gate
|
||||||
|
pub fn create_gate(&mut self, f: impl FnOnce(&mut Self) -> Polynomial<F>) {
|
||||||
|
let poly = f(self);
|
||||||
|
self.gates.push(poly);
|
||||||
|
}
|
||||||
|
|
||||||
/// Allocate a new fixed wire
|
/// Allocate a new fixed wire
|
||||||
pub fn fixed_wire(&mut self) -> FixedWire {
|
pub fn fixed_wire(&mut self) -> FixedWire {
|
||||||
let tmp = FixedWire(self.num_fixed_wires);
|
let tmp = FixedWire(self.num_fixed_wires);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue