snarkvm_synthesizer_program/closure/
mod.rs1use crate::Instruction;
17
18mod input;
19use input::*;
20
21mod output;
22use output::*;
23
24mod bytes;
25mod parse;
26
27use console::{
28 network::{error, prelude::*},
29 program::{Identifier, Register, RegisterType},
30};
31
32use indexmap::IndexSet;
33
34#[derive(Clone, PartialEq, Eq)]
35pub struct ClosureCore<N: Network> {
36 name: Identifier<N>,
38 inputs: IndexSet<Input<N>>,
41 instructions: Vec<Instruction<N>>,
43 outputs: IndexSet<Output<N>>,
45}
46
47impl<N: Network> ClosureCore<N> {
48 pub fn new(name: Identifier<N>) -> Self {
50 Self { name, inputs: IndexSet::new(), instructions: Vec::new(), outputs: IndexSet::new() }
51 }
52
53 pub const fn name(&self) -> &Identifier<N> {
55 &self.name
56 }
57
58 pub const fn inputs(&self) -> &IndexSet<Input<N>> {
60 &self.inputs
61 }
62
63 pub fn instructions(&self) -> &[Instruction<N>] {
65 &self.instructions
66 }
67
68 pub const fn outputs(&self) -> &IndexSet<Output<N>> {
70 &self.outputs
71 }
72
73 pub fn output_types(&self) -> Vec<RegisterType<N>> {
75 self.outputs.iter().map(|output| output.register_type()).cloned().collect()
76 }
77
78 pub fn contains_external_struct(&self) -> bool {
80 self.inputs.iter().any(|input| input.register_type().contains_external_struct())
81 || self.outputs.iter().any(|output| output.register_type().contains_external_struct())
82 || self.instructions.iter().any(|instruction| instruction.contains_external_struct())
83 }
84
85 pub fn contains_string_type(&self) -> bool {
88 self.instructions.iter().any(|instruction| instruction.contains_string_type())
89 }
90
91 pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
93 self.inputs.iter().any(|input| input.register_type().exceeds_max_array_size(max_array_size))
94 || self.outputs.iter().any(|output| output.register_type().exceeds_max_array_size(max_array_size))
95 || self.instructions.iter().any(|instruction| instruction.exceeds_max_array_size(max_array_size))
96 }
97}
98
99impl<N: Network> ClosureCore<N> {
100 #[inline]
107 fn add_input(&mut self, input: Input<N>) -> Result<()> {
108 ensure!(self.instructions.is_empty(), "Cannot add inputs after instructions have been added");
110 ensure!(self.outputs.is_empty(), "Cannot add inputs after outputs have been added");
111
112 ensure!(self.inputs.len() < N::MAX_INPUTS, "Cannot add more than {} inputs", N::MAX_INPUTS);
114 ensure!(!self.inputs.contains(&input), "Cannot add duplicate input statement");
116
117 ensure!(matches!(input.register(), Register::Locator(..)), "Input register must be a locator");
119
120 self.inputs.insert(input);
122 Ok(())
123 }
124
125 #[inline]
131 pub fn add_instruction(&mut self, instruction: Instruction<N>) -> Result<()> {
132 ensure!(self.outputs.is_empty(), "Cannot add instructions after outputs have been added");
134
135 ensure!(
137 self.instructions.len() < N::MAX_INSTRUCTIONS,
138 "Cannot add more than {} instructions",
139 N::MAX_INSTRUCTIONS
140 );
141
142 for register in instruction.destinations() {
144 ensure!(matches!(register, Register::Locator(..)), "Destination register must be a locator");
145 }
146
147 self.instructions.push(instruction);
149 Ok(())
150 }
151
152 #[inline]
157 fn add_output(&mut self, output: Output<N>) -> Result<()> {
158 ensure!(self.outputs.len() < N::MAX_OUTPUTS, "Cannot add more than {} outputs", N::MAX_OUTPUTS);
160
161 ensure!(!matches!(output.register_type(), RegisterType::Record(..)), "Output register cannot be a record");
163
164 self.outputs.insert(output);
166 Ok(())
167 }
168}
169
170impl<N: Network> TypeName for ClosureCore<N> {
171 #[inline]
173 fn type_name() -> &'static str {
174 "closure"
175 }
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 use crate::{Closure, Instruction};
183
184 type CurrentNetwork = console::network::MainnetV0;
185
186 #[test]
187 fn test_add_input() {
188 let name = Identifier::from_str("closure_core_test").unwrap();
190 let mut closure = Closure::<CurrentNetwork>::new(name);
191
192 let input = Input::<CurrentNetwork>::from_str("input r0 as field;").unwrap();
194 assert!(closure.add_input(input.clone()).is_ok());
195
196 assert!(closure.add_input(input).is_err());
198
199 for i in 1..CurrentNetwork::MAX_INPUTS * 2 {
201 let input = Input::<CurrentNetwork>::from_str(&format!("input r{i} as field;")).unwrap();
202
203 match closure.inputs.len() < CurrentNetwork::MAX_INPUTS {
204 true => assert!(closure.add_input(input).is_ok()),
205 false => assert!(closure.add_input(input).is_err()),
206 }
207 }
208 }
209
210 #[test]
211 fn test_add_instruction() {
212 let name = Identifier::from_str("closure_core_test").unwrap();
214 let mut closure = Closure::<CurrentNetwork>::new(name);
215
216 let instruction = Instruction::<CurrentNetwork>::from_str("add r0 r1 into r2;").unwrap();
218 assert!(closure.add_instruction(instruction).is_ok());
219
220 for i in 3..CurrentNetwork::MAX_INSTRUCTIONS * 2 {
222 let instruction = Instruction::<CurrentNetwork>::from_str(&format!("add r0 r1 into r{i};")).unwrap();
223
224 match closure.instructions.len() < CurrentNetwork::MAX_INSTRUCTIONS {
225 true => assert!(closure.add_instruction(instruction).is_ok()),
226 false => assert!(closure.add_instruction(instruction).is_err()),
227 }
228 }
229 }
230
231 #[test]
232 fn test_add_output() {
233 let name = Identifier::from_str("closure_core_test").unwrap();
235 let mut closure = Closure::<CurrentNetwork>::new(name);
236
237 let output = Output::<CurrentNetwork>::from_str("output r0 as field;").unwrap();
239 assert!(closure.add_output(output).is_ok());
240
241 for i in 1..CurrentNetwork::MAX_OUTPUTS * 2 {
243 let output = Output::<CurrentNetwork>::from_str(&format!("output r{i} as field;")).unwrap();
244
245 match closure.outputs.len() < CurrentNetwork::MAX_OUTPUTS {
246 true => assert!(closure.add_output(output).is_ok()),
247 false => assert!(closure.add_output(output).is_err()),
248 }
249 }
250 }
251}