Skip to content

Commit 5650885

Browse files
committed
bindings/rust: add MultiPoint trait.
This allows application to perform multi-point operations directly on arrays of affine points without going through p{1,2}_affines class.
1 parent 0ca12bc commit 5650885

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

bindings/rust/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,13 @@ pub mod min_sig {
19711971
);
19721972
}
19731973

1974+
pub trait MultiPoint {
1975+
type Output;
1976+
1977+
fn mult(&self, scalars: &[u8], nbits: usize) -> Self::Output;
1978+
fn add(&self) -> Self::Output;
1979+
}
1980+
19741981
#[cfg(feature = "std")]
19751982
include!("pippenger.rs");
19761983

bindings/rust/src/pippenger-no_std.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,29 @@ macro_rules! pippenger_mult_impl {
5959
ret
6060
}
6161

62+
#[inline]
6263
pub fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
63-
let npoints = self.points.len();
64+
self.as_slice().mult(scalars, nbits)
65+
}
66+
67+
#[inline]
68+
pub fn add(&self) -> $point {
69+
self.as_slice().add()
70+
}
71+
}
72+
73+
impl MultiPoint for [$point_affine] {
74+
type Output = $point;
75+
76+
fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
77+
let npoints = self.len();
6478
let nbytes = (nbits + 7) / 8;
6579

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

70-
let p: [*const $point_affine; 2] =
71-
[&self.points[0], ptr::null()];
84+
let p: [*const $point_affine; 2] = [&self[0], ptr::null()];
7285
let s: [*const u8; 2] = [&scalars[0], ptr::null()];
7386

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

92-
pub fn add(&self) -> $point {
93-
let npoints = self.points.len();
105+
fn add(&self) -> $point {
106+
let npoints = self.len();
94107

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

@@ -125,7 +138,7 @@ pippenger_mult_impl!(
125138
blst_p1s_tile_pippenger,
126139
blst_p1_add_or_double,
127140
blst_p1_double,
128-
p1_multi_scalar,
141+
p1_multi_point,
129142
blst_p1_generator,
130143
blst_p1_mult,
131144
blst_p1s_add,
@@ -141,7 +154,7 @@ pippenger_mult_impl!(
141154
blst_p2s_tile_pippenger,
142155
blst_p2_add_or_double,
143156
blst_p2_double,
144-
p2_multi_scalar,
157+
p2_multi_point,
145158
blst_p2_generator,
146159
blst_p2_mult,
147160
blst_p2s_add,

bindings/rust/src/pippenger.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,22 @@ macro_rules! pippenger_mult_impl {
113113
ret
114114
}
115115

116+
#[inline]
116117
pub fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
117-
let npoints = self.points.len();
118+
self.as_slice().mult(scalars, nbits)
119+
}
120+
121+
#[inline]
122+
pub fn add(&self) -> $point {
123+
self.as_slice().add()
124+
}
125+
}
126+
127+
impl MultiPoint for [$point_affine] {
128+
type Output = $point;
129+
130+
fn mult(&self, scalars: &[u8], nbits: usize) -> $point {
131+
let npoints = self.len();
118132
let nbytes = (nbits + 7) / 8;
119133

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

131144
unsafe {
@@ -178,7 +191,7 @@ macro_rules! pippenger_mult_impl {
178191
}
179192
let grid = &grid[..];
180193

181-
let points = &self.points[..];
194+
let points = &self[..];
182195
let sz = unsafe { $scratch_sizeof(0) / 8 };
183196

184197
let mut row_sync: Vec<AtomicUsize> = Vec::with_capacity(ny);
@@ -262,13 +275,13 @@ macro_rules! pippenger_mult_impl {
262275
ret
263276
}
264277

265-
pub fn add(&self) -> $point {
266-
let npoints = self.points.len();
278+
fn add(&self) -> $point {
279+
let npoints = self.len();
267280

268281
let pool = mt::da_pool();
269282
let ncpus = pool.max_count();
270283
if ncpus < 2 || npoints < 384 {
271-
let p: [*const _; 2] = [&self.points[0], ptr::null()];
284+
let p: [*const _; 2] = [&self[0], ptr::null()];
272285
let mut ret = <$point>::default();
273286
unsafe { $add(&mut ret, &p[0], npoints) };
274287
return ret;
@@ -295,7 +308,7 @@ macro_rules! pippenger_mult_impl {
295308
if work >= npoints {
296309
break;
297310
}
298-
p[0] = &self.points[work];
311+
p[0] = &self[work];
299312
if work + chunk > npoints {
300313
chunk = npoints - work;
301314
}
@@ -345,7 +358,7 @@ pippenger_mult_impl!(
345358
blst_p1s_tile_pippenger,
346359
blst_p1_add_or_double,
347360
blst_p1_double,
348-
p1_multi_scalar,
361+
p1_multi_point,
349362
blst_p1_generator,
350363
blst_p1_mult,
351364
blst_p1s_add,
@@ -361,7 +374,7 @@ pippenger_mult_impl!(
361374
blst_p2s_tile_pippenger,
362375
blst_p2_add_or_double,
363376
blst_p2_double,
364-
p2_multi_scalar,
377+
p2_multi_point,
365378
blst_p2_generator,
366379
blst_p2_mult,
367380
blst_p2s_add,

0 commit comments

Comments
 (0)