snarkvm_circuit_account/compute_key/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
18impl<A: Aleo> ToFields for ComputeKey<A> {
19    type Field = Field<A>;
20
21    /// Casts a compute key into a list of base fields.
22    fn to_fields(&self) -> Vec<Self::Field> {
23        vec![self.pk_sig().to_field(), self.pr_sig().to_field()]
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::Circuit;
31    use snarkvm_circuit_network::AleoV0;
32    use snarkvm_utilities::TestRng;
33
34    use console::ToFields as ConsoleToFields;
35
36    type CurrentAleo = AleoV0;
37
38    const ITERATIONS: u64 = 100;
39
40    fn check_to_fields(mode: Mode, num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
41        let rng = &mut TestRng::default();
42
43        for i in 0..ITERATIONS {
44            // Sample a random compute key.
45            let expected = console::ComputeKey::try_from(console::PrivateKey::new(rng).unwrap()).unwrap();
46            let candidate = ComputeKey::<CurrentAleo>::new(mode, expected);
47
48            CurrentAleo::scope(format!("{mode} {i}"), || {
49                let candidate = candidate.to_fields();
50                assert_eq!(candidate.len(), 2);
51
52                let expected = expected.to_fields().unwrap();
53                assert_eq!(expected.len(), 2);
54
55                for (expected_field, candidate_field) in expected.iter().zip_eq(candidate.iter()) {
56                    assert_eq!(*expected_field, candidate_field.eject_value());
57                }
58                assert_scope!(num_constants, num_public, num_private, num_constraints);
59            });
60        }
61    }
62
63    #[test]
64    fn test_to_fields_constant() {
65        check_to_fields(Mode::Constant, 0, 0, 0, 0);
66    }
67
68    #[test]
69    fn test_to_fields_public() {
70        check_to_fields(Mode::Public, 0, 0, 0, 0);
71    }
72
73    #[test]
74    fn test_to_fields_private() {
75        check_to_fields(Mode::Private, 0, 0, 0, 0);
76    }
77}