Skip to content

Commit

Permalink
bigint: Provide a fallback implementation for bn_mul_mont.
Browse files Browse the repository at this point in the history
Provide an implementation of `bn_mul_mont` that works on all targets that
don't have an assembly language implementation.

Expand `prefixed_export!` to support prefixing functions defined in Rust.
Function definitions don't end with a semicolon so move the semicolon
insertion from `prefixed_item!` to its callers.

Unify the codepaths in `bigint` so that `bn_mul_mont` is always used.
  • Loading branch information
briansmith committed Nov 11, 2022
1 parent 0121a80 commit 81f4e8d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 72 deletions.
70 changes: 4 additions & 66 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ use core::{
ops::{Deref, DerefMut},
};

mod bn_mul_mont_fallback;

/// A prime modulus.
///
/// # Safety
Expand Down Expand Up @@ -1216,13 +1218,6 @@ impl From<u64> for N0 {
fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0, _cpu_features: cpu::Features) {
debug_assert_eq!(r.len(), m.len());
debug_assert_eq!(a.len(), m.len());

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
))]
unsafe {
bn_mul_mont(
r.as_mut_ptr(),
Expand All @@ -1233,19 +1228,6 @@ fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0, _cpu_features
r.len(),
)
}

#[cfg(not(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
)))]
{
let mut tmp = [0; 2 * MODULUS_MAX_LIMBS];
let tmp = &mut tmp[..(2 * a.len())];
limbs_mul(tmp, r, a);
limbs_from_mont_in_place(r, tmp, m, n0);
}
}

fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N0) {
Expand Down Expand Up @@ -1277,8 +1259,8 @@ fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N
#[cfg(not(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
target_arch = "x86",
target_arch = "x86_64"
)))]
fn limbs_mul(r: &mut [Limb], a: &[Limb], b: &[Limb]) {
debug_assert_eq!(r.len(), 2 * a.len());
Expand Down Expand Up @@ -1312,12 +1294,6 @@ fn limbs_mont_product(
debug_assert_eq!(a.len(), m.len());
debug_assert_eq!(b.len(), m.len());

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
))]
unsafe {
bn_mul_mont(
r.as_mut_ptr(),
Expand All @@ -1328,30 +1304,11 @@ fn limbs_mont_product(
r.len(),
)
}

#[cfg(not(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
)))]
{
let mut tmp = [0; 2 * MODULUS_MAX_LIMBS];
let tmp = &mut tmp[..(2 * a.len())];
limbs_mul(tmp, a, b);
limbs_from_mont_in_place(r, tmp, m, n0)
}
}

/// r = r**2
fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0, _cpu_features: cpu::Features) {
debug_assert_eq!(r.len(), m.len());
#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
))]
unsafe {
bn_mul_mont(
r.as_mut_ptr(),
Expand All @@ -1362,27 +1319,8 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0, _cpu_features: cpu::Fe
r.len(),
)
}

#[cfg(not(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
)))]
{
let mut tmp = [0; 2 * MODULUS_MAX_LIMBS];
let tmp = &mut tmp[..(2 * r.len())];
limbs_mul(tmp, r, r);
limbs_from_mont_in_place(r, tmp, m, n0)
}
}

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
))]
prefixed_extern! {
// `r` and/or 'a' and/or 'b' may alias.
fn bn_mul_mont(
Expand Down
51 changes: 51 additions & 0 deletions src/arithmetic/bigint/bn_mul_mont_fallback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2015-2022 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#![cfg(not(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
)))]

use super::{limbs_from_mont_in_place, limbs_mul, Limb, MODULUS_MAX_LIMBS, N0};
use crate::c;

prefixed_export! {
unsafe fn bn_mul_mont(
r: *mut Limb,
a: *const Limb,
b: *const Limb,
n: *const Limb,
n0: &N0,
num_limbs: c::size_t,
) {
// The mutable pointer `r` may alias `a` and/or `b`, so the lifetimes of
// any slices for `a` or `b` must not overlap with the lifetime of any
// mutable for `r`.

// Nothing aliases `n`
let n = unsafe { core::slice::from_raw_parts(n, num_limbs) };

let mut tmp = [0; 2 * MODULUS_MAX_LIMBS];
let tmp = &mut tmp[..(2 * num_limbs)];
{
let a: &[Limb] = unsafe { core::slice::from_raw_parts(a, num_limbs) };
let b: &[Limb] = unsafe { core::slice::from_raw_parts(b, num_limbs) };
limbs_mul(tmp, a, b);
}
let r: &mut [Limb] = unsafe { core::slice::from_raw_parts_mut(r, num_limbs) };
limbs_from_mont_in_place(r, tmp, n, n0);
}
}
28 changes: 22 additions & 6 deletions src/prefixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ macro_rules! prefixed_extern {
$name
{
$( #[$meta] )*
$vis fn $name ( $( $arg_pat : $arg_ty ),* ) $( -> $ret_ty )?
$vis fn $name ( $( $arg_pat : $arg_ty ),* ) $( -> $ret_ty )?;
}

}
Expand All @@ -33,15 +33,31 @@ macro_rules! prefixed_extern {
$name
{
$( #[$meta] )*
$vis static mut $name: $typ
$vis static mut $name: $typ;
}
}
}
};
}

#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
macro_rules! prefixed_export {
// A function.
{
$( #[$meta:meta] )*
$vis:vis unsafe fn $name:ident ( $( $arg_pat:ident : $arg_ty:ty ),* $(,)? ) $body:block
} => {
prefixed_item! {
export_name
$name
{
$( #[$meta] )*
$vis unsafe fn $name ( $( $arg_pat : $arg_ty ),* ) $body
}
}
};

// A global variable.
{
$( #[$meta:meta] )*
$vis:vis static mut $name:ident: $typ:ty = $initial_value:expr;
Expand All @@ -51,10 +67,10 @@ macro_rules! prefixed_export {
$name
{
$( #[$meta] )*
$vis static mut $name: $typ = $initial_value
$vis static mut $name: $typ = $initial_value;
}
}
}
};
}

macro_rules! prefixed_item {
Expand All @@ -80,6 +96,6 @@ macro_rules! prefixed_item {
{ $( $item:tt )+ }
} => {
#[$attr = $prefixed_name]
$( $item )+;
$( $item )+
};
}

0 comments on commit 81f4e8d

Please sign in to comment.