-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlib.rs
73 lines (65 loc) · 2.15 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright Supranational LLC
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
extern crate semolina;
#[cfg(feature = "cuda")]
sppark::cuda_error!();
#[cfg(feature = "cuda")]
extern "C" {
fn cuda_available() -> bool;
}
#[cfg(feature = "cuda")]
pub static mut CUDA_OFF: bool = false;
macro_rules! multi_scalar_mult {
(
$pasta:ident,
$mult:ident,
$cuda_mult:ident
) => {
use pasta_curves::$pasta;
extern "C" {
fn $mult(
out: *mut $pasta::Point,
points: *const $pasta::Affine,
npoints: usize,
scalars: *const $pasta::Scalar,
is_mont: bool,
);
}
pub fn $pasta(
points: &[$pasta::Affine],
scalars: &[$pasta::Scalar],
) -> $pasta::Point {
let npoints = points.len();
if npoints != scalars.len() {
panic!("length mismatch")
}
#[cfg(feature = "cuda")]
if npoints >= 1 << 16 && unsafe { !CUDA_OFF && cuda_available() } {
extern "C" {
fn $cuda_mult(
out: *mut $pasta::Point,
points: *const $pasta::Affine,
npoints: usize,
scalars: *const $pasta::Scalar,
is_mont: bool,
) -> cuda::Error;
}
let mut ret = $pasta::Point::default();
let err = unsafe {
$cuda_mult(&mut ret, &points[0], npoints, &scalars[0], true)
};
if err.code != 0 {
panic!("{}", String::from(err));
}
return ret;
}
let mut ret = $pasta::Point::default();
unsafe { $mult(&mut ret, &points[0], npoints, &scalars[0], true) };
ret
}
};
}
multi_scalar_mult!(pallas, mult_pippenger_pallas, cuda_pippenger_pallas);
multi_scalar_mult!(vesta, mult_pippenger_vesta, cuda_pippenger_vesta);
include!("tests.rs");