snarkvm_circuit_account/
lib.rs

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