Skip to content

Commit

Permalink
Parallel pippenger from blst (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
belijzajac authored Apr 6, 2023
2 parents 2079928 + 7e3941f commit c4d4893
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
53 changes: 39 additions & 14 deletions blst-from-scratch/src/kzg_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,76 @@
extern crate alloc;

#[cfg(not(feature = "parallel"))]
use alloc::vec;
use alloc::vec::Vec;
#[cfg(not(feature = "parallel"))]
use core::ptr;

#[cfg(feature = "parallel")]
use blst::p1_affines;
#[cfg(not(feature = "parallel"))]
use blst::{
blst_fp12_is_one, blst_p1, blst_p1_affine, blst_p1_cneg, blst_p1_to_affine,
blst_p1s_mult_pippenger, blst_p1s_mult_pippenger_scratch_sizeof, blst_p1s_to_affine,
blst_p2_affine, blst_p2_to_affine, blst_scalar, limb_t, Pairing,
blst_scalar, limb_t,
};

use blst::{
blst_fp12_is_one, blst_p1, blst_p1_affine, blst_p1_cneg, blst_p1_to_affine, blst_p2_affine,
blst_p2_to_affine, Pairing,
};

use kzg::{G1Mul, G1};

use crate::consts::G1_IDENTITY;
use crate::types::fr::FsFr;
use crate::types::g1::FsG1;
use crate::types::g2::FsG2;

pub fn g1_linear_combination(out: &mut FsG1, p: &[FsG1], coeffs: &[FsFr], len: usize) {
pub fn g1_linear_combination(out: &mut FsG1, points: &[FsG1], scalars: &[FsFr], len: usize) {
if len < 8 {
// Direct approach
let mut tmp;
*out = G1_IDENTITY;
*out = FsG1::default();
for i in 0..len {
tmp = p[i].mul(&coeffs[i]);
let tmp = points[i].mul(&scalars[i]);
*out = out.add_or_dbl(&tmp);
}
} else {
return;
}

#[cfg(feature = "parallel")]
{
let points = unsafe { core::slice::from_raw_parts(points.as_ptr() as *const blst_p1, len) };
let points = p1_affines::from(points);

let mut scalar_bytes: Vec<u8> = Vec::with_capacity(len * 32);
for bytes in scalars.iter().map(|b| b.to_scalar()) {
scalar_bytes.extend_from_slice(&bytes);
}

let res = points.mult(scalar_bytes.as_slice(), 255);
*out = FsG1(res)
}

#[cfg(not(feature = "parallel"))]
{
let mut scratch: Vec<u8>;
unsafe {
scratch = vec![0u8; blst_p1s_mult_pippenger_scratch_sizeof(len) as usize];
}

let mut p_affine = vec![blst_p1_affine::default(); len];
let mut scalars = vec![blst_scalar::default(); len];
let mut p_scalars = vec![blst_scalar::default(); len];

let p_arg: [*const blst_p1; 2] = [&p[0].0, ptr::null()];
let p_arg: [*const blst_p1; 2] = [&points[0].0, ptr::null()];
unsafe {
blst_p1s_to_affine(p_affine.as_mut_ptr(), p_arg.as_ptr(), len);
}

for i in 0..len {
scalars[i] = blst_scalar {
b: coeffs[i].to_scalar(),
p_scalars[i] = blst_scalar {
b: scalars[i].to_scalar(),
};
}

let scalars_arg: [*const blst_scalar; 2] = [scalars.as_ptr(), ptr::null()];
let scalars_arg: [*const blst_scalar; 2] = [p_scalars.as_ptr(), ptr::null()];
let points_arg: [*const blst_p1_affine; 2] = [p_affine.as_ptr(), ptr::null()];
unsafe {
blst_p1s_mult_pippenger(
Expand Down
4 changes: 3 additions & 1 deletion blst-from-scratch/src/types/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use blst::{
blst_fp2, blst_p2, blst_p2_add_or_double, blst_p2_cneg, blst_p2_double, blst_p2_is_equal,
blst_p2_mult, blst_scalar, blst_scalar_from_fr,
};
use kzg::{Fr, G2Mul, G2};
#[cfg(feature = "std")]
use kzg::Fr;
use kzg::{G2Mul, G2};

use crate::consts::{G2_GENERATOR, G2_NEGATIVE_GENERATOR};
use crate::types::fr::FsFr;
Expand Down

0 comments on commit c4d4893

Please sign in to comment.