frontend/layouter/nodes/
circuit_inputs.rs

1//! Nodes that represent inputs to a circuit in the circuit DAG.
2
3/// The module which contains functions that combine input data in order
4/// to create one layerwise bookkeeping table.
5pub mod compile_inputs;
6use super::{CircuitNode, NodeId};
7use remainder::{layer::LayerId, mle::evals::MultilinearExtension};
8use serde::{Deserialize, Serialize};
9use shared_types::Field;
10
11/// A struct that represents input data that will be used to instantiate a
12/// [remainder::prover::GKRCircuitDescription] in order to generate a full circuit.
13#[derive(Debug, Clone)]
14pub struct InputLayerNodeData<F: Field> {
15    /// The [InputLayerNode] ID in the circuit building process that corresponds to
16    /// this data.
17    pub corresponding_input_node_id: NodeId,
18    /// The vector of data that goes in this input layer, as [InputShredData].
19    pub data: Vec<InputShredData<F>>,
20}
21
22impl<F: Field> InputLayerNodeData<F> {
23    /// Constructor for [InputLayerNodeData], using the corresponding fields as
24    /// parameters.
25    pub fn new(corresponding_input_node_id: NodeId, data: Vec<InputShredData<F>>) -> Self {
26        Self {
27            corresponding_input_node_id,
28            data,
29        }
30    }
31}
32
33#[derive(Debug, Clone)]
34/// A struct that represents data that corresponds to an [InputShred].
35pub struct InputShredData<F: Field> {
36    /// The corresponding input shred ID that this data belongs to.
37    pub corresponding_input_shred_id: NodeId,
38    /// The data itself, as a [MultilinearExtension].
39    pub data: MultilinearExtension<F>,
40}
41
42impl<F: Field> InputShredData<F> {
43    /// Constructor for [InputShredData], with the fields as parameters.
44    pub fn new(corresponding_input_shred_id: NodeId, data: MultilinearExtension<F>) -> Self {
45        Self {
46            corresponding_input_shred_id,
47            data,
48        }
49    }
50}
51
52/// A struct that represents the description of the data (shape in terms of `num_vars`)
53/// that will be added to an [InputLayerNode].
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct InputShred {
56    id: NodeId,
57    parent: NodeId,
58    num_vars: usize,
59}
60
61impl CircuitNode for InputShred {
62    fn id(&self) -> NodeId {
63        self.id
64    }
65
66    fn sources(&self) -> Vec<NodeId> {
67        vec![]
68    }
69
70    fn get_num_vars(&self) -> usize {
71        self.num_vars
72    }
73}
74
75impl InputShred {
76    /// Creates a new InputShred from data
77    ///
78    /// Specifying a source indicates to the layouter that this
79    /// InputShred should be appended to the source when laying out
80    pub fn new(num_vars: usize, source: &InputLayerNode) -> Self {
81        let id = NodeId::new();
82        let parent = source.id();
83
84        InputShred {
85            id,
86            parent,
87            num_vars,
88        }
89    }
90
91    /// Gets the parent NodeId of this InputShred. The InputLayerNode this InputShred will eventually be added to
92    pub fn get_parent(&self) -> NodeId {
93        self.parent
94    }
95}
96
97/// An enum representing the data type that can go in
98/// a Hyrax input layer, if it is not scalar field.
99#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
100pub enum HyraxInputDType {
101    /// If the input MLE coefficients are u8s.
102    U8,
103    /// If the input MLE coefficients are i8s.
104    I8,
105}
106
107#[derive(Debug, Clone)]
108/// A node that represents an InputLayer
109pub struct InputLayerNode {
110    id: NodeId,
111    input_layer_id: LayerId,
112    /// The MLEs that are combined to make up this InputLayer.
113    pub input_shreds: Vec<InputShred>,
114}
115
116impl CircuitNode for InputLayerNode {
117    fn id(&self) -> NodeId {
118        self.id
119    }
120
121    fn sources(&self) -> Vec<NodeId> {
122        vec![]
123    }
124
125    fn get_num_vars(&self) -> usize {
126        todo!()
127    }
128}
129
130impl InputLayerNode {
131    /// A constructor for an InputLayerNode. Can either be initialized empty
132    /// or with some InputShreds.
133    pub fn new(input_shreds: Option<Vec<InputShred>>) -> Self {
134        InputLayerNode {
135            id: NodeId::new(),
136            input_layer_id: LayerId::next_input_layer_id(),
137            input_shreds: input_shreds.unwrap_or_default(),
138        }
139    }
140
141    /// A method to add an InputShred to this InputLayerNode
142    pub fn add_shred(&mut self, shred: InputShred) {
143        self.input_shreds.push(shred);
144    }
145
146    /// Get the input layer id of this InputLayerNode.
147    pub fn input_layer_id(&self) -> LayerId {
148        self.input_layer_id
149    }
150}