-
Notifications
You must be signed in to change notification settings - Fork 25
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
1 parent
a859cbe
commit 7d8355f
Showing
5 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,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))"); | ||
} | ||
} |
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,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() } } | ||
} | ||
|
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