Skip to content

Commit

Permalink
feat: miniature DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed May 20, 2024
1 parent a859cbe commit 7d8355f
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 2 deletions.
43 changes: 41 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository ="https://github.com/thor314/ronkathon"
version ="0.1.0"

[dependencies]
petgraph ="0.6.5"
rand ="0.8.5"
num-bigint={ version="0.4.3", default-features=false }
ark-std ={ version="0.4.0", default-features=false }
Expand Down
112 changes: 112 additions & 0 deletions src/compiler/dsl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use super::*;

#[derive(Clone, Copy, Debug)]
pub enum Variable {
Public(u32),
Private(u32),
}

#[derive(Clone, Copy, Debug)]
pub enum Expression<ExpL, ExpR> {
Add(ExpL, ExpR),
Mul(ExpL, ExpR),
}

impl Add for Variable {
type Output = Expression<Variable, Variable>;

fn add(self, rhs: Self) -> Self::Output { Expression::Add(self, rhs) }
}

impl<ExpL, ExpR> Add<Expression<ExpL, ExpR>> for Variable {
type Output = Expression<Variable, Expression<ExpL, ExpR>>;

fn add(self, rhs: Expression<ExpL, ExpR>) -> Self::Output { Expression::Add(self, rhs) }
}

impl<ExpL, ExpR> Add<Variable> for Expression<ExpL, ExpR> {
type Output = Expression<Expression<ExpL, ExpR>, Variable>;

fn add(self, rhs: Variable) -> Self::Output { Expression::Add(self, rhs) }
}

impl<EXP1, EXP2, EXP3, EXP4> Add<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> {
type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>;

fn add(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Add(self, rhs) }
}

impl Mul for Variable {
type Output = Expression<Variable, Variable>;

fn mul(self, rhs: Self) -> Self::Output { Expression::Mul(self, rhs) }
}

impl<ExpL, ExpR> Mul<Expression<ExpL, ExpR>> for Variable {
type Output = Expression<Variable, Expression<ExpL, ExpR>>;

fn mul(self, rhs: Expression<ExpL, ExpR>) -> Self::Output { Expression::Mul(self, rhs) }
}

impl<ExpL, ExpR> Mul<Variable> for Expression<ExpL, ExpR> {
type Output = Expression<Expression<ExpL, ExpR>, Variable>;

fn mul(self, rhs: Variable) -> Self::Output { Expression::Mul(self, rhs) }
}

impl<EXP1, EXP2, EXP3, EXP4> Mul<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> {
type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>;

fn mul(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Mul(self, rhs) }
}

impl Display for Variable {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Variable::Public(val) => write!(f, "{}", val),
Variable::Private(val) => write!(f, "{}", val),
}
}
}

impl<EXPL: Display, EXPR: Display> Display for Expression<EXPL, EXPR> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Expression::Add(left, right) => write!(f, "({} + {})", left, right),
Expression::Mul(left, right) => write!(f, "({} * {})", left, right),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn writing_a_program() {
// Create two variables
let a = Variable::Public(7);
let b = Variable::Private(3);

// Create basic expressions with these variables
let add_ab = a + b;
println!("{}", add_ab);
assert_eq!(format!("{}", add_ab), "(7 + 3)");

let mul_ab = a * b;
println!("{}", mul_ab);
assert_eq!(format!("{}", mul_ab), "(7 * 3)");

// Check that we can add a variable to an expression
println!("{}", a + mul_ab);
assert_eq!(format!("{}", a + mul_ab), "(7 + (7 * 3))");

// Check that we can add an expression to a variable
println!("{}", mul_ab + a);
assert_eq!(format!("{}", mul_ab + a), "((7 * 3) + 7)");

// Check that we can add two expressions together
println!("{}", add_ab + mul_ab);
assert_eq!(format!("{}", add_ab + mul_ab), "((7 + 3) + (7 * 3))");
}
}
61 changes: 61 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! The compiler for `ronkathon` circuits.
// TODO: Remove this once the module is fleshed out.
#![allow(missing_docs)]
use petgraph::graph::{DiGraph, NodeIndex};

// TODO: Goal, allow someone to use this library to write rust to generate arithmetic circuits
// basically.
// ```
// use ronkathon::compiler::*;
//
// fn main() {
// let a: Input = Variable::public(7);
// let b: Input = Variable::private(3);
// let b: Expression = a * c;
// }
// ```
// So we can do things like `impl Add`, `impl Mul` for variables and make them into gates?
use super::*;

pub mod dsl;

pub struct Wire {
pub input: Connection,
pub output: Connection,
}

pub enum Connection {
Input(Publicity),
Output,
Internal,
}

pub enum Publicity {
Public(u32),
Private(u32),
}

pub enum Operation {
Add,
Mul,
Sub,
}

// TODO: Want to make it so gates own a reference for who they are connected to, and circuits own
// the gates. This way gates cannot be improperly connected to other gates.
pub struct Gate {
pub left: Wire,
pub right: Wire,
pub output: Wire,
pub op: Operation,
}

pub struct Circuit {
pub gates: DiGraph<Gate, Wire>,
}

impl Circuit {
pub fn new() -> Self { Self { gates: DiGraph::new() } }
}

1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![feature(generic_const_exprs)]
#![warn(missing_docs)]

pub mod compiler;
pub mod curve;
pub mod field;
pub mod kzg;
Expand Down

0 comments on commit 7d8355f

Please sign in to comment.