Skip to content
This repository has been archived by the owner on Dec 9, 2021. It is now read-only.

Commit

Permalink
Overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubarov committed Oct 21, 2020
1 parent 6ee0f6c commit c37807d
Show file tree
Hide file tree
Showing 12 changed files with 637 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "plonky"
description = "Recursive SNARKs based on Plonk and Halo"
version = "0.1.0"
authors = ["Daniel Lubarov"]
authors = ["Daniel Lubarov <[email protected]>", "William Borgeaud <[email protected]>"]
readme = "README.md"
license = "MIT OR Apache-2.0"
repository = "https://github.com/mir-protocol/plonky"
Expand Down
56 changes: 56 additions & 0 deletions src/gates2/arithmetic_gate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::marker::PhantomData;
use std::rc::Rc;

use crate::{CircuitBuilder2, ConstraintPolynomial, DeterministicGate, Field, Gate2, GateInstance, Target2};

pub const ID: &'static str = "ARITHMETIC";
pub const CONST_PRODUCT_WEIGHT: usize = 0;
pub const CONST_ADDEND_WEIGHT: usize = 1;
pub const WIRE_MULTIPLICAND_0: usize = 0;
pub const WIRE_MULTIPLICAND_1: usize = 1;
pub const WIRE_ADDEND: usize = 2;
pub const WIRE_OUTPUT: usize = 3;

/// A gate which can be configured to perform various arithmetic. In particular, it computes
///
/// ```text
/// output := const_product_weight * multiplicand_0 * multiplicand_1
/// + const_addend_weight * addend
/// ```
struct ArithmeticGate<F: Field> {
_phantom: PhantomData<F>
}

impl<F: Field> DeterministicGate<F> for ArithmeticGate<F> {
fn id(&self) -> String {
"ArithmeticGate".into()
}

fn outputs(&self) -> Vec<(usize, ConstraintPolynomial<F>)> {
let const_0 = ConstraintPolynomial::local_constant(CONST_PRODUCT_WEIGHT);
let const_1 = ConstraintPolynomial::local_constant(CONST_ADDEND_WEIGHT);
let multiplicand_0 = ConstraintPolynomial::local_wire_value(WIRE_MULTIPLICAND_0);
let multiplicand_1 = ConstraintPolynomial::local_wire_value(WIRE_MULTIPLICAND_1);
let addend = ConstraintPolynomial::local_wire_value(WIRE_ADDEND);

let out = const_0 * multiplicand_0 * &multiplicand_1 + const_1 * &addend;
vec![(WIRE_OUTPUT, out)]
}
}

fn gate_type<F: Field>() -> Rc<Gate2<F>> {
todo!()
}

fn add<F: Field>(builder: &mut CircuitBuilder2<F>, x: Target2<F>, y: Target2<F>) -> Target2<F> {
let gate_type = gate_type();
let constants = vec![F::ONE, F::ONE];
let gate = builder.add_gate(GateInstance { gate_type, constants });
let one = builder.one();

builder.copy(x, Target2::wire(gate, WIRE_MULTIPLICAND_0));
builder.copy(one, Target2::wire(gate, WIRE_MULTIPLICAND_1));
builder.copy(y, Target2::wire(gate, WIRE_ADDEND));

Target2::wire(gate, WIRE_OUTPUT)
}
65 changes: 65 additions & 0 deletions src/gates2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::rc::Rc;

use crate::{Field, ConstraintPolynomial, WitnessGenerator2, Target2, PartialWitness2};

pub mod arithmetic_gate;

pub trait Gate2<F: Field> {
fn id(&self) -> String;

fn constraints(&self) -> Vec<ConstraintPolynomial<F>>;

fn generators(&self, index: usize) -> Vec<Box<dyn WitnessGenerator2<F>>>;
}

/// A deterministic gate. Each entry in `outputs()` describes how that output is evaluated; this is
/// used to create both the constraint set and the generator set.
pub trait DeterministicGate<F: Field> {
fn id(&self) -> String;

fn outputs(&self) -> Vec<(usize, ConstraintPolynomial<F>)>;
}

impl<F: Field> Gate2<F> for dyn DeterministicGate<F> {
fn id(&self) -> String {
self.id()
}

fn constraints(&self) -> Vec<ConstraintPolynomial<F>> {
self.outputs().into_iter()
.map(|(i, out)| out - ConstraintPolynomial::local_wire_value(i))
.collect()
}

fn generators(&self, index: usize) -> Vec<Box<dyn WitnessGenerator2<F>>> {
struct OutputGenerator<F: Field> {
i: usize,
out: ConstraintPolynomial<F>,
}

impl<F: Field> WitnessGenerator2<F> for OutputGenerator<F> {
fn watch_list(&self) -> Vec<Target2<F>> {
todo!()
}

fn run(&mut self, witness: &PartialWitness2<F>) -> (PartialWitness2<F>, bool) {
// ConstraintPolynomial::evaluate_all(&self.outputs)
todo!()
}
}

self.outputs()
.into_iter()
.map(|(i, out)| {
let b: Box::<dyn WitnessGenerator2<F> + 'static> =
Box::new(OutputGenerator { i, out });
b
})
.collect()
}
}

pub struct GateInstance<F: Field> {
gate_type: Rc<dyn Gate2<F>>,
constants: Vec<F>,
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ pub use curve::*;
pub use fft::*;
pub use field::*;
pub use gates::*;
pub use gates2::*;
pub use hash_to_curve::*;
pub use mds::*;
pub use partition::*;
pub use plonk::*;
pub use plonk2::*;
pub use plonk_proof::*;
pub use plonk_recursion::*;
pub use poly_commit::*;
Expand All @@ -50,11 +52,13 @@ mod curve;
mod fft;
mod field;
mod gates;
mod gates2;
pub mod halo;
mod hash_to_curve;
mod mds;
mod partition;
mod plonk;
mod plonk2;
pub mod plonk_challenger;
mod plonk_proof;
mod plonk_recursion;
Expand Down
Loading

0 comments on commit c37807d

Please sign in to comment.