snarkvm_circuit_account/graph_key/
mod.rs1#[cfg(test)]
17use snarkvm_circuit_types::environment::assert_scope;
18
19use snarkvm_circuit_network::Aleo;
20use snarkvm_circuit_types::{Field, environment::prelude::*};
21
22pub struct GraphKey<A: Aleo> {
23 sk_tag: Field<A>,
25}
26
27impl<A: Aleo> Inject for GraphKey<A> {
28 type Primitive = console::GraphKey<A::Network>;
29
30 fn new(mode: Mode, graph_key: Self::Primitive) -> Self {
32 let sk_tag = Field::new(mode, graph_key.sk_tag());
34 Self { sk_tag }
36 }
37}
38
39impl<A: Aleo> GraphKey<A> {
40 pub const fn sk_tag(&self) -> &Field<A> {
42 &self.sk_tag
43 }
44}
45
46impl<A: Aleo> Eject for GraphKey<A> {
47 type Primitive = console::GraphKey<A::Network>;
48
49 fn eject_mode(&self) -> Mode {
51 self.sk_tag.eject_mode()
52 }
53
54 fn eject_value(&self) -> Self::Primitive {
56 match Self::Primitive::try_from(self.sk_tag.eject_value()) {
57 Ok(graph_key) => graph_key,
58 Err(error) => A::halt(format!("Failed to eject the graph key: {error}")),
59 }
60 }
61}
62
63#[cfg(test)]
64pub(crate) mod 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 i in 0..ITERATIONS {
80 let (_, _, view_key, _) = generate_account()?;
82 let graph_key = console::GraphKey::try_from(&view_key)?;
83
84 Circuit::scope(format!("New {mode}"), || {
85 let candidate = GraphKey::<Circuit>::new(mode, graph_key);
86 assert_eq!(mode, candidate.eject_mode());
87 assert_eq!(graph_key, candidate.eject_value());
88 if i > 0 {
90 assert_scope!(num_constants, num_public, num_private, num_constraints);
91 }
92 });
93 Circuit::reset();
94 }
95 Ok(())
96 }
97
98 #[test]
99 fn test_graph_key_new_constant() -> Result<()> {
100 check_new(Mode::Constant, 1, 0, 0, 0)
101 }
102
103 #[test]
104 fn test_graph_key_new_public() -> Result<()> {
105 check_new(Mode::Public, 0, 1, 0, 0)
106 }
107
108 #[test]
109 fn test_graph_key_new_private() -> Result<()> {
110 check_new(Mode::Private, 0, 0, 1, 0)
111 }
112}