Skip to main content

snarkvm_circuit_program/data/future/
to_bits.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::*;
17use snarkvm_circuit_types::{U8, U32};
18
19impl<A: Aleo> ToBits for Future<A> {
20    type Boolean = Boolean<A>;
21
22    /// Returns the circuit future as a list of **little-endian** bits.
23    #[inline]
24    fn write_bits_le(&self, vec: &mut Vec<Boolean<A>>) {
25        // Write the bits for the program ID.
26        let program_id_bits = self.program_id.to_bits_le();
27        U16::constant(console::U16::new(program_id_bits.len() as u16)).write_bits_le(vec);
28        vec.extend_from_slice(&program_id_bits);
29
30        // Write the bits for the function name.
31        let function_name_bits = self.function_name.to_bits_le();
32        U16::constant(console::U16::new(function_name_bits.len() as u16)).write_bits_le(vec);
33        vec.extend_from_slice(&function_name_bits);
34
35        // Write the number of arguments.
36        U8::constant(console::U8::new(self.arguments.len() as u8)).write_bits_le(vec);
37
38        // Write the arguments.
39        for argument in &self.arguments {
40            let argument_bits = argument.to_bits_le();
41            // Write the size of the argument.
42            let argument_length = argument_bits.len();
43            match argument_length <= u16::MAX as usize {
44                true => U16::constant(console::U16::new(argument_length as u16)).write_bits_le(vec),
45                false => U32::constant(console::U32::new(argument_length as u32)).write_bits_le(vec),
46            }
47            // Write the argument.
48            vec.extend_from_slice(&argument_bits);
49        }
50    }
51
52    /// Returns the circuit future as a list of **big-endian** bits.
53    #[inline]
54    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
55        // Write the bits for the program ID.
56        let program_id_bits = self.program_id.to_bits_be();
57        U16::constant(console::U16::new(program_id_bits.len() as u16)).write_bits_be(vec);
58        vec.extend_from_slice(&program_id_bits);
59
60        // Write the bits for the function name.
61        let function_name_bits = self.function_name.to_bits_be();
62        U16::constant(console::U16::new(function_name_bits.len() as u16)).write_bits_be(vec);
63        vec.extend_from_slice(&function_name_bits);
64
65        // Write the number of arguments.
66        U8::constant(console::U8::new(self.arguments.len() as u8)).write_bits_be(vec);
67
68        // Write the arguments.
69        for argument in &self.arguments {
70            let argument_bits = argument.to_bits_be();
71            // Write the size of the argument.
72            let argument_length = argument_bits.len();
73            match argument_length <= u16::MAX as usize {
74                true => U16::constant(console::U16::new(argument_length as u16)).write_bits_be(vec),
75                false => U32::constant(console::U32::new(argument_length as u32)).write_bits_be(vec),
76            }
77            // Write the argument.
78            vec.extend_from_slice(&argument_bits);
79        }
80    }
81}