Skip to content

Commit e4a4fd2

Browse files
committed
feat: refact multicore to singlecore
1 parent 6f3d3a3 commit e4a4fd2

File tree

2 files changed

+112
-72
lines changed

2 files changed

+112
-72
lines changed

phase2/src/parameters.rs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ impl MPCParameters {
441441
) -> [u8; 64]
442442
{
443443
println!("MPCParameters::contribute()");
444-
// Generate a keypair
445444
let (pubkey, privkey) = keypair(rng, self);
446445

447446
println!("MPCParameters::batch_exp1()");
@@ -450,45 +449,56 @@ impl MPCParameters {
450449
let coeff = coeff.into_repr();
451450

452451
let mut projective = vec![C::Projective::zero(); bases.len()];
453-
let cpus = num_cpus::get();
454-
let chunk_size = if bases.len() < cpus {
455-
1
456-
} else {
457-
bases.len() / cpus
458-
};
459-
460-
// Perform wNAF over multiple cores, placing results into `projective`.
461-
crossbeam::scope(|scope| {
462-
for (bases, projective) in bases.chunks_mut(chunk_size)
463-
.zip(projective.chunks_mut(chunk_size))
464-
{
465-
scope.spawn(move |_| {
466-
let mut wnaf = Wnaf::new();
467-
let mut count = 0;
468-
for (base, projective) in bases.iter_mut()
469-
.zip(projective.iter_mut())
470-
{
471-
*projective = wnaf.base(base.into_projective(), 1).scalar(coeff);
472-
count = count + 1;
473-
if *progress_update_interval > 0 && count % *progress_update_interval == 0 {
474-
println!("progress {} {}", *progress_update_interval, *total_exps)
475-
}
476-
}
477-
});
478-
}
479-
}).unwrap();
480-
481-
// Perform batch normalization
482-
crossbeam::scope(|scope| {
483-
for projective in projective.chunks_mut(chunk_size)
484-
{
485-
scope.spawn(move |_| {
486-
C::Projective::batch_normalization(projective);
487-
});
488-
}
489-
}).unwrap();
490-
491-
// Turn it all back into affine points
452+
// let cpus = num_cpus::get();
453+
// let chunk_size = if bases.len() < cpus {
454+
// 1
455+
// } else {
456+
// bases.len() / cpus
457+
// };
458+
459+
// // Perform wNAF over multiple cores, placing results into `projective`.
460+
// crossbeam::scope(|scope| {
461+
// for (bases, projective) in bases.chunks_mut(chunk_size)
462+
// .zip(projective.chunks_mut(chunk_size))
463+
// {
464+
// scope.spawn(move |_| {
465+
// let mut wnaf = Wnaf::new();
466+
// let mut count = 0;
467+
// for (base, projective) in bases.iter_mut()
468+
// .zip(projective.iter_mut())
469+
// {
470+
// *projective = wnaf.base(base.into_projective(), 1).scalar(coeff);
471+
// count = count + 1;
472+
// if *progress_update_interval > 0 && count % *progress_update_interval == 0 {
473+
// println!("progress {} {}", *progress_update_interval, *total_exps)
474+
// }
475+
// }
476+
// });
477+
// }
478+
// }).unwrap();
479+
480+
// // Perform batch normalization
481+
// crossbeam::scope(|scope| {
482+
// for projective in projective.chunks_mut(chunk_size)
483+
// {
484+
// scope.spawn(move |_| {
485+
// C::Projective::batch_normalization(projective);
486+
// });
487+
// }
488+
// }).unwrap();
489+
490+
let mut wnaf = Wnaf::new();
491+
let mut count = 0;
492+
for (base, projective) in bases.iter_mut().zip(projective.iter_mut()) {
493+
*projective = wnaf.base(base.into_projective(), 1).scalar(coeff);
494+
count += 1;
495+
if *progress_update_interval > 0 && count % *progress_update_interval == 0 {
496+
println!("progress {} {}", *progress_update_interval, *total_exps)
497+
}
498+
}
499+
500+
C::Projective::batch_normalization(&mut projective);
501+
492502
for (projective, affine) in projective.iter().zip(bases.iter_mut()) {
493503
*affine = projective.into_affine();
494504
}
@@ -852,9 +862,11 @@ pub fn verify_contribution(
852862
(G1Affine::one(), pubkey.delta_after),
853863
(G2Affine::one(), after.params.vk.delta_g2)
854864
) {
865+
println!("verify_contribution::27.1");
855866
return Err(());
856867
}
857868

869+
println!("function merge_pairs needs update");
858870
// H and L queries should be updated with delta^-1
859871
if !same_ratio(
860872
merge_pairs(&before.params.h, &after.params.h),

phase2/src/utils.rs

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,48 +58,76 @@ pub fn same_ratio<G1: CurveAffine>(
5858
/// ... with high probability.
5959
pub fn merge_pairs<G: CurveAffine>(v1: &[G], v2: &[G]) -> (G, G)
6060
{
61-
use std::sync::Mutex;
62-
use rand::{thread_rng};
61+
// use std::sync::Mutex;
62+
// use rand::{thread_rng};
6363

64-
assert_eq!(v1.len(), v2.len());
64+
// assert_eq!(v1.len(), v2.len());
6565

66-
let chunk = (v1.len() / num_cpus::get()) + 1;
66+
// let chunk = (v1.len() / num_cpus::get()) + 1;
6767

68-
let s = Arc::new(Mutex::new(G::Projective::zero()));
69-
let sx = Arc::new(Mutex::new(G::Projective::zero()));
68+
// let s = Arc::new(Mutex::new(G::Projective::zero()));
69+
// let sx = Arc::new(Mutex::new(G::Projective::zero()));
7070

71-
crossbeam::scope(|scope| {
72-
for (v1, v2) in v1.chunks(chunk).zip(v2.chunks(chunk)) {
73-
let s = s.clone();
74-
let sx = sx.clone();
71+
// crossbeam::scope(|scope| {
72+
// for (v1, v2) in v1.chunks(chunk).zip(v2.chunks(chunk)) {
73+
// let s = s.clone();
74+
// let sx = sx.clone();
7575

76-
scope.spawn(move |_| {
77-
// We do not need to be overly cautious of the RNG
78-
// used for this check.
79-
let rng = &mut thread_rng();
76+
// scope.spawn(move |_| {
77+
// // We do not need to be overly cautious of the RNG
78+
// // used for this check.
79+
// let rng = &mut thread_rng();
8080

81-
let mut wnaf = Wnaf::new();
82-
let mut local_s = G::Projective::zero();
83-
let mut local_sx = G::Projective::zero();
81+
// let mut wnaf = Wnaf::new();
82+
// let mut local_s = G::Projective::zero();
83+
// let mut local_sx = G::Projective::zero();
8484

85-
for (v1, v2) in v1.iter().zip(v2.iter()) {
86-
let rho = G::Scalar::rand(rng);
87-
let mut wnaf = wnaf.scalar(rho.into_repr());
88-
let v1 = wnaf.base(v1.into_projective());
89-
let v2 = wnaf.base(v2.into_projective());
85+
// for (v1, v2) in v1.iter().zip(v2.iter()) {
86+
// let rho = G::Scalar::rand(rng);
87+
// let mut wnaf = wnaf.scalar(rho.into_repr());
88+
// let v1 = wnaf.base(v1.into_projective());
89+
// let v2 = wnaf.base(v2.into_projective());
9090

91-
local_s.add_assign(&v1);
92-
local_sx.add_assign(&v2);
93-
}
91+
// local_s.add_assign(&v1);
92+
// local_sx.add_assign(&v2);
93+
// }
9494

95-
s.lock().unwrap().add_assign(&local_s);
96-
sx.lock().unwrap().add_assign(&local_sx);
97-
});
98-
}
99-
}).unwrap();
95+
// s.lock().unwrap().add_assign(&local_s);
96+
// sx.lock().unwrap().add_assign(&local_sx);
97+
// });
98+
// }
99+
// }).unwrap();
100+
101+
// let s = s.lock().unwrap().into_affine();
102+
// let sx = sx.lock().unwrap().into_affine();
103+
104+
// (s, sx)
105+
106+
use rand::{thread_rng};
107+
108+
assert_eq!(v1.len(), v2.len());
109+
110+
let mut local_s = G::Projective::zero();
111+
let mut local_sx = G::Projective::zero();
112+
113+
// We do not need to be overly cautious of the RNG
114+
// used for this check.
115+
let rng = &mut thread_rng();
116+
117+
let mut wnaf = Wnaf::new();
118+
119+
for (v1, v2) in v1.iter().zip(v2.iter()) {
120+
let rho = G::Scalar::rand(rng);
121+
let mut wnaf = wnaf.scalar(rho.into_repr());
122+
let v1 = wnaf.base(v1.into_projective());
123+
let v2 = wnaf.base(v2.into_projective());
124+
125+
local_s.add_assign(&v1);
126+
local_sx.add_assign(&v2);
127+
}
100128

101-
let s = s.lock().unwrap().into_affine();
102-
let sx = sx.lock().unwrap().into_affine();
129+
let s = local_s.into_affine();
130+
let sx = local_sx.into_affine();
103131

104132
(s, sx)
105133
}

0 commit comments

Comments
 (0)