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
use crypto_bigint::Uint;
use rand_core::CryptoRngCore;
use crate::{is_prime_with_rng, is_safe_prime_with_rng, prime_with_rng, safe_prime_with_rng};
pub trait RandomPrimeWithRng {
fn prime_with_rng(rng: &mut impl CryptoRngCore, bit_length: usize) -> Self;
fn safe_prime_with_rng(rng: &mut impl CryptoRngCore, bit_length: usize) -> Self;
fn is_prime_with_rng(&self, rng: &mut impl CryptoRngCore) -> bool;
fn is_safe_prime_with_rng(&self, rng: &mut impl CryptoRngCore) -> bool;
}
impl<const L: usize> RandomPrimeWithRng for Uint<L> {
fn prime_with_rng(rng: &mut impl CryptoRngCore, bit_length: usize) -> Self {
prime_with_rng(rng, bit_length)
}
fn safe_prime_with_rng(rng: &mut impl CryptoRngCore, bit_length: usize) -> Self {
safe_prime_with_rng(rng, bit_length)
}
fn is_prime_with_rng(&self, rng: &mut impl CryptoRngCore) -> bool {
is_prime_with_rng(rng, self)
}
fn is_safe_prime_with_rng(&self, rng: &mut impl CryptoRngCore) -> bool {
is_safe_prime_with_rng(rng, self)
}
}
#[cfg(test)]
mod tests {
use crypto_bigint::U64;
use rand_core::OsRng;
use super::RandomPrimeWithRng;
#[test]
fn uint_impl() {
assert!(!U64::from(15u32).is_prime_with_rng(&mut OsRng));
assert!(U64::from(19u32).is_prime_with_rng(&mut OsRng));
assert!(!U64::from(13u32).is_safe_prime_with_rng(&mut OsRng));
assert!(U64::from(11u32).is_safe_prime_with_rng(&mut OsRng));
assert!(U64::prime_with_rng(&mut OsRng, 10).is_prime_with_rng(&mut OsRng));
assert!(U64::safe_prime_with_rng(&mut OsRng, 10).is_safe_prime_with_rng(&mut OsRng));
}
}