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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! A ring of integers modulo a positive integer.

use super::modulo::{ModuloDoubleRaw, ModuloLargeRaw, ModuloSingleRaw};
use crate::{
    arch::word::{DoubleWord, Word},
    buffer::Buffer,
    cmp,
    error::panic_divide_by_0,
    fast_div::{
        ConstDoubleDivisor, ConstLargeDivisor, ConstSingleDivisor, FastDivideNormalized,
        FastDivideNormalized2,
    },
    math,
    primitive::shrink_dword,
    repr::{Repr, TypedRepr},
    ubig::UBig,
};
use core::cmp::Ordering;

/// A ring of integers modulo a positive integer.
///
/// # Examples
///
/// ```
/// # use dashu_int::{modular::ModuloRing, UBig};
/// let ring = ModuloRing::new(UBig::from(100u8));
/// assert_eq!(ring.modulus(), 100);
/// ```
pub struct ModuloRing(ModuloRingRepr);

pub(crate) enum ModuloRingRepr {
    Single(ModuloRingSingle),
    Double(ModuloRingDouble),
    Large(ModuloRingLarge),
}

pub(crate) struct ModuloRingSingle(pub(super) ConstSingleDivisor);

pub(crate) struct ModuloRingDouble(pub(super) ConstDoubleDivisor);

pub(crate) struct ModuloRingLarge(pub(super) ConstLargeDivisor);

impl ModuloRing {
    /// Create a new ring of integers modulo `n`.
    ///
    /// For two [Modulo](crate::modular::Modulo) numbers to be compatible,
    /// they must come from the same [ModuloRing].
    /// Two different [ModuloRing]s are not compatible even if
    /// they have the same modulus `n`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use dashu_int::{modular::ModuloRing, UBig};
    /// let ring = ModuloRing::new(UBig::from(100u8));
    /// assert_eq!(ring.modulus(), 100);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if `n` is zero.
    #[inline]
    pub fn new(n: UBig) -> ModuloRing {
        Self(match n.into_repr() {
            TypedRepr::Small(0) => panic_divide_by_0(),
            TypedRepr::Small(dword) => {
                if let Some(word) = shrink_dword(dword) {
                    ModuloRingRepr::Single(ModuloRingSingle::new(word))
                } else {
                    ModuloRingRepr::Double(ModuloRingDouble::new(dword))
                }
            }
            TypedRepr::Large(words) => ModuloRingRepr::Large(ModuloRingLarge::new(words)),
        })
    }

    #[inline]
    pub(crate) fn repr(&self) -> &ModuloRingRepr {
        &self.0
    }
}

impl ModuloRingSingle {
    /// Create a new ring of integers modulo a single word number `n`.
    #[inline]
    pub const fn new(n: Word) -> Self {
        Self(ConstSingleDivisor::new(n))
    }

    // Directly expose this through public field?
    #[inline]
    pub const fn normalized_modulus(&self) -> Word {
        self.0.fast_div.divisor
    }

    #[inline]
    pub const fn modulus(&self) -> UBig {
        UBig(Repr::from_word(self.0.divisor()))
    }

    #[inline]
    pub const fn shift(&self) -> u32 {
        self.0.shift
    }

    #[inline]
    pub const fn fast_div(&self) -> FastDivideNormalized {
        self.0.fast_div
    }

    #[inline]
    pub const fn is_valid(&self, val: ModuloSingleRaw) -> bool {
        val.0 < self.normalized_modulus() && val.0 & math::ones_word(self.shift()) == 0
    }
}

impl ModuloRingDouble {
    /// Create a new ring of integers modulo a double word number `n`.
    #[inline]
    pub const fn new(n: DoubleWord) -> Self {
        Self(ConstDoubleDivisor::new(n))
    }

    #[inline]
    pub const fn normalized_modulus(&self) -> DoubleWord {
        self.0.fast_div.divisor
    }

    #[inline]
    pub const fn modulus(&self) -> UBig {
        UBig(Repr::from_dword(self.0.divisor()))
    }

    #[inline]
    pub const fn shift(&self) -> u32 {
        self.0.shift
    }

    #[inline]
    pub const fn fast_div(&self) -> FastDivideNormalized2 {
        self.0.fast_div
    }

    #[inline]
    pub const fn is_valid(&self, val: ModuloDoubleRaw) -> bool {
        val.0 < self.normalized_modulus() && val.0 & math::ones_dword(self.shift()) == 0
    }
}

impl ModuloRingLarge {
    /// Create a new large ring of integers modulo `n`.
    #[inline]
    pub fn new(n: Buffer) -> ModuloRingLarge {
        Self(ConstLargeDivisor::new(n))
    }

    #[inline]
    pub fn normalized_modulus(&self) -> &[Word] {
        &self.0.normalized_modulus
    }

    #[inline]
    pub fn modulus(&self) -> UBig {
        UBig(Repr::from_buffer(self.0.divisor()))
    }

    #[inline]
    pub fn shift(&self) -> u32 {
        self.0.shift
    }

    #[inline]
    pub fn fast_div_top(&self) -> FastDivideNormalized2 {
        self.0.fast_div_top
    }

    #[inline]
    pub fn is_valid(&self, val: &ModuloLargeRaw) -> bool {
        val.0.len() == self.normalized_modulus().len()
            && cmp::cmp_same_len(&val.0, self.normalized_modulus()) == Ordering::Less
            && val.0[0] & math::ones_word(self.shift()) == 0 // must be shifted
    }
}