Skip to main content

snarkvm_circuit_account/
lib.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#![forbid(unsafe_code)]
17#![allow(clippy::too_many_arguments)]
18
19extern crate snarkvm_console_account as console;
20
21#[cfg(test)]
22use snarkvm_circuit_network::AleoV0 as Circuit;
23
24pub mod compute_key;
25pub use compute_key::*;
26
27pub mod graph_key;
28pub use graph_key::*;
29
30pub mod private_key;
31pub use private_key::*;
32
33pub mod signature;
34pub use signature::*;
35
36pub mod view_key;
37pub use view_key::*;
38
39#[cfg(test)]
40pub(crate) mod helpers {
41    use snarkvm_circuit_network::AleoV0;
42    use snarkvm_circuit_types::environment::Environment;
43    use snarkvm_utilities::{TestRng, Uniform};
44
45    use anyhow::Result;
46
47    type CurrentNetwork = <AleoV0 as Environment>::Network;
48
49    #[allow(clippy::type_complexity)]
50    pub(crate) fn generate_account() -> Result<(
51        console::PrivateKey<CurrentNetwork>,
52        console::ComputeKey<CurrentNetwork>,
53        console::ViewKey<CurrentNetwork>,
54        console::Address<CurrentNetwork>,
55    )> {
56        // Sample a random private key.
57        let private_key = console::PrivateKey::<CurrentNetwork>::new(&mut TestRng::default())?;
58
59        // Derive the compute key, view key, and address.
60        let compute_key = console::ComputeKey::try_from(&private_key)?;
61        let view_key = console::ViewKey::try_from(&private_key)?;
62        let address = console::Address::try_from(&compute_key)?;
63
64        // Return the private key and compute key components.
65        Ok((private_key, compute_key, view_key, address))
66    }
67
68    pub(crate) fn generate_signature(num_fields: u64, rng: &mut TestRng) -> console::Signature<CurrentNetwork> {
69        // Sample an address and a private key.
70        let private_key = console::PrivateKey::<CurrentNetwork>::new(rng).unwrap();
71        let address = console::Address::try_from(&private_key).unwrap();
72
73        // Generate a signature.
74        let message: Vec<_> = (0..num_fields).map(|_| Uniform::rand(rng)).collect();
75        let signature = console::Signature::sign(&private_key, &message, rng).unwrap();
76        assert!(signature.verify(&address, &message));
77        signature
78    }
79}