Skip to main content

snarkvm_synthesizer_program/view/
bytes.rs

1// Copyright (c) 2019-2026 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
16use super::*;
17
18impl<N: Network> FromBytes for ViewCore<N> {
19    /// Reads the view function from a buffer.
20    #[inline]
21    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
22        // Read the view function name.
23        let name = Identifier::<N>::read_le(&mut reader)?;
24
25        // Read the inputs.
26        let num_inputs = u16::read_le(&mut reader)?;
27        if num_inputs > u16::try_from(N::MAX_INPUTS).map_err(error)? {
28            return Err(error(format!("Failed to deserialize view: too many inputs ({num_inputs})")));
29        }
30        let mut inputs = Vec::with_capacity(num_inputs as usize);
31        for _ in 0..num_inputs {
32            inputs.push(Input::read_le(&mut reader)?);
33        }
34
35        // Read the commands. Zero commands are permitted (passthrough / no-op views).
36        let num_commands = u16::read_le(&mut reader)?;
37        if num_commands > u16::try_from(N::MAX_COMMANDS).map_err(error)? {
38            return Err(error(format!("Failed to deserialize view: too many commands ({num_commands})")));
39        }
40        // Cap the reservation: the count is attacker-supplied, and the elements behind
41        // it may not exist.
42        let mut commands = Vec::with_capacity((num_commands as usize).min(1024));
43        for _ in 0..num_commands {
44            commands.push(Command::read_le(&mut reader)?);
45        }
46
47        // Read the outputs. Zero outputs are permitted (guard views).
48        let num_outputs = u16::read_le(&mut reader)?;
49        if num_outputs > u16::try_from(N::MAX_OUTPUTS).map_err(error)? {
50            return Err(error(format!("Failed to deserialize view: too many outputs ({num_outputs})")));
51        }
52        let mut outputs = Vec::with_capacity(num_outputs as usize);
53        for _ in 0..num_outputs {
54            outputs.push(Output::read_le(&mut reader)?);
55        }
56
57        // Initialize a new view.
58        let mut view = Self::new(name);
59        inputs.into_iter().try_for_each(|input| view.add_input(input)).map_err(error)?;
60        commands.into_iter().try_for_each(|command| view.add_command(command)).map_err(error)?;
61        outputs.into_iter().try_for_each(|output| view.add_output(output)).map_err(error)?;
62
63        Ok(view)
64    }
65}
66
67impl<N: Network> ToBytes for ViewCore<N> {
68    /// Writes the view function to a buffer.
69    #[inline]
70    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
71        // Write the view function name.
72        self.name.write_le(&mut writer)?;
73
74        // Write the number of inputs.
75        let num_inputs = self.inputs.len();
76        match num_inputs <= N::MAX_INPUTS {
77            true => u16::try_from(num_inputs).map_err(error)?.write_le(&mut writer)?,
78            false => return Err(error(format!("Failed to write {num_inputs} inputs as bytes"))),
79        }
80        for input in self.inputs.iter() {
81            input.write_le(&mut writer)?;
82        }
83
84        // Write the number of commands. Zero commands are permitted (passthrough / no-op views).
85        let num_commands = self.commands.len();
86        match num_commands <= N::MAX_COMMANDS {
87            true => u16::try_from(num_commands).map_err(error)?.write_le(&mut writer)?,
88            false => return Err(error(format!("Failed to write {num_commands} commands as bytes"))),
89        }
90        for command in self.commands.iter() {
91            command.write_le(&mut writer)?;
92        }
93
94        // Write the number of outputs. Zero outputs are permitted (guard views).
95        let num_outputs = self.outputs.len();
96        match num_outputs <= N::MAX_OUTPUTS {
97            true => u16::try_from(num_outputs).map_err(error)?.write_le(&mut writer)?,
98            false => return Err(error(format!("Failed to write {num_outputs} outputs as bytes"))),
99        }
100        for output in self.outputs.iter() {
101            output.write_le(&mut writer)?;
102        }
103
104        Ok(())
105    }
106}