-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacss.rs
195 lines (175 loc) · 6.89 KB
/
acss.rs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::collections::HashMap;
#[allow(unused_imports)]
use chacha20poly1305::{
aead::{Aead, AeadCore, KeyInit},
ChaCha20Poly1305, Key, Nonce,
};
use curve25519_dalek::{
constants::RISTRETTO_BASEPOINT_POINT, ristretto::CompressedRistretto, scalar::Scalar,
RistrettoPoint,
};
use rand::rngs::OsRng;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use crate::{
kintsugi_lib::error::KintsugiError,
kintsugi_lib::keypair::{Keypair, PrivateKey, PublicKey},
kintsugi_lib::polynomial::Polynomial,
kintsugi_lib::zkp::ZKP,
};
pub struct ACSS {}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ACSSDealerShare {
pub(crate) index: usize,
pub(crate) nonce: [u8; 12],
pub(crate) v_i: Vec<u8>,
pub(crate) v_hat_i: Vec<u8>,
pub(crate) c_i: RistrettoPoint,
pub(crate) poly_c_i: Vec<RistrettoPoint>,
pub(crate) proof: ZKP,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ACSSNodeShare {
pub(crate) s_i_d: Scalar,
pub(crate) s_hat_i_d: Scalar,
pub(crate) c_i: RistrettoPoint,
}
impl ACSS {
pub fn share_dealer(
h_point: RistrettoPoint,
secret: Scalar,
degree: usize,
dealer_key: PrivateKey,
peer_public_keys: HashMap<String, PublicKey>,
peer_indexes: HashMap<String, i32>,
) -> Result<(HashMap<String, ACSSDealerShare>, Polynomial, Polynomial), KintsugiError> {
let mut shares = HashMap::new();
let phi = Polynomial::new_w_secret(degree, secret);
let phi_hat = Polynomial::new(degree);
for (_, (peer_id, i)) in peer_indexes.iter().enumerate() {
let i = i.clone() as usize;
let public_key = peer_public_keys.get(peer_id).unwrap();
let dealer_private_key_scalar = Scalar::from_bytes_mod_order(dealer_key);
let peer_public_key_point = CompressedRistretto::from_slice(public_key);
if let Err(e) = peer_public_key_point {
return Err(KintsugiError::SerializationError(
"Error deserializing public key ".to_string() + &e.to_string(),
));
}
let peer_public_key_point = peer_public_key_point.unwrap().decompress();
if let None = peer_public_key_point {
return Err(KintsugiError::SerializationError(
"Error deserializing public key".to_string(),
));
}
let shared_secret = dealer_private_key_scalar * peer_public_key_point.unwrap();
let cipher =
ChaCha20Poly1305::new(Key::from_slice(&shared_secret.compress().to_bytes()));
let mut nonce_bytes = [0u8; 12];
OsRng.fill_bytes(&mut nonce_bytes);
let nonce = Nonce::from_slice(&nonce_bytes);
let v_i = cipher.encrypt(nonce, phi.at(i).to_bytes().as_slice());
if let Err(e) = v_i {
return Err(KintsugiError::CryptoError(
"Encryption failed".to_string() + &e.to_string(),
));
}
let v_i = v_i.unwrap();
let v_hat_i = cipher.encrypt(nonce, phi_hat.at(i).to_bytes().as_slice());
if let Err(e) = v_hat_i {
return Err(KintsugiError::CryptoError(
"Encryption failed".to_string() + &e.to_string(),
));
}
let v_hat_i = v_hat_i.unwrap();
let c_i = phi.at(i) * RISTRETTO_BASEPOINT_POINT + phi_hat.at(i) * h_point;
let poly_c_i = phi
.coeffs
.iter()
.map(|c| RISTRETTO_BASEPOINT_POINT * c)
.collect();
let proof = ZKP::new(phi.at(i), phi_hat.at(i), h_point, c_i);
shares.insert(
peer_id.clone(),
ACSSDealerShare {
index: i,
nonce: nonce_bytes,
v_i,
v_hat_i,
c_i,
poly_c_i,
proof,
},
);
}
Ok((shares, phi, phi_hat))
}
pub fn share(
h_point: RistrettoPoint,
share: ACSSDealerShare,
keypair: Keypair,
dealer_key: PublicKey,
) -> Result<ACSSNodeShare, KintsugiError> {
if !share.proof.verify(h_point, share.c_i) {
return Err(KintsugiError::CryptoError(
"ZKP was not accepted".to_string(),
));
}
let private_key_scalar = Scalar::from_bytes_mod_order(keypair.private_key);
let dealer_public_key_point = CompressedRistretto::from_slice(&dealer_key);
if let Err(e) = dealer_public_key_point {
return Err(KintsugiError::SerializationError(
"Error deserializing public key ".to_string() + &e.to_string(),
));
}
let dealer_public_key_point = dealer_public_key_point.unwrap().decompress();
if let None = dealer_public_key_point {
return Err(KintsugiError::SerializationError(
"Error decompressing public key".to_string(),
));
}
let shared_secret = private_key_scalar * dealer_public_key_point.unwrap();
let cipher = ChaCha20Poly1305::new(Key::from_slice(&shared_secret.compress().to_bytes()));
let nonce_bytes = share.nonce;
let nonce = Nonce::from_slice(&nonce_bytes);
let s_i_d_bytes = cipher.decrypt(nonce, share.v_i.as_ref());
if let Err(e) = s_i_d_bytes {
return Err(KintsugiError::CryptoError(
"Decryption failed: ".to_string() + &e.to_string(),
));
}
let s_i_d_bytes = s_i_d_bytes.unwrap();
let s_i_d_bytes: Result<[u8; 32], _> = s_i_d_bytes.try_into();
if let Err(_) = s_i_d_bytes {
return Err(KintsugiError::SerializationError(
"Deserialization failed: ".to_string(),
));
}
let s_i_d_bytes = s_i_d_bytes.unwrap();
let s_i_d = Scalar::from_bytes_mod_order(s_i_d_bytes);
let s_hat_i_d_bytes = cipher.decrypt(nonce, share.v_hat_i.as_ref());
if let Err(e) = s_hat_i_d_bytes {
return Err(KintsugiError::CryptoError(
"Decryption failed: ".to_string() + &e.to_string(),
));
}
let s_hat_i_d_bytes = s_hat_i_d_bytes.unwrap();
let s_hat_i_d_bytes: Result<[u8; 32], _> = s_hat_i_d_bytes.try_into();
if let Err(_) = s_hat_i_d_bytes {
return Err(KintsugiError::SerializationError(
"Deserialization failed: ".to_string(),
));
}
let s_hat_i_d_bytes = s_hat_i_d_bytes.unwrap();
let s_hat_i_d = Scalar::from_bytes_mod_order(s_hat_i_d_bytes);
let c_i = s_i_d * RISTRETTO_BASEPOINT_POINT + s_hat_i_d * h_point;
Ok(ACSSNodeShare {
s_i_d,
s_hat_i_d,
c_i,
})
}
}
#[cfg(test)]
#[path = "acss_tests.rs"]
mod acss_test;