snarkvm_circuit_account/compute_key/helpers/
to_bits.rs

1// Copyright 2024-2025 Aleo Network Foundation
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#[cfg(feature = "console")]
19impl<A: Aleo> ToBits for ComputeKey<A> {
20    type Boolean = Boolean<A>;
21
22    /// Outputs the little-endian bit representation of the compute key *without* trailing zeros.
23    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
24        (&self).write_bits_le(vec);
25    }
26
27    /// Outputs the big-endian bit representation of the compute key *without* leading zeros.
28    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
29        (&self).write_bits_be(vec);
30    }
31}
32
33#[cfg(feature = "console")]
34impl<A: Aleo> ToBits for &ComputeKey<A> {
35    type Boolean = Boolean<A>;
36
37    /// Outputs the little-endian bit representation of the compute key *without* trailing zeros.
38    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
39        // Write the `pk_sig` bits.
40        self.pk_sig.write_bits_le(vec);
41        // Write the `pr_sig` bits.
42        self.pr_sig.write_bits_le(vec);
43    }
44
45    /// Outputs the big-endian bit representation of the compute key *without* leading zeros.
46    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
47        // Write the `pk_sig` bits.
48        self.pk_sig.write_bits_be(vec);
49        // Write the `pr_sig` bits.
50        self.pr_sig.write_bits_be(vec);
51    }
52}
53
54#[cfg(all(test, feature = "console"))]
55mod tests {
56    use super::*;
57    use crate::Circuit;
58    use snarkvm_circuit_network::AleoV0;
59    use snarkvm_utilities::TestRng;
60
61    type CurrentAleo = AleoV0;
62
63    const ITERATIONS: u64 = 100;
64
65    fn check_to_bits_le(mode: Mode, num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
66        let expected_number_of_bits = console::ComputeKey::<<CurrentAleo as Environment>::Network>::size_in_bits();
67
68        let rng = &mut TestRng::default();
69
70        for i in 0..ITERATIONS {
71            // Sample a random compute key.
72            let expected = console::ComputeKey::try_from(console::PrivateKey::new(rng).unwrap()).unwrap();
73            let candidate = ComputeKey::<CurrentAleo>::new(mode, expected);
74
75            CurrentAleo::scope(format!("{mode} {i}"), || {
76                let candidate = candidate.to_bits_le();
77                assert_eq!(expected_number_of_bits, candidate.len());
78
79                // Construct the expected bits.
80                let mut expected_bits = Vec::new();
81                expected_bits.extend(expected.pk_sig().to_bits_le());
82                expected_bits.extend(expected.pr_sig().to_bits_le());
83
84                for (expected_bit, candidate_bit) in expected_bits.iter().zip_eq(candidate.iter()) {
85                    assert_eq!(*expected_bit, candidate_bit.eject_value());
86                }
87                assert_scope!(num_constants, num_public, num_private, num_constraints);
88            });
89        }
90    }
91
92    fn check_to_bits_be(mode: Mode, num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
93        let expected_number_of_bits = console::ComputeKey::<<CurrentAleo as Environment>::Network>::size_in_bits();
94
95        let rng = &mut TestRng::default();
96
97        for i in 0..ITERATIONS {
98            // Sample a random compute key.
99            let expected = console::ComputeKey::try_from(console::PrivateKey::new(rng).unwrap()).unwrap();
100            let candidate = ComputeKey::<CurrentAleo>::new(mode, expected);
101
102            CurrentAleo::scope(format!("{mode} {i}"), || {
103                let candidate = candidate.to_bits_be();
104                assert_eq!(expected_number_of_bits, candidate.len());
105
106                // Construct the expected bits.
107                let mut expected_bits = Vec::new();
108                expected_bits.extend(expected.pk_sig().to_bits_be());
109                expected_bits.extend(expected.pr_sig().to_bits_be());
110
111                for (expected_bit, candidate_bit) in expected_bits.iter().zip_eq(candidate.iter()) {
112                    assert_eq!(*expected_bit, candidate_bit.eject_value());
113                }
114                assert_scope!(num_constants, num_public, num_private, num_constraints);
115            });
116        }
117    }
118
119    #[test]
120    fn test_to_bits_le_constant() {
121        check_to_bits_le(Mode::Constant, 506, 0, 0, 0);
122    }
123
124    #[test]
125    fn test_to_bits_le_public() {
126        check_to_bits_le(Mode::Public, 0, 0, 1010, 1014);
127    }
128
129    #[test]
130    fn test_to_bits_le_private() {
131        check_to_bits_le(Mode::Private, 0, 0, 1010, 1014);
132    }
133
134    #[test]
135    fn test_to_bits_be_constant() {
136        check_to_bits_be(Mode::Constant, 506, 0, 0, 0);
137    }
138
139    #[test]
140    fn test_to_bits_be_public() {
141        check_to_bits_be(Mode::Public, 0, 0, 1010, 1014);
142    }
143
144    #[test]
145    fn test_to_bits_be_private() {
146        check_to_bits_be(Mode::Private, 0, 0, 1010, 1014);
147    }
148}