crypto_bigint/uint/boxed/
neg.rs

1//! [`BoxedUint`] negation operations.
2
3use crate::{BoxedUint, Limb, WideWord, Word, WrappingNeg};
4
5impl BoxedUint {
6    /// Perform wrapping negation.
7    pub fn wrapping_neg(&self) -> Self {
8        let mut ret = vec![Limb::ZERO; self.nlimbs()];
9        let mut carry = 1;
10
11        for i in 0..self.nlimbs() {
12            let r = (!self.limbs[i].0 as WideWord) + carry;
13            ret[i] = Limb(r as Word);
14            carry = r >> Limb::BITS;
15        }
16
17        ret.into()
18    }
19}
20
21impl WrappingNeg for BoxedUint {
22    fn wrapping_neg(&self) -> Self {
23        self.wrapping_neg()
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::BoxedUint;
30
31    #[test]
32    fn wrapping_neg() {
33        assert_eq!(BoxedUint::zero().wrapping_neg(), BoxedUint::zero());
34        assert_eq!(BoxedUint::max(64).wrapping_neg(), BoxedUint::one());
35        // assert_eq!(
36        //     BoxedUint::from(13u64).wrapping_neg(),
37        //     BoxedUint::from(13u64).not().saturating_add(&BoxedUint::one())
38        // );
39        // assert_eq!(
40        //     BoxedUint::from(42u64).wrapping_neg(),
41        //     BoxedUint::from(42u64).saturating_sub(&BoxedUint::one()).not()
42        // );
43    }
44}