1From a26dbf3aa0c3f0c68c4ffcdf1670ec998d088c1e Mon Sep 17 00:00:00 2001 2From: Brian Smith <brian@briansmith.org> 3Date: Fri, 11 Nov 2022 14:32:45 -0800 4Subject: [PATCH 1/2] bigint: Provide a fallback implementation for 5 `bn_mul_mont`. 6 7Provide an implementation of `bn_mul_mont` that works on all targets that 8don't have an assembly language implementation. 9 10Expand `prefixed_export!` to support prefixing functions defined in Rust. 11Function definitions don't end with a semicolon so move the semicolon 12insertion from `prefixed_item!` to its callers. 13 14Unify the codepaths in `bigint` so that `bn_mul_mont` is always used. 15 16(cherry picked from commit 81f4e8d07da3f2ccc57f69a91245b41e6d764a1c) 17Test: builds 18Change-Id: If2cb061684ee1a0831f186c2f4cee3f02c2a236b 19--- 20 src/arithmetic/bigint.rs | 70 ++----------------- 21 src/arithmetic/bigint/bn_mul_mont_fallback.rs | 51 ++++++++++++++ 22 src/prefixed.rs | 28 ++++++-- 23 3 files changed, 77 insertions(+), 72 deletions(-) 24 create mode 100644 src/arithmetic/bigint/bn_mul_mont_fallback.rs 25 26diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs 27index 2b2cdf31f..1eb90fead 100644 28--- a/src/arithmetic/bigint.rs 29+++ b/src/arithmetic/bigint.rs 30@@ -47,6 +47,8 @@ use core::{ 31 ops::{Deref, DerefMut}, 32 }; 33 34+mod bn_mul_mont_fallback; 35+ 36 pub unsafe trait Prime {} 37 38 struct Width<M> { 39@@ -1231,13 +1233,6 @@ impl From<u64> for N0 { 40 fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { 41 debug_assert_eq!(r.len(), m.len()); 42 debug_assert_eq!(a.len(), m.len()); 43- 44- #[cfg(any( 45- target_arch = "aarch64", 46- target_arch = "arm", 47- target_arch = "x86_64", 48- target_arch = "x86" 49- ))] 50 unsafe { 51 bn_mul_mont( 52 r.as_mut_ptr(), 53@@ -1248,19 +1243,6 @@ fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { 54 r.len(), 55 ) 56 } 57- 58- #[cfg(not(any( 59- target_arch = "aarch64", 60- target_arch = "arm", 61- target_arch = "x86_64", 62- target_arch = "x86" 63- )))] 64- { 65- let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; 66- let tmp = &mut tmp[..(2 * a.len())]; 67- limbs_mul(tmp, r, a); 68- limbs_from_mont_in_place(r, tmp, m, n0); 69- } 70 } 71 72 fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N0) { 73@@ -1292,8 +1274,8 @@ fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N 74 #[cfg(not(any( 75 target_arch = "aarch64", 76 target_arch = "arm", 77- target_arch = "x86_64", 78- target_arch = "x86" 79+ target_arch = "x86", 80+ target_arch = "x86_64" 81 )))] 82 fn limbs_mul(r: &mut [Limb], a: &[Limb], b: &[Limb]) { 83 debug_assert_eq!(r.len(), 2 * a.len()); 84@@ -1320,12 +1302,6 @@ fn limbs_mont_product(r: &mut [Limb], a: &[Limb], b: &[Limb], m: &[Limb], n0: &N 85 debug_assert_eq!(a.len(), m.len()); 86 debug_assert_eq!(b.len(), m.len()); 87 88- #[cfg(any( 89- target_arch = "aarch64", 90- target_arch = "arm", 91- target_arch = "x86_64", 92- target_arch = "x86" 93- ))] 94 unsafe { 95 bn_mul_mont( 96 r.as_mut_ptr(), 97@@ -1336,30 +1312,11 @@ fn limbs_mont_product(r: &mut [Limb], a: &[Limb], b: &[Limb], m: &[Limb], n0: &N 98 r.len(), 99 ) 100 } 101- 102- #[cfg(not(any( 103- target_arch = "aarch64", 104- target_arch = "arm", 105- target_arch = "x86_64", 106- target_arch = "x86" 107- )))] 108- { 109- let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; 110- let tmp = &mut tmp[..(2 * a.len())]; 111- limbs_mul(tmp, a, b); 112- limbs_from_mont_in_place(r, tmp, m, n0) 113- } 114 } 115 116 /// r = r**2 117 fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { 118 debug_assert_eq!(r.len(), m.len()); 119- #[cfg(any( 120- target_arch = "aarch64", 121- target_arch = "arm", 122- target_arch = "x86_64", 123- target_arch = "x86" 124- ))] 125 unsafe { 126 bn_mul_mont( 127 r.as_mut_ptr(), 128@@ -1370,27 +1327,8 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { 129 r.len(), 130 ) 131 } 132- 133- #[cfg(not(any( 134- target_arch = "aarch64", 135- target_arch = "arm", 136- target_arch = "x86_64", 137- target_arch = "x86" 138- )))] 139- { 140- let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; 141- let tmp = &mut tmp[..(2 * r.len())]; 142- limbs_mul(tmp, r, r); 143- limbs_from_mont_in_place(r, tmp, m, n0) 144- } 145 } 146 147-#[cfg(any( 148- target_arch = "aarch64", 149- target_arch = "arm", 150- target_arch = "x86_64", 151- target_arch = "x86" 152-))] 153 prefixed_extern! { 154 // `r` and/or 'a' and/or 'b' may alias. 155 fn bn_mul_mont( 156diff --git a/src/arithmetic/bigint/bn_mul_mont_fallback.rs b/src/arithmetic/bigint/bn_mul_mont_fallback.rs 157new file mode 100644 158index 000000000..1357858d0 159--- /dev/null 160+++ b/src/arithmetic/bigint/bn_mul_mont_fallback.rs 161@@ -0,0 +1,51 @@ 162+// Copyright 2015-2022 Brian Smith. 163+// 164+// Permission to use, copy, modify, and/or distribute this software for any 165+// purpose with or without fee is hereby granted, provided that the above 166+// copyright notice and this permission notice appear in all copies. 167+// 168+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 169+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 170+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 171+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 172+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 173+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 174+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 175+ 176+#![cfg(not(any( 177+ target_arch = "aarch64", 178+ target_arch = "arm", 179+ target_arch = "x86", 180+ target_arch = "x86_64" 181+)))] 182+ 183+use super::{limbs_from_mont_in_place, limbs_mul, Limb, MODULUS_MAX_LIMBS, N0}; 184+use crate::c; 185+ 186+prefixed_export! { 187+ unsafe fn bn_mul_mont( 188+ r: *mut Limb, 189+ a: *const Limb, 190+ b: *const Limb, 191+ n: *const Limb, 192+ n0: &N0, 193+ num_limbs: c::size_t, 194+ ) { 195+ // The mutable pointer `r` may alias `a` and/or `b`, so the lifetimes of 196+ // any slices for `a` or `b` must not overlap with the lifetime of any 197+ // mutable for `r`. 198+ 199+ // Nothing aliases `n` 200+ let n = unsafe { core::slice::from_raw_parts(n, num_limbs) }; 201+ 202+ let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; 203+ let tmp = &mut tmp[..(2 * num_limbs)]; 204+ { 205+ let a: &[Limb] = unsafe { core::slice::from_raw_parts(a, num_limbs) }; 206+ let b: &[Limb] = unsafe { core::slice::from_raw_parts(b, num_limbs) }; 207+ limbs_mul(tmp, a, b); 208+ } 209+ let r: &mut [Limb] = unsafe { core::slice::from_raw_parts_mut(r, num_limbs) }; 210+ limbs_from_mont_in_place(r, tmp, n, n0); 211+ } 212+} 213diff --git a/src/prefixed.rs b/src/prefixed.rs 214index c8ac807ee..a35f9212f 100644 215--- a/src/prefixed.rs 216+++ b/src/prefixed.rs 217@@ -14,7 +14,7 @@ macro_rules! prefixed_extern { 218 $name 219 { 220 $( #[$meta] )* 221- $vis fn $name ( $( $arg_pat : $arg_ty ),* ) $( -> $ret_ty )? 222+ $vis fn $name ( $( $arg_pat : $arg_ty ),* ) $( -> $ret_ty )?; 223 } 224 225 } 226@@ -33,15 +33,31 @@ macro_rules! prefixed_extern { 227 $name 228 { 229 $( #[$meta] )* 230- $vis static mut $name: $typ 231+ $vis static mut $name: $typ; 232 } 233 } 234 } 235 }; 236 } 237 238-#[cfg(any(target_arch = "arm", target_arch = "aarch64"))] 239+#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] 240 macro_rules! prefixed_export { 241+ // A function. 242+ { 243+ $( #[$meta:meta] )* 244+ $vis:vis unsafe fn $name:ident ( $( $arg_pat:ident : $arg_ty:ty ),* $(,)? ) $body:block 245+ } => { 246+ prefixed_item! { 247+ export_name 248+ $name 249+ { 250+ $( #[$meta] )* 251+ $vis unsafe fn $name ( $( $arg_pat : $arg_ty ),* ) $body 252+ } 253+ } 254+ }; 255+ 256+ // A global variable. 257 { 258 $( #[$meta:meta] )* 259 $vis:vis static mut $name:ident: $typ:ty = $initial_value:expr; 260@@ -51,10 +67,10 @@ macro_rules! prefixed_export { 261 $name 262 { 263 $( #[$meta] )* 264- $vis static mut $name: $typ = $initial_value 265+ $vis static mut $name: $typ = $initial_value; 266 } 267 } 268- } 269+ }; 270 } 271 272 macro_rules! prefixed_item { 273@@ -81,6 +97,6 @@ macro_rules! prefixed_item { 274 { $( $item:tt )+ } 275 } => { 276 #[$attr = $prefixed_name] 277- $( $item )+; 278+ $( $item )+ 279 }; 280 } 281-- 2822.39.1.519.gcb327c4b5f-goog 283 284