snarkvm_circuit_account/signature/helpers/
to_fields.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
18#[cfg(feature = "console")]
19impl<A: Aleo> ToFields for Signature<A> {
20    type Field = Field<A>;
21
22    /// Casts a compute key into a list of base fields.
23    fn to_fields(&self) -> Vec<Self::Field> {
24        let mut fields = vec![self.challenge.to_field(), self.response.to_field()];
25        fields.extend(self.compute_key.to_fields());
26        fields
27    }
28}
29
30#[cfg(all(test, feature = "console"))]
31mod tests {
32    use super::*;
33    use crate::Circuit;
34    use snarkvm_circuit_network::AleoV0;
35
36    use console::ToFields as ConsoleToFields;
37    use snarkvm_utilities::TestRng;
38
39    type CurrentAleo = AleoV0;
40
41    const ITERATIONS: u64 = 128;
42
43    fn check_to_fields(
44        mode: Mode,
45        rng: &mut TestRng,
46        num_constants: u64,
47        num_public: u64,
48        num_private: u64,
49        num_constraints: u64,
50    ) {
51        for i in 0..ITERATIONS {
52            // Sample a random signature.
53            let expected = crate::helpers::generate_signature(i, rng);
54            let candidate = Signature::<CurrentAleo>::new(mode, expected);
55
56            CurrentAleo::scope(format!("{mode} {expected} {i}"), || {
57                // Perform the operation.
58                let candidate = candidate.to_fields();
59                assert_eq!(4, candidate.len());
60                assert_scope!(num_constants, num_public, num_private, num_constraints);
61
62                // Extract the bits from the base field representation.
63                let candidate_bits_le = candidate.to_bits_le();
64                assert_eq!(1012, candidate_bits_le.len());
65
66                // Ensure all integer bits match with the expected result.
67                let expected_bits = expected.to_fields().unwrap().to_bits_le();
68                for (expected_bit, candidate_bit) in expected_bits.iter().zip_eq(&candidate_bits_le) {
69                    assert_eq!(*expected_bit, candidate_bit.eject_value());
70                }
71            });
72            CurrentAleo::reset();
73        }
74    }
75
76    #[test]
77    fn test_signature_to_fields_constant() {
78        let mut rng = TestRng::default();
79        check_to_fields(Mode::Constant, &mut rng, 0, 0, 0, 0);
80    }
81
82    #[test]
83    fn test_signature_to_fields_public() {
84        let mut rng = TestRng::default();
85        check_to_fields(Mode::Public, &mut rng, 0, 0, 0, 0);
86    }
87
88    #[test]
89    fn test_signature_to_fields_private() {
90        let mut rng = TestRng::default();
91        check_to_fields(Mode::Private, &mut rng, 0, 0, 0, 0);
92    }
93}