frontend/layouter/
nodes.rs1pub use itertools::Either;
3use serde::{Deserialize, Serialize};
4pub use shared_types::{Field, Fr};
5
6use crate::abstract_expr::AbstractExpression;
7use crate::layouter::builder::CircuitMap;
8use remainder::layer::layer_enum::LayerDescriptionEnum;
9
10use remainder::circuit_building_context::CircuitBuildingContext;
11
12use anyhow::Result;
13
14pub mod circuit_inputs;
15pub mod circuit_outputs;
16pub mod fiat_shamir_challenge;
17pub mod gate;
18pub mod identity_gate;
19pub mod lookup;
20pub mod matmult;
21pub mod node_enum;
22pub mod sector;
23pub mod split_node;
24
25#[derive(Clone, Debug, Hash, PartialEq, Eq, Copy, Ord, PartialOrd, Serialize, Deserialize)]
27pub struct NodeId(usize);
28
29impl Default for NodeId {
30 fn default() -> Self {
31 Self::new()
32 }
33}
34
35impl NodeId {
36 pub fn new() -> Self {
38 Self(CircuitBuildingContext::next_node_id())
39 }
40
41 #[cfg(test)]
43 pub fn new_unsafe(id: usize) -> Self {
44 Self(id)
45 }
46
47 pub fn expr<F: Field>(self) -> AbstractExpression<F> {
49 AbstractExpression::<F>::mle(self)
50 }
51
52 pub fn get_id(self) -> usize {
54 self.0
55 }
56}
57
58impl std::fmt::Display for NodeId {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(f, "{:#x}", self.0)
62 }
63}
64
65pub trait CircuitNode {
71 fn id(&self) -> NodeId;
73
74 fn sources(&self) -> Vec<NodeId>;
78
79 fn get_num_vars(&self) -> usize;
81}
82
83pub trait CompilableNode<F: Field>: CircuitNode {
89 fn generate_circuit_description(
92 &self,
93 circuit_map: &mut CircuitMap,
94 ) -> Result<Vec<LayerDescriptionEnum<F>>>;
95}
96
97#[macro_export]
98macro_rules! node_enum {
105 ($type_name:ident: $bound:tt, $(($var_name:ident: $variant:ty)),+) => {
106 #[derive(Clone, Debug)]
107 #[doc = r"Remainder generated trait enum"]
108 pub enum $type_name<F: $bound> {
109 $(
110 #[doc = "Remainder generated node variant"]
111 $var_name($variant),
112 )*
113 }
114
115
116 impl<F: $bound> $crate::layouter::nodes::CircuitNode for $type_name<F> {
117 fn id(&self) -> $crate::layouter::nodes::NodeId {
118 match self {
119 $(
120 Self::$var_name(node) => node.id(),
121 )*
122 }
123 }
124
125 fn sources(&self) -> Vec<$crate::layouter::nodes::NodeId> {
126 match self {
127 $(
128 Self::$var_name(node) => node.sources(),
129 )*
130 }
131 }
132
133 fn get_num_vars(&self) -> usize {
134 match self {
135 $(
136 Self::$var_name(node) => node.get_num_vars(),
137 )*
138 }
139 }
140 }
141
142 $(
143 impl<F: $bound> From<$variant> for $type_name<F> {
144 fn from(var: $variant) -> $type_name<F> {
145 Self::$var_name(var)
146 }
147 }
148 )*
149 }
150}