snarkvm_console_account/private_key/mod.rs
1// Copyright (c) 2019-2025 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
16mod bytes;
17mod serialize;
18mod string;
19mod try_from;
20
21#[cfg(feature = "signature")]
22mod sign;
23
24use snarkvm_console_network::prelude::*;
25use snarkvm_console_types::{Field, Scalar};
26
27use zeroize::Zeroize;
28
29#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Zeroize)]
30pub struct PrivateKey<N: Network> {
31 /// The account seed that derives the full private key.
32 seed: Field<N>,
33 /// The derived signature secret key.
34 sk_sig: Scalar<N>,
35 /// The derived signature randomizer.
36 r_sig: Scalar<N>,
37}
38
39impl<N: Network> PrivateKey<N> {
40 /// Samples a new random private key.
41 #[inline]
42 pub fn new<R: Rng + CryptoRng>(rng: &mut R) -> Result<Self> {
43 // Sample a random account seed.
44 Self::try_from(Uniform::rand(rng))
45 }
46
47 /// Returns the account seed.
48 pub const fn seed(&self) -> Field<N> {
49 self.seed
50 }
51
52 /// Returns the signature secret key.
53 pub const fn sk_sig(&self) -> Scalar<N> {
54 self.sk_sig
55 }
56
57 /// Returns the signature randomizer.
58 pub const fn r_sig(&self) -> Scalar<N> {
59 self.r_sig
60 }
61}