snarkvm_synthesizer_program/finalize/
mod.rs1use crate::Command;
17
18mod input;
19use input::*;
20
21mod bytes;
22mod parse;
23
24use console::{
25 network::prelude::*,
26 program::{FinalizeType, Identifier, Register},
27};
28
29use indexmap::IndexSet;
30use std::collections::HashMap;
31
32#[derive(Clone, PartialEq, Eq)]
33pub struct FinalizeCore<N: Network> {
34 name: Identifier<N>,
36 inputs: IndexSet<Input<N>>,
39 commands: Vec<Command<N>>,
41 num_writes: u16,
43 positions: HashMap<Identifier<N>, usize>,
45}
46
47impl<N: Network> FinalizeCore<N> {
48 pub fn new(name: Identifier<N>) -> Self {
50 Self { name, inputs: IndexSet::new(), commands: Vec::new(), num_writes: 0, positions: HashMap::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 input_types(&self) -> Vec<FinalizeType<N>> {
65 self.inputs.iter().map(|input| input.finalize_type()).cloned().collect()
66 }
67
68 pub fn commands(&self) -> &[Command<N>] {
70 &self.commands
71 }
72
73 pub const fn num_writes(&self) -> u16 {
75 self.num_writes
76 }
77
78 pub const fn positions(&self) -> &HashMap<Identifier<N>, usize> {
80 &self.positions
81 }
82
83 pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
85 self.inputs.iter().any(|input| {
86 matches!(
87 input.finalize_type(),
88 FinalizeType::Plaintext(plaintext_type) if plaintext_type.exceeds_max_array_size(max_array_size)
89 )
90 }) || self.commands.iter().any(|command| {
91 matches!(
92 command,
93 Command::Instruction(instruction) if instruction.exceeds_max_array_size(max_array_size)
94 )
95 })
96 }
97}
98
99impl<N: Network> FinalizeCore<N> {
100 #[inline]
107 fn add_input(&mut self, input: Input<N>) -> Result<()> {
108 ensure!(self.commands.is_empty(), "Cannot add inputs after commands have been added");
110
111 ensure!(self.inputs.len() < N::MAX_INPUTS, "Cannot add more than {} inputs", N::MAX_INPUTS);
113 ensure!(!self.inputs.contains(&input), "Cannot add duplicate input statement");
115
116 ensure!(matches!(input.register(), Register::Locator(..)), "Input register must be a locator");
118
119 self.inputs.insert(input);
121 Ok(())
122 }
123
124 #[inline]
129 pub fn add_command(&mut self, command: Command<N>) -> Result<()> {
130 ensure!(self.commands.len() < N::MAX_COMMANDS, "Cannot add more than {} commands", N::MAX_COMMANDS);
132 if command.is_write() {
134 ensure!(
135 self.num_writes < N::MAX_WRITES,
136 "Cannot add more than {} 'set' & 'remove' commands",
137 N::MAX_WRITES
138 );
139 }
140
141 ensure!(!command.is_async(), "Forbidden operation: Finalize cannot invoke an 'async' instruction");
143 ensure!(!command.is_call(), "Forbidden operation: Finalize cannot invoke a 'call'");
145 ensure!(!command.is_cast_to_record(), "Forbidden operation: Finalize cannot cast to a record");
147
148 for register in command.destinations() {
150 ensure!(matches!(register, Register::Locator(..)), "Destination register must be a locator");
152 }
153
154 if let Some(position) = command.branch_to() {
156 ensure!(!self.positions.contains_key(position), "Cannot branch to an earlier position '{position}'");
158 }
159
160 if let Some(position) = command.position() {
162 ensure!(!self.positions.contains_key(position), "Cannot redefine position '{position}'");
164 ensure!(self.positions.len() < N::MAX_POSITIONS, "Cannot add more than {} positions", N::MAX_POSITIONS);
166 self.positions.insert(*position, self.commands.len());
168 }
169
170 if command.is_write() {
172 self.num_writes += 1;
174 }
175
176 self.commands.push(command);
178 Ok(())
179 }
180}
181
182impl<N: Network> TypeName for FinalizeCore<N> {
183 #[inline]
185 fn type_name() -> &'static str {
186 "finalize"
187 }
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193
194 use crate::{Command, Finalize};
195
196 type CurrentNetwork = console::network::MainnetV0;
197
198 #[test]
199 fn test_add_input() {
200 let name = Identifier::from_str("finalize_core_test").unwrap();
202 let mut finalize = Finalize::<CurrentNetwork>::new(name);
203
204 let input = Input::<CurrentNetwork>::from_str("input r0 as field.public;").unwrap();
206 assert!(finalize.add_input(input.clone()).is_ok());
207
208 assert!(finalize.add_input(input).is_err());
210
211 for i in 1..CurrentNetwork::MAX_INPUTS * 2 {
213 let input = Input::<CurrentNetwork>::from_str(&format!("input r{i} as field.public;")).unwrap();
214
215 match finalize.inputs.len() < CurrentNetwork::MAX_INPUTS {
216 true => assert!(finalize.add_input(input).is_ok()),
217 false => assert!(finalize.add_input(input).is_err()),
218 }
219 }
220 }
221
222 #[test]
223 fn test_add_command() {
224 let name = Identifier::from_str("finalize_core_test").unwrap();
226 let mut finalize = Finalize::<CurrentNetwork>::new(name);
227
228 let command = Command::<CurrentNetwork>::from_str("add r0 r1 into r2;").unwrap();
230 assert!(finalize.add_command(command).is_ok());
231
232 for i in 3..CurrentNetwork::MAX_COMMANDS * 2 {
234 let command = Command::<CurrentNetwork>::from_str(&format!("add r0 r1 into r{i};")).unwrap();
235
236 match finalize.commands.len() < CurrentNetwork::MAX_COMMANDS {
237 true => assert!(finalize.add_command(command).is_ok()),
238 false => assert!(finalize.add_command(command).is_err()),
239 }
240 }
241
242 let name = Identifier::from_str("finalize_core_test").unwrap();
246 let mut finalize = Finalize::<CurrentNetwork>::new(name);
247
248 for _ in 0..CurrentNetwork::MAX_WRITES * 2 {
249 let command = Command::<CurrentNetwork>::from_str("remove object[r0];").unwrap();
250
251 match finalize.commands.len() < CurrentNetwork::MAX_WRITES as usize {
252 true => assert!(finalize.add_command(command).is_ok()),
253 false => assert!(finalize.add_command(command).is_err()),
254 }
255 }
256 }
257
258 #[test]
259 fn test_add_command_duplicate_positions() {
260 let name = Identifier::from_str("finalize_core_test").unwrap();
262 let mut finalize = Finalize::<CurrentNetwork>::new(name);
263
264 let command = Command::<CurrentNetwork>::from_str("position start;").unwrap();
266 assert!(finalize.add_command(command.clone()).is_ok());
267
268 assert!(finalize.add_command(command).is_err());
270
271 #[allow(clippy::cast_possible_truncation)]
273 fn to_unique_string(mut n: usize) -> String {
274 let mut s = String::new();
275 while n > 0 {
276 s.push((b'A' + (n % 26) as u8) as char);
277 n /= 26;
278 }
279 s.chars().rev().collect::<String>()
280 }
281
282 for i in 1..u8::MAX as usize * 2 {
284 let position = to_unique_string(i);
285 let command = Command::<CurrentNetwork>::from_str(&format!("position {position};")).unwrap();
287
288 match finalize.commands.len() < u8::MAX as usize {
289 true => assert!(finalize.add_command(command).is_ok()),
290 false => assert!(finalize.add_command(command).is_err()),
291 }
292 }
293 }
294}