Skip to main content

snarkvm_circuit_account/view_key/
mod.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
16mod from_private_key;
17mod to_address;
18
19#[cfg(test)]
20use snarkvm_circuit_types::environment::assert_scope;
21
22use crate::PrivateKey;
23use snarkvm_circuit_network::Aleo;
24use snarkvm_circuit_types::{Address, Scalar, environment::prelude::*};
25
26use core::ops::Deref;
27use std::cell::OnceCell;
28
29/// The account view key is able to decrypt records and ciphertext.
30pub struct ViewKey<A: Aleo>(Scalar<A>, OnceCell<Address<A>>);
31
32impl<A: Aleo> Inject for ViewKey<A> {
33    type Primitive = console::ViewKey<A::Network>;
34
35    /// Initializes an account view key from the given mode and scalar field element.
36    fn new(mode: Mode, view_key: Self::Primitive) -> ViewKey<A> {
37        Self(Scalar::new(mode, *view_key), Default::default())
38    }
39}
40
41impl<A: Aleo> Eject for ViewKey<A> {
42    type Primitive = console::ViewKey<A::Network>;
43
44    /// Ejects the mode of the view key.
45    fn eject_mode(&self) -> Mode {
46        self.0.eject_mode()
47    }
48
49    /// Ejects the view key as a scalar field element.
50    fn eject_value(&self) -> Self::Primitive {
51        Self::Primitive::from_scalar(self.0.eject_value())
52    }
53}
54
55impl<A: Aleo> Deref for ViewKey<A> {
56    type Target = Scalar<A>;
57
58    fn deref(&self) -> &Self::Target {
59        &self.0
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use crate::{Circuit, helpers::generate_account};
67
68    use anyhow::Result;
69
70    const ITERATIONS: u64 = 10;
71
72    fn check_new(
73        mode: Mode,
74        num_constants: u64,
75        num_public: u64,
76        num_private: u64,
77        num_constraints: u64,
78    ) -> Result<()> {
79        for _ in 0..ITERATIONS {
80            // Generate a private key, compute key, view key, and address.
81            let (_private_key, _compute_key, view_key, _address) = generate_account()?;
82
83            Circuit::scope(format!("New {mode}"), || {
84                let candidate = ViewKey::<Circuit>::new(mode, view_key);
85                assert_eq!(mode, candidate.eject_mode());
86                assert_eq!(view_key, candidate.eject_value());
87                assert_scope!(num_constants, num_public, num_private, num_constraints);
88            });
89            Circuit::reset();
90        }
91        Ok(())
92    }
93
94    #[test]
95    fn test_view_key_new_constant() -> Result<()> {
96        check_new(Mode::Constant, 1, 0, 0, 0)
97    }
98
99    #[test]
100    fn test_view_key_new_public() -> Result<()> {
101        check_new(Mode::Public, 0, 1, 0, 0)
102    }
103
104    #[test]
105    fn test_view_key_new_private() -> Result<()> {
106        check_new(Mode::Private, 0, 0, 1, 0)
107    }
108}