1use crate::{Opcode, Operand, RegistersCircuit, RegistersTrait, StackTrait};
17use console::{
18 network::prelude::*,
19 program::{ArrayType, Identifier, LiteralType, Locator, Plaintext, PlaintextType, Register, RegisterType, Value},
20};
21
22pub type SerializeBits<N> = SerializeInstruction<N, { SerializeVariant::ToBits as u8 }>;
24pub type SerializeBitsRaw<N> = SerializeInstruction<N, { SerializeVariant::ToBitsRaw as u8 }>;
26
27#[derive(Debug, Clone, Eq, PartialEq)]
29pub enum SerializeVariant {
30 ToBits,
31 ToBitsRaw,
32}
33
34impl SerializeVariant {
35 pub const fn opcode(variant: u8) -> &'static str {
37 match variant {
38 0 => "serialize.bits",
39 1 => "serialize.bits.raw",
40 _ => panic!("Invalid 'serialize' instruction opcode"),
41 }
42 }
43}
44
45fn check_number_of_operands(variant: u8, num_operands: usize) -> Result<()> {
47 if num_operands != 1 {
48 bail!("Instruction '{}' expects 1 operand, found {num_operands} operands", SerializeVariant::opcode(variant))
49 }
50 Ok(())
51}
52
53fn check_operand_type_is_valid(variant: u8, operand_type: &PlaintextType<impl Network>) -> Result<()> {
55 fn check_literal_type(literal_type: &LiteralType) -> Result<()> {
57 match literal_type {
58 LiteralType::Address
59 | LiteralType::Boolean
60 | LiteralType::Field
61 | LiteralType::Group
62 | LiteralType::I8
63 | LiteralType::I16
64 | LiteralType::I32
65 | LiteralType::I64
66 | LiteralType::I128
67 | LiteralType::U8
68 | LiteralType::U16
69 | LiteralType::U32
70 | LiteralType::U64
71 | LiteralType::U128
72 | LiteralType::Scalar => Ok(()),
73 _ => bail!("Invalid literal type '{literal_type}' for 'serialize' instruction"),
74 }
75 }
76
77 match operand_type {
78 PlaintextType::Literal(literal_type) => check_literal_type(literal_type),
79 PlaintextType::Array(array_type) => match array_type.base_element_type() {
80 PlaintextType::Literal(literal_type) => check_literal_type(literal_type),
81 _ => bail!("Invalid element type '{array_type}' for 'serialize' instruction"),
82 },
83 _ => bail!("Instruction '{}' cannot take type '{operand_type}' as input", SerializeVariant::opcode(variant)),
84 }
85}
86
87fn check_destination_type_is_valid(variant: u8, destination_type: &ArrayType<impl Network>) -> Result<()> {
89 match (variant, destination_type) {
90 (0 | 1, array_type) if array_type.is_bit_array() => Ok(()),
91 _ => {
92 bail!("Instruction '{}' cannot output type '{destination_type}'", SerializeVariant::opcode(variant))
93 }
94 }
95}
96
97#[derive(Clone, PartialEq, Eq, Hash)]
99pub struct SerializeInstruction<N: Network, const VARIANT: u8> {
100 operands: Vec<Operand<N>>,
102 operand_type: PlaintextType<N>,
104 destination: Register<N>,
106 destination_type: ArrayType<N>,
108}
109
110impl<N: Network, const VARIANT: u8> SerializeInstruction<N, VARIANT> {
111 pub fn new(
113 operands: Vec<Operand<N>>,
114 operand_type: PlaintextType<N>,
115 destination: Register<N>,
116 destination_type: ArrayType<N>,
117 ) -> Result<Self> {
118 check_number_of_operands(VARIANT, operands.len())?;
120 check_operand_type_is_valid(VARIANT, &operand_type)?;
122 check_destination_type_is_valid(VARIANT, &destination_type)?;
124 Ok(Self { operands, operand_type, destination, destination_type })
126 }
127
128 pub const fn opcode() -> Opcode {
130 Opcode::Serialize(SerializeVariant::opcode(VARIANT))
131 }
132
133 pub fn operands(&self) -> &[Operand<N>] {
135 if cfg!(debug_assertions) {
137 check_number_of_operands(VARIANT, self.operands.len()).unwrap();
138 check_operand_type_is_valid(VARIANT, &self.operand_type).unwrap();
139 check_destination_type_is_valid(VARIANT, &self.destination_type).unwrap();
140 }
141 &self.operands
143 }
144
145 pub const fn operand_type(&self) -> &PlaintextType<N> {
147 &self.operand_type
148 }
149
150 #[inline]
152 pub fn destinations(&self) -> Vec<Register<N>> {
153 vec![self.destination.clone()]
154 }
155
156 #[inline]
158 pub const fn destination_type(&self) -> &ArrayType<N> {
159 &self.destination_type
160 }
161
162 #[inline]
164 pub fn contains_external_struct(&self) -> bool {
165 self.operand_type.contains_external_struct()
166 }
167}
168
169pub fn evaluate_serialize<N: Network>(
174 variant: SerializeVariant,
175 input: &Value<N>,
176 destination_type: &ArrayType<N>,
177) -> Result<Value<N>> {
178 evaluate_serialize_internal(variant as u8, input, destination_type)
179}
180
181fn evaluate_serialize_internal<N: Network>(
182 variant: u8,
183 input: &Value<N>,
184 destination_type: &ArrayType<N>,
185) -> Result<Value<N>> {
186 match (variant, destination_type) {
187 (0, array_type) if array_type.is_bit_array() => {
188 let length = **array_type.length();
190 let bits = input.to_bits_le();
192 Ok(Value::Plaintext(Plaintext::from_bit_array(bits, length)?))
194 }
195 (1, array_type) if array_type.is_bit_array() => {
196 let length = **array_type.length();
198 let bits = input.to_bits_raw_le();
200 Ok(Value::Plaintext(Plaintext::from_bit_array(bits, length)?))
202 }
203 _ => bail!(
204 "Invalid destination type '{}' for instruction '{}'",
205 destination_type,
206 SerializeVariant::opcode(variant)
207 ),
208 }
209}
210
211impl<N: Network, const VARIANT: u8> SerializeInstruction<N, VARIANT> {
212 pub fn evaluate(&self, stack: &impl StackTrait<N>, registers: &mut impl RegistersTrait<N>) -> Result<()> {
214 check_number_of_operands(VARIANT, self.operands.len())?;
216 check_operand_type_is_valid(VARIANT, &self.operand_type)?;
218 check_destination_type_is_valid(VARIANT, &self.destination_type)?;
220
221 let input = registers.load(stack, &self.operands[0])?;
223
224 let output = evaluate_serialize_internal(VARIANT, &input, &self.destination_type)?;
225
226 registers.store(stack, &self.destination, output)
228 }
229
230 pub fn execute<A: circuit::Aleo<Network = N>>(
232 &self,
233 stack: &impl StackTrait<N>,
234 registers: &mut impl RegistersCircuit<N, A>,
235 ) -> Result<()> {
236 use crate::circuit::traits::{ToBits, ToBitsRaw};
237
238 check_number_of_operands(VARIANT, self.operands.len())?;
240 check_operand_type_is_valid(VARIANT, &self.operand_type)?;
242 check_destination_type_is_valid(VARIANT, &self.destination_type)?;
244
245 let input = registers.load_circuit(stack, &self.operands[0])?;
247
248 let output = match (VARIANT, &self.destination_type) {
249 (0, array_type) if array_type.is_bit_array() => {
250 let length = **array_type.length();
252 let bits = input.to_bits_le();
254 circuit::Value::Plaintext(circuit::Plaintext::from_bit_array(bits, length)?)
256 }
257 (1, array_type) if array_type.is_bit_array() => {
258 let length = **array_type.length();
260 let bits = input.to_bits_raw_le();
262 circuit::Value::Plaintext(circuit::Plaintext::from_bit_array(bits, length)?)
264 }
265 _ => bail!("Invalid destination type '{}' for instruction '{}'", &self.destination_type, Self::opcode(),),
266 };
267
268 registers.store_circuit(stack, &self.destination, output)
270 }
271
272 #[inline]
274 pub fn finalize(&self, stack: &impl StackTrait<N>, registers: &mut impl RegistersTrait<N>) -> Result<()> {
275 self.evaluate(stack, registers)
276 }
277
278 pub fn output_types(
280 &self,
281 stack: &impl StackTrait<N>,
282 input_types: &[RegisterType<N>],
283 ) -> Result<Vec<RegisterType<N>>> {
284 check_number_of_operands(VARIANT, self.operands.len())?;
286 check_operand_type_is_valid(VARIANT, &self.operand_type)?;
288 check_destination_type_is_valid(VARIANT, &self.destination_type)?;
290
291 ensure!(input_types.len() == 1, "Expected exactly one input type");
293 match &input_types[0] {
294 RegisterType::Plaintext(plaintext_type) => {
295 ensure!(
296 plaintext_type == &self.operand_type,
297 "Input type {} does not match operand type {}",
298 input_types[0],
299 self.operand_type
300 )
301 }
302 type_ => bail!("Input type {type_} does not match operand type {}", self.operand_type),
303 }
304
305 let get_struct = |identifier: &Identifier<N>| stack.program().get_struct(identifier).cloned();
307
308 let get_external_struct = |locator: &Locator<N>| {
310 stack.get_external_stack(locator.program_id())?.program().get_struct(locator.resource()).cloned()
311 };
312
313 let size_in_bits = match VARIANT {
315 0 => self.operand_type.size_in_bits(&get_struct, &get_external_struct)?,
316 1 => self.operand_type.size_in_bits_raw(&get_struct, &get_external_struct)?,
317 variant => bail!("Invalid `serialize` variant '{variant}'"),
318 };
319
320 ensure!(
322 size_in_bits == **self.destination_type.length() as usize,
323 "The number of bits of the operand '{size_in_bits}' does not match the destination '{}'",
324 **self.destination_type.length()
325 );
326
327 Ok(vec![RegisterType::Plaintext(PlaintextType::Array(self.destination_type.clone()))])
328 }
329}
330
331impl<N: Network, const VARIANT: u8> Parser for SerializeInstruction<N, VARIANT> {
332 fn parse(string: &str) -> ParserResult<Self> {
334 fn parse_operands<N: Network>(string: &str, num_operands: usize) -> ParserResult<Vec<Operand<N>>> {
336 let mut operands = Vec::with_capacity(num_operands);
337 let mut string = string;
338
339 for _ in 0..num_operands {
340 let (next_string, _) = Sanitizer::parse_whitespaces(string)?;
342 let (next_string, operand) = Operand::parse(next_string)?;
344 string = next_string;
346 operands.push(operand);
348 }
349
350 Ok((string, operands))
351 }
352
353 let (string, _) = tag(*Self::opcode())(string)?;
355 let (string, operands) = parse_operands(string, 1)?;
357
358 let (string, _) = Sanitizer::parse_whitespaces(string)?;
360 let (string, _) = tag("(")(string)?;
362 let (string, _) = Sanitizer::parse_whitespaces(string)?;
364 let (string, operand_type) = PlaintextType::parse(string)?;
366 let (string, _) = tag(")")(string)?;
368
369 let (string, _) = Sanitizer::parse_whitespaces(string)?;
371 let (string, _) = tag("into")(string)?;
373 let (string, _) = Sanitizer::parse_whitespaces(string)?;
375 let (string, destination) = Register::parse(string)?;
377
378 let (string, _) = Sanitizer::parse_whitespaces(string)?;
380 let (string, _) = tag("(")(string)?;
382 let (string, _) = Sanitizer::parse_whitespaces(string)?;
384 let (string, destination_type) = ArrayType::parse(string)?;
386 let (string, _) = tag(")")(string)?;
388
389 match Self::new(operands, operand_type, destination, destination_type) {
391 Ok(instruction) => Ok((string, instruction)),
392 Err(e) => map_res(fail, |_: ParserResult<Self>| {
393 Err(error(format!("Failed to parse '{}' instruction: {e}", Self::opcode())))
394 })(string),
395 }
396 }
397}
398
399impl<N: Network, const VARIANT: u8> FromStr for SerializeInstruction<N, VARIANT> {
400 type Err = Error;
401
402 fn from_str(string: &str) -> Result<Self> {
404 match Self::parse(string) {
405 Ok((remainder, object)) => {
406 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
408 Ok(object)
410 }
411 Err(error) => bail!("Failed to parse string. {error}"),
412 }
413 }
414}
415
416impl<N: Network, const VARIANT: u8> Debug for SerializeInstruction<N, VARIANT> {
417 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
419 Display::fmt(self, f)
420 }
421}
422
423impl<N: Network, const VARIANT: u8> Display for SerializeInstruction<N, VARIANT> {
424 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
426 write!(f, "{} ", Self::opcode())?;
427 self.operands.iter().try_for_each(|operand| write!(f, "{operand} "))?;
428 write!(f, " ({}) into {} ({})", self.operand_type, self.destination, self.destination_type)
429 }
430}
431
432impl<N: Network, const VARIANT: u8> FromBytes for SerializeInstruction<N, VARIANT> {
433 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
435 let operand = Operand::read_le(&mut reader)?;
437 let operand_type = PlaintextType::read_le(&mut reader)?;
439 let destination = Register::read_le(&mut reader)?;
441 let destination_type = ArrayType::read_le(&mut reader)?;
443 match Self::new(vec![operand], operand_type, destination, destination_type) {
445 Ok(instruction) => Ok(instruction),
446 Err(e) => Err(error(format!("Failed to read '{}' instruction: {e}", Self::opcode()))),
447 }
448 }
449}
450
451impl<N: Network, const VARIANT: u8> ToBytes for SerializeInstruction<N, VARIANT> {
452 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
454 self.operands.iter().try_for_each(|operand| operand.write_le(&mut writer))?;
456 self.operand_type.write_le(&mut writer)?;
458 self.destination.write_le(&mut writer)?;
460 self.destination_type.write_le(&mut writer)
462 }
463}
464
465#[cfg(test)]
466mod tests {
467 use super::*;
468 use console::{network::MainnetV0, types::U32};
469
470 type CurrentNetwork = MainnetV0;
471
472 fn valid_source_types<N: Network>() -> &'static [PlaintextType<N>] {
474 &[
475 PlaintextType::Literal(LiteralType::Address),
476 PlaintextType::Literal(LiteralType::Field),
477 PlaintextType::Literal(LiteralType::Group),
478 PlaintextType::Literal(LiteralType::I8),
479 PlaintextType::Literal(LiteralType::I16),
480 PlaintextType::Literal(LiteralType::I32),
481 PlaintextType::Literal(LiteralType::I128),
482 PlaintextType::Literal(LiteralType::I64),
483 PlaintextType::Literal(LiteralType::U8),
484 PlaintextType::Literal(LiteralType::U16),
485 PlaintextType::Literal(LiteralType::U32),
486 PlaintextType::Literal(LiteralType::U64),
487 PlaintextType::Literal(LiteralType::U128),
488 PlaintextType::Literal(LiteralType::Scalar),
489 ]
490 }
491
492 fn sample_destination_type<N: Network, const VARIANT: u8>(rng: &mut TestRng) -> ArrayType<N> {
494 let array_length = 1 + (u32::rand(rng) % u32::try_from(N::MAX_ARRAY_ELEMENTS).unwrap());
496 match VARIANT {
497 0 | 1 => {
498 ArrayType::new(PlaintextType::Literal(LiteralType::Boolean), vec![U32::new(array_length)]).unwrap()
499 }
500 _ => panic!("Invalid variant"),
501 }
502 }
503
504 fn run_parser_test<const VARIANT: u8>(rng: &mut TestRng) {
505 for source_type in valid_source_types() {
506 {
507 let opcode = SerializeVariant::opcode(VARIANT);
508 let destination_type = sample_destination_type::<CurrentNetwork, VARIANT>(rng);
509 let instruction = format!("{opcode} r0 ({source_type}) into r1 ({destination_type})");
510 println!("Parsing instruction: '{instruction}'");
511
512 let (string, serialize) = SerializeInstruction::<CurrentNetwork, VARIANT>::parse(&instruction).unwrap();
513 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
514 assert_eq!(serialize.operands.len(), 1, "The number of operands is incorrect");
515 assert_eq!(
516 serialize.operands[0],
517 Operand::Register(Register::Locator(0)),
518 "The first operand is incorrect"
519 );
520 assert_eq!(&serialize.operand_type, source_type, "The operand type is incorrect");
521 assert_eq!(serialize.destination, Register::Locator(1), "The destination register is incorrect");
522 assert_eq!(&serialize.destination_type, &destination_type, "The destination type is incorrect");
523 }
524 }
525 }
526
527 #[test]
528 fn test_parse() {
529 let rng = &mut TestRng::default();
531
532 run_parser_test::<{ SerializeVariant::ToBits as u8 }>(rng);
534 run_parser_test::<{ SerializeVariant::ToBitsRaw as u8 }>(rng);
535
536 SerializeBitsRaw::<CurrentNetwork>::from_str("serialize.bits.raw r0 (boolean) into r1 ([boolean; 1u32])")
537 .unwrap();
538 }
539}