/rust/registry/src/index.crates.io-1949cf8c6b5b557f/crypto-bigint-0.5.5/src/uint/neg.rs
Line | Count | Source |
1 | | use core::ops::Neg; |
2 | | |
3 | | use crate::{CtChoice, Limb, Uint, WideWord, Word, Wrapping}; |
4 | | |
5 | | impl<const LIMBS: usize> Neg for Wrapping<Uint<LIMBS>> { |
6 | | type Output = Self; |
7 | | |
8 | 0 | fn neg(self) -> Self::Output { |
9 | 0 | Self(self.0.wrapping_neg()) |
10 | 0 | } |
11 | | } |
12 | | |
13 | | impl<const LIMBS: usize> Uint<LIMBS> { |
14 | | /// Negates based on `choice` by wrapping the integer. |
15 | 0 | pub(crate) const fn conditional_wrapping_neg(&self, choice: CtChoice) -> Uint<LIMBS> { |
16 | 0 | Uint::ct_select(self, &self.wrapping_neg(), choice) |
17 | 0 | } |
18 | | |
19 | | /// Perform wrapping negation. |
20 | 0 | pub const fn wrapping_neg(&self) -> Self { |
21 | 0 | let mut ret = [Limb::ZERO; LIMBS]; |
22 | 0 | let mut carry = 1; |
23 | 0 | let mut i = 0; |
24 | 0 | while i < LIMBS { |
25 | 0 | let r = (!self.limbs[i].0 as WideWord) + carry; |
26 | 0 | ret[i] = Limb(r as Word); |
27 | 0 | carry = r >> Limb::BITS; |
28 | 0 | i += 1; |
29 | 0 | } |
30 | 0 | Uint::new(ret) |
31 | 0 | } |
32 | | } |
33 | | |
34 | | #[cfg(test)] |
35 | | mod tests { |
36 | | use crate::U256; |
37 | | |
38 | | #[test] |
39 | | fn wrapping_neg() { |
40 | | assert_eq!(U256::ZERO.wrapping_neg(), U256::ZERO); |
41 | | assert_eq!(U256::MAX.wrapping_neg(), U256::ONE); |
42 | | assert_eq!( |
43 | | U256::from_u64(13).wrapping_neg(), |
44 | | U256::from_u64(13).not().saturating_add(&U256::ONE) |
45 | | ); |
46 | | assert_eq!( |
47 | | U256::from_u64(42).wrapping_neg(), |
48 | | U256::from_u64(42).saturating_sub(&U256::ONE).not() |
49 | | ); |
50 | | } |
51 | | } |