snarkvm_circuit_account/compute_key/helpers/
to_bits.rs

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