Skip to main content

snarkvm_console_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::*;
17
18impl<N: Network> ToBits for Future<N> {
19    /// Returns the future as a list of **little-endian** bits.
20    // Note: Any updates to bit serialization will require updating `FinalizeType::future_size_in_bits`.
21    #[inline]
22    fn write_bits_le(&self, vec: &mut Vec<bool>) {
23        // Write the bits for the program ID.
24        let program_id_bits = self.program_id.to_bits_le();
25        u16::try_from(program_id_bits.len()).or_halt_with::<N>("Program ID exceeds u16::MAX bits").write_bits_le(vec);
26        vec.extend_from_slice(&program_id_bits);
27
28        // Write the bits for the function name.
29        let function_name_bits = self.function_name.to_bits_le();
30        u16::try_from(function_name_bits.len())
31            .or_halt_with::<N>("Function name exceeds u16::MAX bits")
32            .write_bits_le(vec);
33        vec.extend_from_slice(&function_name_bits);
34
35        // Write the number of arguments.
36        u8::try_from(self.arguments.len()).or_halt_with::<N>("arguments exceed u8::MAX").write_bits_le(vec);
37
38        // Write the arguments.
39        for argument in &self.arguments {
40            let argument_bits = argument.to_bits_le();
41
42            // Write the size of the argument.
43            let argument_length = argument_bits.len();
44            match argument_length <= u16::MAX as usize {
45                true => u16::try_from(argument_length)
46                    .or_halt_with::<N>("argument exceeds u16::MAX bits")
47                    .write_bits_le(vec),
48                false => u32::try_from(argument_length)
49                    .or_halt_with::<N>("argument exceeds u32::MAX bits")
50                    .write_bits_le(vec),
51            }
52
53            // Write the argument.
54            vec.extend_from_slice(&argument_bits);
55        }
56    }
57
58    /// Returns the future as a list of **big-endian** bits.
59    // Note: Any updates to bit serialization will require updating `FinalizeType::future_size_in_bits`.
60    #[inline]
61    fn write_bits_be(&self, vec: &mut Vec<bool>) {
62        // Write the bits for the program ID.
63        let program_id_bits = self.program_id.to_bits_be();
64        u16::try_from(program_id_bits.len()).or_halt_with::<N>("Program ID exceeds u16::MAX bits").write_bits_be(vec);
65        vec.extend_from_slice(&program_id_bits);
66
67        // Write the bits for the function name.
68        let function_name_bits = self.function_name.to_bits_be();
69        u16::try_from(function_name_bits.len())
70            .or_halt_with::<N>("Function name exceeds u16::MAX bits")
71            .write_bits_be(vec);
72        vec.extend_from_slice(&function_name_bits);
73
74        // Write the number of arguments.
75        u8::try_from(self.arguments.len()).or_halt_with::<N>("arguments exceed u8::MAX").write_bits_be(vec);
76
77        // Write the arguments.
78        for argument in &self.arguments {
79            let argument_bits = argument.to_bits_be();
80
81            // Write the size of the argument.
82            let argument_length = argument_bits.len();
83            match argument_length <= u16::MAX as usize {
84                true => u16::try_from(argument_length)
85                    .or_halt_with::<N>("argument exceeds u16::MAX bits")
86                    .write_bits_be(vec),
87                false => u32::try_from(argument_length)
88                    .or_halt_with::<N>("argument exceeds u32::MAX bits")
89                    .write_bits_be(vec),
90            }
91
92            // Write the argument.
93            vec.extend_from_slice(&argument_bits);
94        }
95    }
96}