to_bits.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use super::*; 20 21 impl<N: Network> ToBits for Plaintext<N> { 22 /// Returns this plaintext as a list of **little-endian** bits. 23 fn write_bits_le(&self, vec: &mut Vec<bool>) { 24 match self { 25 Self::Literal(literal, bits_le) => { 26 // Compute the bits. 27 let bits = bits_le.get_or_init(|| { 28 let mut bits_le = PlaintextType::<N>::LITERAL_PREFIX_BITS.to_vec(); // Variant bits. 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 33 }); 34 // Extend the vector with the bits. 35 vec.extend_from_slice(bits) 36 } 37 Self::Struct(struct_, bits_le) => { 38 // Compute the bits. 39 let bits = bits_le.get_or_init(|| { 40 let mut bits_le = PlaintextType::<N>::STRUCT_PREFIX_BITS.to_vec(); // Variant bits. 41 42 // Write the length of the struct. 43 u8::try_from(struct_.len()) 44 .or_halt_with::<N>("Plaintext struct length exceeds u8::MAX") 45 .write_bits_le(&mut bits_le); 46 47 // Write each member of the struct. 48 for (identifier, value) in struct_ { 49 // Write the identifier of the member. 50 identifier.size_in_bits().write_bits_le(&mut bits_le); 51 identifier.write_bits_le(&mut bits_le); 52 53 // Write the value of the member. 54 let value_bits = value.to_bits_le(); 55 u16::try_from(value_bits.len()) 56 .or_halt_with::<N>("Plaintext member exceeds u16::MAX bits") 57 .write_bits_le(&mut bits_le); 58 bits_le.extend_from_slice(&value_bits); 59 } 60 bits_le 61 }); 62 // Extend the vector with the bits. 63 vec.extend_from_slice(bits) 64 } 65 Self::Array(array, bits_le) => { 66 // Compute the bits. 67 let bits = bits_le.get_or_init(|| { 68 let mut bits_le = PlaintextType::<N>::ARRAY_PREFIX_BITS.to_vec(); // Variant bits. 69 70 // Write the length of the array. 71 u32::try_from(array.len()) 72 .or_halt_with::<N>("Plaintext array length exceeds u32::MAX") 73 .write_bits_le(&mut bits_le); 74 75 // Write each element of the array. 76 for element in array { 77 let element_bits = element.to_bits_le(); 78 79 // Write the size of the element. 80 u16::try_from(element_bits.len()) 81 .or_halt_with::<N>("Plaintext element exceeds u16::MAX bits") 82 .write_bits_le(&mut bits_le); 83 84 // Write the element. 85 bits_le.extend(element_bits); 86 } 87 bits_le 88 }); 89 // Extend the vector with the bits. 90 vec.extend_from_slice(bits) 91 } 92 } 93 } 94 95 /// Returns this plaintext as a list of **big-endian** bits. 96 fn write_bits_be(&self, vec: &mut Vec<bool>) { 97 match self { 98 Self::Literal(literal, bits_be) => { 99 // Compute the bits. 100 let bits = bits_be.get_or_init(|| { 101 let mut bits_be = PlaintextType::<N>::LITERAL_PREFIX_BITS.to_vec(); // Variant bits. 102 literal.variant().write_bits_be(&mut bits_be); 103 literal.size_in_bits().write_bits_be(&mut bits_be); 104 literal.write_bits_be(&mut bits_be); 105 bits_be 106 }); 107 // Extend the vector with the bits. 108 vec.extend_from_slice(bits) 109 } 110 Self::Struct(struct_, bits_be) => { 111 // Compute the bits. 112 let bits = bits_be.get_or_init(|| { 113 let mut bits_be = PlaintextType::<N>::STRUCT_PREFIX_BITS.to_vec(); // Variant bits. 114 115 // Write the length of the struct. 116 u8::try_from(struct_.len()) 117 .or_halt_with::<N>("Plaintext struct length exceeds u8::MAX") 118 .write_bits_be(&mut bits_be); 119 120 // Write each member of the struct. 121 for (identifier, value) in struct_ { 122 // Write the identifier of the member. 123 identifier.size_in_bits().write_bits_be(&mut bits_be); 124 identifier.write_bits_be(&mut bits_be); 125 126 // Write the value of the member. 127 let value_bits = value.to_bits_be(); 128 u16::try_from(value_bits.len()) 129 .or_halt_with::<N>("Plaintext member exceeds u16::MAX bits") 130 .write_bits_be(&mut bits_be); 131 bits_be.extend_from_slice(&value_bits); 132 } 133 134 bits_be 135 }); 136 // Extend the vector with the bits. 137 vec.extend_from_slice(bits) 138 } 139 Self::Array(array, bits_be) => { 140 // Compute the bits. 141 let bits = bits_be.get_or_init(|| { 142 let mut bits_be = PlaintextType::<N>::ARRAY_PREFIX_BITS.to_vec(); // Variant bits. 143 144 // Write the length of the array. 145 u32::try_from(array.len()) 146 .or_halt_with::<N>("Plaintext array length exceeds u32::MAX") 147 .write_bits_be(&mut bits_be); 148 149 // Write each element of the array. 150 for element in array { 151 let element_bits = element.to_bits_be(); 152 153 // Write the size of the element. 154 u16::try_from(element_bits.len()) 155 .or_halt_with::<N>("Plaintext element exceeds u16::MAX bits") 156 .write_bits_be(&mut bits_be); 157 158 // Write the element. 159 bits_be.extend(element_bits); 160 } 161 bits_be 162 }); 163 // Extend the vector with the bits. 164 vec.extend_from_slice(bits) 165 } 166 } 167 } 168 }