snarkvm_synthesizer_program/constructor/
bytes.rs1use super::*;
17
18impl<N: Network> FromBytes for ConstructorCore<N> {
19 #[inline]
21 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
22 let num_commands = u16::read_le(&mut reader)?;
24 if num_commands.is_zero() {
25 return Err(error("Failed to deserialize constructor: needs at least one command"));
26 }
27 if num_commands > u16::try_from(N::MAX_COMMANDS).map_err(error)? {
28 return Err(error(format!("Failed to deserialize constructor: too many commands ({num_commands})")));
29 }
30 let mut commands = Vec::with_capacity(num_commands as usize);
31 for _ in 0..num_commands {
32 commands.push(Command::read_le(&mut reader)?);
33 }
34 let mut constructor = Self { commands: Default::default(), num_writes: 0, positions: Default::default() };
36 commands.into_iter().try_for_each(|command| constructor.add_command(command)).map_err(error)?;
37
38 Ok(constructor)
39 }
40}
41
42impl<N: Network> ToBytes for ConstructorCore<N> {
43 #[inline]
45 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
46 let num_commands = self.commands.len();
48 match 0 < num_commands && num_commands <= N::MAX_COMMANDS {
49 true => u16::try_from(num_commands).map_err(error)?.write_le(&mut writer)?,
50 false => return Err(error(format!("Failed to write {num_commands} commands as bytes"))),
51 }
52 for command in self.commands.iter() {
54 command.write_le(&mut writer)?;
55 }
56 Ok(())
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63 use crate::Constructor;
64 use console::network::MainnetV0;
65
66 type CurrentNetwork = MainnetV0;
67
68 #[test]
69 fn test_constructor_bytes() -> Result<()> {
70 let constructor_string = r"
71constructor:
72 add r0 r1 into r2;
73 add r0 r1 into r3;
74 add r0 r1 into r4;
75 add r0 r1 into r5;
76 add r0 r1 into r6;
77 add r0 r1 into r7;
78 add r0 r1 into r8;
79 add r0 r1 into r9;
80 add r0 r1 into r10;
81 add r0 r1 into r11;
82 get accounts[r0] into r12;
83 get accounts[r1] into r13;";
84
85 let expected = Constructor::<CurrentNetwork>::from_str(constructor_string)?;
86 let expected_bytes = expected.to_bytes_le()?;
87 println!("String size: {:?}, Bytecode size: {:?}", constructor_string.len(), expected_bytes.len());
88
89 let candidate = Constructor::<CurrentNetwork>::from_bytes_le(&expected_bytes)?;
90 assert_eq!(expected.to_string(), candidate.to_string());
91 assert_eq!(expected_bytes, candidate.to_bytes_le()?);
92 Ok(())
93 }
94}