snarkvm_synthesizer_program/closure/
bytes.rsuse super::*;
impl<N: Network, Instruction: InstructionTrait<N>> FromBytes for ClosureCore<N, Instruction> {
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let name = Identifier::<N>::read_le(&mut reader)?;
let num_inputs = u16::read_le(&mut reader)?;
if num_inputs.is_zero() {
return Err(error("Failed to deserialize a closure: needs at least one input".to_string()));
}
if num_inputs > u16::try_from(N::MAX_INPUTS).map_err(error)? {
return Err(error(format!("Failed to deserialize a closure: too many inputs ({num_inputs})")));
}
let mut inputs = Vec::with_capacity(num_inputs as usize);
for _ in 0..num_inputs {
inputs.push(Input::read_le(&mut reader)?);
}
let num_instructions = u32::read_le(&mut reader)?;
if num_instructions.is_zero() {
return Err(error("Failed to deserialize a closure: needs at least one instruction".to_string()));
}
if num_instructions > u32::try_from(N::MAX_INSTRUCTIONS).map_err(error)? {
return Err(error(format!("Failed to deserialize a closure: too many instructions ({num_instructions})")));
}
let mut instructions = Vec::with_capacity(num_instructions as usize);
for _ in 0..num_instructions {
instructions.push(Instruction::read_le(&mut reader)?);
}
let num_outputs = u16::read_le(&mut reader)?;
if num_outputs > u16::try_from(N::MAX_OUTPUTS).map_err(error)? {
return Err(error(format!("Failed to deserialize a closure: too many outputs ({num_outputs})")));
}
let mut outputs = Vec::with_capacity(num_outputs as usize);
for _ in 0..num_outputs {
outputs.push(Output::read_le(&mut reader)?);
}
let mut closure = Self::new(name);
inputs.into_iter().try_for_each(|input| closure.add_input(input)).map_err(error)?;
instructions.into_iter().try_for_each(|instruction| closure.add_instruction(instruction)).map_err(error)?;
outputs.into_iter().try_for_each(|output| closure.add_output(output)).map_err(error)?;
Ok(closure)
}
}
impl<N: Network, Instruction: InstructionTrait<N>> ToBytes for ClosureCore<N, Instruction> {
#[inline]
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.name.write_le(&mut writer)?;
let num_inputs = self.inputs.len();
match 0 < num_inputs && num_inputs <= N::MAX_INPUTS {
true => u16::try_from(num_inputs).map_err(error)?.write_le(&mut writer)?,
false => return Err(error(format!("Failed to write {num_inputs} inputs as bytes"))),
}
for input in self.inputs.iter() {
input.write_le(&mut writer)?;
}
let num_instructions = self.instructions.len();
match 0 < num_instructions && num_instructions <= N::MAX_INSTRUCTIONS {
true => u32::try_from(num_instructions).map_err(error)?.write_le(&mut writer)?,
false => return Err(error(format!("Failed to write {num_instructions} instructions as bytes"))),
}
for instruction in self.instructions.iter() {
instruction.write_le(&mut writer)?;
}
let num_outputs = self.outputs.len();
match num_outputs <= N::MAX_OUTPUTS {
true => u16::try_from(num_outputs).map_err(error)?.write_le(&mut writer)?,
false => return Err(error(format!("Failed to write {num_outputs} outputs as bytes"))),
}
for output in self.outputs.iter() {
output.write_le(&mut writer)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Closure;
use console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
#[test]
fn test_closure_bytes() -> Result<()> {
let closure_string = r"
closure main:
input r0 as field;
input r1 as field;
add r0 r1 into r2;
add r0 r1 into r3;
add r0 r1 into r4;
add r0 r1 into r5;
add r0 r1 into r6;
add r0 r1 into r7;
add r0 r1 into r8;
add r0 r1 into r9;
add r0 r1 into r10;
add r0 r1 into r11;
output r11 as field;";
let expected = Closure::<CurrentNetwork>::from_str(closure_string)?;
let expected_bytes = expected.to_bytes_le()?;
println!("String size: {:?}, Bytecode size: {:?}", closure_string.as_bytes().len(), expected_bytes.len());
let candidate = Closure::<CurrentNetwork>::from_bytes_le(&expected_bytes)?;
assert_eq!(expected.to_string(), candidate.to_string());
assert_eq!(expected_bytes, candidate.to_bytes_le()?);
Ok(())
}
}