Skip to main content

snarkvm_circuit_account/graph_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
16#[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    /// The graph key `sk_tag` := Hash(view_key || ctr).
24    sk_tag: Field<A>,
25}
26
27impl<A: Aleo> Inject for GraphKey<A> {
28    type Primitive = console::GraphKey<A::Network>;
29
30    /// Initializes an account graph key from the given mode and native graph key.
31    fn new(mode: Mode, graph_key: Self::Primitive) -> Self {
32        // Inject `sk_tag`.
33        let sk_tag = Field::new(mode, graph_key.sk_tag());
34        // Output the graph key.
35        Self { sk_tag }
36    }
37}
38
39impl<A: Aleo> GraphKey<A> {
40    /// Returns the graph key.
41    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    /// Ejects the mode of the graph key.
50    fn eject_mode(&self) -> Mode {
51        self.sk_tag.eject_mode()
52    }
53
54    /// Ejects the graph key.
55    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            // Generate a view key and graph key.
81            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                // TODO (howardwu): Resolve skipping the cost count checks for the burn-in round.
89                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}