-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from recmo/redc
Add mul_redc
- Loading branch information
Showing
7 changed files
with
190 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use super::mul; | ||
use core::iter::zip; | ||
|
||
/// See Handbook of Applied Cryptography, Algorithm 14.32, p. 601. | ||
pub fn mul_redc(a: &[u64], b: &[u64], result: &mut [u64], m: &[u64], inv: u64) { | ||
debug_assert!(!m.is_empty()); | ||
debug_assert_eq!(a.len(), m.len()); | ||
debug_assert_eq!(b.len(), m.len()); | ||
debug_assert_eq!(result.len(), m.len()); | ||
debug_assert_eq!(inv.wrapping_mul(m[0]), u64::MAX); | ||
|
||
// Compute temp full product. | ||
// OPT: Do combined multiplication and reduction. | ||
let mut temp = vec![0; 2 * m.len() + 1]; | ||
mul(a, b, &mut temp); | ||
|
||
// Reduce temp. | ||
for i in 0..m.len() { | ||
let u = temp[i].wrapping_mul(inv); | ||
|
||
// REFACTOR: Create add_mul1 routine. | ||
let mut carry = 0; | ||
#[allow(clippy::cast_possible_truncation)] // Intentional | ||
for j in 0..m.len() { | ||
carry += u128::from(temp[i + j]) + u128::from(m[j]) * u128::from(u); | ||
temp[i + j] = carry as u64; | ||
carry >>= 64; | ||
} | ||
#[allow(clippy::cast_possible_truncation)] // Intentional | ||
for j in m.len()..(temp.len() - i) { | ||
carry += u128::from(temp[i + j]); | ||
temp[i + j] = carry as u64; | ||
carry >>= 64; | ||
} | ||
debug_assert!(carry == 0); | ||
} | ||
debug_assert!(temp[temp.len() - 1] <= 1); // Basically a carry flag. | ||
|
||
// Copy result. | ||
result.copy_from_slice(&temp[m.len()..2 * m.len()]); | ||
|
||
// Subtract one more m if result >= m | ||
let mut reduce = true; | ||
// REFACTOR: Create cmp routine | ||
if temp[temp.len() - 1] == 0 { | ||
for (r, m) in zip(result.iter().rev(), m.iter().rev()) { | ||
if r < m { | ||
reduce = false; | ||
break; | ||
} | ||
if r > m { | ||
break; | ||
} | ||
} | ||
} | ||
if reduce { | ||
// REFACTOR: Create sub routine | ||
let mut carry = 0; | ||
#[allow(clippy::cast_possible_truncation)] // Intentional | ||
#[allow(clippy::cast_sign_loss)] // Intentional | ||
for (r, m) in zip(result.iter_mut(), m.iter().copied()) { | ||
carry += i128::from(*r) - i128::from(m); | ||
*r = carry as u64; | ||
carry >>= 64; // Sign extending shift | ||
} | ||
debug_assert!(carry == 0 || temp[temp.len() - 1] == 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Product of primes up to and including 47. | ||
const SMALL_PRIMES: u64 = 614889782588491410; | ||
|
||
/// Miller-Rabin primality test | ||
/// | ||
/// See <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test> | ||
pub fn miller_rabin(n: u64, base: u64) -> bool { | ||
todo!{} | ||
} | ||
|
||
/// Exact 64 bit primality test | ||
pub fn is_prime(n: u64) -> bool { | ||
// Sufficient set of bases for `u64` | ||
// See <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases> | ||
// See <https://miller-rabin.appspot.com/> | ||
// OPT: This method <https://www.techneon.com/> ? | ||
// OPT: Combined basis srp | ||
miller_rabin(n, 2) && | ||
miller_rabin(n, 325) && | ||
miller_rabin(n, 9375) && | ||
miller_rabin(n, 28178) && | ||
miller_rabin(n, 450775) && | ||
miller_rabin(n, 9780504) && | ||
miller_rabin(n, 1795265022) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters