Skip to main content

snarkvm_circuit_account/compute_key/
from.rs

1// Copyright (c) 2019-2026 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> From<(Group<A>, Group<A>)> for ComputeKey<A> {
19    /// Derives the account compute key from a tuple `(pk_sig, pr_sig)`.
20    fn from((pk_sig, pr_sig): (Group<A>, Group<A>)) -> Self {
21        // Compute sk_prf := HashToScalar(pk_sig || pr_sig).
22        let sk_prf = A::hash_to_scalar_psd4(&[pk_sig.to_x_coordinate(), pr_sig.to_x_coordinate()]);
23        // Output the compute key.
24        Self { pk_sig, pr_sig, sk_prf }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use crate::{Circuit, helpers::generate_account};
32
33    use anyhow::Result;
34    use snarkvm_circuit_network::AleoV0;
35
36    const ITERATIONS: u64 = 10;
37
38    fn check_from(
39        mode: Mode,
40        num_constants: u64,
41        num_public: u64,
42        num_private: u64,
43        num_constraints: u64,
44    ) -> Result<()> {
45        for i in 0..ITERATIONS {
46            // Generate a private key, compute key, view key, and address.
47            let (_, compute_key, _, _) = generate_account()?;
48
49            // Initialize the pk_sig and pr_sig.
50            let pk_sig = Group::new(mode, compute_key.pk_sig());
51            let pr_sig = Group::new(mode, compute_key.pr_sig());
52
53            Circuit::scope(format!("{mode} {i}"), || {
54                let candidate = ComputeKey::<AleoV0>::from((pk_sig, pr_sig));
55                assert_eq!(compute_key, candidate.eject_value());
56                if i > 0 {
57                    assert_scope!(num_constants, num_public, num_private, num_constraints);
58                }
59            });
60            Circuit::reset();
61        }
62        Ok(())
63    }
64
65    #[test]
66    fn test_from_constant() -> Result<()> {
67        check_from(Mode::Constant, 254, 0, 0, 0)
68    }
69
70    #[test]
71    fn test_from_public() -> Result<()> {
72        check_from(Mode::Public, 1, 0, 845, 847)
73    }
74
75    #[test]
76    fn test_from_private() -> Result<()> {
77        check_from(Mode::Private, 1, 0, 845, 847)
78    }
79}