snarkvm_circuit_program/data/plaintext/
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::*;
17
18impl<A: Aleo> ToBits for Plaintext<A> {
19    type Boolean = Boolean<A>;
20
21    /// Returns this plaintext as a list of **little-endian** bits.
22    fn write_bits_le(&self, vec: &mut Vec<Boolean<A>>) {
23        match self {
24            Self::Literal(literal, bits_le) => {
25                // Compute the bits of the literal.
26                let bits = bits_le.get_or_init(|| {
27                    let mut bits_le = Vec::new();
28                    bits_le.extend([Boolean::constant(false), Boolean::constant(false)]); // Variant bit.
29                    literal.variant().write_bits_le(&mut bits_le);
30                    literal.size_in_bits().write_bits_le(&mut bits_le);
31                    literal.write_bits_le(&mut bits_le);
32                    bits_le.shrink_to_fit();
33                    bits_le
34                });
35                // Extend the vector with the bits of the literal.
36                vec.extend_from_slice(bits);
37            }
38            Self::Struct(members, bits_le) => {
39                // Compute the bits of the struct.
40                let bits = bits_le.get_or_init(|| {
41                    let mut bits_le = Vec::new();
42                    bits_le.extend([Boolean::constant(false), Boolean::constant(true)]); // Variant bit.
43                    U8::constant(console::U8::new(members.len() as u8)).write_bits_le(&mut bits_le);
44                    for (identifier, value) in members {
45                        let value_bits = value.to_bits_le();
46                        identifier.size_in_bits().write_bits_le(&mut bits_le);
47                        identifier.write_bits_le(&mut bits_le);
48                        U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_le(&mut bits_le);
49                        bits_le.extend(value_bits);
50                    }
51                    bits_le.shrink_to_fit();
52                    bits_le
53                });
54                // Extend the vector with the bits of the struct.
55                vec.extend_from_slice(bits);
56            }
57            Self::Array(elements, bits_le) => {
58                // Compute the bits of the array.
59                let bits = bits_le.get_or_init(|| {
60                    let mut bits_le = Vec::new();
61                    bits_le.extend([Boolean::constant(true), Boolean::constant(false)]); // Variant bit.
62                    U32::constant(console::U32::new(elements.len() as u32)).write_bits_le(&mut bits_le);
63                    for value in elements {
64                        let value_bits = value.to_bits_le();
65                        U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_le(&mut bits_le);
66                        bits_le.extend(value_bits);
67                    }
68                    bits_le.shrink_to_fit();
69                    bits_le
70                });
71                // Extend the vector with the bits of the array.
72                vec.extend_from_slice(bits);
73            }
74        }
75    }
76
77    /// Returns this plaintext as a list of **big-endian** bits.
78    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
79        match self {
80            Self::Literal(literal, bits_be) => {
81                // Compute the bits of the literal.
82                let bits = bits_be.get_or_init(|| {
83                    let mut bits_be = Vec::new();
84                    bits_be.extend([Boolean::constant(false), Boolean::constant(false)]); // Variant bit.
85                    literal.variant().write_bits_be(&mut bits_be);
86                    literal.size_in_bits().write_bits_be(&mut bits_be);
87                    literal.write_bits_be(&mut bits_be);
88                    bits_be.shrink_to_fit();
89                    bits_be
90                });
91                // Extend the vector with the bits of the literal.
92                vec.extend_from_slice(bits);
93            }
94            Self::Struct(members, bits_be) => {
95                // Compute the bits of the struct.
96                let bits = bits_be.get_or_init(|| {
97                    let mut bits_be = Vec::new();
98                    bits_be.extend([Boolean::constant(false), Boolean::constant(true)]); // Variant bit.
99                    U8::constant(console::U8::new(members.len() as u8)).write_bits_be(&mut bits_be);
100                    for (identifier, value) in members {
101                        let value_bits = value.to_bits_be();
102                        identifier.size_in_bits().write_bits_be(&mut bits_be);
103                        identifier.write_bits_be(&mut bits_be);
104                        U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_be(&mut bits_be);
105                        bits_be.extend(value_bits);
106                    }
107                    bits_be.shrink_to_fit();
108                    bits_be
109                });
110                // Extend the vector with the bits of the struct.
111                vec.extend_from_slice(bits)
112            }
113            Self::Array(elements, bits_be) => {
114                // Compute the bits of the array.
115                let bits = bits_be.get_or_init(|| {
116                    let mut bits_be = Vec::new();
117                    bits_be.extend([Boolean::constant(true), Boolean::constant(false)]); // Variant bit.
118                    U32::constant(console::U32::new(elements.len() as u32)).write_bits_be(&mut bits_be);
119                    for value in elements {
120                        let value_bits = value.to_bits_be();
121                        U16::constant(console::U16::new(value_bits.len() as u16)).write_bits_be(&mut bits_be);
122                        bits_be.extend(value_bits);
123                    }
124                    bits_be.shrink_to_fit();
125                    bits_be
126                });
127                // Extend the vector with the bits of the array.
128                vec.extend_from_slice(bits)
129            }
130        }
131    }
132}