Skip to content

Commit

Permalink
wip: cleanup + notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed May 20, 2024
1 parent 7d8355f commit d3258c1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 103 deletions.
43 changes: 2 additions & 41 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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
70 changes: 41 additions & 29 deletions src/compiler/dsl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use super::*;

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

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

#[derive(Clone, Copy, Debug)]
Expand All @@ -12,64 +18,65 @@ pub enum Expression<ExpL, ExpR> {
Mul(ExpL, ExpR),
}

impl Add for Variable {
type Output = Expression<Variable, Variable>;
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 Variable {
type Output = Expression<Variable, Expression<ExpL, ExpR>>;
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<Variable> for Expression<ExpL, ExpR> {
type Output = Expression<Expression<ExpL, ExpR>, Variable>;
impl<ExpL, ExpR> Add<Input> for Expression<ExpL, ExpR> {
type Output = Expression<Expression<ExpL, ExpR>, Input>;

fn add(self, rhs: Variable) -> Self::Output { Expression::Add(self, rhs) }
fn add(self, rhs: Input) -> 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>>;
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<EXP3, EXP4>) -> Self::Output { Expression::Add(self, rhs) }
fn add(self, rhs: Expression<ExpL2, ExpR2>) -> Self::Output { Expression::Add(self, rhs) }
}

impl Mul for Variable {
type Output = Expression<Variable, Variable>;
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 Variable {
type Output = Expression<Variable, Expression<ExpL, ExpR>>;
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<Variable> for Expression<ExpL, ExpR> {
type Output = Expression<Expression<ExpL, ExpR>, Variable>;
impl<ExpL, ExpR> Mul<Input> for Expression<ExpL, ExpR> {
type Output = Expression<Expression<ExpL, ExpR>, Input>;

fn mul(self, rhs: Variable) -> Self::Output { Expression::Mul(self, rhs) }
fn mul(self, rhs: Input) -> 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>>;
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<EXP3, EXP4>) -> Self::Output { Expression::Mul(self, rhs) }
fn mul(self, rhs: Expression<ExpL2, ExpR2>) -> Self::Output { Expression::Mul(self, rhs) }
}

impl Display for Variable {
impl Display for Input {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Variable::Public(val) => write!(f, "{}", val),
Variable::Private(val) => write!(f, "{}", val),
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> {
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),
Expand All @@ -85,8 +92,9 @@ mod tests {
#[test]
fn writing_a_program() {
// Create two variables
let a = Variable::Public(7);
let b = Variable::Private(3);
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;
Expand All @@ -108,5 +116,9 @@ mod tests {
// 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)");
}
}
60 changes: 28 additions & 32 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.
Expand All @@ -16,46 +15,43 @@ use petgraph::graph::{DiGraph, NodeIndex};
// }
// ```
// 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::*;

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

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

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

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

// 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,
}
#[cfg(test)]
mod tests {
use super::*;

pub struct Circuit {
pub gates: DiGraph<Gate, Wire>,
}
#[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);

impl Circuit {
pub fn new() -> Self { Self { gates: DiGraph::new() } }
let expr = x_0 * x_1 + x_2;
println!("{}", expr);
}
}

0 comments on commit d3258c1

Please sign in to comment.