crypto_bigint/uint/boxed/
neg.rs1use crate::{BoxedUint, Limb, WideWord, Word, WrappingNeg};
4
5impl BoxedUint {
6 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 }
44}