Skip to content

Commit 7d8355f

Browse files
committed
feat: miniature DSL
1 parent a859cbe commit 7d8355f

5 files changed

Lines changed: 216 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ repository ="https://github.com/thor314/ronkathon"
88
version ="0.1.0"
99

1010
[dependencies]
11+
petgraph ="0.6.5"
1112
rand ="0.8.5"
1213
num-bigint={ version="0.4.3", default-features=false }
1314
ark-std ={ version="0.4.0", default-features=false }

src/compiler/dsl.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use super::*;
2+
3+
#[derive(Clone, Copy, Debug)]
4+
pub enum Variable {
5+
Public(u32),
6+
Private(u32),
7+
}
8+
9+
#[derive(Clone, Copy, Debug)]
10+
pub enum Expression<ExpL, ExpR> {
11+
Add(ExpL, ExpR),
12+
Mul(ExpL, ExpR),
13+
}
14+
15+
impl Add for Variable {
16+
type Output = Expression<Variable, Variable>;
17+
18+
fn add(self, rhs: Self) -> Self::Output { Expression::Add(self, rhs) }
19+
}
20+
21+
impl<ExpL, ExpR> Add<Expression<ExpL, ExpR>> for Variable {
22+
type Output = Expression<Variable, Expression<ExpL, ExpR>>;
23+
24+
fn add(self, rhs: Expression<ExpL, ExpR>) -> Self::Output { Expression::Add(self, rhs) }
25+
}
26+
27+
impl<ExpL, ExpR> Add<Variable> for Expression<ExpL, ExpR> {
28+
type Output = Expression<Expression<ExpL, ExpR>, Variable>;
29+
30+
fn add(self, rhs: Variable) -> Self::Output { Expression::Add(self, rhs) }
31+
}
32+
33+
impl<EXP1, EXP2, EXP3, EXP4> Add<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> {
34+
type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>;
35+
36+
fn add(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Add(self, rhs) }
37+
}
38+
39+
impl Mul for Variable {
40+
type Output = Expression<Variable, Variable>;
41+
42+
fn mul(self, rhs: Self) -> Self::Output { Expression::Mul(self, rhs) }
43+
}
44+
45+
impl<ExpL, ExpR> Mul<Expression<ExpL, ExpR>> for Variable {
46+
type Output = Expression<Variable, Expression<ExpL, ExpR>>;
47+
48+
fn mul(self, rhs: Expression<ExpL, ExpR>) -> Self::Output { Expression::Mul(self, rhs) }
49+
}
50+
51+
impl<ExpL, ExpR> Mul<Variable> for Expression<ExpL, ExpR> {
52+
type Output = Expression<Expression<ExpL, ExpR>, Variable>;
53+
54+
fn mul(self, rhs: Variable) -> Self::Output { Expression::Mul(self, rhs) }
55+
}
56+
57+
impl<EXP1, EXP2, EXP3, EXP4> Mul<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> {
58+
type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>;
59+
60+
fn mul(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Mul(self, rhs) }
61+
}
62+
63+
impl Display for Variable {
64+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
65+
match self {
66+
Variable::Public(val) => write!(f, "{}", val),
67+
Variable::Private(val) => write!(f, "{}", val),
68+
}
69+
}
70+
}
71+
72+
impl<EXPL: Display, EXPR: Display> Display for Expression<EXPL, EXPR> {
73+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
74+
match self {
75+
Expression::Add(left, right) => write!(f, "({} + {})", left, right),
76+
Expression::Mul(left, right) => write!(f, "({} * {})", left, right),
77+
}
78+
}
79+
}
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
85+
#[test]
86+
fn writing_a_program() {
87+
// Create two variables
88+
let a = Variable::Public(7);
89+
let b = Variable::Private(3);
90+
91+
// Create basic expressions with these variables
92+
let add_ab = a + b;
93+
println!("{}", add_ab);
94+
assert_eq!(format!("{}", add_ab), "(7 + 3)");
95+
96+
let mul_ab = a * b;
97+
println!("{}", mul_ab);
98+
assert_eq!(format!("{}", mul_ab), "(7 * 3)");
99+
100+
// Check that we can add a variable to an expression
101+
println!("{}", a + mul_ab);
102+
assert_eq!(format!("{}", a + mul_ab), "(7 + (7 * 3))");
103+
104+
// Check that we can add an expression to a variable
105+
println!("{}", mul_ab + a);
106+
assert_eq!(format!("{}", mul_ab + a), "((7 * 3) + 7)");
107+
108+
// Check that we can add two expressions together
109+
println!("{}", add_ab + mul_ab);
110+
assert_eq!(format!("{}", add_ab + mul_ab), "((7 + 3) + (7 * 3))");
111+
}
112+
}

src/compiler/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! The compiler for `ronkathon` circuits.
2+
3+
// TODO: Remove this once the module is fleshed out.
4+
#![allow(missing_docs)]
5+
use petgraph::graph::{DiGraph, NodeIndex};
6+
7+
// TODO: Goal, allow someone to use this library to write rust to generate arithmetic circuits
8+
// basically.
9+
// ```
10+
// use ronkathon::compiler::*;
11+
//
12+
// fn main() {
13+
// let a: Input = Variable::public(7);
14+
// let b: Input = Variable::private(3);
15+
// let b: Expression = a * c;
16+
// }
17+
// ```
18+
// So we can do things like `impl Add`, `impl Mul` for variables and make them into gates?
19+
use super::*;
20+
21+
pub mod dsl;
22+
23+
pub struct Wire {
24+
pub input: Connection,
25+
pub output: Connection,
26+
}
27+
28+
pub enum Connection {
29+
Input(Publicity),
30+
Output,
31+
Internal,
32+
}
33+
34+
pub enum Publicity {
35+
Public(u32),
36+
Private(u32),
37+
}
38+
39+
pub enum Operation {
40+
Add,
41+
Mul,
42+
Sub,
43+
}
44+
45+
// TODO: Want to make it so gates own a reference for who they are connected to, and circuits own
46+
// the gates. This way gates cannot be improperly connected to other gates.
47+
pub struct Gate {
48+
pub left: Wire,
49+
pub right: Wire,
50+
pub output: Wire,
51+
pub op: Operation,
52+
}
53+
54+
pub struct Circuit {
55+
pub gates: DiGraph<Gate, Wire>,
56+
}
57+
58+
impl Circuit {
59+
pub fn new() -> Self { Self { gates: DiGraph::new() } }
60+
}
61+

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(generic_const_exprs)]
2222
#![warn(missing_docs)]
2323

24+
pub mod compiler;
2425
pub mod curve;
2526
pub mod field;
2627
pub mod kzg;

0 commit comments

Comments
 (0)