]>
git.proxmox.com Git - rustc.git/blob - vendor/crypto-bigint/src/uint/neg_mod.rs
1 //! [`UInt`] negation modulus operations.
3 use crate::{Limb, NegMod, UInt}
;
5 impl<const LIMBS
: usize> UInt
<LIMBS
> {
6 /// Computes `-a mod p` in constant time.
7 /// Assumes `self` is in `[0, p)`.
8 pub const fn neg_mod(&self, p
: &Self) -> Self {
9 let z
= self.ct_is_nonzero();
10 let mut ret
= p
.sbb(self, Limb
::ZERO
).0;
13 // Set ret to 0 if the original value was 0, in which
14 // case ret would be p.
21 /// Computes `-a mod p` in constant time for the special modulus
22 /// `p = MAX+1-c` where `c` is small enough to fit in a single [`Limb`].
23 pub const fn neg_mod_special(&self, c
: Limb
) -> Self {
24 Self::ZERO
.sub_mod_special(self, c
)
28 impl<const LIMBS
: usize> NegMod
for UInt
<LIMBS
> {
31 fn neg_mod(&self, p
: &Self) -> Self {
32 debug_assert
!(self < p
);
44 U256
::from_be_hex("8d16e171674b4e6d8529edba4593802bf30b8cb161dd30aa8e550d41380007c2");
46 U256
::from_be_hex("928334a4e4be0843ec225a4c9c61df34bdc7a81513e4b6f76f2bfa3148e2e1b5");
48 let actual
= x
.neg_mod(&p
);
50 U256
::from_be_hex("056c53337d72b9d666f86c9256ce5f08cabc1b63b207864ce0d6ecf010e2d9f3");
52 assert_eq
!(expected
, actual
);
58 U256
::from_be_hex("0000000000000000000000000000000000000000000000000000000000000000");
60 U256
::from_be_hex("928334a4e4be0843ec225a4c9c61df34bdc7a81513e4b6f76f2bfa3148e2e1b5");
62 let actual
= x
.neg_mod(&p
);
64 U256
::from_be_hex("0000000000000000000000000000000000000000000000000000000000000000");
66 assert_eq
!(expected
, actual
);