Skip to main content

snarkvm_circuit_program/data/record/entry/
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
18/// Visibility prefix bits for (de)serializing the `Constant` variant.
19const VISIBILITY_CONSTANT: [bool; 2] = [false, false];
20/// Visibility prefix bits for (de)serializing the `Public` variant.
21const VISIBILITY_PUBLIC: [bool; 2] = [false, true];
22/// Visibility prefix bits for (de)serializing the `Private` variant.
23const VISIBILITY_PRIVATE: [bool; 2] = [true, false];
24
25impl<A: Aleo> Entry<A, Plaintext<A>> {
26    /// Returns this entry as a list of **little-endian** bits, with the specified mode.
27    pub(super) fn write_bits_le_with_visibility_as_mode(&self, vec: &mut Vec<Boolean<A>>, mode: Mode) {
28        // A helper function to construct a `Boolean` with the specified mode.
29        // This is needed to avoid introducing new variables for constant booleans.
30        let boolean_new = |mode: Mode, value: bool| match mode {
31            Mode::Constant => Boolean::constant(value),
32            Mode::Public => Boolean::new(Mode::Public, value),
33            Mode::Private => Boolean::new(Mode::Private, value),
34        };
35        // Write the variant bits.
36        let visibility_bits = match self {
37            Self::Constant(..) => VISIBILITY_CONSTANT,
38            Self::Public(..) => VISIBILITY_PUBLIC,
39            Self::Private(..) => VISIBILITY_PRIVATE,
40        };
41        vec.extend(visibility_bits.iter().map(|&bit| boolean_new(mode, bit)));
42        // Write the data bits.
43        match self {
44            Self::Constant(plaintext) => plaintext.write_bits_le(vec),
45            Self::Public(plaintext) => plaintext.write_bits_le(vec),
46            Self::Private(plaintext) => plaintext.write_bits_le(vec),
47        };
48    }
49}
50
51impl<A: Aleo> ToBits for Entry<A, Plaintext<A>> {
52    type Boolean = Boolean<A>;
53
54    /// Returns this entry as a list of **little-endian** bits.
55    fn write_bits_le(&self, vec: &mut Vec<Boolean<A>>) {
56        self.write_bits_le_with_visibility_as_mode(vec, Mode::Constant);
57    }
58
59    /// Returns this entry as a list of **big-endian** bits.
60    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
61        let visibility_bits = match self {
62            Self::Constant(..) => VISIBILITY_CONSTANT,
63            Self::Public(..) => VISIBILITY_PUBLIC,
64            Self::Private(..) => VISIBILITY_PRIVATE,
65        };
66        vec.extend(visibility_bits.iter().map(|&bit| Boolean::constant(bit)));
67        match self {
68            Self::Constant(plaintext) => plaintext.write_bits_be(vec),
69            Self::Public(plaintext) => plaintext.write_bits_be(vec),
70            Self::Private(plaintext) => plaintext.write_bits_be(vec),
71        };
72    }
73}
74
75impl<A: Aleo> ToBits for Entry<A, Ciphertext<A>> {
76    type Boolean = Boolean<A>;
77
78    /// Returns this entry as a list of **little-endian** bits.
79    fn write_bits_le(&self, vec: &mut Vec<Boolean<A>>) {
80        let visibility_bits = match self {
81            Self::Constant(..) => VISIBILITY_CONSTANT,
82            Self::Public(..) => VISIBILITY_PUBLIC,
83            Self::Private(..) => VISIBILITY_PRIVATE,
84        };
85        vec.extend(visibility_bits.iter().map(|&bit| Boolean::constant(bit)));
86        match self {
87            Self::Constant(plaintext) => plaintext.write_bits_le(vec),
88            Self::Public(plaintext) => plaintext.write_bits_le(vec),
89            Self::Private(plaintext) => plaintext.write_bits_le(vec),
90        };
91    }
92
93    /// Returns this entry as a list of **big-endian** bits.
94    fn write_bits_be(&self, vec: &mut Vec<Boolean<A>>) {
95        let visibility_bits = match self {
96            Self::Constant(..) => VISIBILITY_CONSTANT,
97            Self::Public(..) => VISIBILITY_PUBLIC,
98            Self::Private(..) => VISIBILITY_PRIVATE,
99        };
100        vec.extend(visibility_bits.iter().map(|&bit| Boolean::constant(bit)));
101        match self {
102            Self::Constant(plaintext) => plaintext.write_bits_be(vec),
103            Self::Public(plaintext) => plaintext.write_bits_be(vec),
104            Self::Private(plaintext) => plaintext.write_bits_be(vec),
105        };
106    }
107}