snarkvm_console_account/compute_key/
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<N: Network> ToBits for ComputeKey<N> {
19    /// Returns the little-endian bits of the compute key.
20    fn write_bits_le(&self, vec: &mut Vec<bool>) {
21        // Write the `pk_sig` bits.
22        self.pk_sig.write_bits_le(vec);
23        // Write the `pr_sig` bits.
24        self.pr_sig.write_bits_le(vec);
25    }
26
27    /// Returns the big-endian bits of the compute key.
28    fn write_bits_be(&self, vec: &mut Vec<bool>) {
29        // Write the `pk_sig` bits.
30        self.pk_sig.write_bits_be(vec);
31        // Write the `pr_sig` bits.
32        self.pr_sig.write_bits_be(vec);
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use snarkvm_console_network::MainnetV0;
40
41    type CurrentNetwork = MainnetV0;
42
43    const ITERATIONS: u64 = 1_000;
44
45    #[test]
46    fn test_to_bits_le() {
47        let rng = &mut TestRng::default();
48
49        for _ in 0..ITERATIONS {
50            // Sample a random compute_key.
51            let compute_key = ComputeKey::<CurrentNetwork>::try_from(PrivateKey::new(rng).unwrap()).unwrap();
52
53            let candidate = compute_key.to_bits_le();
54            assert_eq!(ComputeKey::<CurrentNetwork>::size_in_bits(), candidate.len());
55
56            // Construct the expected bits.
57            let mut expected = Vec::new();
58            expected.extend(compute_key.pk_sig.to_bits_le());
59            expected.extend(compute_key.pr_sig.to_bits_le());
60
61            for (expected, candidate) in expected.iter().zip_eq(&candidate) {
62                assert_eq!(expected, candidate);
63            }
64        }
65    }
66
67    #[test]
68    fn test_to_bits_be() {
69        let rng = &mut TestRng::default();
70
71        for _ in 0..ITERATIONS {
72            // Sample a random compute_key.
73            let compute_key = ComputeKey::<CurrentNetwork>::try_from(PrivateKey::new(rng).unwrap()).unwrap();
74
75            let candidate = compute_key.to_bits_be();
76            assert_eq!(ComputeKey::<CurrentNetwork>::size_in_bits(), candidate.len());
77
78            // Construct the expected bits.
79            let mut expected = Vec::new();
80            expected.extend(compute_key.pk_sig.to_bits_be());
81            expected.extend(compute_key.pr_sig.to_bits_be());
82
83            for (expected, candidate) in expected.iter().zip_eq(&candidate) {
84                assert_eq!(expected, candidate);
85            }
86        }
87    }
88}