Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindings/rust: add MultiScalar trait. #219

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,13 @@ pub mod min_sig {
);
}

pub trait MultiPoint {
type Output;

fn mult(&self, scalars: &[u8], nbits: usize) -> Self::Output;
fn add(&self) -> Self::Output;
}

#[cfg(feature = "std")]
include!("pippenger.rs");

Expand Down
29 changes: 21 additions & 8 deletions bindings/rust/src/pippenger-no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,29 @@ macro_rules! pippenger_mult_impl {
ret
}

#[inline]
pub fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
let npoints = self.points.len();
self.as_slice().mult(scalars, nbits)
}

#[inline]
pub fn add(&self) -> $point {
self.as_slice().add()
}
}

impl MultiPoint for [$point_affine] {
type Output = $point;

fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
let npoints = self.len();
let nbytes = (nbits + 7) / 8;

if scalars.len() < nbytes * npoints {
panic!("scalars length mismatch");
}

let p: [*const $point_affine; 2] =
[&self.points[0], ptr::null()];
let p: [*const $point_affine; 2] = [&self[0], ptr::null()];
let s: [*const u8; 2] = [&scalars[0], ptr::null()];

let mut ret = <$point>::default();
Expand All @@ -89,10 +102,10 @@ macro_rules! pippenger_mult_impl {
ret
}

pub fn add(&self) -> $point {
let npoints = self.points.len();
fn add(&self) -> $point {
let npoints = self.len();

let p: [*const _; 2] = [&self.points[0], ptr::null()];
let p: [*const _; 2] = [&self[0], ptr::null()];
let mut ret = <$point>::default();
unsafe { $add(&mut ret, &p[0], npoints) };

Expand Down Expand Up @@ -125,7 +138,7 @@ pippenger_mult_impl!(
blst_p1s_tile_pippenger,
blst_p1_add_or_double,
blst_p1_double,
p1_multi_scalar,
p1_multi_point,
blst_p1_generator,
blst_p1_mult,
blst_p1s_add,
Expand All @@ -141,7 +154,7 @@ pippenger_mult_impl!(
blst_p2s_tile_pippenger,
blst_p2_add_or_double,
blst_p2_double,
p2_multi_scalar,
p2_multi_point,
blst_p2_generator,
blst_p2_mult,
blst_p2s_add,
Expand Down
33 changes: 23 additions & 10 deletions bindings/rust/src/pippenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,22 @@ macro_rules! pippenger_mult_impl {
ret
}

#[inline]
pub fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
let npoints = self.points.len();
self.as_slice().mult(scalars, nbits)
}

#[inline]
pub fn add(&self) -> $point {
self.as_slice().add()
}
}

impl MultiPoint for [$point_affine] {
type Output = $point;

fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
let npoints = self.len();
let nbytes = (nbits + 7) / 8;

if scalars.len() < nbytes * npoints {
Expand All @@ -124,8 +138,7 @@ macro_rules! pippenger_mult_impl {
let pool = mt::da_pool();
let ncpus = pool.max_count();
if ncpus < 2 || npoints < 32 {
let p: [*const $point_affine; 2] =
[&self.points[0], ptr::null()];
let p: [*const $point_affine; 2] = [&self[0], ptr::null()];
let s: [*const u8; 2] = [&scalars[0], ptr::null()];

unsafe {
Expand Down Expand Up @@ -178,7 +191,7 @@ macro_rules! pippenger_mult_impl {
}
let grid = &grid[..];

let points = &self.points[..];
let points = &self[..];
let sz = unsafe { $scratch_sizeof(0) / 8 };

let mut row_sync: Vec<AtomicUsize> = Vec::with_capacity(ny);
Expand Down Expand Up @@ -262,13 +275,13 @@ macro_rules! pippenger_mult_impl {
ret
}

pub fn add(&self) -> $point {
let npoints = self.points.len();
fn add(&self) -> $point {
let npoints = self.len();

let pool = mt::da_pool();
let ncpus = pool.max_count();
if ncpus < 2 || npoints < 384 {
let p: [*const _; 2] = [&self.points[0], ptr::null()];
let p: [*const _; 2] = [&self[0], ptr::null()];
let mut ret = <$point>::default();
unsafe { $add(&mut ret, &p[0], npoints) };
return ret;
Expand All @@ -295,7 +308,7 @@ macro_rules! pippenger_mult_impl {
if work >= npoints {
break;
}
p[0] = &self.points[work];
p[0] = &self[work];
if work + chunk > npoints {
chunk = npoints - work;
}
Expand Down Expand Up @@ -345,7 +358,7 @@ pippenger_mult_impl!(
blst_p1s_tile_pippenger,
blst_p1_add_or_double,
blst_p1_double,
p1_multi_scalar,
p1_multi_point,
blst_p1_generator,
blst_p1_mult,
blst_p1s_add,
Expand All @@ -361,7 +374,7 @@ pippenger_mult_impl!(
blst_p2s_tile_pippenger,
blst_p2_add_or_double,
blst_p2_double,
p2_multi_scalar,
p2_multi_point,
blst_p2_generator,
blst_p2_mult,
blst_p2s_add,
Expand Down