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
140
141
142
use crate::{AccountError, Network, PrivateKey};
use snarkvm_algorithms::SignatureSchemeOperations;
use snarkvm_curves::AffineCurve;
use snarkvm_utilities::{FromBytes, ToBytes};
use rand::thread_rng;
use std::{
fmt,
io::{Read, Result as IoResult, Write},
};
#[derive(Derivative)]
#[derivative(
Clone(bound = "N: Network"),
PartialEq(bound = "N: Network"),
Eq(bound = "N: Network")
)]
pub struct ComputeKey<N: Network> {
pk_sig: N::ProgramAffineCurve,
pr_sig: N::ProgramAffineCurve,
sk_prf: N::ProgramScalarField,
}
impl<N: Network> ComputeKey<N> {
pub(crate) fn new(pk_sig: N::ProgramAffineCurve, pr_sig: N::ProgramAffineCurve) -> Self {
let sk_prf =
N::account_signature_scheme().hash_to_scalar_field(&[pk_sig.to_x_coordinate(), pr_sig.to_x_coordinate()]);
Self { pk_sig, pr_sig, sk_prf }
}
pub fn from_private_key(private_key: &PrivateKey<N>) -> Self {
let pk_sig = N::account_signature_scheme().g_scalar_multiply(&private_key.sk_sig);
let pr_sig = N::account_signature_scheme().g_scalar_multiply(&private_key.r_sig);
Self::new(pk_sig, pr_sig)
}
pub fn from_signature(signature: &N::AccountSignature) -> Result<Self, AccountError> {
let pk_sig = N::AccountSignatureScheme::pk_sig(signature)?;
let pr_sig = N::AccountSignatureScheme::pr_sig(signature)?;
Ok(Self::new(pk_sig, pr_sig))
}
pub fn is_valid(&self) -> bool {
let candidate_sk_prf = N::account_signature_scheme()
.hash_to_scalar_field(&[self.pk_sig.to_x_coordinate(), self.pr_sig.to_x_coordinate()]);
self.sk_prf == candidate_sk_prf
}
pub fn pk_sig(&self) -> &N::ProgramAffineCurve {
&self.pk_sig
}
pub fn pr_sig(&self) -> &N::ProgramAffineCurve {
&self.pr_sig
}
pub fn sk_prf(&self) -> &N::ProgramScalarField {
&self.sk_prf
}
pub fn to_encryption_key(&self) -> N::ProgramAffineCurve {
let pk_prf = N::account_signature_scheme().g_scalar_multiply(&self.sk_prf);
self.pk_sig + self.pr_sig + pk_prf
}
}
impl<N: Network> FromBytes for ComputeKey<N> {
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let pk_sig = FromBytes::read_le(&mut reader)?;
let pr_sig = FromBytes::read_le(&mut reader)?;
Ok(Self::new(pk_sig, pr_sig))
}
}
impl<N: Network> ToBytes for ComputeKey<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.pk_sig.write_le(&mut writer)?;
self.pr_sig.write_le(&mut writer)
}
}
impl<N: Network> fmt::Debug for ComputeKey<N> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"ComputeKey {{ pk_sig: {:?}, pr_sig: {:?} }}",
self.pk_sig, self.pr_sig
)
}
}
impl<N: Network> Default for ComputeKey<N> {
fn default() -> Self {
PrivateKey::new(&mut thread_rng()).to_compute_key()
}
}