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
302
303
304
305
306
307
308
309
310
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp::*;
use std::ops::Deref;
use std::ops::DerefMut;
use crate::traits::{Pow, RemDiv, TrueDiv};
#[pyclass]
#[derive(Clone)]
pub struct BigInt(pub crate::BigInt<u64>);
impl Deref for BigInt {
type Target = crate::BigInt<u64>;
fn deref(&self) -> &crate::BigInt<u64> {
&self.0
}
}
impl DerefMut for BigInt {
fn deref_mut(&mut self) -> &mut crate::BigInt<u64> {
&mut self.0
}
}
impl AsRef<crate::BigInt<u64>> for BigInt {
fn as_ref(&self) -> &crate::BigInt<u64> {
&self.0
}
}
#[pymethods]
impl BigInt {
#[new]
/// Python constructor for BigInt, implicitely using the TryFrom<PyAny> implementation. \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __init__(n: &PyAny) -> PyResult<Self> {
Ok(Self::try_from(n)?)
}
/// Python binding of the `abs` operation
pub fn __abs__(&self) -> Self {
let mut ret = self.clone();
ret.0.sign = true;
ret
}
/// Python binding to the unary neg operation.
pub fn __neg__(&self) -> Self {
Self(-self.as_ref())
}
/// Python binding to convert to a float (returns a double
/// precision floating point number)
pub fn __float__(&self) -> f64 {
self.as_ref().into()
}
/// Python binding to convert to a int
pub fn __int__(&self, py: Python<'_>) -> PyResult<PyObject> {
if self.0.sign {
self.0.uint.__int__(py)
} else {
Ok(self.0.uint.__int__(py)?.call_method0(py, "__neg__")?)
}
}
/// Python binding for the `+` operation. \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __add__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() + Self::try_from(other)?.as_ref()))
}
/// Python binding for the `+=` operation, specifically binded here because
/// the crate implements an actual add assign operation, more efficient than
/// add + clone. \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __iadd__(&mut self, other: &PyAny) -> PyResult<()> {
self.0 += Self::try_from(other)?.as_ref();
Ok(())
}
/// Python binding for the reverse `+` operation, allowing performing an addition
/// of the form (Python int) + (BigInt). \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __radd__(&self, other: &PyAny) -> PyResult<Self> {
self.__add__(other)
}
/// Python binding for the `-` operation. \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __sub__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() - Self::try_from(other)?.as_ref()))
}
/// Python binding for the `-=` operation, specifically binded here because
/// the crate implements an actual sub assign operation, more efficient than
/// sub + clone. \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __isub__(&mut self, other: &PyAny) -> PyResult<()> {
self.0 -= Self::try_from(other)?.as_ref();
Ok(())
}
/// Python binding for the reverse `-` operation, allowing performing an subtraction
/// of the form (Python int) - (BigInt). \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __rsub__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(Self::try_from(other)?.as_ref() - self.as_ref()))
}
/// Python binding to the `*` operation. \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __mul__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() * Self::try_from(other)?.as_ref()))
}
/// Python binding for the reverse `*` operation, allowing performing an multiplication
/// of the form (Python int) * (BigInt). \
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __rmul__(&self, other: &PyAny) -> PyResult<Self> {
self.__mul__(other)
}
/// Python binding to the `//` operation. \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
pub fn __floordiv__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() / Self::try_from(other)?.as_ref()))
}
/// Python binding to the reverse `//` operation, allowing performing a floor division
/// of the form (Python int) // (BigInt). \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
pub fn __rfloordiv__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(Self::try_from(other)?.as_ref() / self.as_ref()))
}
/// Python binding to the `%` operation. \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
pub fn __mod__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() % Self::try_from(other)?.as_ref()))
}
/// Python binding to the reverse `%` operation, allowing performing a floor division
/// of the form (Python int) % (BigInt). \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
pub fn __rmod__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(Self::try_from(other)?.as_ref() % self.as_ref()))
}
/// Python binding to the `divmod` operation. \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
pub fn __divmod__(&self, other: &PyAny) -> PyResult<(Self, Self)> {
let (q, r) = crate::BigInt::<u64>::rem_div(self.as_ref(), Self::try_from(other)?.as_ref())?;
Ok((Self(q), Self(r)))
}
/// Python binding to the reverse `divmod` operation. \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
pub fn __rdivmod__(&self, other: &PyAny) -> PyResult<(Self, Self)> {
let (q, r) = crate::BigInt::<u64>::rem_div(Self::try_from(other)?.as_ref(), self.as_ref())?;
Ok((Self(q), Self(r)))
}
/// Python binding to the `/` operation (returns a double precision float).
/// Only implemented for systems with a little endian float format. \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
#[cfg(target_endian = "little")]
pub fn __truediv__(&self, other: &PyAny) -> PyResult<f64> {
Ok(crate::BigInt::<u64>::truediv(
self.as_ref(),
Self::try_from(other)?.as_ref(),
)?)
}
/// Python binding to the reverse `/` operation (returns a double precision float).
/// Only implemented for systems with a little endian float format. \
/// This will raise an error if the operand is not compatible with a BigInt,
/// or in the case of a divison by zero.
#[cfg(target_endian = "little")]
pub fn __rtruediv__(&self, other: &PyAny) -> PyResult<f64> {
Ok(crate::BigInt::<u64>::truediv(
Self::try_from(other)?.as_ref(),
self.as_ref(),
)?)
}
/// Python binding to the `**` operation. \
/// This will raise an error when called with the modulus argument.
pub fn __pow__(&self, other: usize, modulus: Option<usize>) -> PyResult<Self> {
if matches!(modulus, Some(_)) {
Err(PyErr::new::<PyValueError, _>(
"Modulus argument of pow function not supported",
))
} else {
Ok(Self(crate::BigInt::<u64>::pow(self.as_ref(), other)))
}
}
/// Python binding to the `<<` operation
pub fn __lshift__(&self, n: usize) -> Self {
Self(self.as_ref() << n)
}
/// Python binding to the `>>` operation
pub fn __rshift__(&self, n: usize) -> Self {
Self(self.as_ref() >> n)
}
/// Python binding to the `<<=` operation
pub fn __ilshift__(&mut self, n: usize) {
self.0 <<= n;
}
/// Python binding to the `>>=` operation
pub fn __irshift__(&mut self, n: usize) {
self.0 >>= n;
}
/// Python binding to the `&` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __and__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() & Self::try_from(other)?.as_ref()))
}
/// Python binding to the `&=` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __iand__(&mut self, other: &PyAny) -> PyResult<()> {
self.0 &= Self::try_from(other)?.as_ref();
Ok(())
}
/// Python binding to the reverse `&` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __rand__(&self, other: &PyAny) -> PyResult<Self> {
self.__and__(other)
}
/// Python binding to the `|` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __or__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() | Self::try_from(other)?.as_ref()))
}
/// Python binding to the `|=` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __ior__(&mut self, other: &PyAny) -> PyResult<()> {
self.0 |= Self::try_from(other)?.as_ref();
Ok(())
}
/// Python binding to the reverse `|` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __ror__(&self, other: &PyAny) -> PyResult<Self> {
self.__or__(other)
}
/// Python binding to the `^` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __xor__(&self, other: &PyAny) -> PyResult<Self> {
Ok(Self(self.as_ref() ^ Self::try_from(other)?.as_ref()))
}
/// Python binding to the `^=` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __ixor__(&mut self, other: &PyAny) -> PyResult<()> {
self.0 ^= Self::try_from(other)?.as_ref();
Ok(())
}
/// Python binding to the reverse `^` operation.
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __rxor__(&self, other: &PyAny) -> PyResult<Self> {
self.__xor__(other)
}
/// Python binding to the invert `~` operation.
pub fn __invert__(&self) -> Self {
Self(!self.as_ref())
}
/// Python binding to all the comparators: `==`, `!=`, `<`, `<=`, `>`, and `>=`
/// This will raise an error if the operand is not compatible with a BigInt.
pub fn __richcmp__(&self, other: &PyAny, cmp: pyo3::basic::CompareOp) -> PyResult<bool> {
let int = Self::try_from(other)?;
Ok(match cmp {
Lt => self.as_ref() < int.as_ref(),
Le => self.as_ref() <= int.as_ref(),
Eq => self.as_ref() == int.as_ref(),
Ne => self.as_ref() != int.as_ref(),
Gt => self.as_ref() > int.as_ref(),
Ge => self.as_ref() >= int.as_ref(),
})
}
/// Python binding to the `bool` function
pub fn __bool__(&self) -> bool {
self.0.uint != crate::BigUint::default()
}
/// Python binding to the `str` function
pub fn __str__(&self) -> PyResult<String> {
Ok(self.as_ref().to_string())
}
/// Python binding to the `repr` function
pub fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", self.as_ref()))
}
/// Python binding to the `len` function. This is for debug purposes,
/// as this returns internal information that aren't supposed to be useful
/// for the users of this module.
pub fn __len__(&self) -> usize {
self.0.uint.val.len()
}
/// Python bindings for exporting integer to file
pub fn write_to_file(&self, path: String) -> PyResult<()> {
Ok(self.0.write_to_file(&path)?)
}
}