1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Toggles the swap parity of the GCD state.
use super::GcdState;
impl GcdState {
/// Flips the cumulative swap parity.
///
/// Each Euclidean reduction step (full-precision, scalar, or Lehmer matrix
/// application) swaps the roles of `r0`/`r1` and `t0`/`t1`. This method
/// records that swap by toggling `even`, which determines the sign
/// correction applied when extracting the final inverse.
///
/// # Examples
///
/// ```
/// use cnfy_uint::gcd_state::GcdState;
/// use cnfy_uint::u256::U256;
///
/// let mut state = GcdState::new(
/// U256::from_be_limbs([0, 0, 0, 97]),
/// U256::from_be_limbs([0, 0, 0, 42]),
/// );
/// assert!(state.even);
/// state.toggle_even();
/// assert!(!state.even);
/// ```
#[inline]
pub fn toggle_even(&mut self) {
self.even = !self.even;
}
}
#[cfg(test)]
mod ai_tests {
use crate::u256::U256;
use super::*;
/// Toggling once flips from true to false.
#[test]
fn toggle_once() {
let mut state = GcdState::new(
U256::from_be_limbs([0, 0, 0, 97]),
U256::from_be_limbs([0, 0, 0, 42]),
);
assert!(state.even);
state.toggle_even();
assert!(!state.even);
}
/// Toggling twice restores original parity.
#[test]
fn toggle_twice_restores() {
let mut state = GcdState::new(
U256::from_be_limbs([0, 0, 0, 97]),
U256::from_be_limbs([0, 0, 0, 42]),
);
state.toggle_even();
state.toggle_even();
assert!(state.even);
}
/// Toggling from false goes to true.
#[test]
fn toggle_from_false() {
let mut state = GcdState {
modulus: U256::ZERO,
r0: U256::ZERO,
r1: U256::ZERO,
t0: U256::ZERO,
t1: U256::ZERO,
even: false,
};
state.toggle_even();
assert!(state.even);
}
}