snarkvm_circuit_account/view_key/
mod.rs1mod 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
29pub 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 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 fn eject_mode(&self) -> Mode {
46 self.0.eject_mode()
47 }
48
49 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 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}