mpz
mpz copied to clipboard
feat(mpz-circuits-generic): implement generic circuit struct
Description
This PR aims to introduce a new generic circuit struct:
pub struct Circuit<T> {
input_count: usize,
output_count: usize,
gates: Vec<T>,
}
The Circuit
is a collection of gates. Since for the execution of these gates is important that these form a directed acyclic graph (DAG), the correct way of building this circuit is using the CircuitBuilder
pub struct CircuitBuilder<T> {
current_node: Node,
inputs: Vec<Node>,
outputs: Vec<Node>,
gates: Vec<T>,
stack_size: usize,
}
The CircuitBuilder
API ensures that the gates are ordered topologically, this means in the order they should be executed taking dependencies into account
Here is our only constrain for the gates, they have to implement the Component
trait, this means that they should be able to return an iterator over the nodes.
pub trait Component {
fn get_inputs(&self) -> impl Iterator<Item = &Node>;
fn get_outputs(&self) -> impl Iterator<Item = &Node>;
}
The Node
struct:
pub struct Node(pub(crate) u32);
Example
let mut builder = CircuitBuilder::<Gate>::new();
let (in_0, in_1) = (builder.add_input(), builder.add_input());
let &Gate { output, .. } = builder
.add_gate(|next| Gate {
inputs: vec![in_0, in_1],
output: next.next(),
})
.unwrap();
let &Gate { output, .. } = builder
.add_gate(|next| Gate {
inputs: vec![in_0, output],
output: next.next(),
})
.unwrap();
builder.add_output(output);