Skip to main content

nash_protocol/protocol/dh_fill_pool/
response.rs

1use super::super::State;
2use super::{DhFillPoolRequest, DhFillPoolResponse, ServerPublics};
3use crate::errors::{ProtocolError, Result};
4use crate::graphql::dh_fill_pool;
5use crate::types::Blockchain;
6#[cfg(feature = "secp256k1")]
7use nash_mpc::curves::secp256_k1::Secp256k1Point;
8#[cfg(feature = "k256")]
9use nash_mpc::curves::secp256_k1_rust::Secp256k1Point;
10use nash_mpc::curves::secp256_r1::Secp256r1Point;
11use nash_mpc::curves::traits::ECPoint;
12
13use std::sync::Arc;
14use tokio::sync::RwLock;
15use tracing::trace;
16
17impl ServerPublics {
18    /// Transform a list of strings provided by Nash server into points on the required
19    /// ECDSA curve.
20    pub fn from_hexstrings(chain: Blockchain, response: &DhFillPoolResponse) -> Result<Self> {
21        match chain {
22            Blockchain::Bitcoin => {
23                let k1s: Result<Vec<Secp256k1Point>> = response
24                    .server_publics
25                    .iter()
26                    .map(|hex_str| k1_from_hexstring(hex_str))
27                    .collect();
28                Ok(Self::Bitcoin(k1s?))
29            }
30            Blockchain::Ethereum => {
31                let k1s: Result<Vec<Secp256k1Point>> = response
32                    .server_publics
33                    .iter()
34                    .map(|hex_str| k1_from_hexstring(hex_str))
35                    .collect();
36                Ok(Self::Ethereum(k1s?))
37            }
38            Blockchain::NEO => {
39                let r1s: Result<Vec<Secp256r1Point>> = response
40                    .server_publics
41                    .iter()
42                    .map(|hex_str| r1_from_hexstring(hex_str))
43                    .collect();
44                Ok(Self::NEO(r1s?))
45            }
46        }
47    }
48    /// Get k1 values if this wrapper contains them or error
49    pub fn publics_for_k1(&self) -> Result<Vec<Secp256k1Point>> {
50        match self {
51            Self::Bitcoin(values) => Ok(values.clone()),
52            Self::Ethereum(values) => Ok(values.clone()),
53            Self::NEO(_) => Err(ProtocolError("Tried to get k1 values for r1 chain")),
54        }
55    }
56    /// Get r1 values if this wrapper contains them or error
57    pub fn publics_for_r1(&self) -> Result<Vec<Secp256r1Point>> {
58        match self {
59            Self::NEO(values) => Ok(values.clone()),
60            Self::Bitcoin(_) | Self::Ethereum(_) => {
61                Err(ProtocolError("Tried to get r1 values for k1 chain"))
62            }
63        }
64    }
65}
66
67fn r1_from_hexstring(hex_str: &str) -> Result<Secp256r1Point> {
68    Secp256r1Point::from_hex(hex_str)
69        .map_err(|_| ProtocolError("Could not parse Secp256r1Point from hex"))
70}
71
72fn k1_from_hexstring(hex_str: &str) -> Result<Secp256k1Point> {
73    Secp256k1Point::from_hex(hex_str)
74        .map_err(|_| ProtocolError("Could not parse Secp256k1Point from hex"))
75}
76
77impl From<dh_fill_pool::ResponseData> for DhFillPoolResponse {
78    fn from(res: dh_fill_pool::ResponseData) -> Self {
79        let server_publics = res.dh_fill_pool.iter().map(|value| value.clone()).collect();
80        Self { server_publics }
81    }
82}
83
84/// Fill the pool of r values with server responses
85pub async fn fill_pool(
86    request: &DhFillPoolRequest,
87    server_publics: ServerPublics,
88    state: Arc<RwLock<State>>,
89) -> Result<()> {
90    let paillier_pk = state.read().await.signer()?.paillier_pk().clone();
91    // FIXME: State should manage the pools
92    match request {
93        DhFillPoolRequest::Bitcoin(request) | DhFillPoolRequest::Ethereum(request) => {
94            let k1_secrets = request.secrets.clone();
95            let k1_server_publics = server_publics.publics_for_k1()?;
96            trace!("Begin fill_rpool_secp256k1");
97            tokio::task::spawn_blocking(move || {
98                nash_mpc::client::fill_rpool_secp256k1(k1_secrets, &k1_server_publics, &paillier_pk)
99                    .map_err(|_| ProtocolError("Error filling k1 pool"))
100            })
101            .await
102            .map_err(|_| ProtocolError("Error filling k1 pool"))??;
103            trace!("End fill_rpool_secp256k1");
104        }
105        DhFillPoolRequest::NEO(request) => {
106            let r1_secrets = request.secrets.clone();
107            let r1_server_publics = server_publics.publics_for_r1()?;
108            trace!("Begin fill_rpool_secp256r1");
109            tokio::task::spawn_blocking(move || {
110                nash_mpc::client::fill_rpool_secp256r1(r1_secrets, &r1_server_publics, &paillier_pk)
111                    .map_err(|_| ProtocolError("Error filling r1 pool"))
112            })
113            .await
114            .map_err(|_| ProtocolError("Error filling r1 pool"))??;
115            trace!("End fill_rpool_secp256r1");
116        }
117    }
118    Ok(())
119}