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
use super::super::State;
use super::{DhFillPoolRequest, DhFillPoolResponse, ServerPublics};
use crate::errors::{ProtocolError, Result};
use crate::graphql::dh_fill_pool;
use crate::types::Blockchain;
#[cfg(feature = "secp256k1")]
use nash_mpc::curves::secp256_k1::Secp256k1Point;
#[cfg(feature = "k256")]
use nash_mpc::curves::secp256_k1_rust::Secp256k1Point;
use nash_mpc::curves::secp256_r1::Secp256r1Point;
use nash_mpc::curves::traits::ECPoint;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::trace;
impl ServerPublics {
pub fn from_hexstrings(chain: Blockchain, response: &DhFillPoolResponse) -> Result<Self> {
match chain {
Blockchain::Bitcoin => {
let k1s: Result<Vec<Secp256k1Point>> = response
.server_publics
.iter()
.map(|hex_str| k1_from_hexstring(hex_str))
.collect();
Ok(Self::Bitcoin(k1s?))
}
Blockchain::Ethereum => {
let k1s: Result<Vec<Secp256k1Point>> = response
.server_publics
.iter()
.map(|hex_str| k1_from_hexstring(hex_str))
.collect();
Ok(Self::Ethereum(k1s?))
}
Blockchain::NEO => {
let r1s: Result<Vec<Secp256r1Point>> = response
.server_publics
.iter()
.map(|hex_str| r1_from_hexstring(hex_str))
.collect();
Ok(Self::NEO(r1s?))
}
}
}
pub fn publics_for_k1(&self) -> Result<Vec<Secp256k1Point>> {
match self {
Self::Bitcoin(values) => Ok(values.clone()),
Self::Ethereum(values) => Ok(values.clone()),
Self::NEO(_) => Err(ProtocolError("Tried to get k1 values for r1 chain")),
}
}
pub fn publics_for_r1(&self) -> Result<Vec<Secp256r1Point>> {
match self {
Self::NEO(values) => Ok(values.clone()),
Self::Bitcoin(_) | Self::Ethereum(_) => {
Err(ProtocolError("Tried to get r1 values for k1 chain"))
}
}
}
}
fn r1_from_hexstring(hex_str: &str) -> Result<Secp256r1Point> {
Secp256r1Point::from_hex(hex_str)
.map_err(|_| ProtocolError("Could not parse Secp256r1Point from hex"))
}
fn k1_from_hexstring(hex_str: &str) -> Result<Secp256k1Point> {
Secp256k1Point::from_hex(hex_str)
.map_err(|_| ProtocolError("Could not parse Secp256k1Point from hex"))
}
impl From<dh_fill_pool::ResponseData> for DhFillPoolResponse {
fn from(res: dh_fill_pool::ResponseData) -> Self {
let server_publics = res.dh_fill_pool.iter().map(|value| value.clone()).collect();
Self { server_publics }
}
}
pub async fn fill_pool(
request: &DhFillPoolRequest,
server_publics: ServerPublics,
state: Arc<RwLock<State>>,
) -> Result<()> {
let paillier_pk = state.read().await.signer()?.paillier_pk().clone();
match request {
DhFillPoolRequest::Bitcoin(request) | DhFillPoolRequest::Ethereum(request) => {
let k1_secrets = request.secrets.clone();
let k1_server_publics = server_publics.publics_for_k1()?;
trace!("Begin fill_rpool_secp256k1");
tokio::task::spawn_blocking(move || {
nash_mpc::client::fill_rpool_secp256k1(k1_secrets, &k1_server_publics, &paillier_pk)
.map_err(|_| ProtocolError("Error filling k1 pool"))
})
.await
.map_err(|_| ProtocolError("Error filling k1 pool"))??;
trace!("End fill_rpool_secp256k1");
}
DhFillPoolRequest::NEO(request) => {
let r1_secrets = request.secrets.clone();
let r1_server_publics = server_publics.publics_for_r1()?;
trace!("Begin fill_rpool_secp256r1");
tokio::task::spawn_blocking(move || {
nash_mpc::client::fill_rpool_secp256r1(r1_secrets, &r1_server_publics, &paillier_pk)
.map_err(|_| ProtocolError("Error filling r1 pool"))
})
.await
.map_err(|_| ProtocolError("Error filling r1 pool"))??;
trace!("End fill_rpool_secp256r1");
}
}
Ok(())
}