Skip to content

Commit d3258c1

Browse files
committed
wip: cleanup + notes
1 parent 7d8355f commit d3258c1

4 files changed

Lines changed: 71 additions & 103 deletions

File tree

Cargo.lock

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

Cargo.toml

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

1010
[dependencies]
11-
petgraph ="0.6.5"
1211
rand ="0.8.5"
1312
num-bigint={ version="0.4.3", default-features=false }
1413
ark-std ={ version="0.4.0", default-features=false }

src/compiler/dsl.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use super::*;
22

33
#[derive(Clone, Copy, Debug)]
4-
pub enum Variable {
5-
Public(u32),
6-
Private(u32),
4+
pub enum Input {
5+
Variable(Variable),
6+
Public(usize),
7+
Private(usize),
8+
}
9+
10+
#[derive(Clone, Copy, Debug)]
11+
pub struct Variable {
12+
pub label: usize,
713
}
814

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

15-
impl Add for Variable {
16-
type Output = Expression<Variable, Variable>;
21+
impl Add for Input {
22+
type Output = Expression<Input, Input>;
1723

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

21-
impl<ExpL, ExpR> Add<Expression<ExpL, ExpR>> for Variable {
22-
type Output = Expression<Variable, Expression<ExpL, ExpR>>;
27+
impl<ExpL, ExpR> Add<Expression<ExpL, ExpR>> for Input {
28+
type Output = Expression<Input, Expression<ExpL, ExpR>>;
2329

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

27-
impl<ExpL, ExpR> Add<Variable> for Expression<ExpL, ExpR> {
28-
type Output = Expression<Expression<ExpL, ExpR>, Variable>;
33+
impl<ExpL, ExpR> Add<Input> for Expression<ExpL, ExpR> {
34+
type Output = Expression<Expression<ExpL, ExpR>, Input>;
2935

30-
fn add(self, rhs: Variable) -> Self::Output { Expression::Add(self, rhs) }
36+
fn add(self, rhs: Input) -> Self::Output { Expression::Add(self, rhs) }
3137
}
3238

33-
impl<EXP1, EXP2, EXP3, EXP4> Add<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> {
34-
type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>;
39+
impl<ExpL1, ExpR1, ExpL2, ExpR2> Add<Expression<ExpL2, ExpR2>> for Expression<ExpL1, ExpR1> {
40+
type Output = Expression<Expression<ExpL1, ExpR1>, Expression<ExpL2, ExpR2>>;
3541

36-
fn add(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Add(self, rhs) }
42+
fn add(self, rhs: Expression<ExpL2, ExpR2>) -> Self::Output { Expression::Add(self, rhs) }
3743
}
3844

39-
impl Mul for Variable {
40-
type Output = Expression<Variable, Variable>;
45+
impl Mul for Input {
46+
type Output = Expression<Input, Input>;
4147

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

45-
impl<ExpL, ExpR> Mul<Expression<ExpL, ExpR>> for Variable {
46-
type Output = Expression<Variable, Expression<ExpL, ExpR>>;
51+
impl<ExpL, ExpR> Mul<Expression<ExpL, ExpR>> for Input {
52+
type Output = Expression<Input, Expression<ExpL, ExpR>>;
4753

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

51-
impl<ExpL, ExpR> Mul<Variable> for Expression<ExpL, ExpR> {
52-
type Output = Expression<Expression<ExpL, ExpR>, Variable>;
57+
impl<ExpL, ExpR> Mul<Input> for Expression<ExpL, ExpR> {
58+
type Output = Expression<Expression<ExpL, ExpR>, Input>;
5359

54-
fn mul(self, rhs: Variable) -> Self::Output { Expression::Mul(self, rhs) }
60+
fn mul(self, rhs: Input) -> Self::Output { Expression::Mul(self, rhs) }
5561
}
5662

57-
impl<EXP1, EXP2, EXP3, EXP4> Mul<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> {
58-
type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>;
63+
impl<ExpL1, ExpR1, ExpL2, ExpR2> Mul<Expression<ExpL2, ExpR2>> for Expression<ExpL1, ExpR1> {
64+
type Output = Expression<Expression<ExpL1, ExpR1>, Expression<ExpL2, ExpR2>>;
5965

60-
fn mul(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Mul(self, rhs) }
66+
fn mul(self, rhs: Expression<ExpL2, ExpR2>) -> Self::Output { Expression::Mul(self, rhs) }
6167
}
6268

63-
impl Display for Variable {
69+
impl Display for Input {
6470
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
6571
match self {
66-
Variable::Public(val) => write!(f, "{}", val),
67-
Variable::Private(val) => write!(f, "{}", val),
72+
Input::Variable(val) => write!(f, "x_{}", val.label),
73+
Input::Public(val) => write!(f, "{}", val),
74+
Input::Private(val) => write!(f, "{}", val),
6875
}
6976
}
7077
}
7178

72-
impl<EXPL: Display, EXPR: Display> Display for Expression<EXPL, EXPR> {
79+
impl<ExpL: Display, ExpR: Display> Display for Expression<ExpL, ExpR> {
7380
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
7481
match self {
7582
Expression::Add(left, right) => write!(f, "({} + {})", left, right),
@@ -85,8 +92,9 @@ mod tests {
8592
#[test]
8693
fn writing_a_program() {
8794
// Create two variables
88-
let a = Variable::Public(7);
89-
let b = Variable::Private(3);
95+
let a = Input::Public(7);
96+
let b = Input::Private(3);
97+
let x = Input::Variable(Variable { label: 0 });
9098

9199
// Create basic expressions with these variables
92100
let add_ab = a + b;
@@ -108,5 +116,9 @@ mod tests {
108116
// Check that we can add two expressions together
109117
println!("{}", add_ab + mul_ab);
110118
assert_eq!(format!("{}", add_ab + mul_ab), "((7 + 3) + (7 * 3))");
119+
120+
// Check that we can multiply an expression by a variable
121+
println!("{}", mul_ab * x);
122+
assert_eq!(format!("{}", mul_ab * x), "((7 * 3) * x_0)");
111123
}
112124
}

src/compiler/mod.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
// TODO: Remove this once the module is fleshed out.
44
#![allow(missing_docs)]
5-
use petgraph::graph::{DiGraph, NodeIndex};
65

76
// TODO: Goal, allow someone to use this library to write rust to generate arithmetic circuits
87
// basically.
@@ -16,46 +15,43 @@ use petgraph::graph::{DiGraph, NodeIndex};
1615
// }
1716
// ```
1817
// So we can do things like `impl Add`, `impl Mul` for variables and make them into gates?
18+
19+
use std::array;
20+
21+
// Above seems done. Now we need to have a way to unravel a collection of expressions into a
22+
// circuit that may have the same inputs and outputs as the expressions. Inputs are going to be
23+
// the terminal variables found by fully unravelling expressions and they should be named. The
24+
// fully ravelled expressions are the outputs, and they can also be named
1925
use super::*;
2026

2127
pub mod dsl;
28+
pub use dsl::*;
2229

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,
30+
#[derive(Debug, Clone, Copy)]
31+
pub struct Circuit<const INPUTS: usize> {
32+
pub inputs: [Input; INPUTS],
3233
}
3334

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

39-
pub enum Operation {
40-
Add,
41-
Mul,
42-
Sub,
40+
pub const fn input(&self, label: usize) -> Input { self.inputs[label] }
4341
}
4442

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-
}
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
5346

54-
pub struct Circuit {
55-
pub gates: DiGraph<Gate, Wire>,
56-
}
47+
#[test]
48+
fn creating_a_circuit() {
49+
let circuit = Circuit::<3>::new();
50+
let x_0 = circuit.input(0);
51+
let x_1 = circuit.input(1);
52+
let x_2 = circuit.input(2);
5753

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

0 commit comments

Comments
 (0)