This repository has been archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
637 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.