Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) feat: compiler #71

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/compiler/dsl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use super::*;

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

#[derive(Clone, Copy, Debug)]
pub struct Variable {
pub label: usize,
}

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

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

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

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

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

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

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

impl<ExpL1, ExpR1, ExpL2, ExpR2> Add<Expression<ExpL2, ExpR2>> for Expression<ExpL1, ExpR1> {
type Output = Expression<Expression<ExpL1, ExpR1>, Expression<ExpL2, ExpR2>>;

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

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

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

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

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

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

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

impl<ExpL1, ExpR1, ExpL2, ExpR2> Mul<Expression<ExpL2, ExpR2>> for Expression<ExpL1, ExpR1> {
type Output = Expression<Expression<ExpL1, ExpR1>, Expression<ExpL2, ExpR2>>;

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

impl Display for Input {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Input::Variable(val) => write!(f, "x_{}", val.label),
Input::Public(val) => write!(f, "{}", val),
Input::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 = Input::Public(7);
let b = Input::Private(3);
let x = Input::Variable(Variable { label: 0 });

// 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))");

// Check that we can multiply an expression by a variable
println!("{}", mul_ab * x);
assert_eq!(format!("{}", mul_ab * x), "((7 * 3) * x_0)");
}
}
57 changes: 57 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! The compiler for `ronkathon` circuits.

// TODO: Remove this once the module is fleshed out.
#![allow(missing_docs)]

// 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 std::array;

// Above seems done. Now we need to have a way to unravel a collection of expressions into a
// circuit that may have the same inputs and outputs as the expressions. Inputs are going to be
// the terminal variables found by fully unravelling expressions and they should be named. The
// fully ravelled expressions are the outputs, and they can also be named
use super::*;

pub mod dsl;
pub use dsl::*;

#[derive(Debug, Clone, Copy)]
pub struct Circuit<const INPUTS: usize> {
pub inputs: [Input; INPUTS],
}

impl<const INPUTS: usize> Circuit<INPUTS> {
pub fn new() -> Self {
Self { inputs: array::from_fn(|label| Input::Variable(Variable { label })) }
}

pub const fn input(&self, label: usize) -> Input { self.inputs[label] }
}

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

#[test]
fn creating_a_circuit() {
let circuit = Circuit::<3>::new();
let x_0 = circuit.input(0);
let x_1 = circuit.input(1);
let x_2 = circuit.input(2);

let expr = x_0 * x_1 + x_2;
println!("{}", expr);
}
}
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
Loading