/ console / program / src / data / plaintext / to_bits.rs
to_bits.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the alphavm 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  
 16  use super::*;
 17  
 18  impl<N: Network> ToBits for Plaintext<N> {
 19      /// Returns this plaintext as a list of **little-endian** bits.
 20      fn write_bits_le(&self, vec: &mut Vec<bool>) {
 21          match self {
 22              Self::Literal(literal, bits_le) => {
 23                  // Compute the bits.
 24                  let bits = bits_le.get_or_init(|| {
 25                      let mut bits_le = PlaintextType::<N>::LITERAL_PREFIX_BITS.to_vec(); // Variant bits.
 26                      literal.variant().write_bits_le(&mut bits_le);
 27                      literal.size_in_bits().write_bits_le(&mut bits_le);
 28                      literal.write_bits_le(&mut bits_le);
 29                      bits_le
 30                  });
 31                  // Extend the vector with the bits.
 32                  vec.extend_from_slice(bits)
 33              }
 34              Self::Struct(struct_, bits_le) => {
 35                  // Compute the bits.
 36                  let bits = bits_le.get_or_init(|| {
 37                      let mut bits_le = PlaintextType::<N>::STRUCT_PREFIX_BITS.to_vec(); // Variant bits.
 38  
 39                      // Write the length of the struct.
 40                      u8::try_from(struct_.len())
 41                          .or_halt_with::<N>("Plaintext struct length exceeds u8::MAX")
 42                          .write_bits_le(&mut bits_le);
 43  
 44                      // Write each member of the struct.
 45                      for (identifier, value) in struct_ {
 46                          // Write the identifier of the member.
 47                          identifier.size_in_bits().write_bits_le(&mut bits_le);
 48                          identifier.write_bits_le(&mut bits_le);
 49  
 50                          // Write the value of the member.
 51                          let value_bits = value.to_bits_le();
 52                          u16::try_from(value_bits.len())
 53                              .or_halt_with::<N>("Plaintext member exceeds u16::MAX bits")
 54                              .write_bits_le(&mut bits_le);
 55                          bits_le.extend_from_slice(&value_bits);
 56                      }
 57                      bits_le
 58                  });
 59                  // Extend the vector with the bits.
 60                  vec.extend_from_slice(bits)
 61              }
 62              Self::Array(array, bits_le) => {
 63                  // Compute the bits.
 64                  let bits = bits_le.get_or_init(|| {
 65                      let mut bits_le = PlaintextType::<N>::ARRAY_PREFIX_BITS.to_vec(); // Variant bits.
 66  
 67                      // Write the length of the array.
 68                      u32::try_from(array.len())
 69                          .or_halt_with::<N>("Plaintext array length exceeds u32::MAX")
 70                          .write_bits_le(&mut bits_le);
 71  
 72                      // Write each element of the array.
 73                      for element in array {
 74                          let element_bits = element.to_bits_le();
 75  
 76                          // Write the size of the element.
 77                          u16::try_from(element_bits.len())
 78                              .or_halt_with::<N>("Plaintext element exceeds u16::MAX bits")
 79                              .write_bits_le(&mut bits_le);
 80  
 81                          // Write the element.
 82                          bits_le.extend(element_bits);
 83                      }
 84                      bits_le
 85                  });
 86                  // Extend the vector with the bits.
 87                  vec.extend_from_slice(bits)
 88              }
 89          }
 90      }
 91  
 92      /// Returns this plaintext as a list of **big-endian** bits.
 93      fn write_bits_be(&self, vec: &mut Vec<bool>) {
 94          match self {
 95              Self::Literal(literal, bits_be) => {
 96                  // Compute the bits.
 97                  let bits = bits_be.get_or_init(|| {
 98                      let mut bits_be = PlaintextType::<N>::LITERAL_PREFIX_BITS.to_vec(); // Variant bits.
 99                      literal.variant().write_bits_be(&mut bits_be);
100                      literal.size_in_bits().write_bits_be(&mut bits_be);
101                      literal.write_bits_be(&mut bits_be);
102                      bits_be
103                  });
104                  // Extend the vector with the bits.
105                  vec.extend_from_slice(bits)
106              }
107              Self::Struct(struct_, bits_be) => {
108                  // Compute the bits.
109                  let bits = bits_be.get_or_init(|| {
110                      let mut bits_be = PlaintextType::<N>::STRUCT_PREFIX_BITS.to_vec(); // Variant bits.
111  
112                      // Write the length of the struct.
113                      u8::try_from(struct_.len())
114                          .or_halt_with::<N>("Plaintext struct length exceeds u8::MAX")
115                          .write_bits_be(&mut bits_be);
116  
117                      // Write each member of the struct.
118                      for (identifier, value) in struct_ {
119                          // Write the identifier of the member.
120                          identifier.size_in_bits().write_bits_be(&mut bits_be);
121                          identifier.write_bits_be(&mut bits_be);
122  
123                          // Write the value of the member.
124                          let value_bits = value.to_bits_be();
125                          u16::try_from(value_bits.len())
126                              .or_halt_with::<N>("Plaintext member exceeds u16::MAX bits")
127                              .write_bits_be(&mut bits_be);
128                          bits_be.extend_from_slice(&value_bits);
129                      }
130  
131                      bits_be
132                  });
133                  // Extend the vector with the bits.
134                  vec.extend_from_slice(bits)
135              }
136              Self::Array(array, bits_be) => {
137                  // Compute the bits.
138                  let bits = bits_be.get_or_init(|| {
139                      let mut bits_be = PlaintextType::<N>::ARRAY_PREFIX_BITS.to_vec(); // Variant bits.
140  
141                      // Write the length of the array.
142                      u32::try_from(array.len())
143                          .or_halt_with::<N>("Plaintext array length exceeds u32::MAX")
144                          .write_bits_be(&mut bits_be);
145  
146                      // Write each element of the array.
147                      for element in array {
148                          let element_bits = element.to_bits_be();
149  
150                          // Write the size of the element.
151                          u16::try_from(element_bits.len())
152                              .or_halt_with::<N>("Plaintext element exceeds u16::MAX bits")
153                              .write_bits_be(&mut bits_be);
154  
155                          // Write the element.
156                          bits_be.extend(element_bits);
157                      }
158                      bits_be
159                  });
160                  // Extend the vector with the bits.
161                  vec.extend_from_slice(bits)
162              }
163          }
164      }
165  }