|
| 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 | + |
| 16 | +use circuit::{Eject, Inject}; |
| 17 | +use console::{ |
| 18 | + network::prelude::*, |
| 19 | + program::{Literal, LiteralType, Plaintext, PlaintextType, Register, RegisterType, Value}, |
| 20 | + types::Boolean, |
| 21 | +}; |
| 22 | + |
| 23 | +use crate::{Opcode, Operand, RegistersCircuit, RegistersTrait, StackTrait}; |
| 24 | + |
| 25 | +/// Computes an equality operation on two operands, and stores the outcome in `destination`. |
| 26 | +#[derive(Clone, PartialEq, Eq, Hash)] |
| 27 | +pub struct In<N: Network> { |
| 28 | + /// The operands. |
| 29 | + operands: [Operand<N>; 2], |
| 30 | + /// The destination register. |
| 31 | + destination: Register<N>, |
| 32 | +} |
| 33 | + |
| 34 | +impl<N: Network> In<N> { |
| 35 | + /// Initializes a new `in` instruction. |
| 36 | + #[inline] |
| 37 | + pub fn new(operands: [Operand<N>; 2], destination: Register<N>) -> Result<Self> { |
| 38 | + // Return the instruction. |
| 39 | + Ok(Self { operands, destination }) |
| 40 | + } |
| 41 | + |
| 42 | + /// Returns the opcode.s |
| 43 | + #[inline] |
| 44 | + pub const fn opcode() -> Opcode { |
| 45 | + Opcode::In |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns the operands in the operation. |
| 49 | + #[inline] |
| 50 | + pub fn operands(&self) -> &[Operand<N>] { |
| 51 | + // Return the operands. |
| 52 | + &self.operands |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns the destination register. |
| 56 | + #[inline] |
| 57 | + pub fn destinations(&self) -> Vec<Register<N>> { |
| 58 | + vec![self.destination.clone()] |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<N: Network> In<N> { |
| 63 | + /// Evaluates the instruction. |
| 64 | + pub fn evaluate(&self, stack: &impl StackTrait<N>, registers: &mut impl RegistersTrait<N>) -> Result<()> { |
| 65 | + // Retrieve the inputs. |
| 66 | + let input_a = registers.load(stack, &self.operands[0])?; |
| 67 | + let input_b = registers.load(stack, &self.operands[1])?; |
| 68 | + |
| 69 | + // Make sure the second operand is an array. |
| 70 | + let Value::Plaintext(Plaintext::Array(array, _)) = &input_b else { |
| 71 | + bail!("Instruction '{}' requires second operand to be an array but found {}", Self::opcode(), input_b) |
| 72 | + }; |
| 73 | + |
| 74 | + // Make sure the first operand is not an illegal type for this case. |
| 75 | + let Value::Plaintext(val) = &input_a else { bail!("Array cannot have records or futures as its elements.") }; |
| 76 | + |
| 77 | + // Check if the array contains the value. |
| 78 | + let output = Literal::Boolean(Boolean::new(array.contains(val))); |
| 79 | + |
| 80 | + // Store the output. |
| 81 | + registers.store(stack, &self.destination, Value::Plaintext(Plaintext::from(output))) |
| 82 | + } |
| 83 | + |
| 84 | + /// Executes the instruction. |
| 85 | + pub fn execute<A: circuit::Aleo<Network = N>>( |
| 86 | + &self, |
| 87 | + stack: &impl StackTrait<N>, |
| 88 | + registers: &mut impl RegistersCircuit<N, A>, |
| 89 | + ) -> Result<()> { |
| 90 | + // Retrieve the inputs. |
| 91 | + let input_a = registers.load_circuit(stack, &self.operands[0])?; |
| 92 | + let input_b = registers.load_circuit(stack, &self.operands[1])?; |
| 93 | + |
| 94 | + // Make sure the second operand is an array. |
| 95 | + let circuit::Value::Plaintext(circuit::Plaintext::Array(array, _)) = &input_b else { |
| 96 | + bail!( |
| 97 | + "Instruction '{}' requires second operand to be an array but found {}", |
| 98 | + Self::opcode(), |
| 99 | + input_b.eject_value() |
| 100 | + ) |
| 101 | + }; |
| 102 | + |
| 103 | + // Make sure the first operand is not an illegal type for this case. |
| 104 | + let circuit::Value::Plaintext(val) = &input_a else { |
| 105 | + bail!("Array cannot have records or futures as its elements.") |
| 106 | + }; |
| 107 | + |
| 108 | + // Check if the array contains the value. |
| 109 | + let output = (0..array.len()).any(|index| val.is_equal(&array[index]).eject_value()); |
| 110 | + |
| 111 | + // Store the output. |
| 112 | + registers.store_literal_circuit( |
| 113 | + stack, |
| 114 | + &self.destination, |
| 115 | + circuit::Literal::from(circuit::Boolean::constant(output)), |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + /// Finalizes the instruction. |
| 120 | + #[inline] |
| 121 | + pub fn finalize(&self, stack: &impl StackTrait<N>, registers: &mut impl RegistersTrait<N>) -> Result<()> { |
| 122 | + self.evaluate(stack, registers) |
| 123 | + } |
| 124 | + |
| 125 | + /// Returns the output type from the given program and input types. |
| 126 | + pub fn output_types( |
| 127 | + &self, |
| 128 | + _stack: &impl StackTrait<N>, |
| 129 | + input_types: &[RegisterType<N>], |
| 130 | + ) -> Result<Vec<RegisterType<N>>> { |
| 131 | + // Ensure the number of input types is correct. |
| 132 | + if input_types.len() != 2 { |
| 133 | + bail!("Instruction '{}' expects 2 inputs, found {} inputs", Self::opcode(), input_types.len()) |
| 134 | + } |
| 135 | + |
| 136 | + let RegisterType::Plaintext(PlaintextType::Array(arr_type)) = &input_types[1] else { |
| 137 | + bail!("Instruction {} expects the second input to be an array got {}", Self::opcode(), input_types[1]) |
| 138 | + }; |
| 139 | + |
| 140 | + let element_type = RegisterType::Plaintext(arr_type.next_element_type().clone()); |
| 141 | + |
| 142 | + // Ensure the operand are of the same type. |
| 143 | + if input_types[0] != element_type { |
| 144 | + bail!( |
| 145 | + "Instruction '{}' expects first input and element type of array to be of the same type. Found inputs of type '{}' and '{}'", |
| 146 | + Self::opcode(), |
| 147 | + input_types[0], |
| 148 | + element_type |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + Ok(vec![RegisterType::Plaintext(PlaintextType::Literal(LiteralType::Boolean))]) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +impl<N: Network> Parser for In<N> { |
| 157 | + /// Parses a string into an operation. |
| 158 | + fn parse(string: &str) -> ParserResult<Self> { |
| 159 | + // Parse the opcode from the string. |
| 160 | + let (string, _) = tag(*Self::opcode())(string)?; |
| 161 | + // Parse the whitespace from the string. |
| 162 | + let (string, _) = Sanitizer::parse_whitespaces(string)?; |
| 163 | + // Parse the first operand from the string. |
| 164 | + let (string, first) = Operand::parse(string)?; |
| 165 | + // Parse the whitespace from the string. |
| 166 | + let (string, _) = Sanitizer::parse_whitespaces(string)?; |
| 167 | + // Parse the second operand from the string. |
| 168 | + let (string, second) = Operand::parse(string)?; |
| 169 | + // Parse the whitespace from the string. |
| 170 | + let (string, _) = Sanitizer::parse_whitespaces(string)?; |
| 171 | + // Parse the "into" from the string. |
| 172 | + let (string, _) = tag("into")(string)?; |
| 173 | + // Parse the whitespace from the string. |
| 174 | + let (string, _) = Sanitizer::parse_whitespaces(string)?; |
| 175 | + // Parse the destination register from the string. |
| 176 | + let (string, destination) = Register::parse(string)?; |
| 177 | + |
| 178 | + Ok((string, Self { operands: [first, second], destination })) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +impl<N: Network> FromStr for In<N> { |
| 183 | + type Err = Error; |
| 184 | + |
| 185 | + /// Parses a string into an operation. |
| 186 | + fn from_str(string: &str) -> Result<Self> { |
| 187 | + match Self::parse(string) { |
| 188 | + Ok((remainder, object)) => { |
| 189 | + // Ensure the remainder is empty. |
| 190 | + ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\""); |
| 191 | + // Return the object. |
| 192 | + Ok(object) |
| 193 | + } |
| 194 | + Err(error) => bail!("Failed to parse string. {error}"), |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +impl<N: Network> Debug for In<N> { |
| 200 | + /// Prints the operation as a string. |
| 201 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 202 | + Display::fmt(self, f) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +impl<N: Network> Display for In<N> { |
| 207 | + /// Prints the operation to a string. |
| 208 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 209 | + // Print the operation. |
| 210 | + write!(f, "{} ", Self::opcode())?; |
| 211 | + self.operands.iter().try_for_each(|operand| write!(f, "{operand} "))?; |
| 212 | + write!(f, "into {}", self.destination) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +impl<N: Network> FromBytes for In<N> { |
| 217 | + /// Reads the operation from a buffer. |
| 218 | + fn read_le<R: Read>(mut reader: R) -> IoResult<Self> { |
| 219 | + // Initialize the array and read the operands. |
| 220 | + let operands = [Operand::read_le(&mut reader)?, Operand::read_le(&mut reader)?]; |
| 221 | + |
| 222 | + // Read the destination register. |
| 223 | + let destination = Register::read_le(&mut reader)?; |
| 224 | + |
| 225 | + // Return the operation. |
| 226 | + Ok(Self { operands, destination }) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +impl<N: Network> ToBytes for In<N> { |
| 231 | + /// Writes the operation to a buffer. |
| 232 | + fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> { |
| 233 | + // Write the operands. |
| 234 | + self.operands.iter().try_for_each(|operand| operand.write_le(&mut writer))?; |
| 235 | + // Write the destination register. |
| 236 | + self.destination.write_le(&mut writer) |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +#[cfg(test)] |
| 241 | +mod tests { |
| 242 | + use super::*; |
| 243 | + use console::network::MainnetV0; |
| 244 | + |
| 245 | + type CurrentNetwork = MainnetV0; |
| 246 | + |
| 247 | + #[test] |
| 248 | + fn test_parse() { |
| 249 | + let (string, in_) = In::<CurrentNetwork>::parse("in r0 r1 into r2").unwrap(); |
| 250 | + assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'"); |
| 251 | + assert_eq!(in_.operands.len(), 2, "The number of operands is incorrect"); |
| 252 | + assert_eq!(in_.operands[0], Operand::Register(Register::Locator(0)), "The first operand is incorrect"); |
| 253 | + assert_eq!(in_.operands[1], Operand::Register(Register::Locator(1)), "The second operand is incorrect"); |
| 254 | + assert_eq!(in_.destination, Register::Locator(2), "The destination register is incorrect"); |
| 255 | + } |
| 256 | +} |
0 commit comments