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
//! Performs Tonelli–Shanks algorithm.
//!
//! # Example
//!
//! ```
//! # use __acl_modint as acl_modint;
//! use acl_modint::ModInt1000000007 as Mint;
//! use tonelli_shanks::ModIntBaseExt as _;
//!
//! assert_eq!(Some(82_681_419.into()), Mint::new(42).sqrt());
//! ```

mod extern_crates {
    pub(super) extern crate __acl_modint as acl_modint;
    pub(super) extern crate __xorshift as xorshift;
}

use self::extern_crates::{acl_modint::ModIntBase, xorshift::Xorshift64};

/// Performs Tonelli–Shanks algorithm.
pub trait ModIntBaseExt: ModIntBase {
    /// Performs Tonelli–Shanks algorithm.
    fn sqrt(self) -> Option<Self> {
        let q = u64::from((Self::modulus() - 1) >> (Self::modulus() - 1).trailing_zeros());
        let s = (Self::modulus() - 1).trailing_zeros();

        let z = {
            let mut xor64 = Xorshift64::new();
            loop {
                let z = Self::new(xor64.next_u64() % u64::from(Self::modulus()));
                if z.pow(((Self::modulus() - 1) / 2).into()).val() == Self::modulus() - 1 {
                    break z;
                }
            }
        };

        let mut m = s;
        let mut c = z.pow(q);
        let mut t = self.pow(q);
        let mut r = self.pow((q + 1) / 2);

        'ret: loop {
            if t.val() == 0 {
                break Some(Self::new(0));
            }

            if t.val() == 1 {
                break Some(r);
            }

            let i = {
                let mut acc = t * t;
                let mut i = 1;
                loop {
                    if i == m {
                        break 'ret None;
                    }
                    if acc.val() == 1 {
                        break i;
                    }
                    acc *= acc;
                    i += 1;
                }
            };

            let b = {
                let mut b = c;
                for _ in 0..m - i - 1 {
                    b *= b;
                }
                b
            };

            m = i;
            c = b * b;
            t *= b * b;
            r *= b;
        }
    }
}

impl<Z: ModIntBase> ModIntBaseExt for Z {}