snarkvm_circuit_account/private_key/
mod.rs1mod to_compute_key;
17mod to_view_key;
18
19#[cfg(test)]
20use snarkvm_circuit_types::environment::assert_scope;
21
22use crate::{ComputeKey, ViewKey};
23use snarkvm_circuit_network::Aleo;
24use snarkvm_circuit_types::{Scalar, environment::prelude::*};
25
26pub struct PrivateKey<A: Aleo> {
27 sk_sig: Scalar<A>,
29 r_sig: Scalar<A>,
31}
32
33impl<A: Aleo> Inject for PrivateKey<A> {
34 type Primitive = console::PrivateKey<A::Network>;
35
36 fn new(mode: Mode, private_key: Self::Primitive) -> Self {
38 Self { sk_sig: Scalar::new(mode, private_key.sk_sig()), r_sig: Scalar::new(mode, private_key.r_sig()) }
39 }
40}
41
42impl<A: Aleo> PrivateKey<A> {
43 pub const fn sk_sig(&self) -> &Scalar<A> {
45 &self.sk_sig
46 }
47
48 pub const fn r_sig(&self) -> &Scalar<A> {
50 &self.r_sig
51 }
52}
53
54impl<A: Aleo> Eject for PrivateKey<A> {
55 type Primitive = (console::Scalar<A::Network>, console::Scalar<A::Network>);
56
57 fn eject_mode(&self) -> Mode {
59 (&self.sk_sig, &self.r_sig).eject_mode()
60 }
61
62 fn eject_value(&self) -> Self::Primitive {
64 (&self.sk_sig, &self.r_sig).eject_value()
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use crate::{Circuit, helpers::generate_account};
72
73 use anyhow::Result;
74
75 const ITERATIONS: u64 = 10;
76
77 fn check_new(
78 mode: Mode,
79 num_constants: u64,
80 num_public: u64,
81 num_private: u64,
82 num_constraints: u64,
83 ) -> Result<()> {
84 for _ in 0..ITERATIONS {
85 let (private_key, _compute_key, _view_key, _address) = generate_account()?;
87
88 let sk_sig = private_key.sk_sig();
90 let r_sig = private_key.r_sig();
91
92 Circuit::scope(format!("New {mode}"), || {
93 let candidate = PrivateKey::<Circuit>::new(mode, private_key);
94 assert_eq!(mode, candidate.eject_mode());
95 assert_eq!((sk_sig, r_sig), candidate.eject_value());
96 assert_scope!(num_constants, num_public, num_private, num_constraints);
97 });
98 Circuit::reset();
99 }
100 Ok(())
101 }
102
103 #[test]
104 fn test_private_key_new_constant() -> Result<()> {
105 check_new(Mode::Constant, 2, 0, 0, 0)
106 }
107
108 #[test]
109 fn test_private_key_new_public() -> Result<()> {
110 check_new(Mode::Public, 0, 2, 0, 0)
111 }
112
113 #[test]
114 fn test_private_key_new_private() -> Result<()> {
115 check_new(Mode::Private, 0, 0, 2, 0)
116 }
117}