Skip to content

Commit 39350f5

Browse files
committed
mpc server: back to old commit
1 parent 7a40871 commit 39350f5

File tree

4 files changed

+135
-27
lines changed

4 files changed

+135
-27
lines changed

mpc-server/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ ark-bn254 = "^0.5.0"
1818

1919
# co-noir = { path = "../../conoir-experiments/co-snarks/co-noir/co-noir" }
2020
# co-ultrahonk = { path = "../../conoir-experiments/co-snarks/co-noir/co-ultrahonk" }
21-
co-noir = { git = "https://github.com/TaceoLabs/co-snarks", rev = "55f4ca3" }
22-
co-ultrahonk = { git = "https://github.com/TaceoLabs/co-snarks", rev = "55f4ca3" }
21+
# co-noir = { git = "https://github.com/TaceoLabs/co-snarks", rev = "55f4ca3" }
22+
# co-ultrahonk = { git = "https://github.com/TaceoLabs/co-snarks", rev = "55f4ca3" }
23+
co-noir = { git = "https://github.com/TaceoLabs/co-snarks", rev = "b294667" }
24+
co-ultrahonk = { git = "https://github.com/TaceoLabs/co-snarks", rev = "b294667" }
2325

2426
noirc-artifacts = { version = "1.0.0-beta.4", git = "https://github.com/noir-lang/noir/", tag = "v1.0.0-beta.4", package = "noirc_artifacts" }
2527
axum = { version = "0.8.4", features = ["multipart"] }

mpc-server/src/main.rs

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,110 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
186186
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
187187
);
188188

189-
// let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await?;
190-
// axum::serve(listener, app).await?;
191-
let addr = SocketAddr::from(([0, 0, 0, 0], 8000));
192-
axum_server::bind_rustls(addr, config)
193-
.serve(app.into_make_service())
194-
.await?;
189+
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await?;
190+
axum::serve(listener, app).await?;
191+
// let addr = SocketAddr::from(([0, 0, 0, 0], 8000));
192+
// axum_server::bind_rustls(addr, config)
193+
// .serve(app.into_make_service())
194+
// .await?;
195195

196196
Ok(())
197197
}
198+
199+
#[cfg(test)]
200+
mod tests {
201+
use super::*;
202+
use crate::{matching::run_match, shares::split_input};
203+
204+
#[tokio::test]
205+
async fn test_match() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
206+
let prover_toml = r#"[user1]
207+
age = 30
208+
region = 1
209+
gender = 0
210+
id = "0x1fed07ad686a727dfc33b91206d526e61f519dca9c5054ae729231c201717633"
211+
interests = [2, 4, 6]
212+
213+
[user1.preferences]
214+
age_max = 35
215+
age_min = 25
216+
gender = 1
217+
218+
[user2]
219+
age = 32
220+
region = 1
221+
gender = 1
222+
id = "0x16e31ced6c74696a601f45f1bb2b9833380d51348fe89644360d0e5abeaf244a"
223+
interests = [1, 2, 3]
224+
225+
[user2.preferences]
226+
age_max = 35
227+
age_min = 25
228+
gender = 0"#;
229+
230+
std::fs::write("Prover.toml", prover_toml).unwrap();
231+
232+
rustls::crypto::aws_lc_rs::default_provider()
233+
.install_default()
234+
.unwrap();
235+
236+
let parties = vec![
237+
NetworkParty::new(
238+
PartyID::ID0.into(),
239+
Address::new("localhost".to_string(), 10000),
240+
CertificateDer::from(std::fs::read(CONFIG_DIR.join("cert0.der"))?).into_owned(),
241+
),
242+
NetworkParty::new(
243+
PartyID::ID1.into(),
244+
Address::new("localhost".to_string(), 10001),
245+
CertificateDer::from(std::fs::read(CONFIG_DIR.join("cert1.der"))?).into_owned(),
246+
),
247+
NetworkParty::new(
248+
PartyID::ID2.into(),
249+
Address::new("localhost".to_string(), 10002),
250+
CertificateDer::from(std::fs::read(CONFIG_DIR.join("cert2.der"))?).into_owned(),
251+
),
252+
];
253+
254+
let program_artifact =
255+
Utils::get_program_artifact_from_file(DATA_DIR.join("circuit.json"))?;
256+
let constraint_system = Arc::new(Utils::get_constraint_system_from_artifact(
257+
&program_artifact,
258+
true,
259+
));
260+
261+
let recursive = true;
262+
let has_zk = ZeroKnowledge::No;
263+
264+
let crs_size = co_noir::compute_circuit_size::<Bn254>(&constraint_system, recursive)?;
265+
let crs: (ProverCrs<Bn254>, ark_bn254::G2Affine) = CrsParser::<Bn254>::get_crs(
266+
CONFIG_DIR.join("bn254_g1.dat"),
267+
CONFIG_DIR.join("bn254_g2.dat"),
268+
crs_size,
269+
has_zk,
270+
)?
271+
.split();
272+
let prover_crs = Arc::new(crs.0);
273+
let verifier_crs = Arc::new(crs.1);
274+
275+
let shares = split_input(PathBuf::from("Prover.toml"), &program_artifact)?;
276+
277+
let result = run_match(
278+
shares,
279+
parties.clone(),
280+
&program_artifact,
281+
constraint_system.clone(),
282+
recursive,
283+
has_zk,
284+
prover_crs.clone(),
285+
verifier_crs.clone(),
286+
)
287+
.await;
288+
289+
println!("result: {:?}", result);
290+
291+
std::fs::remove_file("Prover.toml").unwrap();
292+
293+
Ok(())
294+
}
295+
}

mpc-server/src/matching.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use co_noir::{
22
AcirFormat, Bn254, NetworkConfig, NetworkParty, PartyID, Poseidon2Sponge, Rep3CoUltraHonk,
33
Rep3MpcNet, UltraHonk, merge_input_shares,
44
};
5-
use co_ultrahonk::prelude::{Crs, ProverCrs, ZeroKnowledge};
5+
use co_ultrahonk::prelude::{ProverCrs, ZeroKnowledge};
66
use noirc_artifacts::program::ProgramArtifact;
77
use once_cell::sync::Lazy;
88
use rustls::pki_types::{PrivateKeyDer, PrivatePkcs8KeyDer};
@@ -95,7 +95,7 @@ pub async fn run_matches(
9595
Ok(())
9696
}
9797

98-
async fn run_match(
98+
pub async fn run_match(
9999
[share0, share1, share2]: [Share; 3],
100100
parties: Vec<NetworkParty>,
101101
program_artifact: &ProgramArtifact,
@@ -224,23 +224,26 @@ fn spawn_party(
224224
let net = Rep3MpcNet::new(network_config)?;
225225
println!("network setup time: {:?}", start_network.elapsed());
226226

227-
let start_proof = Instant::now();
227+
let total_time = Instant::now();
228228

229-
// generate witness
229+
let witness_time = Instant::now();
230230
let (witness_share, net) = co_noir::generate_witness_rep3(share, program_artifact, net)?;
231+
println!("witness time: {:?}", witness_time.elapsed());
231232

232-
// generate proving key and vk
233+
let pk_time = Instant::now();
233234
let (pk, net) =
234235
co_noir::generate_proving_key_rep3(net, &constraint_system, witness_share, recursive)?;
235236
let vk = pk.create_vk(&prover_crs, *verifier_crs)?;
237+
println!("pk time: {:?}", pk_time.elapsed());
236238

237-
// generate proof
238-
let (proof, public_inputs, _) =
239-
Rep3CoUltraHonk::<_, _, Poseidon2Sponge>::prove(net, pk, &prover_crs, has_zk)?;
239+
let proof_time = Instant::now();
240+
// let (proof, _) = Rep3CoUltraHonk::<_, _, Poseidon2Sponge>::prove(net, pk, &prover_crs, has_zk)?;
241+
let (proof, _) = Rep3CoUltraHonk::<_, _, Poseidon2Sponge>::prove(net, pk, &prover_crs, has_zk)?;
242+
println!("proof time: {:?}", proof_time.elapsed());
240243

241-
println!("proof time: {:?}", start_proof.elapsed());
244+
println!("TOTAL time: {:?}", total_time.elapsed());
242245

243-
let verified = UltraHonk::<_, Poseidon2Sponge>::verify(proof, &public_inputs, &vk, has_zk)?;
246+
let verified = UltraHonk::<_, Poseidon2Sponge>::verify(proof, &vk, has_zk)?;
244247

245248
Ok(verified)
246249
}

mpc-server/src/shares/split.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use rand::{Rng, distributions::Alphanumeric};
44
use serde::{Deserialize, Serialize};
55
use std::path::PathBuf;
66

7+
use super::Share;
8+
79
#[derive(Serialize, Deserialize, Debug)]
810
pub struct ProverData {
911
user1: User,
@@ -33,8 +35,8 @@ pub async fn split_handler(
3335
let prover_path1 = save_prover_data(&payload, false)?;
3436
let prover_path2 = save_prover_data(&payload, true)?;
3537

36-
let shares1 = split_input(prover_path1, &program_artifact)?;
37-
let shares2 = split_input(prover_path2, &program_artifact)?;
38+
let shares1 = shares_to_vec_u8(split_input(prover_path1, &program_artifact)?)?;
39+
let shares2 = shares_to_vec_u8(split_input(prover_path2, &program_artifact)?)?;
3840

3941
let mut out = shares1
4042
.iter()
@@ -51,22 +53,25 @@ pub async fn split_handler(
5153
Ok(out)
5254
}
5355

54-
fn split_input(
56+
pub fn split_input(
5557
input_path: PathBuf,
5658
program_artifact: &ProgramArtifact,
57-
) -> Result<Vec<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>> {
59+
) -> Result<[Share; 3], Box<dyn std::error::Error + Send + Sync + 'static>> {
5860
let inputs = co_noir::parse_input(input_path, &program_artifact)?;
5961

6062
let mut rng = rand::thread_rng();
6163
let shares = co_noir::split_input_rep3::<Bn254, Rep3MpcNet, _>(inputs, &mut rng);
6264

63-
let out = shares
65+
Ok(shares)
66+
}
67+
68+
fn shares_to_vec_u8(
69+
shares: [Share; 3],
70+
) -> Result<Vec<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>> {
71+
Ok(shares
6472
.iter()
6573
.map(|share| bincode::serialize(share))
66-
.collect::<Result<Vec<Vec<u8>>, _>>()
67-
.unwrap();
68-
69-
Ok(out)
74+
.collect::<Result<Vec<Vec<u8>>, _>>()?)
7075
}
7176

7277
fn save_prover_data(

0 commit comments

Comments
 (0)