snarkvm_synthesizer_program/constructor/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod bytes;
17mod parse;
18
19use crate::Command;
20
21use console::{
22    network::prelude::*,
23    program::{Identifier, Register},
24};
25
26use std::collections::HashMap;
27
28#[derive(Clone, PartialEq, Eq)]
29pub struct ConstructorCore<N: Network> {
30    /// The commands, in order of execution.
31    commands: Vec<Command<N>>,
32    /// The number of write commands.
33    num_writes: u16,
34    /// A mapping from `Position`s to their index in `commands`.
35    positions: HashMap<Identifier<N>, usize>,
36}
37
38impl<N: Network> ConstructorCore<N> {
39    /// Returns the constructor commands.
40    pub fn commands(&self) -> &[Command<N>] {
41        &self.commands
42    }
43
44    /// Returns the number of write commands.
45    pub const fn num_writes(&self) -> u16 {
46        self.num_writes
47    }
48
49    /// Returns the mapping of `Position`s to their index in `commands`.
50    pub const fn positions(&self) -> &HashMap<Identifier<N>, usize> {
51        &self.positions
52    }
53}
54
55impl<N: Network> ConstructorCore<N> {
56    /// Adds the given command to constructor.
57    ///
58    /// # Errors
59    /// This method will halt if the maximum number of commands has been reached.
60    #[inline]
61    pub fn add_command(&mut self, command: Command<N>) -> Result<()> {
62        // Ensure the maximum number of commands has not been exceeded.
63        ensure!(self.commands.len() < N::MAX_COMMANDS, "Cannot add more than {} commands", N::MAX_COMMANDS);
64        // Ensure the number of write commands has not been exceeded.
65        if command.is_write() {
66            ensure!(
67                self.num_writes < N::MAX_WRITES,
68                "Cannot add more than {} 'set' & 'remove' commands",
69                N::MAX_WRITES
70            );
71        }
72
73        // Ensure the command is not an async instruction.
74        ensure!(!command.is_async(), "Forbidden operation: Constructor cannot invoke an 'async' instruction");
75        // Ensure the command is not a call instruction.
76        ensure!(!command.is_call(), "Forbidden operation: Constructor cannot invoke a 'call'");
77        // Ensure the command is not a cast to record instruction.
78        ensure!(!command.is_cast_to_record(), "Forbidden operation: Constructor cannot cast to a record");
79        // Ensure the command is not an await command.
80        ensure!(!command.is_await(), "Forbidden operation: Constructor cannot 'await'");
81
82        // Check the destination registers.
83        for register in command.destinations() {
84            // Ensure the destination register is a locator.
85            ensure!(matches!(register, Register::Locator(..)), "Destination register must be a locator");
86        }
87
88        // Check if the command is a branch command.
89        if let Some(position) = command.branch_to() {
90            // Ensure the branch target does not reference an earlier position.
91            ensure!(!self.positions.contains_key(position), "Cannot branch to an earlier position '{position}'");
92        }
93
94        // Check if the command is a position command.
95        if let Some(position) = command.position() {
96            // Ensure the position is not yet defined.
97            ensure!(!self.positions.contains_key(position), "Cannot redefine position '{position}'");
98            // Ensure that there are less than `u8::MAX` positions.
99            ensure!(self.positions.len() < N::MAX_POSITIONS, "Cannot add more than {} positions", N::MAX_POSITIONS);
100            // Insert the position.
101            self.positions.insert(*position, self.commands.len());
102        }
103
104        // Check if the command is a write command.
105        if command.is_write() {
106            // Increment the number of write commands.
107            self.num_writes += 1;
108        }
109
110        // Insert the command.
111        self.commands.push(command);
112        Ok(())
113    }
114}
115
116impl<N: Network> TypeName for ConstructorCore<N> {
117    /// Returns the type name as a string.
118    #[inline]
119    fn type_name() -> &'static str {
120        "constructor"
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    use crate::{Command, Constructor};
129
130    type CurrentNetwork = console::network::MainnetV0;
131
132    #[test]
133    fn test_add_command() {
134        // Initialize a new constructor instance.
135        let mut constructor = Constructor::<CurrentNetwork> {
136            commands: Default::default(),
137            num_writes: 0,
138            positions: Default::default(),
139        };
140
141        // Ensure that a command can be added.
142        let command = Command::<CurrentNetwork>::from_str("add r0 r1 into r2;").unwrap();
143        assert!(constructor.add_command(command).is_ok());
144
145        // Ensure that adding more than the maximum number of commands will fail.
146        for i in 3..CurrentNetwork::MAX_COMMANDS * 2 {
147            let command = Command::<CurrentNetwork>::from_str(&format!("add r0 r1 into r{i};")).unwrap();
148
149            match constructor.commands.len() < CurrentNetwork::MAX_COMMANDS {
150                true => assert!(constructor.add_command(command).is_ok()),
151                false => assert!(constructor.add_command(command).is_err()),
152            }
153        }
154
155        // Ensure that adding more than the maximum number of writes will fail.
156
157        // Initialize a new constructor instance.
158        let mut constructor = Constructor::<CurrentNetwork> {
159            commands: Default::default(),
160            num_writes: 0,
161            positions: Default::default(),
162        };
163
164        for _ in 0..CurrentNetwork::MAX_WRITES * 2 {
165            let command = Command::<CurrentNetwork>::from_str("remove object[r0];").unwrap();
166
167            match constructor.commands.len() < CurrentNetwork::MAX_WRITES as usize {
168                true => assert!(constructor.add_command(command).is_ok()),
169                false => assert!(constructor.add_command(command).is_err()),
170            }
171        }
172    }
173
174    #[test]
175    fn test_add_command_duplicate_positions() {
176        // Initialize a new constructor instance.
177        let mut constructor =
178            Constructor { commands: Default::default(), num_writes: 0, positions: Default::default() };
179
180        // Ensure that a command can be added.
181        let command = Command::<CurrentNetwork>::from_str("position start;").unwrap();
182        assert!(constructor.add_command(command.clone()).is_ok());
183
184        // Ensure that adding a duplicate position will fail.
185        assert!(constructor.add_command(command).is_err());
186
187        // Helper method to convert a number to a unique string.
188        #[allow(clippy::cast_possible_truncation)]
189        fn to_unique_string(mut n: usize) -> String {
190            let mut s = String::new();
191            while n > 0 {
192                s.push((b'A' + (n % 26) as u8) as char);
193                n /= 26;
194            }
195            s.chars().rev().collect::<String>()
196        }
197
198        // Ensure that adding more than the maximum number of positions will fail.
199        for i in 1..u8::MAX as usize * 2 {
200            let position = to_unique_string(i);
201            println!("position: {position}");
202            let command = Command::<CurrentNetwork>::from_str(&format!("position {position};")).unwrap();
203
204            match constructor.commands.len() < u8::MAX as usize {
205                true => assert!(constructor.add_command(command).is_ok()),
206                false => assert!(constructor.add_command(command).is_err()),
207            }
208        }
209    }
210}