snarkvm_circuit_account/view_key/
mod.rsmod from_private_key;
mod to_address;
#[cfg(test)]
use snarkvm_circuit_types::environment::assert_scope;
use crate::PrivateKey;
use snarkvm_circuit_network::Aleo;
use snarkvm_circuit_types::{Address, Scalar, environment::prelude::*};
use core::ops::Deref;
pub struct ViewKey<A: Aleo>(Scalar<A>, OnceCell<Address<A>>);
#[cfg(feature = "console")]
impl<A: Aleo> Inject for ViewKey<A> {
type Primitive = console::ViewKey<A::Network>;
fn new(mode: Mode, view_key: Self::Primitive) -> ViewKey<A> {
Self(Scalar::new(mode, *view_key), Default::default())
}
}
#[cfg(feature = "console")]
impl<A: Aleo> Eject for ViewKey<A> {
type Primitive = console::ViewKey<A::Network>;
fn eject_mode(&self) -> Mode {
self.0.eject_mode()
}
fn eject_value(&self) -> Self::Primitive {
Self::Primitive::from_scalar(self.0.eject_value())
}
}
impl<A: Aleo> Deref for ViewKey<A> {
type Target = Scalar<A>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(all(test, feature = "console"))]
mod tests {
use super::*;
use crate::{Circuit, helpers::generate_account};
use anyhow::Result;
const ITERATIONS: u64 = 500;
fn check_new(
mode: Mode,
num_constants: u64,
num_public: u64,
num_private: u64,
num_constraints: u64,
) -> Result<()> {
for _ in 0..ITERATIONS {
let (_private_key, _compute_key, view_key, _address) = generate_account()?;
Circuit::scope(format!("New {mode}"), || {
let candidate = ViewKey::<Circuit>::new(mode, view_key);
assert_eq!(mode, candidate.eject_mode());
assert_eq!(view_key, candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
}
Ok(())
}
#[test]
fn test_view_key_new_constant() -> Result<()> {
check_new(Mode::Constant, 1, 0, 0, 0)
}
#[test]
fn test_view_key_new_public() -> Result<()> {
check_new(Mode::Public, 0, 1, 0, 0)
}
#[test]
fn test_view_key_new_private() -> Result<()> {
check_new(Mode::Private, 0, 0, 1, 0)
}
}