Skip to content

Commit 0547456

Browse files
committed
Clean up
1 parent ae13205 commit 0547456

File tree

6 files changed

+23
-70
lines changed

6 files changed

+23
-70
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
members = ["remix", "mix-node"]
33
resolver = "2"
44

5-
[profile.test]
5+
[profile.dev]
66
opt-level = 1
77

88
[profile.release]
@@ -13,4 +13,3 @@ panic = "unwind"
1313
# Speed up compile time checks for sqlx queries
1414
[profile.dev.package.sqlx-macros]
1515
opt-level = 3
16-
# Cm8IuZ8tbGqRIhjR

mix-node/benches/mix_node.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,13 @@ fn bench_serialization_json(c: &mut Criterion) {
204204
})
205205
});
206206

207-
group.bench_function("to value", |b| {
208-
b.iter(|| {
209-
let _value = serde_json::to_value(&payload);
210-
})
211-
});
212-
213207
let payload = payload_subset(&payload, 0..N_SMALL_BITS);
214208

215209
group.bench_function(f!("to string {N_SMALL_BITS} bits subset"), |b| {
216210
b.iter(|| {
217211
let _string = serde_json::to_string(&payload);
218212
})
219213
});
220-
221-
group.bench_function(f!("to value {N_SMALL_BITS} bits subset"), |b| {
222-
b.iter(|| {
223-
let _value = serde_json::to_value(&payload);
224-
})
225-
});
226214
}
227215

228216
fn bench_deserialization_json(c: &mut Criterion) {
@@ -263,7 +251,7 @@ pub fn decrypt_bits<'a>(
263251
pk: &'a SecretKey<Ristretto>,
264252
) -> impl Iterator<Item = bool> + 'a {
265253
ct.iter().map(|ct| {
266-
let point = pk.decrypt(*ct, &DiscreteLogTable::new(0..2)).unwrap();
254+
let point = pk.decrypt(*ct, &DiscreteLogTable::new(0..=1)).unwrap();
267255
point != 0u64
268256
})
269257
}

mix-node/src/bin/lambda.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
use std::net::SocketAddr;
2-
3-
use axum::{extract::ConnectInfo, Router};
4-
use lambda_http::tower::util::MapRequest;
5-
use lambda_http::RequestExt;
6-
use lambda_http::{http::Request, request::RequestContext};
71
use mix_node::{
82
config::{self, Config},
93
db, rest, AppState,
@@ -32,32 +26,5 @@ async fn main() -> Result<(), lambda_http::Error> {
3226

3327
let conn = db::connect_database(db_config);
3428
let state = AppState::new(app_config.auth_token, conn, crypto_config);
35-
lambda_http::run(
36-
rest::app(state).layer(MapRequest::<Router, _>::layer(extract_lambda_source_ip)),
37-
)
38-
.await
39-
}
40-
41-
fn extract_lambda_source_ip<B>(mut request: Request<B>) -> Request<B> {
42-
if request
43-
.extensions()
44-
.get::<ConnectInfo<SocketAddr>>()
45-
.is_some()
46-
{
47-
return request;
48-
}
49-
50-
let Some(RequestContext::ApiGatewayV2(cx)) = request.request_context_ref() else {
51-
return request;
52-
};
53-
54-
let Some(source_ip) = &cx.http.source_ip else {
55-
return request;
56-
};
57-
58-
if let Ok(addr) = source_ip.parse::<SocketAddr>() {
59-
request.extensions_mut().insert(ConnectInfo(addr));
60-
}
61-
62-
request
29+
lambda_http::run(rest::app(state)).await
6330
}

mix-node/src/crypto.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ impl DecryptionShare {
2727
}
2828
}
2929

30-
// TODO: lookup table can be just 0..1 in prod
3130
pub static LOOKUP_TABLE: LazyLock<DiscreteLogTable<Ristretto>> =
3231
LazyLock::new(|| DiscreteLogTable::<Ristretto>::new(0..=1));
3332

@@ -86,7 +85,6 @@ pub fn decryption_share_for(
8685
DecryptionShare::new(active_participant.index(), share)
8786
}
8887

89-
// PERF: parallelize this
9088
pub fn decrypt_shares(
9189
key_set: &PublicKeySet<Ristretto>,
9290
enc: &[Ciphertext],
@@ -121,6 +119,7 @@ pub fn decrypt_shares(
121119
.context("decrypted values out of range of lookup table")?
122120
== 1u64)
123121
})
122+
// PERF: collect directly to BitVec
124123
.collect::<anyhow::Result<Vec<_>>>()
125124
.map(Bits::from_iter)
126125
}

mix-node/src/rest/error.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ impl IntoResponse for Error {
1818
fn into_response(self) -> Response {
1919
let status_code = match &self {
2020
Error::InvalidLength(_) => StatusCode::BAD_REQUEST,
21-
Error::Unexpected(_) => {
22-
tracing::error!("{self:?}");
23-
StatusCode::INTERNAL_SERVER_ERROR
24-
}
21+
Error::Unexpected(_) => StatusCode::INTERNAL_SERVER_ERROR,
2522
};
2623
(status_code, self.to_string()).into_response()
2724
}

mix-node/src/rest/routes.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use anyhow::Context;
77
use axum::{extract::State, response::Json};
88
use elastic_elgamal::{group::Ristretto, sharing::PublicKeySet};
99
use futures::FutureExt;
10-
// use futures::FutureExt;
1110
use reqwest::Client;
1211
use serde::{Deserialize, Serialize};
1312
use std::sync::Arc;
@@ -97,17 +96,14 @@ pub async fn hamming_distance(
9796
// Remix
9897
tracing::trace!("remix self");
9998
let (mut x_code, mut y_code) = {
100-
let inner_state = Arc::clone(&state);
99+
let state = Arc::clone(&state);
101100
rokio::spawn(move || -> Result<_, CryptoError> {
102-
crypto::remix(
103-
&mut x_code,
104-
&mut y_code,
105-
inner_state.pub_key_set().shared_key(),
106-
)?;
101+
crypto::remix(&mut x_code, &mut y_code, state.pub_key_set().shared_key())?;
107102
Ok((x_code, y_code))
108103
})
109104
.await?
110105
};
106+
// This algorithm runs serially and it's not fast to serialize and deserialize 2*25600 bits
111107
tracing::trace!("remix participants");
112108
for node in &state.crypto.participants {
113109
(x_code, y_code) = request_remix(
@@ -120,23 +116,30 @@ pub async fn hamming_distance(
120116
.unwrap_or((x_code, y_code));
121117
tracing::trace!(id = ?node, "remix participant");
122118
}
123-
let (x_code, y_code) = (Arc::new(x_code), Arc::new(y_code));
119+
120+
let x_code = Arc::new(x_code);
121+
let y_code = Arc::new(y_code);
124122

125123
// Decrypt
126124
tracing::trace!("request shares");
127125
let (x_shares, y_shares) = {
128-
let (x_state, y_state) = (Arc::clone(&state), Arc::clone(&state));
129126
let (x_inner_code, y_inner_code) = (Arc::clone(&x_code), Arc::clone(&y_code));
130127

131128
let (mut x_shares, mut y_shares, x_self_share, y_self_share) = tokio::join!(
132129
request_all_shares(&x_code, &state),
133130
request_all_shares(&y_code, &state),
134-
rokio::spawn(move || {
135-
crypto::decryption_share_for(&x_state.crypto.active_participant, &x_inner_code)
136-
}),
137-
rokio::spawn(move || {
138-
crypto::decryption_share_for(&y_state.crypto.active_participant, &y_inner_code)
139-
})
131+
{
132+
let state = Arc::clone(&state);
133+
rokio::spawn(move || {
134+
crypto::decryption_share_for(&state.crypto.active_participant, &x_inner_code)
135+
})
136+
},
137+
{
138+
let state = Arc::clone(&state);
139+
rokio::spawn(move || {
140+
crypto::decryption_share_for(&state.crypto.active_participant, &y_inner_code)
141+
})
142+
}
140143
);
141144
x_shares.push(x_self_share);
142145
y_shares.push(y_self_share);

0 commit comments

Comments
 (0)