Skip to content

Commit

Permalink
Simply backend gates via build
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkforest committed Jun 17, 2023
1 parent 840f8e5 commit b87ad6b
Showing 1 changed file with 21 additions and 114 deletions.
135 changes: 21 additions & 114 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,21 @@ use crate::Scalar;

pub mod serial;

#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial"),
))]
#[cfg(curve25519_dalek_backend = "simd")]
pub mod vector;

#[derive(Copy, Clone)]
enum BackendKind {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(curve25519_dalek_backend = "simd")]
Avx2,
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
Avx512,
Serial,
}

#[inline]
fn get_selected_backend() -> BackendKind {
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
{
cpufeatures::new!(cpuid_avx512, "avx512ifma", "avx512vl");
let token_avx512: cpuid_avx512::InitToken = cpuid_avx512::init();
Expand All @@ -84,12 +62,7 @@ fn get_selected_backend() -> BackendKind {
}
}

#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(curve25519_dalek_backend = "simd")]
{
cpufeatures::new!(cpuid_avx2, "avx2");
let token_avx2: cpuid_avx2::InitToken = cpuid_avx2::init();
Expand All @@ -112,10 +85,10 @@ where
use crate::traits::VartimeMultiscalarMul;

match get_selected_backend() {
#[cfg(all(target_arch = "x86_64", curve25519_dalek_bits = "64", not(curve25519_dalek_backend = "fiat"), not(curve25519_dalek_backend = "serial")))]
#[cfg(curve25519_dalek_backend = "simd")]
BackendKind::Avx2 =>
self::vector::scalar_mul::pippenger::spec_avx2::Pippenger::optional_multiscalar_mul::<I, J>(scalars, points),
#[cfg(all(target_arch = "x86_64", nightly, curve25519_dalek_bits = "64", not(curve25519_dalek_backend = "fiat"), not(curve25519_dalek_backend = "serial")))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
BackendKind::Avx512 =>
self::vector::scalar_mul::pippenger::spec_avx512ifma_avx512vl::Pippenger::optional_multiscalar_mul::<I, J>(scalars, points),
BackendKind::Serial =>
Expand All @@ -125,20 +98,9 @@ where

#[cfg(feature = "alloc")]
pub(crate) enum VartimePrecomputedStraus {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(curve25519_dalek_backend = "simd")]
Avx2(self::vector::scalar_mul::precomputed_straus::spec_avx2::VartimePrecomputedStraus),
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
Avx512ifma(
self::vector::scalar_mul::precomputed_straus::spec_avx512ifma_avx512vl::VartimePrecomputedStraus,
),
Expand All @@ -155,10 +117,10 @@ impl VartimePrecomputedStraus {
use crate::traits::VartimePrecomputedMultiscalarMul;

match get_selected_backend() {
#[cfg(all(target_arch = "x86_64", curve25519_dalek_bits = "64", not(curve25519_dalek_backend = "fiat"), not(curve25519_dalek_backend = "serial")))]
#[cfg(curve25519_dalek_backend = "simd")]
BackendKind::Avx2 =>
VartimePrecomputedStraus::Avx2(self::vector::scalar_mul::precomputed_straus::spec_avx2::VartimePrecomputedStraus::new(static_points)),
#[cfg(all(target_arch = "x86_64", nightly, curve25519_dalek_bits = "64", not(curve25519_dalek_backend = "fiat"), not(curve25519_dalek_backend = "serial")))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
BackendKind::Avx512 =>
VartimePrecomputedStraus::Avx512ifma(self::vector::scalar_mul::precomputed_straus::spec_avx512ifma_avx512vl::VartimePrecomputedStraus::new(static_points)),
BackendKind::Serial =>
Expand All @@ -182,24 +144,13 @@ impl VartimePrecomputedStraus {
use crate::traits::VartimePrecomputedMultiscalarMul;

match self {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial"),
))]
#[cfg(curve25519_dalek_backend = "simd")]
VartimePrecomputedStraus::Avx2(inner) => inner.optional_mixed_multiscalar_mul(
static_scalars,
dynamic_scalars,
dynamic_points,
),
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial"),
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
VartimePrecomputedStraus::Avx512ifma(inner) => inner.optional_mixed_multiscalar_mul(
static_scalars,
dynamic_scalars,
Expand All @@ -226,24 +177,13 @@ where
use crate::traits::MultiscalarMul;

match get_selected_backend() {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial"),
))]
#[cfg(curve25519_dalek_backend = "simd")]
BackendKind::Avx2 => {
self::vector::scalar_mul::straus::spec_avx2::Straus::multiscalar_mul::<I, J>(
scalars, points,
)
}
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial"),
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
BackendKind::Avx512 => {
self::vector::scalar_mul::straus::spec_avx512ifma_avx512vl::Straus::multiscalar_mul::<
I,
Expand All @@ -267,24 +207,13 @@ where
use crate::traits::VartimeMultiscalarMul;

match get_selected_backend() {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial"),
))]
#[cfg(curve25519_dalek_backend = "simd")]
BackendKind::Avx2 => {
self::vector::scalar_mul::straus::spec_avx2::Straus::optional_multiscalar_mul::<I, J>(
scalars, points,
)
}
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
BackendKind::Avx512 => {
self::vector::scalar_mul::straus::spec_avx512ifma_avx512vl::Straus::optional_multiscalar_mul::<
I,
Expand All @@ -302,20 +231,9 @@ where
/// Perform constant-time, variable-base scalar multiplication.
pub fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
match get_selected_backend() {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(curve25519_dalek_backend = "simd")]
BackendKind::Avx2 => self::vector::scalar_mul::variable_base::spec_avx2::mul(point, scalar),
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
BackendKind::Avx512 => {
self::vector::scalar_mul::variable_base::spec_avx512ifma_avx512vl::mul(point, scalar)
}
Expand All @@ -327,20 +245,9 @@ pub fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint
#[allow(non_snake_case)]
pub fn vartime_double_base_mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
match get_selected_backend() {
#[cfg(all(
target_arch = "x86_64",
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(curve25519_dalek_backend = "simd")]
BackendKind::Avx2 => self::vector::scalar_mul::vartime_double_base::spec_avx2::mul(a, A, b),
#[cfg(all(
target_arch = "x86_64",
nightly,
curve25519_dalek_bits = "64",
not(curve25519_dalek_backend = "fiat"),
not(curve25519_dalek_backend = "serial")
))]
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
BackendKind::Avx512 => {
self::vector::scalar_mul::vartime_double_base::spec_avx512ifma_avx512vl::mul(a, A, b)
}
Expand Down

0 comments on commit b87ad6b

Please sign in to comment.