-
Notifications
You must be signed in to change notification settings - Fork 734
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 done 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. ``` git difftool HEAD^1:src/limb.rs src/arithmetic/limbs/reduce_once.rs ```
- Loading branch information
1 parent
a5220ab
commit d231bf8
Showing
12 changed files
with
171 additions
and
52 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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 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 AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 super::super::super::inout::AliasingSlices3; | ||
use crate::{c, error::LenMismatchError, limb::*}; | ||
use core::num::NonZeroUsize; | ||
|
||
// `if cond { r = a; }`, assuming `cond` is 0 (false) or 0xff..ff (true). | ||
pub fn limbs_cmov( | ||
cond: Limb, | ||
r: &mut [Limb], | ||
a: &[Limb], | ||
num_limbs: NonZeroUsize, | ||
) -> Result<(), LenMismatchError> { | ||
prefixed_extern! { | ||
// r, a, and/or b may alias. | ||
fn LIMBS_select( | ||
cond: Limb, | ||
r: *mut Limb, | ||
a: *const Limb, | ||
b: *const Limb, | ||
num_limbs: c::NonZero_size_t); | ||
} | ||
(r, a).with_non_dangling_non_null_pointers_rab(num_limbs, |r, a, b| unsafe { | ||
LIMBS_select(cond, r, b, a, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 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 AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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. | ||
|
||
pub(super) mod cmov; | ||
pub(super) mod sub; |
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,40 @@ | ||
// Copyright 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 AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 super::super::super::inout::AliasingSlices3; | ||
use crate::{c, error::LenMismatchError, limb::*}; | ||
use core::num::NonZeroUsize; | ||
|
||
/// r = a - b, where `a` is considered to have `a_high` appended to it as its most | ||
/// significant word. The new value of `a_high` is returned. | ||
#[inline] | ||
pub fn limbs_sub( | ||
a_high: Limb, | ||
in_out: impl AliasingSlices3<Limb>, | ||
num_limbs: NonZeroUsize, | ||
) -> Result<Limb, LenMismatchError> { | ||
prefixed_extern! { | ||
// `r`, 'a', and/or `b` may alias. | ||
fn LIMBS_sub( | ||
a_high: Limb, | ||
r: *mut Limb, | ||
a: *const Limb, | ||
b: *const Limb, | ||
num_limbs: c::NonZero_size_t) | ||
-> Limb; | ||
} | ||
in_out.with_non_dangling_non_null_pointers_rab(num_limbs, |r, a, b| unsafe { | ||
LIMBS_sub(a_high, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 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 AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 super::*; | ||
use crate::{error::LenMismatchError, limb::*}; | ||
use core::num::NonZeroUsize; | ||
|
||
/// Equivalent to `if (r >= m) { r -= m; }` | ||
#[inline] | ||
pub fn limbs_reduce_once(r: &mut [Limb], a: &[Limb], m: &[Limb]) -> Result<(), LenMismatchError> { | ||
let num_limbs = NonZeroUsize::new(m.len()).ok_or_else(|| LenMismatchError::new(m.len()))?; | ||
reduce_once(0, r, a, m, num_limbs) | ||
} | ||
|
||
fn reduce_once( | ||
a_high: Limb, | ||
r: &mut [Limb], | ||
a: &[Limb], | ||
m: &[Limb], | ||
num_limbs: NonZeroUsize, | ||
) -> Result<(), LenMismatchError> { | ||
#[allow(clippy::useless_asref)] | ||
let borrow = limbs_sub(a_high, (r.as_mut(), a, m), num_limbs)?; | ||
limbs_cmov(borrow, r, a, 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