snarkvm_circuit_program/data/future/
to_bits.rs

1// Copyright (c) 2019-2025 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;
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            U16::constant(console::U16::new(argument_bits.len() as u16)).write_bits_le(vec);
43            // Write the argument.
44            vec.extend_from_slice(&argument_bits);
45        }
46    }
47
48    /// Returns the circuit future as a list of **big-endian** bits.
49    #[inline]
50    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
51        // Write the bits for the program ID.
52        let program_id_bits = self.program_id.to_bits_be();
53        U16::constant(console::U16::new(program_id_bits.len() as u16)).write_bits_be(vec);
54        vec.extend_from_slice(&program_id_bits);
55
56        // Write the bits for the function name.
57        let function_name_bits = self.function_name.to_bits_be();
58        U16::constant(console::U16::new(function_name_bits.len() as u16)).write_bits_be(vec);
59        vec.extend_from_slice(&function_name_bits);
60
61        // Write the number of arguments.
62        U8::constant(console::U8::new(self.arguments.len() as u8)).write_bits_be(vec);
63
64        // Write the arguments.
65        for argument in &self.arguments {
66            let argument_bits = argument.to_bits_be();
67            // Write the size of the argument.
68            U16::constant(console::U16::new(argument_bits.len() as u16)).write_bits_be(vec);
69            // Write the argument.
70            vec.extend_from_slice(&argument_bits);
71        }
72    }
73}