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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
use libc;

use crate::algorithms;
use crate::pid::*;
use crate::ordered::OrderedRing;
use crate::ordered::OrderedRingStore;
use crate::primitive_int::StaticRing;
use crate::primitive_int::StaticRingBase;
use crate::divisibility::*;
use crate::ring::*;
use crate::homomorphism::*;
use crate::integer::*;
use crate::rings::rust_bigint::*;

mod mpir_bindings;

///
/// An `mpir` integer.
/// 
pub struct MPZEl {
    integer: mpir_bindings::__mpz_struct
}

impl MPZEl {

    fn new() -> Self {
        unsafe {
            let mut result = mpir_bindings::UNINIT_MPZ;
            mpir_bindings::__gmpz_init(&mut result as mpir_bindings::mpz_ptr);
            return MPZEl { integer: result };
        }
    }

    pub fn assign(&mut self, rhs: &MPZEl) {
        unsafe {
            mpir_bindings::__gmpz_set(&mut self.integer as mpir_bindings::mpz_ptr, &rhs.integer as mpir_bindings::mpz_srcptr);
        }
    }
}

impl Drop for MPZEl {

    fn drop(&mut self) {
        unsafe {
            mpir_bindings::__gmpz_clear(&mut self.integer as mpir_bindings::mpz_ptr)
        }
    }
}

///
/// Arbitrary-precision integer ring, implemented by binding to the well-known
/// and heavily optimized library mpir (fork of gmp).
/// 
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MPZBase;

///
/// [`RingStore`] corresponding to [`MPZBase`].
/// 
pub type MPZ = RingValue<MPZBase>;

impl MPZ {
    
    pub const RING: MPZ = RingValue::from(MPZBase);
}

impl MPZBase {

    pub fn from_base_u64_repr(&self, dst: &mut MPZEl, input: &[u64]) {
        unsafe {
            assert_eq!(std::mem::size_of::<mpir_bindings::mpir_ui>(), std::mem::size_of::<u64>());
            if input.len() == 0 {
                mpir_bindings::__gmpz_set_ui(&mut dst.integer as mpir_bindings::mpz_ptr, 0);
                return;
            }
            assert!(input.len() > 0);
            mpir_bindings::__gmpz_import(
                &mut dst.integer as mpir_bindings::mpz_ptr, 
                input.len(), 
                -1i32,
                (u64::BITS / 8) as libc::size_t,
                0, 
                0, 
                (input.as_ptr() as *const mpir_bindings::mpir_ui) as *const libc::c_void
            );
        }
    }

    pub fn abs_base_u64_repr_len(&self, src: &MPZEl) -> usize {
        self.abs_highest_set_bit(src).map(|n| (n / u64::BITS as usize) + 1).unwrap_or(0)
    }

    pub fn abs_base_u64_repr(&self, src: &MPZEl, out: &mut [u64]) {
        unsafe {
            if self.abs_base_u64_repr_len(src) > 0 {
                assert!(out.len() >= self.abs_base_u64_repr_len(src));
                let mut size = 0;
        
                mpir_bindings::__gmpz_export(
                    (out.as_mut_ptr() as *mut mpir_bindings::mpir_ui) as *mut libc::c_void,
                    &mut size,
                    -1,
                    (u64::BITS / 8) as libc::size_t,
                    0,
                    0,
                    &src.integer as mpir_bindings::mpz_srcptr
                );
                for i in size..out.len() {
                    out[i] = 0;
                }
            } else {
                for i in 0..out.len() {
                    out[i] = 0;
                }
            }
        }
    }
}

impl RingBase for MPZBase {
    
    type Element = MPZEl;

    fn clone_el(&self, val: &Self::Element) -> Self::Element {
        unsafe {
            let mut result = MPZEl::new();
            mpir_bindings::__gmpz_set(&mut result.integer as mpir_bindings::mpz_ptr, &val.integer as mpir_bindings::mpz_srcptr);
            return result;
        }
    }

    fn mul_ref(&self, lhs: &Self::Element, rhs: &Self::Element) -> Self::Element {
        unsafe {
            let mut result = MPZEl::new();
            mpir_bindings::__gmpz_mul(&mut result.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr, &rhs.integer as mpir_bindings::mpz_srcptr);
            return result;
        }
    }

    fn negate_inplace(&self, lhs: &mut Self::Element) {
        unsafe {
            mpir_bindings::__gmpz_neg(&mut lhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr)
        }
    }

    fn zero(&self) -> Self::Element {
        MPZEl::new()
    }

    fn one(&self) -> Self::Element {
        self.from_int(1)
    }

    fn sub_ref_fst(&self, lhs: &Self::Element, mut rhs: Self::Element) -> Self::Element {
        unsafe {
            mpir_bindings::__gmpz_sub(&mut rhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr, &rhs.integer as mpir_bindings::mpz_srcptr);
            return rhs;
        }
    }

    fn sub_ref_snd(&self, mut lhs: Self::Element, rhs: &Self::Element) -> Self::Element {
        unsafe {
            mpir_bindings::__gmpz_sub(&mut lhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr, &rhs.integer as mpir_bindings::mpz_srcptr);
            return lhs;
        }
    }

    fn add_assign(&self, lhs: &mut Self::Element, rhs: Self::Element) { 
        self.add_assign_ref(lhs, &rhs);
    }

    fn add_assign_ref(&self, lhs: &mut Self::Element, rhs: &Self::Element) { 
        unsafe {
            mpir_bindings::__gmpz_add(&mut lhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr, &rhs.integer as mpir_bindings::mpz_srcptr);
        }
    }

    fn mul_assign(&self, lhs: &mut Self::Element, rhs: Self::Element) {
        self.mul_assign_ref(lhs, &rhs)
    }

    fn mul_assign_ref(&self, lhs: &mut Self::Element, rhs: &Self::Element) { 
        unsafe {
            mpir_bindings::__gmpz_mul(&mut lhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr, &rhs.integer as mpir_bindings::mpz_srcptr);
        }
    }

    fn mul_assign_int(&self, lhs: &mut Self::Element, rhs: i32) {
        unsafe {
            mpir_bindings::__gmpz_mul_ui(&mut lhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr, rhs.abs() as u64);
            if rhs < 0 {
                mpir_bindings::__gmpz_neg(&mut lhs.integer as mpir_bindings::mpz_ptr, &lhs.integer as mpir_bindings::mpz_srcptr)
            }
        }
    }

    fn sub(&self, lhs: Self::Element, rhs: Self::Element) -> Self::Element {
        self.sub_ref_snd(lhs, &rhs)
    }

    fn from_int(&self, value: i32) -> Self::Element {
        unsafe {
            let mut result = MPZEl::new();
            mpir_bindings::__gmpz_set_ui(&mut result.integer as mpir_bindings::mpz_ptr, value.abs() as u64);
            if value < 0 {
                return self.negate(result);
            } else {
                return result;
            }
        }
    }

    fn eq_el(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool {
        unsafe {
            mpir_bindings::__gmpz_cmp(&lhs.integer as mpir_bindings::mpz_srcptr, &rhs.integer as mpir_bindings::mpz_srcptr) == 0
        }
    }

    fn is_zero(&self, val: &Self::Element) -> bool { 
        unsafe {
            mpir_bindings::__gmpz_cmp_si(&val.integer as mpir_bindings::mpz_srcptr, 0) == 0
        }
    }
    
    fn is_one(&self, val: &Self::Element) -> bool { 
        unsafe {
            mpir_bindings::__gmpz_cmp_si(&val.integer as mpir_bindings::mpz_srcptr, 1) == 0
        }
    }

    fn is_neg_one(&self, val: &Self::Element) -> bool {
        unsafe {
            mpir_bindings::__gmpz_cmp_si(&val.integer as mpir_bindings::mpz_srcptr, -1) == 0
        }
    }

    fn is_noetherian(&self) -> bool {
        true
    }

    fn is_commutative(&self) -> bool {
        true
    }

    fn dbg<'a>(&self, value: &Self::Element, out: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        RustBigintRing::RING.get_ring().dbg(&self.map_out(RustBigintRing::RING.get_ring(), self.clone_el(value), &()), out)
    }
    
    fn characteristic<I: IntegerRingStore>(&self, other_ZZ: &I) -> Option<El<I>>
        where I::Type: IntegerRing
    {
        Some(other_ZZ.zero())
    }
}

impl OrderedRing for MPZBase {

    fn cmp(&self, lhs: &Self::Element, rhs: &Self::Element) -> std::cmp::Ordering {
        unsafe {
            let res = mpir_bindings::__gmpz_cmp(
                &lhs.integer as mpir_bindings::mpz_srcptr,
                &rhs.integer as mpir_bindings::mpz_srcptr
            );
            if res < 0 {
                return std::cmp::Ordering::Less;
            } else if res > 0 {
                return std::cmp::Ordering::Greater;
            } else {
                return std::cmp::Ordering::Equal;
            }
        }
    }

    fn is_neg(&self, value: &Self::Element) -> bool {
        unsafe {
            mpir_bindings::mpz_is_neg(&value.integer as mpir_bindings::mpz_srcptr)
        }
    }
}

impl DivisibilityRing for MPZBase {

    fn checked_left_div(&self, lhs: &Self::Element, rhs: &Self::Element) -> Option<Self::Element> {
        if self.is_zero(rhs) {
            if self.is_zero(lhs) {
                return Some(self.zero());
            } else {
                return None;
            }
        }
        let (quo, rem) = self.euclidean_div_rem(self.clone_el(lhs), rhs);
        if self.is_zero(&rem) {
            return Some(quo);
        } else {
            return None;
        }
    }
}

impl PrincipalIdealRing for MPZBase {
    
    fn extended_ideal_gen(&self, lhs: &Self::Element, rhs: &Self::Element) -> (Self::Element, Self::Element, Self::Element) {
        algorithms::eea::eea(self.clone_el(lhs), self.clone_el(rhs), MPZ::RING)
    }
}

impl EuclideanRing for MPZBase {
    
    fn euclidean_div_rem(&self, mut lhs: Self::Element, rhs: &Self::Element) -> (Self::Element, Self::Element) {
        unsafe {
            assert!(!self.is_zero(rhs));
            let mut quo = MPZEl::new();
            mpir_bindings::__gmpz_tdiv_qr(
                &mut quo.integer as mpir_bindings::mpz_ptr, 
                &mut lhs.integer as mpir_bindings::mpz_ptr, 
                &lhs.integer as mpir_bindings::mpz_srcptr, 
                &rhs.integer as mpir_bindings::mpz_srcptr
            );
            return (quo, lhs);
        }
    }

    fn euclidean_rem(&self, lhs: Self::Element, rhs: &Self::Element) -> Self::Element { 
        unsafe {
            assert!(!self.is_zero(rhs));
            let mut rem = MPZEl::new();
            mpir_bindings::__gmpz_tdiv_r(
                &mut rem.integer as mpir_bindings::mpz_ptr, 
                &lhs.integer as mpir_bindings::mpz_srcptr, 
                &rhs.integer as mpir_bindings::mpz_srcptr
            );
            return rem;
        }
    }

    fn euclidean_div(&self, lhs: Self::Element, rhs: &Self::Element) -> Self::Element {
        unsafe {
            assert!(!self.is_zero(rhs));
            let mut rem = MPZEl::new();
            mpir_bindings::__gmpz_tdiv_q(
                &mut rem.integer as mpir_bindings::mpz_ptr, 
                &lhs.integer as mpir_bindings::mpz_srcptr, 
                &rhs.integer as mpir_bindings::mpz_srcptr
            );
            return rem;
        }
    }

    fn euclidean_deg(&self, val: &Self::Element) -> Option<usize> {
        if self.abs_highest_set_bit(val).unwrap_or(0) < usize::BITS as usize {
            unsafe {
                mpir_bindings::__gmpz_get_ui(&val.integer as mpir_bindings::mpz_srcptr).try_into().ok()
            }
        } else {
            None
        }
    }
}

impl Domain for MPZBase {}

impl IntegerRing for MPZBase {
    
    fn to_float_approx(&self, el: &Self::Element) -> f64 {
        unsafe {
            mpir_bindings::__gmpz_get_d(&el.integer as mpir_bindings::mpz_srcptr)
        }
    }

    fn from_float_approx(&self, el: f64) -> Option<Self::Element> {
        unsafe {
            let mut result = MPZEl::new();
            mpir_bindings::__gmpz_set_d(&mut result.integer as mpir_bindings::mpz_ptr, el);
            return Some(result);
        }
    }

    fn mul_pow_2(&self, el: &mut Self::Element, power: usize) {
        unsafe {
            mpir_bindings::__gmpz_mul_2exp(&mut el.integer as mpir_bindings::mpz_ptr, &el.integer as mpir_bindings::mpz_srcptr, power as u64)
        }
    }

    fn euclidean_div_pow_2(&self, el: &mut Self::Element, power: usize) {
        unsafe {
            mpir_bindings::__gmpz_tdiv_q_2exp(&mut el.integer as mpir_bindings::mpz_ptr, &el.integer as mpir_bindings::mpz_srcptr, power as u64)
        }
    }

    fn abs_is_bit_set(&self, el: &Self::Element, bit: usize) -> bool {
        unsafe {
            if mpir_bindings::mpz_is_neg(&el.integer as mpir_bindings::mpz_srcptr) {
                let value = mpir_bindings::__gmpz_tstbit(&el.integer as mpir_bindings::mpz_srcptr, bit as u64) == 1;
                let least_significant_zero = mpir_bindings::__gmpz_scan1(&el.integer as mpir_bindings::mpz_srcptr, 0);
                if bit <= least_significant_zero as usize {
                    value
                } else {
                    !value
                }
            } else {
                mpir_bindings::__gmpz_tstbit(&el.integer as mpir_bindings::mpz_srcptr, bit as u64) == 1
            }
        }
    }
    
    fn abs_highest_set_bit(&self, value: &Self::Element) -> Option<usize> {
        unsafe {
            if self.is_zero(value) {
                return None;
            }
            Some(mpir_bindings::__gmpz_sizeinbase(&value.integer as mpir_bindings::mpz_srcptr, 2) - 1)
        }
    }

    fn abs_lowest_set_bit(&self, value: &Self::Element) -> Option<usize> {
        unsafe {
            let result = mpir_bindings::__gmpz_scan1(&value.integer as mpir_bindings::mpz_srcptr, 0);
            if result == mpir_bindings::mpir_ui::MAX {
                return None;
            } else {
                return Some(result as usize);
            }
        }
    }

    fn rounded_div(&self, lhs: Self::Element, rhs: &Self::Element) -> Self::Element {
        let mut rhs_half = self.abs(self.clone_el(rhs));
        self.euclidean_div_pow_2(&mut rhs_half, 1);
        if self.is_neg(&lhs) {
            return self.euclidean_div(self.sub(lhs, rhs_half), rhs);
        } else {
            return self.euclidean_div(self.add(lhs, rhs_half), rhs);
        }
    }

    fn get_uniformly_random_bits<G: FnMut() -> u64>(&self, log2_bound_exclusive: usize, rng: G) -> Self::Element {
        unsafe {
            let mut result = MPZEl::new();
            let len = (log2_bound_exclusive - 1) / u64::BITS as usize + 1;
            let mut data = Vec::new();
            data.resize_with(len, rng);

            mpir_bindings::__gmpz_import(
                &mut result.integer as mpir_bindings::mpz_ptr, 
                data.len(), 
                -1i32,
                (u64::BITS / 8) as libc::size_t,
                0, 
                0, 
                (data.as_ptr() as *const mpir_bindings::mpir_ui) as *const libc::c_void
            );

            self.euclidean_div_pow_2(&mut result, len * u64::BITS as usize - log2_bound_exclusive);
            return result;
        }
    }

    fn representable_bits(&self) -> Option<usize> {
        None
    }
}

impl HashableElRing for MPZBase {

    fn hash<H: std::hash::Hasher>(&self, el: &Self::Element, h: &mut H) {
        unsafe {
            <_ as std::hash::Hash>::hash(&(self.is_neg(&el), self.abs_highest_set_bit(&el), mpir_bindings::__gmpz_get_ui(&el.integer as mpir_bindings::mpz_srcptr)), h)
        }
    }
}

impl IntCast<RustBigintRingBase> for MPZBase {

    fn cast(&self, _: &RustBigintRingBase, el: RustBigint) -> Self::Element {
        let mut result = MPZEl::new();
        self.from_base_u64_repr(&mut result, &RustBigintRing::RING.get_ring().abs_base_u64_repr(&el).collect::<Vec<_>>()[..]);
        if RustBigintRing::RING.is_neg(&el) {
            self.negate_inplace(&mut result);
        }
        return result;
    }
}

impl IntCast<MPZBase> for RustBigintRingBase {

    fn cast(&self, from: &MPZBase, el: MPZEl) -> RustBigint {
        let mut result = (0..from.abs_base_u64_repr_len(&el)).map(|_| 0u64).collect::<Vec<_>>();
        from.abs_base_u64_repr(&el, &mut result[..]);
        let result = RustBigintRing::RING.get_ring().from_base_u64_repr(result.into_iter());
        if from.is_neg(&el) {
            return RustBigintRing::RING.negate(result);
        } else {
            return result;
        }
    }
}

impl IntCast<StaticRingBase<i64>> for MPZBase {

    fn cast(&self, _: &StaticRingBase<i64>, el: i64) -> Self::Element {
        unsafe {
            let mut result = MPZEl::new(); 
            mpir_bindings::__gmpz_set_si(&mut result.integer as *mut _, el);
            return result;
        }
    }
}

impl IntCast<MPZBase> for StaticRingBase<i64> {

    fn cast(&self, from: &MPZBase, el: MPZEl) -> i64 {
        assert!(from.abs_highest_set_bit(&el).unwrap_or(0) < u64::BITS as usize);
        let negative = from.is_neg(&el);
        unsafe {
            let result = mpir_bindings::__gmpz_get_ui(&el.integer as mpir_bindings::mpz_srcptr);
            if !negative {
                result.try_into().unwrap()
            } else if result == i64::MAX as u64 + 1 {
                i64::MIN
            } else {
                -i64::try_from(result).unwrap()
            }
        }
    }
}

impl IntCast<StaticRingBase<i128>> for MPZBase {

    fn cast(&self, _: &StaticRingBase<i128>, el: i128) -> MPZEl {
        let el_abs = el.unsigned_abs();
        let data = [(el_abs & u64::MAX as u128) as u64, (el_abs >> u64::BITS) as u64];
        let mut result = MPZEl::new();
        self.from_base_u64_repr(&mut result, &data[..]);
        if el < 0 {
            self.negate_inplace(&mut result);
        }
        return result;
    }
}

impl IntCast<MPZBase> for StaticRingBase<i128> {

    fn cast(&self, from: &MPZBase, el: MPZEl) -> i128 {
        assert!(from.abs_base_u64_repr_len(&el) <= 2);
        let mut data = [0u64; 2];
        from.abs_base_u64_repr(&el, &mut data);
        let result = data[0] as u128 + ((data[1] as u128) << u64::BITS);
        if from.is_neg(&el) {
            if result == i128::MIN.unsigned_abs() {
                return i128::MIN;
            } else {
                return -i128::try_from(result).unwrap();
            }
        } else {
            return result.try_into().unwrap();
        }
    }
}

macro_rules! specialize_int_cast {
    ($($from:ty),*) => {
        $(
            impl IntCast<StaticRingBase<$from>> for MPZBase {

                fn cast(&self, _: &StaticRingBase<$from>, el: $from) -> MPZEl {
                    int_cast(el as i64, &RingRef::new(self), StaticRing::<i64>::RING)
                }
            }

            impl IntCast<MPZBase> for StaticRingBase<$from> {

                fn cast(&self, _: &MPZBase, el: MPZEl) -> $from {
                    int_cast(el, &StaticRing::<i64>::RING, &MPZ::RING) as $from
                }
            }
        )*
    }
}

specialize_int_cast!{ i8, i16, i32 }

#[cfg(test)]
use crate::pid::EuclideanRingStore;

#[cfg(test)]
fn edge_case_elements_bigint() -> impl Iterator<Item = RustBigint> {
    [
        RustBigintRing::RING.int_hom().map(0),
        RustBigintRing::RING.int_hom().map(1),
        RustBigintRing::RING.int_hom().map(-1),
        RustBigintRing::RING.int_hom().map(7),
        RustBigintRing::RING.int_hom().map(-7),
        RustBigintRing::RING.int_hom().map(i32::MAX),
        RustBigintRing::RING.int_hom().map(i32::MIN),
        RustBigintRing::RING.power_of_two(64),
        RustBigintRing::RING.negate(RustBigintRing::RING.power_of_two(64)),
        RustBigintRing::RING.sub(RustBigintRing::RING.power_of_two(64), RustBigintRing::RING.one()),
        RustBigintRing::RING.power_of_two(128),
        RustBigintRing::RING.negate(RustBigintRing::RING.power_of_two(128)),
        RustBigintRing::RING.sub(RustBigintRing::RING.power_of_two(128), RustBigintRing::RING.one()),
        RustBigintRing::RING.power_of_two(192),
        RustBigintRing::RING.negate(RustBigintRing::RING.power_of_two(192)),
        RustBigintRing::RING.sub(RustBigintRing::RING.power_of_two(192), RustBigintRing::RING.one())
    ].into_iter()
}

#[cfg(test)]
fn edge_case_elements() -> impl Iterator<Item = MPZEl> {
    edge_case_elements_bigint().map(|n| MPZ::RING.coerce(&RustBigintRing::RING, n))
}

#[test]
fn test_negate_inplace() {
    let mut a = MPZ::RING.power_of_two(64);
    MPZ::RING.negate_inplace(&mut a);
    assert!(MPZ::RING.is_neg(&a));

    for a in edge_case_elements() {
        let mut b = MPZ::RING.clone_el(&a);
        MPZ::RING.negate_inplace(&mut b);
        assert!(MPZ::RING.is_zero(&a) || (MPZ::RING.is_neg(&a) ^ MPZ::RING.is_neg(&b)));
    }
}

#[test]
fn test_ring_axioms() {
    crate::ring::generic_tests::test_ring_axioms(MPZ::RING, edge_case_elements())
}

#[test]
fn test_divisibility_ring_axioms() {
    crate::divisibility::generic_tests::test_divisibility_axioms(MPZ::RING, edge_case_elements())
}

#[test]
fn test_euclidean_ring_axioms() {
    crate::pid::generic_tests::test_euclidean_ring_axioms(MPZ::RING, edge_case_elements())
}

#[test]
fn test_integer_ring_axioms() {
    crate::integer::generic_tests::test_integer_axioms(MPZ::RING, edge_case_elements())
}

#[test]
fn test_canonical_iso_axioms_i32() {
    crate::ring::generic_tests::test_hom_axioms(StaticRing::<i32>::RING, MPZ::RING, [0, -1, 1, i16::MAX as i32, i16::MIN as i32].into_iter());
    crate::ring::generic_tests::test_iso_axioms(StaticRing::<i32>::RING, MPZ::RING, [0, -1, 1, i16::MIN as i32, i16::MAX as i32].into_iter());
}

#[test]
fn test_canonical_iso_axioms_i64() {
    crate::ring::generic_tests::test_hom_axioms(StaticRing::<i64>::RING, MPZ::RING, [0, -1, 1, i32::MAX as i64, i32::MIN as i64].into_iter());
    crate::ring::generic_tests::test_iso_axioms(StaticRing::<i64>::RING, MPZ::RING, [0, -1, 1, i32::MIN as i64, i32::MAX as i64].into_iter());

    assert_el_eq!(MPZ::RING, MPZ::RING.sub(MPZ::RING.power_of_two(63), MPZ::RING.one()), &MPZ::RING.coerce(&StaticRing::<i64>::RING, i64::MAX));
    assert_el_eq!(MPZ::RING, MPZ::RING.negate(MPZ::RING.power_of_two(63)), MPZ::RING.coerce(&StaticRing::<i64>::RING, i64::MIN));
    assert_eq!(i64::MAX, int_cast(MPZ::RING.sub(MPZ::RING.power_of_two(63), MPZ::RING.one()), &StaticRing::<i64>::RING, MPZ::RING));
    assert_eq!(i64::MIN, int_cast(MPZ::RING.negate(MPZ::RING.power_of_two(63)), &StaticRing::<i64>::RING, MPZ::RING));
}

#[test]
fn test_canonical_iso_axioms_i128() {
    crate::ring::generic_tests::test_hom_axioms(StaticRing::<i128>::RING, MPZ::RING, [0, -1, 1, i64::MAX as i128, i64::MIN as i128].into_iter());
    crate::ring::generic_tests::test_iso_axioms(StaticRing::<i128>::RING, MPZ::RING, [0, -1, 1, i64::MIN as i128, i64::MAX as i128].into_iter());

    assert_el_eq!(MPZ::RING, MPZ::RING.sub(MPZ::RING.power_of_two(127), MPZ::RING.one()), &MPZ::RING.coerce(&StaticRing::<i128>::RING, i128::MAX));
    assert_el_eq!(MPZ::RING, MPZ::RING.negate(MPZ::RING.power_of_two(127)), MPZ::RING.coerce(&StaticRing::<i128>::RING, i128::MIN));
    assert_eq!(i128::MAX, int_cast(MPZ::RING.sub(MPZ::RING.power_of_two(127), MPZ::RING.one()), &StaticRing::<i128>::RING, MPZ::RING));
    assert_eq!(i128::MIN, int_cast(MPZ::RING.negate(MPZ::RING.power_of_two(127)), &StaticRing::<i128>::RING, MPZ::RING));
}

#[test]
fn test_canonical_iso_axioms_bigint() {
    crate::ring::generic_tests::test_hom_axioms(RustBigintRing::RING, MPZ::RING, edge_case_elements_bigint());
    crate::ring::generic_tests::test_iso_axioms(RustBigintRing::RING, MPZ::RING, edge_case_elements_bigint());
}

#[test]
fn test_abs_is_bit_set() {
    let a = MPZ::RING.int_hom().map(1 << 15);
    assert_eq!(true, MPZ::RING.abs_is_bit_set(&a, 15));
    assert_eq!(false, MPZ::RING.abs_is_bit_set(&a, 16));
    assert_eq!(false, MPZ::RING.abs_is_bit_set(&a, 14));

    let a = MPZ::RING.int_hom().map(-7);
    assert_eq!(true, MPZ::RING.abs_is_bit_set(&a, 0));
    assert_eq!(true, MPZ::RING.abs_is_bit_set(&a, 1));
    assert_eq!(true, MPZ::RING.abs_is_bit_set(&a, 2));
    assert_eq!(false, MPZ::RING.abs_is_bit_set(&a, 3));

    let a = MPZ::RING.int_hom().map(-1 << 15);
    assert_eq!(true, MPZ::RING.abs_is_bit_set(&a, 15));
    assert_eq!(false, MPZ::RING.abs_is_bit_set(&a, 16));
    assert_eq!(false, MPZ::RING.abs_is_bit_set(&a, 14));
}

#[test]
fn test_highest_set_bit() {
    assert_eq!(None, MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(0)));
    assert_eq!(Some(0), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(1)));
    assert_eq!(Some(0), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(-1)));
    assert_eq!(Some(1), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(2)));
    assert_eq!(Some(1), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(-2)));
    assert_eq!(Some(1), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(3)));
    assert_eq!(Some(1), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(-3)));
    assert_eq!(Some(2), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(4)));
    assert_eq!(Some(2), MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(-4)));
}

#[test]
fn test_lowest_set_bit() {
    assert_eq!(None, MPZ::RING.abs_highest_set_bit(&MPZ::RING.int_hom().map(0)));
    assert_eq!(Some(0), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(1)));
    assert_eq!(Some(0), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(-1)));
    assert_eq!(Some(1), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(2)));
    assert_eq!(Some(1), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(-2)));
    assert_eq!(Some(0), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(3)));
    assert_eq!(Some(0), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(-3)));
    assert_eq!(Some(2), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(4)));
    assert_eq!(Some(2), MPZ::RING.abs_lowest_set_bit(&MPZ::RING.int_hom().map(-4)));
}

#[test]
fn from_to_float_approx() {
    let x: f64 = 83465209236517892563478156042389675783219532497861237985328563.;
    let y = MPZ::RING.to_float_approx(&MPZ::RING.from_float_approx(x).unwrap());
    assert!(x * 0.99 < y);
    assert!(y < x * 1.01);
}

#[bench]
fn bench_mul_300_bits(bencher: &mut test::Bencher) {
    let x = MPZ::RING.coerce(&RustBigintRing::RING, RustBigintRing::RING.get_ring().parse("2382385687561872365981723456981723456987134659834659813491964132897159283746918732563498628754", 10).unwrap());
    let y = MPZ::RING.coerce(&RustBigintRing::RING, RustBigintRing::RING.get_ring().parse("48937502893645789234569182735646324895723409587234", 10).unwrap());
    let z = MPZ::RING.coerce(&RustBigintRing::RING, RustBigintRing::RING.get_ring().parse("116588006478839442056346504147013274749794691549803163727888681858469844569693215953808606899770104590589390919543097259495176008551856143726436", 10).unwrap());
    bencher.iter(|| {
        let p = MPZ::RING.mul_ref(&x, &y);
        assert_el_eq!(MPZ::RING, z, p);
    })
}

#[bench]
fn bench_div_300_bits(bencher: &mut test::Bencher) {
    let x = MPZ::RING.coerce(&RustBigintRing::RING, RustBigintRing::RING.get_ring().parse("2382385687561872365981723456981723456987134659834659813491964132897159283746918732563498628754", 10).unwrap());
    let y = MPZ::RING.coerce(&RustBigintRing::RING, RustBigintRing::RING.get_ring().parse("48937502893645789234569182735646324895723409587234", 10).unwrap());
    let z = MPZ::RING.coerce(&RustBigintRing::RING, RustBigintRing::RING.get_ring().parse("116588006478839442056346504147013274749794691549803163727888681858469844569693215953808606899770104590589390919543097259495176008551856143726436", 10).unwrap());
    bencher.iter(|| {
        let q = MPZ::RING.euclidean_div(MPZ::RING.clone_el(&z), &y);
        assert_el_eq!(MPZ::RING, x, q);
    })
}