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        let mut commands = Vec::with_capacity(num_commands as usize);
41        for _ in 0..num_commands {
42            commands.push(Command::read_le(&mut reader)?);
43        }
44
45        // Read the outputs. Zero outputs are permitted (guard views).
46        let num_outputs = u16::read_le(&mut reader)?;
47        if num_outputs > u16::try_from(N::MAX_OUTPUTS).map_err(error)? {
48            return Err(error(format!("Failed to deserialize view: too many outputs ({num_outputs})")));
49        }
50        let mut outputs = Vec::with_capacity(num_outputs as usize);
51        for _ in 0..num_outputs {
52            outputs.push(Output::read_le(&mut reader)?);
53        }
54
55        // Initialize a new view.
56        let mut view = Self::new(name);
57        inputs.into_iter().try_for_each(|input| view.add_input(input)).map_err(error)?;
58        commands.into_iter().try_for_each(|command| view.add_command(command)).map_err(error)?;
59        outputs.into_iter().try_for_each(|output| view.add_output(output)).map_err(error)?;
60
61        Ok(view)
62    }
63}
64
65impl<N: Network> ToBytes for ViewCore<N> {
66    /// Writes the view function to a buffer.
67    #[inline]
68    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
69        // Write the view function name.
70        self.name.write_le(&mut writer)?;
71
72        // Write the number of inputs.
73        let num_inputs = self.inputs.len();
74        match num_inputs <= N::MAX_INPUTS {
75            true => u16::try_from(num_inputs).map_err(error)?.write_le(&mut writer)?,
76            false => return Err(error(format!("Failed to write {num_inputs} inputs as bytes"))),
77        }
78        for input in self.inputs.iter() {
79            input.write_le(&mut writer)?;
80        }
81
82        // Write the number of commands. Zero commands are permitted (passthrough / no-op views).
83        let num_commands = self.commands.len();
84        match num_commands <= N::MAX_COMMANDS {
85            true => u16::try_from(num_commands).map_err(error)?.write_le(&mut writer)?,
86            false => return Err(error(format!("Failed to write {num_commands} commands as bytes"))),
87        }
88        for command in self.commands.iter() {
89            command.write_le(&mut writer)?;
90        }
91
92        // Write the number of outputs. Zero outputs are permitted (guard views).
93        let num_outputs = self.outputs.len();
94        match num_outputs <= N::MAX_OUTPUTS {
95            true => u16::try_from(num_outputs).map_err(error)?.write_le(&mut writer)?,
96            false => return Err(error(format!("Failed to write {num_outputs} outputs as bytes"))),
97        }
98        for output in self.outputs.iter() {
99            output.write_le(&mut writer)?;
100        }
101
102        Ok(())
103    }
104}