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