Skip to content

Commit

Permalink
add various optimizations to layered circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
siq1 committed Aug 22, 2024
1 parent 6d584ea commit af304c1
Show file tree
Hide file tree
Showing 7 changed files with 895 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.txt
__*
target
libec_go_lib.so
libec_go_lib.*

# dev IDE
.idea
Expand Down
1 change: 1 addition & 0 deletions build-rust.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
cd "$(dirname "$0")"
cd expander_compiler/ec_go_lib
cargo build --release
cd ..
Expand Down
33 changes: 29 additions & 4 deletions expander_compiler/src/circuit/layered/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use super::config::Config;
#[cfg(test)]
mod tests;

pub mod opt;
pub mod serde;
pub mod stats;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Coef<C: Config> {
Constant(C::CircuitField),
Random,
Expand All @@ -30,6 +31,7 @@ impl<C: Config> Coef<C> {
}
}
}

pub fn validate(&self, num_public_inputs: usize) -> Result<(), Error> {
match self {
Coef::Constant(_) => Ok(()),
Expand All @@ -47,6 +49,27 @@ impl<C: Config> Coef<C> {
}
}

pub fn is_constant(&self) -> bool {
match self {
Coef::Constant(_) => true,
_ => false,
}
}

pub fn add_constant(&self, c: C::CircuitField) -> Self {
match self {
Coef::Constant(x) => Coef::Constant(*x + c),
_ => panic!("add_constant called on non-constant"),
}
}

pub fn get_constant(&self) -> Option<C::CircuitField> {
match self {
Coef::Constant(x) => Some(x.clone()),
_ => None,
}
}

#[cfg(test)]
pub fn random_no_random(mut rnd: impl rand::RngCore, num_public_inputs: usize) -> Self {
use rand::Rng;
Expand All @@ -58,7 +81,7 @@ impl<C: Config> Coef<C> {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash)]
pub struct Gate<C: Config, const INPUT_NUM: usize> {
pub inputs: [usize; INPUT_NUM],
pub output: usize,
Expand All @@ -69,22 +92,23 @@ pub type GateMul<C> = Gate<C, 2>;
pub type GateAdd<C> = Gate<C, 1>;
pub type GateConst<C> = Gate<C, 0>;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash)]
pub struct GateCustom<C: Config> {
pub gate_type: usize,
pub inputs: Vec<usize>,
pub output: usize,
pub coef: Coef<C>,
}

#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct Allocation {
pub input_offset: usize,
pub output_offset: usize,
}

pub type ChildSpec = (usize, Vec<Allocation>);

#[derive(Default)]
#[derive(Default, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct Segment<C: Config> {
pub num_inputs: usize,
pub num_outputs: usize,
Expand All @@ -95,6 +119,7 @@ pub struct Segment<C: Config> {
pub gate_customs: Vec<GateCustom<C>>,
}

#[derive(Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct Circuit<C: Config> {
pub num_public_inputs: usize,
pub num_actual_outputs: usize,
Expand Down
Loading

0 comments on commit af304c1

Please sign in to comment.