arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/src/modulus/prime.rs
src/modulus/prime.rsRaw
 1use primality_test::is_prime;
 2use prime_factorization::Factorization;
 3
 4pub struct Prime<O> {
 5	q: O, /// q_base^q_powers
 6    q_base: O,
 7    q_powers: O,
 8    factors: Vec<O>, /// distinct factors of q-1
 9    nth_root: O,
10}
11
12impl Prime<u64>{
13    pub fn new(q_base: u64, q_power: u64) -> Self{
14		assert!(is_prime(q) && q > 2);
15        assert!()
16
17        q_exp
18        for i in 0..q_power{
19
20        }
21
22        Self::new_unchecked(q)
23	}
24
25	pub fn new_unchecked(q: u64) -> Self {
26        assert!(q.next_power_of_two().ilog2() <= 61);
27        Self {
28            q,
29        }
30    }
31
32    /// Returns returns Phi(BaseModulus^BaseModulusPower)
33    pub fn phi() -> u64 {
34
35    }
36
37    /// Returns the smallest primitive root. The unique factors
38    /// can be given as argument to avoid factorization of q-1.
39    pub fn primitive_root(&self) -> u64{
40        if self.factors.len() != 0{
41            self.check_factors();
42        }else{
43            let factors = Factorization::run(q).prime_factor_repr();
44            let mut distincts_factors: Vec<u64> = Vec::with_capacity(factors.len());
45            for factor in factors.iter(){
46                distincts_factors.push(factor.0)
47            }
48            self.factors = distincts_factors
49        }
50
51        let log_nth_root = 64 - self.q.leading_zeros() as usize;
52
53        0
54    }
55
56    pub fn check_factors(&self){
57
58        if self.factors.len() == 0{
59            panic!("invalid factor list: empty")
60        }
61
62        let mut q = self.q;
63
64        for &factor in &self.factors{
65            if !is_prime(factor){
66                panic!("invalid factor list: factor {} is not prime", factor)
67            }
68
69            while q%factor != 0{
70                q /= factor
71            }
72        }
73
74        if q != 1{
75            panic!("invalid factor list: does not fully divide q: q % (alll factors) = {}", q)
76        }
77    }
78}