11use co_noir:: {
2- AcirFormat , Bn254 , NetworkConfig , NetworkParty , PartyID , Poseidon2Sponge , Rep3CoUltraHonk ,
3- Rep3MpcNet , UltraHonk , merge_input_shares,
2+ AcirFormat , Address , Bn254 , NetworkConfig , NetworkParty , PartyID , Poseidon2Sponge ,
3+ Rep3CoUltraHonk , Rep3MpcNet , UltraHonk , merge_input_shares,
44} ;
55use co_ultrahonk:: prelude:: { ProverCrs , ZeroKnowledge } ;
66use noirc_artifacts:: program:: ProgramArtifact ;
77use once_cell:: sync:: Lazy ;
8+ use rayon:: prelude:: * ;
9+ use rustls:: pki_types:: CertificateDer ;
810use rustls:: pki_types:: { PrivateKeyDer , PrivatePkcs8KeyDer } ;
9- use std:: sync:: Arc ;
10- use std:: thread;
1111use std:: {
1212 path:: PathBuf ,
13+ sync:: Arc ,
14+ thread,
1315 time:: { Duration , Instant } ,
1416} ;
1517
16- use crate :: db:: { connect_db, get_all_users, get_user, insert_matches, update_checked} ;
18+ use crate :: db:: {
19+ connect_db, get_all_users, get_user, insert_matches, update_checked, update_checked_many,
20+ } ;
1721use crate :: shares:: { Share , get_shares} ;
1822
1923pub const DATA_DIR : Lazy < PathBuf > =
@@ -25,7 +29,7 @@ pub const SHARES_DIR_2: Lazy<PathBuf> = Lazy::new(|| DATA_DIR.join("user2"));
2529
2630pub async fn run_matches (
2731 user_id : String ,
28- parties : Vec < NetworkParty > ,
32+ parties_certs : [ CertificateDer < ' static > ; 3 ] ,
2933 program_artifact : & ProgramArtifact ,
3034 constraint_system : Arc < AcirFormat < ark_bn254:: Fr > > ,
3135 recursive : bool ,
@@ -52,38 +56,45 @@ pub async fn run_matches(
5256 . collect :: < Vec < String > > ( ) ,
5357 ) ?;
5458
55- let mut verified_matches = Vec :: new ( ) ;
56-
57- for user2 in all_users {
58- update_checked ( & conn , & user2 . id , vec ! [ user_id . clone ( ) ] ) ? ;
59+ let users2 = all_users
60+ . iter ( )
61+ . map ( |u| u . id . clone ( ) )
62+ . collect :: < Vec < String > > ( ) ;
5963
60- let shares_user1 = get_shares ( & user1. id , true ) ?;
61- let shares_user2 = get_shares ( & user2. id , false ) ?;
64+ let verified_matches = all_users
65+ . into_par_iter ( )
66+ . enumerate ( )
67+ . map (
68+ |( thread_id, user2) | -> Result < String , Box < dyn std:: error:: Error + Send + Sync + ' static > > {
69+ let shares_user1 = get_shares ( & user1. id , true ) ?;
70+ let shares_user2 = get_shares ( & user2. id , false ) ?;
6271
63- let share0 = merge_shares ( shares_user1[ 0 ] . clone ( ) , shares_user2[ 0 ] . clone ( ) ) ?;
64- let share1 = merge_shares ( shares_user1[ 1 ] . clone ( ) , shares_user2[ 1 ] . clone ( ) ) ?;
65- let share2 = merge_shares ( shares_user1[ 2 ] . clone ( ) , shares_user2[ 2 ] . clone ( ) ) ?;
72+ let share0 = merge_shares ( shares_user1[ 0 ] . clone ( ) , shares_user2[ 0 ] . clone ( ) ) ?;
73+ let share1 = merge_shares ( shares_user1[ 1 ] . clone ( ) , shares_user2[ 1 ] . clone ( ) ) ?;
74+ let share2 = merge_shares ( shares_user1[ 2 ] . clone ( ) , shares_user2[ 2 ] . clone ( ) ) ?;
6675
67- match run_match (
68- [ share0, share1, share2] ,
69- parties. clone ( ) ,
70- program_artifact,
71- constraint_system. clone ( ) ,
72- recursive,
73- has_zk,
74- prover_crs. clone ( ) ,
75- verifier_crs. clone ( ) ,
76+ match run_match (
77+ thread_id,
78+ [ share0, share1, share2] ,
79+ parties_certs. clone ( ) ,
80+ program_artifact,
81+ constraint_system. clone ( ) ,
82+ recursive,
83+ has_zk,
84+ prover_crs. clone ( ) ,
85+ verifier_crs. clone ( ) ,
86+ ) {
87+ Ok ( _) => Ok ( user2. id ) ,
88+ Err ( e) => Err ( e) ,
89+ }
90+ } ,
7691 )
77- . await
78- {
79- Ok ( _) => {
80- verified_matches. push ( user2. id ) ;
81- }
82- Err ( e) => {
83- println ! ( "Error: {:?}" , e) ;
84- }
85- }
86- }
92+ . filter ( |m| m. is_ok ( ) )
93+ . collect :: < Result < Vec < String > , _ > > ( ) ?;
94+
95+ println ! ( "verified matches: {verified_matches:?}" ) ;
96+
97+ update_checked_many ( & conn, users2, vec ! [ user_id. clone( ) ] ) ?;
8798
8899 insert_matches (
89100 & conn,
@@ -95,9 +106,10 @@ pub async fn run_matches(
95106 Ok ( ( ) )
96107}
97108
98- pub async fn run_match (
109+ pub fn run_match (
110+ thread_id : usize ,
99111 [ share0, share1, share2] : [ Share ; 3 ] ,
100- parties : Vec < NetworkParty > ,
112+ parties_certs : [ CertificateDer < ' static > ; 3 ] ,
101113 program_artifact : & ProgramArtifact ,
102114 constraint_system : Arc < AcirFormat < ark_bn254:: Fr > > ,
103115 recursive : bool ,
@@ -107,9 +119,31 @@ pub async fn run_match(
107119) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync + ' static > > {
108120 let match_time = Instant :: now ( ) ;
109121
122+ let party0_port = 10000 + thread_id as u16 ;
123+ let party1_port = 11000 + thread_id as u16 ;
124+ let party2_port = 12000 + thread_id as u16 ;
125+
126+ let parties = vec ! [
127+ NetworkParty :: new(
128+ PartyID :: ID0 . into( ) ,
129+ Address :: new( "localhost" . to_string( ) , party0_port) ,
130+ parties_certs[ 0 ] . clone( ) ,
131+ ) ,
132+ NetworkParty :: new(
133+ PartyID :: ID1 . into( ) ,
134+ Address :: new( "localhost" . to_string( ) , party1_port) ,
135+ parties_certs[ 1 ] . clone( ) ,
136+ ) ,
137+ NetworkParty :: new(
138+ PartyID :: ID2 . into( ) ,
139+ Address :: new( "localhost" . to_string( ) , party2_port) ,
140+ parties_certs[ 2 ] . clone( ) ,
141+ ) ,
142+ ] ;
143+
110144 let data0 = DataForThread {
111145 id : PartyID :: ID0 ,
112- port : 10000 ,
146+ port : party0_port ,
113147 key : PrivateKeyDer :: Pkcs8 ( PrivatePkcs8KeyDer :: from ( std:: fs:: read (
114148 CONFIG_DIR . join ( "key0.der" ) ,
115149 ) ?) )
@@ -125,7 +159,7 @@ pub async fn run_match(
125159 } ;
126160 let data1 = DataForThread {
127161 id : PartyID :: ID1 ,
128- port : 10001 ,
162+ port : party1_port ,
129163 key : PrivateKeyDer :: Pkcs8 ( PrivatePkcs8KeyDer :: from ( std:: fs:: read (
130164 CONFIG_DIR . join ( "key1.der" ) ,
131165 ) ?) )
@@ -141,7 +175,7 @@ pub async fn run_match(
141175 } ;
142176 let data2 = DataForThread {
143177 id : PartyID :: ID2 ,
144- port : 10002 ,
178+ port : party2_port ,
145179 key : PrivateKeyDer :: Pkcs8 ( PrivatePkcs8KeyDer :: from ( std:: fs:: read (
146180 CONFIG_DIR . join ( "key2.der" ) ,
147181 ) ?) )
@@ -237,7 +271,6 @@ fn spawn_party(
237271 println ! ( "pk time: {:?}" , pk_time. elapsed( ) ) ;
238272
239273 let proof_time = Instant :: now ( ) ;
240- // let (proof, _) = Rep3CoUltraHonk::<_, _, Poseidon2Sponge>::prove(net, pk, &prover_crs, has_zk)?;
241274 let ( proof, _) = Rep3CoUltraHonk :: < _ , _ , Poseidon2Sponge > :: prove ( net, pk, & prover_crs, has_zk) ?;
242275 println ! ( "proof time: {:?}" , proof_time. elapsed( ) ) ;
243276
0 commit comments