snarkvm_console_account/signature/
to_bits.rs

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