Skip to content

Commit

Permalink
Merge branch 'main' into feat/dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed May 15, 2024
2 parents 69cc35a + d580c83 commit c654e91
Show file tree
Hide file tree
Showing 22 changed files with 1,877 additions and 1,431 deletions.
26 changes: 3 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ repository ="https://github.com/thor314/ronkathon"
version ="0.1.0"

[dependencies]
rand ="0.8"
serde ={ version="1.0", default-features=false, features=["derive"] }
num-bigint={ version="0.4.5", default-features=false }
rand ="0.8.5"
num-bigint={ version="0.4.3", default-features=false }
ark-std ={ version="0.4.0", default-features=false }
ark-bn254 ="0.4.0"
ark-poly ="0.4.0"
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/errors.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{error::Error, fmt::Display};

use crate::field::gf_101::GF101;
use crate::field::prime::PlutoScalarField;

/// Errors from parsing the DSL
#[derive(Debug)]
pub enum ProgramError<'a> {
PublicAssignmentInvalidStatement,
CircuitEvaluationOutputMismatch(GF101, GF101),
CircuitEvaluationOutputMismatch(PlutoScalarField, PlutoScalarField),
ParserError(ParserError<'a>),
}

Expand Down
78 changes: 39 additions & 39 deletions src/compiler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ use super::{
errors::ParserError,
utils::{get_product_key, is_valid_var_name},
};
use crate::field::{gf_101::GF101, FiniteField};
use crate::field::{prime::PlutoScalarField, FiniteField};

/// Fan-in 2 Gate representing a constraint in the computation.
/// Each constraint satisfies PLONK's arithmetic equation: `a(X)QL(X) + b(X)QR(X) + a(X)b(X)QM(X) +
/// o(X)QO(X) + QC(X) = 0`.
pub struct Gate {
/// left wire value
pub l: GF101,
pub l: PlutoScalarField,
/// right wire value
pub r: GF101,
pub r: PlutoScalarField,
/// output wire, represented as `$output_coeffs` in wire coefficients
pub o: GF101,
pub o: PlutoScalarField,
/// multiplication wire
pub m: GF101,
pub m: PlutoScalarField,
/// constant wire, represented as `$constant` in coefficients
pub c: GF101,
pub c: PlutoScalarField,
}

/// Values of wires with coefficients of each wire name
Expand All @@ -59,48 +59,48 @@ pub struct WireCoeffs<'a> {
}

impl<'a> WireCoeffs<'a> {
fn l(&self) -> GF101 {
fn l(&self) -> PlutoScalarField {
match self.wires[0] {
Some(wire) => match self.coeffs.get(wire) {
Some(val) => -GF101::from(*val),
None => GF101::ZERO,
Some(val) => -PlutoScalarField::from(*val),
None => PlutoScalarField::ZERO,
},
None => GF101::ZERO,
None => PlutoScalarField::ZERO,
}
}

fn r(&self) -> GF101 {
fn r(&self) -> PlutoScalarField {
if self.wires[0].is_some() && self.wires[1].is_some() && self.wires[0] != self.wires[1] {
match self.coeffs.get(self.wires[1].unwrap()) {
Some(val) => -GF101::from(*val),
None => GF101::ZERO,
Some(val) => -PlutoScalarField::from(*val),
None => PlutoScalarField::ZERO,
}
} else {
GF101::ZERO
PlutoScalarField::ZERO
}
}

fn o(&self) -> GF101 {
fn o(&self) -> PlutoScalarField {
match self.coeffs.get("$output_coeffs") {
Some(val) => GF101::from(*val),
None => GF101::ONE,
Some(val) => PlutoScalarField::from(*val),
None => PlutoScalarField::ONE,
}
}

fn c(&self) -> GF101 {
fn c(&self) -> PlutoScalarField {
match self.coeffs.get("$constant") {
Some(val) => -GF101::from(*val),
None => GF101::ZERO,
Some(val) => -PlutoScalarField::from(*val),
None => PlutoScalarField::ZERO,
}
}

fn m(&self) -> GF101 {
fn m(&self) -> PlutoScalarField {
match (self.wires[0], self.wires[1]) {
(Some(a), Some(b)) => match self.coeffs.get(&get_product_key(a, b)) {
Some(val) => -GF101::from(*val),
None => GF101::ZERO,
Some(val) => -PlutoScalarField::from(*val),
None => PlutoScalarField::ZERO,
},
_ => GF101::ZERO,
_ => PlutoScalarField::ZERO,
}
}

Expand Down Expand Up @@ -286,22 +286,22 @@ mod tests {
]),
};
let gate = wire_values.gate();
assert_eq!(gate.l, -GF101::from(-1));
assert_eq!(gate.r, GF101::ZERO);
assert_eq!(gate.m, GF101::ZERO);
assert_eq!(gate.o, GF101::from(2));
assert_eq!(gate.c, -GF101::from(9));
assert_eq!(gate.l, -PlutoScalarField::from(-1));
assert_eq!(gate.r, PlutoScalarField::ZERO);
assert_eq!(gate.m, PlutoScalarField::ZERO);
assert_eq!(gate.o, PlutoScalarField::from(2));
assert_eq!(gate.c, -PlutoScalarField::from(9));

let wire_values = WireCoeffs {
wires: vec![Some("a"), Some("b"), Some("c")],
coeffs: HashMap::from([(String::from("b"), -1), (String::from("a*b"), -9)]),
};
let gate = wire_values.gate();
assert_eq!(gate.l, -GF101::ZERO);
assert_eq!(gate.r, -GF101::from(-1));
assert_eq!(gate.m, -GF101::from(-9));
assert_eq!(gate.o, GF101::ONE);
assert_eq!(gate.c, -GF101::ZERO);
assert_eq!(gate.l, -PlutoScalarField::ZERO);
assert_eq!(gate.r, -PlutoScalarField::from(-1));
assert_eq!(gate.m, -PlutoScalarField::from(-9));
assert_eq!(gate.o, PlutoScalarField::ONE);
assert_eq!(gate.c, -PlutoScalarField::ZERO);

let wire_values = WireCoeffs {
wires: vec![Some("a"), None, None],
Expand All @@ -312,11 +312,11 @@ mod tests {
]),
};
let gate = wire_values.gate();
assert_eq!(gate.l, GF101::ONE);
assert_eq!(gate.r, GF101::ZERO);
assert_eq!(gate.m, GF101::ZERO);
assert_eq!(gate.o, GF101::ZERO);
assert_eq!(gate.c, GF101::ZERO);
assert_eq!(gate.l, PlutoScalarField::ONE);
assert_eq!(gate.r, PlutoScalarField::ZERO);
assert_eq!(gate.m, PlutoScalarField::ZERO);
assert_eq!(gate.o, PlutoScalarField::ZERO);
assert_eq!(gate.c, PlutoScalarField::ZERO);
}

#[rstest]
Expand Down
Loading

0 comments on commit c654e91

Please sign in to comment.