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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Definition of the [`M61`] type as well as basic operations on it.

use core::fmt;
use core::iter;
use core::ops;

/// The modulus on which arithmetic is performed.
/// Also functions as a bitmask for calculating
/// digit sums base `2^61`.
pub(crate) const MODULUS: u64 = (1 << 61) - 1;

/// When calculating the reduction of an arbitary precision integer
/// using a digit sum, the sum itself must be reduced aswell.
/// This function performs this reduction, assuming that
/// are themselves partially reduced, meaning `x <= 2 * (2^61 - 1)`.
#[inline(always)]
pub(crate) fn final_reduction(mut x: u64) -> M61 {
    if x >= MODULUS {
        x -= MODULUS;
    }

    if x >= MODULUS {
        M61(x - MODULUS)
    } else {
        M61(x)
    }
}

/// A 64-bit integer in which arithmetic is performed modulp `2^61 - 1`.
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct M61(pub(crate) u64);

impl M61 {
    /// Returns the contained value.
    #[inline(always)]
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// Helper macro for the quick generation
/// of formatting trait implementations.
macro_rules! make_fmt_impl {
    ($trait:ident) => {
        impl fmt::$trait for M61 {
            #[inline(always)]
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                <u64 as fmt::$trait>::fmt(&self.0, f)
            }
        }
    };
}

make_fmt_impl!(Display);
make_fmt_impl!(Debug);
make_fmt_impl!(LowerExp);
make_fmt_impl!(UpperExp);
make_fmt_impl!(LowerHex);
make_fmt_impl!(UpperHex);
make_fmt_impl!(Octal);
make_fmt_impl!(Binary);

/// Helper macro for generation of [`From`] implementations
/// where the numerical bounds of the source type are smaller
/// than the modulus.
macro_rules! make_trivial_from {
    ($type:ty) => {
        impl From<$type> for M61 {
            #[inline(always)]
            #[must_use]
            fn from(value: $type) -> Self {
                // rustc warns us against this seemingly
                // useless comparison whenever the argument is
                // an unsigned type. The macro works properly for those
                // types, which means that the warning can be disabled.
                #[allow(unused_comparisons)]
                if value < 0 {
                    Self((value as i64 + MODULUS as i64) as u64)
                } else {
                    Self(value as u64)
                }
            }
        }
    };
}

make_trivial_from!(u8);
make_trivial_from!(u16);
make_trivial_from!(u32);
#[cfg(not(target_pointer_width = "64"))]
make_trivial_from!(usize);

#[cfg(target_pointer_width = "64")]
impl From<usize> for M61 {
    #[inline(always)]
    #[must_use]
    fn from(value: usize) -> Self {
        Self::from(value as u64)
    }
}

make_trivial_from!(i8);
make_trivial_from!(i16);
make_trivial_from!(i32);
#[cfg(not(target_pointer_width = "64"))]
make_trivial_from!(isize);

#[cfg(target_pointer_width = "64")]
impl From<isize> for M61 {
    #[inline(always)]
    #[must_use]
    fn from(value: isize) -> Self {
        Self::from(value as i64)
    }
}

impl From<u64> for M61 {
    #[inline]
    #[must_use]
    fn from(value: u64) -> Self {
        let tmp = (value & MODULUS) + (value >> 61);
        if tmp >= MODULUS {
            Self(tmp - MODULUS)
        } else {
            Self(tmp)
        }
    }
}

impl From<i64> for M61 {
    #[inline]
    #[must_use]
    fn from(mut value: i64) -> Self {
        if value < 0 {
            value = value.wrapping_add(4 * MODULUS as i64);
        }
        if value < 0 {
            value = value.wrapping_add(MODULUS as i64);
        }

        Self::from(value as u64)
    }
}

impl From<u128> for M61 {
    #[inline]
    #[must_use]
    fn from(value: u128) -> Self {
        let mut x = value as u64 & MODULUS;
        x += (value >> 61) as u64 & MODULUS;
        x += (value >> 122) as u64;
        Self::from(x)
    }
}

impl From<i128> for M61 {
    #[inline]
    #[must_use]
    fn from(mut value: i128) -> Self {
        while value < 0 {
            value += 16 * ((1 << 122) - 1);
        }

        Self::from(value as u128)
    }
}

/// Helper macro for the quick implementation
/// of arithmetic operators.
macro_rules! make_arith_impl {
    ($trait:ident, $trait_assign:ident, $func:ident, $func_assign:ident, $op:tt, $impl:expr) => {
        impl ops::$trait for M61 {
            type Output = Self;

            #[inline]
            #[must_use]
            fn $func(self, rhs: Self) -> Self::Output {
                #[allow(clippy::redundant_closure_call)]
                Self($impl(self.0, rhs.0))
            }
        }

        impl<'a> ops::$trait<&'a M61> for M61 {
            type Output = Self;

            #[inline(always)]
            #[must_use]
            fn $func(self, rhs: &Self) -> Self::Output {
                self $op *rhs
            }
        }

        impl ops::$trait_assign for M61 {
            #[inline(always)]
            fn $func_assign(&mut self, rhs: Self) {
                *self = *self $op rhs
            }
        }

        impl<'a> ops::$trait_assign<&'a M61> for M61 {
            #[inline(always)]
            fn $func_assign(&mut self, rhs: &Self) {
                *self = *self $op rhs
            }
        }
    };
}

make_arith_impl!(Add, AddAssign, add, add_assign, +, |a, b| {
    let x = a + b;
    if x >= MODULUS {
        x - MODULUS
    } else {
        x
    }
});
make_arith_impl!(Sub, SubAssign, sub, sub_assign, -, |a, b| {
    a + MODULUS - b
});
make_arith_impl!(Mul, MulAssign, mul, mul_assign, *, |a, b| {
    let x = a as u128 * b as u128;
    let mut hi = (x >> 61) as u64;
    let mut lo = (x as u64) & MODULUS;
    lo = lo.wrapping_add(hi);
    hi = lo.wrapping_sub(MODULUS);
    if lo < MODULUS {
        lo
    } else {
        hi
    }
});
make_arith_impl!(Div, DivAssign, div, div_assign, /, |a, b| {
    if b == 0 {
        panic!("attempt to divide by zero");
    }

    // Calculate the multiplicative inverse
    // using the extended Euclidean algorithm.
    // (https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm)

    let mut r0 = MODULUS;
    let mut r1 = b;
    let mut s0 = 1i64;
    let mut s1 = 0i64;
    let mut t0 = 0i64;
    let mut t1 = 1i64;

    while r1 != 0 {
        let (q, rn) = (r0 / r1, r0 % r1);
        let sn = s0 - q as i64 * s1;
        let tn = t0 - q as i64 * t1;

        r0 = r1;
        r1 = rn;
        s0 = s1;
        s1 = sn;
        t0 = t1;
        t1 = tn;
    }

    debug_assert_eq!(MODULUS as i128 * s0 as i128 + b as i128 * t0 as i128, 1);

    (Self(a) * Self::from(t0)).0
});
//make_arith_impl!(Rem, RemAssign, rem, rem_assign, %, |a, b| {
//    a % b
//});

impl iter::Sum for M61 {
    #[inline(always)]
    #[must_use]
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self(0), |a, b| a + b)
    }
}

impl<'a> iter::Sum<&'a M61> for M61 {
    #[inline(always)]
    #[must_use]
    fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(Self(0), |a, b| a + b)
    }
}

impl iter::Product for M61 {
    #[inline(always)]
    #[must_use]
    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self(1), |a, b| a * b)
    }
}

impl<'a> iter::Product<&'a M61> for M61 {
    #[inline(always)]
    #[must_use]
    fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(Self(1), |a, b| a * b)
    }
}