Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/ciphers/diffie_hellman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,14 @@ impl DiffieHellman {
.unwrap_or_else(|_| BigUint::parse_bytes(b"0", 16).unwrap());

// Check if the other public key is valid based on NIST SP800-56
if BigUint::from(2_u8) <= key
BigUint::from(2_u8) <= key
&& key <= &self.prime - BigUint::from(2_u8)
&& !key
.modpow(
&((&self.prime - BigUint::from(1_u8)) / BigUint::from(2_u8)),
&self.prime,
)
.is_zero()
{
return true;
}
false
}

/// Generate the shared key
Expand Down
2 changes: 1 addition & 1 deletion src/dynamic_programming/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub fn binary_lifting_fibonacci(n: u32) -> u128 {
// the state always stores F(k), F(k+1) for some k, initially F(0), F(1)
let mut state = (0u128, 1u128);

for i in (0..u32::BITS - n.leading_zeros()).rev() {
for i in (0..n.bit_width()).rev() {
// compute F(2k), F(2k+1) from F(k), F(k+1)
state = (
state.0 * (2 * state.1 - state.0),
Expand Down
8 changes: 4 additions & 4 deletions src/math/miller_rabin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub fn big_miller_rabin(number_ref: &BigUint, bases: &[u64]) -> u64 {
let number = number_ref.clone();

if BigUint::from(5u32).cmp(&number) == Ordering::Greater {
if number.eq(&BigUint::zero()) {
return if number.eq(&BigUint::zero()) {
panic!("0 is invalid input for Miller-Rabin. 0 is not prime by definition, but has no witness");
} else if number.eq(&BigUint::from(2u32)) || number.eq(&BigUint::from(3u32)) {
return 0;
0
} else {
return number.to_u64().unwrap();
}
number.to_u64().unwrap()
};
}

if let Some(num) = number.to_u64() {
Expand Down
15 changes: 4 additions & 11 deletions src/number_theory/kth_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
// The idea is to check for each number in the range [N, 1], and print the Kth number that divides N completely.

pub fn kth_factor(n: i32, k: i32) -> i32 {
let mut factors: Vec<i32> = Vec::new();
let k = (k as usize) - 1;
for i in 1..=n {
if n % i == 0 {
factors.push(i);
}
if let Some(number) = factors.get(k) {
return *number;
}
}
-1
(1..=n)
.filter(|&i| n % i == 0)
.nth(k as usize - 1)
.unwrap_or(-1)
}

#[cfg(test)]
Expand Down