1 // Copyright 2015-2022 Brian Smith. 2 // 3 // Permission to use, copy, modify, and/or distribute this software for any 4 // purpose with or without fee is hereby granted, provided that the above 5 // copyright notice and this permission notice appear in all copies. 6 // 7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 15 #![cfg(not(any( 16 target_arch = "aarch64", 17 target_arch = "arm", 18 target_arch = "x86", 19 target_arch = "x86_64" 20 )))] 21 22 use super::{limbs_from_mont_in_place, limbs_mul, Limb, MODULUS_MAX_LIMBS, N0}; 23 use crate::c; 24 25 prefixed_export! { 26 unsafe fn bn_mul_mont( 27 r: *mut Limb, 28 a: *const Limb, 29 b: *const Limb, 30 n: *const Limb, 31 n0: &N0, 32 num_limbs: c::size_t, 33 ) { 34 // The mutable pointer `r` may alias `a` and/or `b`, so the lifetimes of 35 // any slices for `a` or `b` must not overlap with the lifetime of any 36 // mutable for `r`. 37 38 // Nothing aliases `n` 39 let n = unsafe { core::slice::from_raw_parts(n, num_limbs) }; 40 41 let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; 42 let tmp = &mut tmp[..(2 * num_limbs)]; 43 { 44 let a: &[Limb] = unsafe { core::slice::from_raw_parts(a, num_limbs) }; 45 let b: &[Limb] = unsafe { core::slice::from_raw_parts(b, num_limbs) }; 46 limbs_mul(tmp, a, b); 47 } 48 let r: &mut [Limb] = unsafe { core::slice::from_raw_parts_mut(r, num_limbs) }; 49 limbs_from_mont_in_place(r, tmp, n, n0); 50 } 51 } 52