-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathuint.rs
605 lines (542 loc) · 15.4 KB
/
uint.rs
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Zero-allocation U256 and U512 types built on GMP. We created this module specifically for our
//! use case of implementing primality checking over 256-bit integers, but it may be worth
//! polishing a bit for more general use.
//!
//! Obviously there are a lot of `unsafe` blocks to work with GMP. Take care when using this module
//! because there may be bugs we did not catch.
//!
//! TODO: Benchmark our U256 vs. 256-bit `rug::Integer` vs. Parity U256.
#![allow(clippy::cast_sign_loss)]
use gmp_mpfr_sys::gmp;
use gmp_mpfr_sys::gmp::mpz_t;
use rug::integer::Order;
use rug::Integer;
use std::cmp::{min, Ord, Ordering, PartialOrd};
use std::convert::From;
use std::mem::transmute;
use std::ops;
use std::ptr;
macro_rules! u_types {
($($t:ident,$size:expr),+) => {
$(
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub struct $t {
// Field `size` also denotes the sign of the number, while `limbs` reflect only the
// magnitude.
// We keep size >= 0 except in very rare circumstances.
size: i64,
limbs: [u64; $size],
}
impl $t {
fn data(&self) -> *mut u64 {
&self.limbs as *const u64 as *mut u64
}
fn normalize_size(&mut self) {
self.size = 0;
for i in (0..$size).rev() {
if self.limbs[i] != 0 {
self.size = (i + 1) as i64;
break;
}
}
}
// The cast from `i64` to `i32` is fine since |`size`| is <= 4 for `U256`, and <= 8 for
// `U512`.
#[allow(clippy::cast_possible_truncation)]
fn as_mpz(&self) -> mpz_t {
mpz_t {
size: self.size as i32,
d: std::ptr::NonNull::new((self.data())).unwrap(),
alloc: $size,
}
}
pub fn zero() -> Self {
Self { size: 0, limbs: [0; $size] }
}
pub fn is_zero(&self) -> bool {
self.size == 0
}
pub fn one() -> Self {
let mut limbs = [0; $size];
limbs[0] = 1;
Self { size: 1, limbs }
}
pub fn is_odd(&self) -> bool {
self.limbs[0] & 1 == 1
}
#[allow(clippy::if_not_else)]
/// Panics if `m == 0`.
pub fn mod_inv(self, m: &Self) -> Option<Self> {
let mut out = Self::zero();
let outmpz = out.as_mpz();
let s = self.as_mpz();
let m = m.as_mpz();
let exists = unsafe { gmp::mpz_invert(mut_ptr(&outmpz), mut_ptr(&s), mut_ptr(&m)) };
if exists != 0 {
out.size = i64::from(outmpz.size);
Some(out)
}
else {
None
}
}
/// Panics if `m == 0`.
pub fn pow_mod(self, e: Self, m: &Self) -> Self {
let mut out = Self::zero();
let outmpz = out.as_mpz();
let s = self.as_mpz();
let e = e.as_mpz();
let m = m.as_mpz();
unsafe { gmp::mpz_powm(mut_ptr(&outmpz), mut_ptr(&s), mut_ptr(&e), mut_ptr(&m)) };
out.size = i64::from(outmpz.size);
out
}
pub fn is_perfect_square(&self) -> bool {
let issqr = unsafe { gmp::mpn_perfect_square_p(self.data(), self.size) };
issqr != 0
}
pub fn jacobi(a: i32, b: &Self) -> i32 {
let mut a_data = 0;
let a = i32_to_mpz(a, &mut a_data);
let b = b.as_mpz();
unsafe { gmp::mpz_jacobi(&a as *const mpz_t, &b as *const mpz_t) }
}
pub fn is_congruent(self, i: i32, m: &Self) -> bool {
let mut data = 0;
let x = i32_to_mpz(i, &mut data);
let s = self.as_mpz();
let m = m.as_mpz();
let res = unsafe { gmp::mpz_congruent_p(mut_ptr(&s), mut_ptr(&x), mut_ptr(&m)) };
res != 0
}
pub fn is_divisible_u(&self, u: u64) -> bool {
let s = self.as_mpz();
let divisible = unsafe {gmp::mpz_divisible_ui_p(mut_ptr(&s), u)};
divisible != 0
}
/// Panics if `buf` is not large enough.
pub fn write_binary(&self, buf: &mut [u8]) -> usize {
unsafe { gmp::mpn_get_str(mut_ptr(&buf[0]), 2, self.data(), self.size) }
}
pub fn from_be_bytes(bytes: &[u8]) -> Self {
let x = Self::zero();
unsafe { gmp::mpn_set_str(x.data(), &bytes[0] as *const u8, bytes.len(), 256) };
x
}
}
impl PartialEq<u64> for $t {
fn eq(&self, u: &u64) -> bool {
(*u == 0 && self.size == 0) || (self.size == 1 && self.limbs[0] == *u)
}
}
impl From<[u64; $size]> for $t {
fn from(limbs: [u64; $size]) -> Self {
let mut x = Self { size: 0, limbs };
x.normalize_size();
x
}
}
impl From<u64> for $t {
fn from(x: u64) -> Self {
let mut limbs = [0; $size];
limbs[0] = x;
Self::from(limbs)
}
}
/// Lower-endian `bytes`.
impl From<[u8; $size * 8]> for $t {
fn from(bytes: [u8; $size * 8]) -> Self {
let chunks = unsafe { transmute::<[u8; $size * 8], [[u8; 8]; $size]>(bytes) };
let mut limbs = [0; $size];
for i in 0..$size {
limbs[i] = u64::from_le_bytes(chunks[i]);
}
Self::from(limbs)
}
}
/// Lower-endian `bytes`.
impl From<&[u8; $size * 8]> for $t {
fn from(bytes: &[u8; $size * 8]) -> Self {
let chunks = unsafe { transmute::<[u8; $size * 8], [[u8; 8]; $size]>(*bytes) };
let mut limbs = [0; $size];
for i in 0..$size {
limbs[i] = u64::from_le_bytes(chunks[i]);
}
Self::from(limbs)
}
}
impl PartialOrd for $t {
fn partial_cmp(&self, x: &Self) -> Option<Ordering> {
let x = unsafe { gmp::mpn_cmp(self.data(), x.data(), $size) };
Some({
if x < 0 {
Ordering::Less
} else if x == 0 {
Ordering::Equal
} else {
Ordering::Greater
}
})
}
}
impl Ord for $t {
fn cmp(&self, x: &Self) -> Ordering {
let x = unsafe { gmp::mpn_cmp(self.data(), x.data(), $size) };
if x < 0 {
Ordering::Less
} else if x == 0 {
Ordering::Equal
} else {
Ordering::Greater
}
}
}
impl ops::ShlAssign<u32> for $t {
fn shl_assign(&mut self, mut x: u32) {
while x != 0 {
let sz = min(gmp::LIMB_BITS as u32, x);
x -= sz;
unsafe { gmp::mpn_lshift(self.data(), self.data(), $size, sz) };
}
self.normalize_size();
}
}
impl ops::Shl<u32> for $t {
type Output = Self;
fn shl(self, x: u32) -> Self {
let mut y = self;
y <<= x;
y
}
}
impl ops::ShrAssign<u32> for $t {
fn shr_assign(&mut self, mut x: u32) {
while x != 0 {
let sz = min(gmp::LIMB_BITS as u32, x);
x -= sz;
unsafe { gmp::mpn_rshift(self.data(), self.data(), $size, sz) };
}
self.normalize_size();
}
}
impl ops::Shr<u32> for $t {
type Output = Self;
fn shr(self, x: u32) -> Self {
let mut y = self;
y >>= x;
y
}
}
impl ops::AddAssign for $t {
/// Panics if result overflows.
fn add_assign(&mut self, x: Self) {
let carry = unsafe { gmp::mpn_add_n(self.data(), self.data(), x.data(), $size) };
assert!(carry == 0);
self.normalize_size();
}
}
impl ops::Add for $t {
/// Panics if result overflows.
type Output = Self;
fn add(self, x: Self) -> Self {
let mut y = self;
y += x;
y
}
}
impl ops::Add<u64> for $t {
type Output = Self;
/// Panics if result overflows.
fn add(self, x: u64) -> Self {
self + Self::from(x)
}
}
impl ops::SubAssign for $t {
/// Panics if result is negative.
fn sub_assign(&mut self, x: Self) {
let borrow = unsafe { gmp::mpn_sub_n(self.data(), self.data(), x.data(), $size) };
assert!(borrow == 0);
self.normalize_size();
}
}
impl ops::Sub for $t {
type Output = Self;
/// Panics if result is negative.
fn sub(self, x: Self) -> Self {
let mut y = self;
y -= x;
y
}
}
impl ops::Sub<u64> for $t {
type Output = Self;
/// Panics if result is negative.
fn sub(self, x: u64) -> Self {
self - Self::from(x)
}
}
impl ops::Sub<u64> for &$t {
type Output = $t;
/// Panics if result is negative.
fn sub(self, x: u64) -> $t {
*self - $t::from(x)
}
}
impl ops::Rem<&Self> for $t {
type Output = Self;
fn rem(self, x: &Self) -> Self {
if x.size > self.size {
return self;
}
let (y, mut rem) = (Self::zero(), Self::zero());
unsafe {
gmp::mpn_tdiv_qr(
y.data(),
rem.data(),
0,
self.data(),
self.size,
x.data(),
x.size,
)
};
rem.normalize_size();
rem
}
}
impl ops::Rem for $t {
type Output = Self;
fn rem(self, x: Self) -> Self {
#![allow(clippy::op_ref)]
self % &x
}
}
impl ops::RemAssign<&Self> for $t {
fn rem_assign(&mut self, x: &Self) {
if x.size > self.size {
return;
}
let y = Self::zero();
unsafe {
gmp::mpn_tdiv_qr(
y.data(),
self.data(),
0,
self.data(),
self.size,
x.data(),
x.size,
)
};
self.normalize_size();
}
}
impl ops::RemAssign for $t {
fn rem_assign(&mut self, x: Self) {
#![allow(clippy::op_ref)]
*self %= &x;
}
}
impl ops::Div<&Self> for $t {
type Output = Self;
fn div(self, x: &Self) -> Self {
if x.size > self.size {
return self;
}
let (mut y, rem) = (Self::zero(), Self::zero());
unsafe {
gmp::mpn_tdiv_qr(
y.data(),
rem.data(),
0,
self.data(),
self.size,
x.data(),
x.size,
)
};
y.normalize_size();
y
}
}
impl ops::Div for $t {
type Output = Self;
fn div(self, x: Self) -> Self {
#![allow(clippy::op_ref)]
self / &x
}
}
impl From<$t> for Integer {
fn from(x: $t) -> Self {
Self::from_digits(&x.limbs, Order::Lsf)
}
}
)+
}
}
u_types!(U256, 4, U512, 8);
impl U512 {
/// Returns the lower half of this `U512` as a `U256`.
/// TODO: Make checked?
pub fn low_u256(self) -> U256 {
let mut x = unsafe { transmute::<Self, (U256, [u64; 4])>(self) }.0;
x.normalize_size();
x
}
}
impl From<&U256> for U512 {
fn from(x: &U256) -> Self {
let mut limbs = [0; 8];
limbs[..4].copy_from_slice(&x.limbs);
Self {
size: x.size,
limbs,
}
}
}
impl From<U256> for U512 {
fn from(x: U256) -> Self {
Self::from(&x)
}
}
// This gets its own implementation for performance.
impl ops::Rem<&U256> for U512 {
type Output = U256;
fn rem(self, x: &U256) -> U256 {
if x.size > self.size {
return self.low_u256();
}
let (y, mut rem) = (Self::zero(), U256::zero());
unsafe {
gmp::mpn_tdiv_qr(
y.data(),
rem.data(),
0,
self.data(),
self.size,
x.data(),
x.size,
)
};
rem.normalize_size();
rem
}
}
impl ops::Rem<U256> for U512 {
type Output = U256;
fn rem(self, x: U256) -> U256 {
#![allow(clippy::op_ref)]
self % &x
}
}
impl U256 {
/// Returns (result of removing all `f`s, number of `f`s removed)
pub fn remove_factor(self, f: Self) -> (Self, u64) {
// For some reason this needs extra scratch space.
let mut out = U512::zero();
let outmpz = out.as_mpz();
let s = self.as_mpz();
let f = f.as_mpz();
let c = unsafe { gmp::mpz_remove(mut_ptr(&outmpz), mut_ptr(&s), mut_ptr(&f)) };
out.size = i64::from(outmpz.size);
(out.low_u256(), c)
}
}
/// It turns out to be faster to provide multiplication as `U256 * U256 -> U512`, because it lets us
/// use `mpn_mul_n` instead of `mpn_mul`.
impl ops::Mul<&Self> for U256 {
type Output = U512;
fn mul(self, x: &Self) -> U512 {
let mut y = U512::zero();
unsafe { gmp::mpn_mul_n(y.data(), self.data(), x.data(), 4) };
y.normalize_size();
y
}
}
impl ops::Mul for U256 {
type Output = U512;
fn mul(self, x: Self) -> U512 {
#![allow(clippy::op_ref)]
self * &x
}
}
#[allow(unused_mut)]
fn mut_ptr<T>(mut t: &T) -> *mut T {
t as *const T as *mut T
}
pub fn u256<T>(t: T) -> U256
where
U256: From<T>,
{
U256::from(t)
}
pub fn u512<T>(t: T) -> U512
where
U512: From<T>,
{
U512::from(t)
}
fn i32_to_mpz(i: i32, data: &mut u64) -> mpz_t {
*data = i.abs() as u64;
mpz_t {
size: i.signum(),
d: std::ptr::NonNull::new(data).unwrap() ,
alloc: 1,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert!(u256(1) + u256(0) == u256(1));
assert!(u256(1) + u256(2) == u256(3));
assert!(u256([0, 1, 0, 0]) + u256([0, 1, 0, 0]) == u256([0, 2, 0, 0]));
assert!(u256([0, 1, 0, 0]) + u256([0, 1, 1, 1]) == u256([0, 2, 1, 1]));
}
#[should_panic(expected = "assertion failed: carry == 0")]
#[test]
fn test_add_overflow() {
let _ = u256([0, 0, 0, u64::max_value()]) + u256([0, 0, 0, u64::max_value()]);
}
#[test]
fn test_sub() {
assert!(u256(1) - u256(0) == u256(1));
assert!(u256(1) - u256(1) == u256(0));
assert!(u256([0, 1, 0, 0]) - u256([0, 1, 0, 0]) == u256([0, 0, 0, 0]));
assert!(u256([0, 1, 0, 1]) - u256([0, 1, 0, 0]) == u256([0, 0, 0, 1]));
}
#[should_panic(expected = "assertion failed: borrow == 0")]
#[test]
fn test_sub_borrow() {
let _ = u256([0, 1, 0, 0]) - u256([0, 0, 1, 0]);
}
#[test]
fn test_mul() {
assert!(u256(0) * u256(3) == u512(0));
assert!(u256(2) * u256(3) == u512(6));
assert!(u256([0, 1, 0, 0]) * u256([0, 1, 0, 0]) == u512([0, 0, 1, 0, 0, 0, 0, 0]));
assert!(u256([0, 2, 0, 0]) * u256([0, 1, 0, 1]) == u512([0, 0, 2, 0, 2, 0, 0, 0]));
}
#[test]
fn test_div() {
assert!(u256(0) / u256(3) == u256(0));
assert!(u256(5) / u256(3) == u256(1));
assert!(u256(6) / u256(3) == u256(2));
assert!(u256([0, 0, 1, 0]) / u256([0, 1, 0, 0]) == u256([0, 1, 0, 0]));
}
#[test]
fn test_rem() {
assert!(u256(0) % u256(3) == u256(0));
assert!(u256(5) % u256(3) == u256(2));
assert!(u256(6) % u256(3) == u256(0));
assert!(u256([1, 0, 1, 0]) % u256([0, 1, 0, 0]) == u256(1));
}
#[test]
fn test_rem512() {
assert!(u512(0) % u256(3) == u256(0));
assert!(u512(5) % u256(3) == u256(2));
assert!(u512(6) % u256(3) == u256(0));
assert!(u512([1, 0, 1, 0, 0, 0, 0, 0]) % u256([0, 1, 0, 0]) == u256(1));
}
}