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
use either::*;

use crate::numbers::*;

/// A coordinate for a Markoff triple.
/// May represent any of $a$, $b$, or $c$ in a Markoff triple $(a, b, c)$.
/// This is a single field struct containing only an `FpNum<P>` for prime `P`.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Coord<const P: u128>(pub FpNum<P>);

#[derive(PartialEq, Eq, Debug)]
/// The order of a rotation map, along with which type of conic it is.
pub enum RotOrder {
    /// An orbit of order dividing $p - 1$ (and not equal to 2).
    Hyperbola(u128),
    /// An orbit of order dividing $p + 1$ (and not equal to 2).
    Ellipse(u128),
    /// An orbit of order exactly 1 or 2.
    Parabola,
}

impl<const P: u128> Coord<P> {
    /// Returns an element $\chi$ such that, for a coordinate $a$, $a = \chi + \chi^{-1}$.
    /// If $a$ is a quadratic residue modulo `P`, then $\chi \in \mathbb{F}\_p$, and the result
    /// will be a `Right<FpNum<P>>`. Otherwise, $\chi \in \mathbb{F}\_{p^2}$, and the result will
    /// be a `Left<QuadNum<P>>`.
    pub fn to_chi(&self) -> Either<QuadNum<P>, FpNum<P>> {
        let disc = self.0.pow(2) - FpNum::from(4);
        let two_inv = FpNum::from(2).inverse();
        QuadNum::int_sqrt_either(disc).map_either(
            |x| (QuadNum::<P>::from(self.0) + x) * QuadNum::<P>::from(two_inv),
            |x| (self.0 + x) * two_inv,
        )
    }

    /// Returns an iterator yielding the coordinates $(b, c)$ contained in the orbit with fixed coordinate
    /// $a$ (the coordinate on which `rot` is called), beginning with $(a, b, c)$.
    pub fn rot(self, b: Coord<P>, c: Coord<P>) -> impl Iterator<Item = (Coord<P>, Coord<P>)> {
        std::iter::successors(Some((b, c)), move |(y, z)| {
            let (b_, c_) = (*z, self.0 * z.0 + FpNum::from(P) - y.0);
            if b_ == b && c_ == c.0 {
                None
            } else {
                Some((b_, Coord(c_)))
            }
        })
    }

    /// Returns an iterator yielding the values $b$ such that $(a, b, c)$ is a Markoff triple for
    /// some value $c$.
    pub fn part(self, b: Coord<P>) -> Option<Box<dyn Iterator<Item = Coord<P>>>> {
        let a = self.0;
        let Some(disc) = (a * a * b.0 * b.0 - 4 * (a * a + b.0 * b.0)).int_sqrt() else {
            return None;
        };
        let c = (a * b.0 + disc) * FpNum::from(2).inverse();
        Some(Box::new(self.rot(b, Coord(c)).map(|x| x.1)))
    }

    /// Returns the order of the map $\text{rot}\_a$, that is, $\lvert \langle \text{rot}\_a \rangle \rvert$, along with the type of [`RotOrder`] that it is.
    pub fn rot_order<S1, S2>(&self) -> RotOrder
    where
        FpNum<P>: Factor<S1>,
        QuadNum<P>: Factor<S2>,
    {
        match self
            .to_chi()
            .as_ref()
            .map_either(|l| l.order(), |r| r.order())
        {
            Left(1) | Right(1) => RotOrder::Parabola,
            Left(2) | Right(2) => RotOrder::Parabola,
            Left(d) => RotOrder::Ellipse(d),
            Right(d) => RotOrder::Hyperbola(d),
        }
    }

    /// Returns an upper bound on the endgame breakpoint.
    /// That is, every triple with order larger than the value returned by this method is
    /// guarenteed to lie in the endgame.
    pub fn endgame<S>() -> (u128, u128)
    where
        FpNum<P>: Factor<S>,
        QuadNum<P>: Factor<S>,
    {
        let tmp = 8.0 * (P as f64).sqrt();
        let hyper = tmp * ((P - 1) * FpNum::FACTORS.tau()) as f64;
        let hyper = hyper / (FpNum::FACTORS.phi() as f64);
        let ellip = tmp * ((P + 1) * QuadNum::FACTORS.tau()) as f64;
        let ellip = ellip / (QuadNum::FACTORS.phi() as f64);
        let res = (hyper.ceil() as u128, ellip.ceil() as u128);
        (std::cmp::min(res.0, P - 2), std::cmp::min(res.1, P))
    }
}

impl<const P: u128> From<u128> for Coord<P> {
    fn from(src: u128) -> Coord<P> {
        Coord(FpNum::from(src))
    }
}

impl<const P: u128> From<Coord<P>> for u128 {
    fn from(src: Coord<P>) -> u128 {
        u128::from(src.0)
    }
}

/// Common trait for the `from_chi` and `from_chi_conj` methods to be defined on both `FpNum` and
/// `QuadNum`.
pub trait FromChi<S, const P: u128>: SylowDecomposable<S>
{
    /// Returns $\chi + \chi^{-1}$.
    fn from_chi<const L: usize>(
        chi: &SylowElem<S, L, Self>,
        decomp: &SylowDecomp<S, L, Self>,
    ) -> FpNum<P>;

    /// Returns $\chi - \chi^{-1}$.
    fn from_chi_conj<const L: usize>(
        chi: &SylowElem<S, L, Self>,
        decomp: &SylowDecomp<S, L, Self>,
    ) -> FpNum<P>;
}

impl<S, const P: u128> FromChi<S, P> for FpNum<P>
where
    FpNum<P>: Factor<S>,
{
    fn from_chi<const L: usize>(
        chi: &SylowElem<S, L, FpNum<P>>,
        decomp: &SylowDecomp<S, L, FpNum<P>>,
    ) -> FpNum<P> {
        let chi_inv = chi.inverse().to_product(decomp);
        let chi = chi.to_product(decomp);
        chi + chi_inv
    }

    fn from_chi_conj<const L: usize>(
        chi: &SylowElem<S, L, FpNum<P>>,
        decomp: &SylowDecomp<S, L, FpNum<P>>,
    ) -> FpNum<P> {
        let chi_inv = chi.inverse().to_product(decomp);
        let chi = chi.to_product(decomp);
        chi - chi_inv
    }
}

impl<S, const P: u128> FromChi<S, P> for QuadNum<P>
where
    QuadNum<P>: Factor<S>,
{
    fn from_chi<const L: usize>(
        chi: &SylowElem<S, L, QuadNum<P>>,
        decomp: &SylowDecomp<S, L, QuadNum<P>>,
    ) -> FpNum<P> {
        let chi_inv = chi.inverse().to_product(decomp);
        let chi = chi.to_product(decomp);
        let res = chi + chi_inv;
        assert_eq!(res.1, FpNum::<P>::ZERO);
        res.0
    }

    fn from_chi_conj<const L: usize>(
        chi: &SylowElem<S, L, QuadNum<P>>,
        decomp: &SylowDecomp<S, L, QuadNum<P>>,
    ) -> FpNum<P> {
        let chi_inv = chi.inverse().to_product(decomp);
        let chi = chi.to_product(decomp);
        let res = chi - chi_inv;
        assert_eq!(res.0, FpNum::<P>::ZERO);
        res.1
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(PartialEq, Eq)]
    struct Ph {}

    impl_factors!(Ph, 3001);

    #[test]
    fn from_chi() {
        for i in 0..3000 {
            let a = FpNum::<3001>::from(i);
            match Coord(a).to_chi() {
                Left(chi) => {
                    let test = chi + chi.inverse();
                    assert_eq!(test.1, FpNum::from(0));
                    assert_eq!(a, test.0);
                }
                Right(chi) => {
                    assert_eq!(a, chi + chi.inverse());
                }
            }
        }
    }
}