1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
mod from_private_key;
mod to_address;
#[cfg(test)]
use snarkvm_circuit_types::environment::assert_scope;
use crate::PrivateKey;
use snarkvm_circuit_network::Aleo;
use snarkvm_circuit_types::{environment::prelude::*, Address, Group, Scalar};
pub struct ComputeKey<A: Aleo> {
pk_sig: Group<A>,
pr_sig: Group<A>,
sk_prf: Scalar<A>,
}
#[cfg(console)]
impl<A: Aleo> Inject for ComputeKey<A> {
type Primitive = console::ComputeKey<A::Network>;
fn new(mode: Mode, compute_key: Self::Primitive) -> Self {
let pk_sig = Group::new(mode, compute_key.pk_sig());
let pr_sig = Group::new(mode, compute_key.pr_sig());
let sk_prf = A::hash_to_scalar_psd4(&[pk_sig.to_x_coordinate(), pr_sig.to_x_coordinate()]);
Self { pk_sig, pr_sig, sk_prf }
}
}
impl<A: Aleo> ComputeKey<A> {
pub const fn pk_sig(&self) -> &Group<A> {
&self.pk_sig
}
pub const fn pr_sig(&self) -> &Group<A> {
&self.pr_sig
}
pub const fn sk_prf(&self) -> &Scalar<A> {
&self.sk_prf
}
}
#[cfg(console)]
impl<A: Aleo> Eject for ComputeKey<A> {
type Primitive = console::ComputeKey<A::Network>;
fn eject_mode(&self) -> Mode {
(&self.pk_sig, &self.pr_sig, &self.sk_prf).eject_mode()
}
fn eject_value(&self) -> Self::Primitive {
match Self::Primitive::try_from((&self.pk_sig, &self.pr_sig).eject_value()) {
Ok(compute_key) => compute_key,
Err(error) => A::halt(format!("Failed to eject the compute key: {error}")),
}
}
}
#[cfg(all(test, console))]
pub(crate) mod tests {
use super::*;
use crate::{helpers::generate_account, Circuit};
use anyhow::Result;
const ITERATIONS: u64 = 250;
fn check_new(
mode: Mode,
num_constants: u64,
num_public: u64,
num_private: u64,
num_constraints: u64,
) -> Result<()> {
for i in 0..ITERATIONS {
let (_private_key, compute_key, _view_key, _address) = generate_account()?;
Circuit::scope(format!("New {mode}"), || {
let candidate = ComputeKey::<Circuit>::new(mode, compute_key);
match mode.is_constant() {
true => assert_eq!(Mode::Constant, candidate.eject_mode()),
false => assert_eq!(Mode::Private, candidate.eject_mode()),
};
assert_eq!(compute_key, candidate.eject_value());
if i > 0 {
assert_scope!(num_constants, num_public, num_private, num_constraints);
}
});
Circuit::reset();
}
Ok(())
}
#[test]
fn test_compute_key_new_constant() -> Result<()> {
check_new(Mode::Constant, 274, 0, 0, 0)
}
#[test]
fn test_compute_key_new_public() -> Result<()> {
check_new(Mode::Public, 9, 4, 621, 622)
}
#[test]
fn test_compute_key_new_private() -> Result<()> {
check_new(Mode::Private, 9, 0, 621, 620)
}
}