-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
arithmetic: Rewrite
limbs_reduce_once
.
Move the function to `arithmetic` from `limb`. This is step towards moving all arithmetic out of `limb`. Change the signature so that the reduction is non separately instead of in-place. It was already being done separately in `bigint` and it costs very little, if anything, to do the same in the other caller in `ec`. Optimize the implementation to take advantage of the fact that `r` and `a` do not alias each other. To do so, replace `LIMBS_reduce_once` with two separate helper functions, `LIMBS_sub` and `LIMBS_cmov`. As part of doing this, ensure we're not passing any empty slices to the relevant C code.
- Loading branch information
1 parent
a75be07
commit d5be4f5
Showing
8 changed files
with
88 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ mod constant; | |
#[cfg(feature = "alloc")] | ||
pub mod bigint; | ||
|
||
pub mod add; | ||
pub mod montgomery; | ||
|
||
mod n0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2015-2025 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. | ||
|
||
use crate::{c, error::LenMismatchError, limb::*, polyfill::AliasingSlices as _}; | ||
use core::num::NonZeroUsize; | ||
|
||
/// Equivalent to `r = if a < m { a } else { a - m };` | ||
#[inline] | ||
pub fn limbs_reduce_once(r: &mut [Limb], a: &[Limb], m: &[Limb]) -> Result<(), LenMismatchError> { | ||
let underflow = limbs_sub(r, a, m)?; | ||
limbs_cmov(r, a, underflow) | ||
} | ||
|
||
// `r = a - b`, returning true on underflow, false otherwise. | ||
fn limbs_sub(r: &mut [Limb], a: &[Limb], b: &[Limb]) -> Result<LimbMask, LenMismatchError> { | ||
prefixed_extern! { | ||
// `r`, 'a', and/or `b` may alias. | ||
fn LIMBS_sub(r: *mut Limb, a: *const Limb, b: *const Limb, num_limbs: c::NonZero_size_t) | ||
-> LimbMask; | ||
} | ||
let num_limbs = NonZeroUsize::new(r.len()).ok_or_else(|| LenMismatchError::new(r.len()))?; | ||
(r, a, b).with_pointers(num_limbs.into(), |r, a, b| unsafe { | ||
LIMBS_sub(r, a, b, num_limbs) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters