Skip to content

Commit

Permalink
[dkg] remove aptos-dkg unsafe code (#12328)
Browse files Browse the repository at this point in the history
Co-authored-by: Zekun Li <[email protected]>
Co-authored-by: Alin Tomescu <[email protected]>
  • Loading branch information
3 people authored Mar 1, 2024
1 parent 4a507f1 commit f77f51c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
5 changes: 2 additions & 3 deletions crates/aptos-dkg/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright © Aptos Foundation

use crate::utils::{
parallel_multi_pairing::parallel_multi_miller_loop_and_final_exp,
random::random_scalar_from_uniform_bytes,
parallel_multi_pairing::parallel_multi_pairing_slice, random::random_scalar_from_uniform_bytes,
};
use blstrs::{
pairing, Bls12, G1Affine, G1Projective, G2Affine, G2Prepared, G2Projective, Gt, Scalar,
Expand Down Expand Up @@ -112,7 +111,7 @@ where
I1: Iterator<Item = &'a G1Projective>,
I2: Iterator<Item = &'a G2Projective>,
{
parallel_multi_miller_loop_and_final_exp(
parallel_multi_pairing_slice(
lhs.zip(rhs)
.map(|(g1, g2)| (g1.to_affine(), g2.to_affine()))
.collect::<Vec<(G1Affine, G2Affine)>>()
Expand Down
28 changes: 7 additions & 21 deletions crates/aptos-dkg/src/utils/parallel_multi_pairing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright © Aptos Foundation

use blst::{blst_final_exp, blst_fp12, blst_fp12_mul, blst_fp12_one, blst_miller_loop};
use blst::blst_fp12;
use blstrs::{Fp12, G1Affine, G2Affine, Gt};
use group::prime::PrimeCurveAffine;
use rayon::{prelude::*, ThreadPool};

/// Computes $$\sum_{i=1}^n \textbf{ML}(a_i, b_i)$$ given a series of terms
/// $$(a_1, b_1), (a_2, b_2), ..., (a_n, b_n).$$
pub fn parallel_multi_miller_loop_and_final_exp(
/// Computes a multi-pairing $$\prod_{i=1}^n e(a_i, b_i)$$ using multiple threads from `pool`.
pub fn parallel_multi_pairing_slice(
terms: &[(&G1Affine, &G2Affine)],
pool: &ThreadPool,
min_length: usize,
Expand All @@ -19,27 +18,14 @@ pub fn parallel_multi_miller_loop_and_final_exp(
.map(|(p, q)| {
if (p.is_identity() | q.is_identity()).into() {
// Define pairing with zero as one, matching what `pairing` does.
unsafe { *blst_fp12_one() }
blst_fp12::default()
} else {
unsafe {
let mut tmp = blst_fp12::default();
blst_miller_loop(&mut tmp, q.as_ref(), p.as_ref());
tmp
}
blst_fp12::miller_loop(q.as_ref(), p.as_ref())
}
})
.reduce(
|| unsafe { *blst_fp12_one() },
|mut acc, val| {
unsafe {
blst_fp12_mul(&mut acc, &acc, &val);
}
acc
},
)
.reduce(|| blst_fp12::default(), |acc, val| acc * val)
});

let mut out = blst_fp12::default();
unsafe { blst_final_exp(&mut out, &res) };
let out = blst_fp12::final_exp(&res);
Fp12::from(out).into()
}

0 comments on commit f77f51c

Please sign in to comment.