Documentation

Ratpoints.Fast.Sieve

A fast search for the points #

Ratpoints.Fast.fastPoints c H bLow bHigh lists the normalized points of height at most H on y ^ 2 = F(x, z) whose denominator z lies in the range of bLow and bHigh (the points at infinity always), each once, in an order of its own: only such points in any case, and all of them assuming Sturm's theorem, on which the region of the numerators rests. It is not a model of the C code: it sieves in the way that suits Lean, with bit masks held in natural numbers, which Lean represents by GMP integers, so that one &&& ANDs thousands of candidates without an allocation per word. The program ratpoints-lean runs Ratpoints.FastMask.fastPoints, the same sieve with the masks held in byte arrays, which lists the same points in another order.

The choice of G, L and the families affects the speed only: the proofs hold for every choice.

Main definitions #

Residues that allow a point #

The squares modulo m: entry v is true when v = y * y % m for some y < m (each y * y % m marked in turn).

Equations
Instances For

    Horner's rule modulo m: the value w₀ + x (w₁ + x (w₂ + ⋯)) reduced modulo m at each step.

    Equations
    Instances For
      def Ratpoints.Fast.powMod (r m : Nat) :
      Nat → Nat

      r ^ e % m, by repeated multiplication modulo m.

      Equations
      Instances For

        The products cₖ r^(d - k) of Ratpoints.BruteForce.scaledCoeffs c d r, reduced modulo m.

        Equations
        Instances For
          @[irreducible]

          Nat.gcd a b by Euclid's algorithm in Lean, which on small numbers is much faster than the runtime's Nat.gcd: that one goes through GMP.

          Equations
          Instances For

            Whether x has no prime factor in common with g, quickly when g = 1.

            Equations
            Instances For
              def Ratpoints.Fast.admitRow (c : List Int) (d m : Nat) (sq : Array Bool) (r : Nat) :

              The row r of Ratpoints.Fast.admitTable c d m, computed directly (sq is Ratpoints.Fast.sqTable m): entry x is true when x, r and m have no prime factor in common and F(x, r) is a square modulo m.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                Candidates for the inverses modulo m, entry i by inv i = -(m / i) inv (m % i): the inverses when m is prime, and checked where they are used.

                Equations
                Instances For

                  The row one permuted: entry x < m is one[x * u % m], the index stepped by u (for u < m), without a division.

                  Equations
                  Instances For
                    @[irreducible]
                    def Ratpoints.Fast.permRow.go (one : Array Bool) (m u x i : Nat) (acc : Array Bool) :

                    acc with the entries one[i] pushed for x up to m - 1, the index i stepped by u modulo m.

                    Equations
                    Instances For

                      The residues modulo m that allow a point (a, y, b) with gcd(a, b) = 1 of the binary form of degree d of c: entry x of row r (for r, x < m) is true when x, r and m have no prime factor in common and F(x, r) is a square modulo m. A row r with an inverse u < m modulo m (from Ratpoints.Fast.invTable) is row 1 permuted (Ratpoints.Fast.permRow): F(x, r) = r ^ D F(x u, 1) with D even; the others come from Ratpoints.Fast.admitRow.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[irreducible]
                        def Ratpoints.Fast.admitTable.go (c : List Int) (d m : Nat) (sq one : Array Bool) (inv : Array Nat) (r : Nat) (acc : Array (Array Bool)) :

                        acc with the rows r to m - 1 of Ratpoints.Fast.admitTable pushed.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Bit masks #

                          def Ratpoints.Fast.pattern (m : Nat) (f : Nat → Bool) :

                          The number with bit j set for every j < m with f j.

                          Equations
                          Instances For

                            The pattern P of m bits repeated 2 ^ k times.

                            Equations
                            Instances For

                              A number k of doublings with N ≤ m * 2 ^ k for 0 < m.

                              Equations
                              Instances For
                                @[irreducible]

                                The positions of the bits set in n, from the highest down, one Nat.log2 at a time.

                                Equations
                                Instances For

                                  The positions, offset by o, of the bits set in n: those of n >>> h and of n % 2 ^ h for h = 64 * 2 ^ (k - 1) in turn, down to k = 0, where Ratpoints.Fast.setBitsSmall takes all the bits that are left.

                                  Equations
                                  Instances For

                                    The positions of the bits set in n, in descending order.

                                    Equations
                                    Instances For

                                      Families of moduli #

                                      def Ratpoints.Fast.baseRow (T : Array (Array Bool)) (m G N r : Nat) :

                                      The row of the modulus m for the residue r of the denominator, for the numerators 0, G, 2G, … and at least N of them, from the table T = Ratpoints.Fast.admitTable c d m: bit j is set when the residues G j and r modulo m allow a point.

                                      Equations
                                      Instances For

                                        A family of moduli with product M, for the step G of the numerators: an inverse ginv of G modulo M and the rows for the residues r < M of the denominator, for the numerators 0, G, 2G, …: the AND of the rows of its moduli (Ratpoints.Fast.baseRow).

                                        • M : Nat

                                          The product of the moduli.

                                        • ginv : Nat

                                          An inverse of the step G modulo M.

                                        • rows : Array Nat

                                          The rows, for the residues 0, …, M - 1 of the denominator.

                                        Instances For

                                          The family of the moduli m of the list ts of pairs (m, T), T the table Ratpoints.Fast.admitTable c d m, for the step G and chunks of at most L numerators, if every modulus is positive and G is invertible modulo their product M: its rows have L + 8 M bits, enough for a chunk at every shift below 8 M (Ratpoints.Fast.Family.shift).

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For

                                            The shift that aligns the rows of the family F with a chunk starting at the numerator a₀: s < 8 F.M with G s ≡ a₀ modulo F.M, the least one plus the multiple of F.M that makes it a multiple of 8 when F.M is odd (F.M ^ 2 ≡ 1 modulo 8), so that the search on byte arrays reads whole bytes of the rows.

                                            Equations
                                            Instances For

                                              The sieve #

                                              def Ratpoints.Fast.sieve (rs sh : Array Nat) (o mask : Nat) :

                                              The candidates of a chunk: the mask ANDed with the rows rs[i] shifted by sh[i] + o, in turn; once nothing is left, the rest is skipped.

                                              Equations
                                              Instances For

                                                The number of the indices i < n with i % G = ρ.

                                                Equations
                                                Instances For

                                                  The chunks of [0, cnt): their starts k L and lengths min L (cnt - k L).

                                                  Equations
                                                  Instances For

                                                    The chunks of the search, class by class: for the numerators x with x + H ≡ ρ modulo G, each chunk as the residue of its numerators modulo G, its first numerator a₀ and its length len, for the numerators a₀, a₀ + G, …, a₀ + (len - 1) G.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      def Ratpoints.Fast.chunkPoints (c : List Int) (d G : Nat) (rs sh : Array Nat) (o : Nat) (a₀ : Int) (len b : Nat) :

                                                      The points with the denominator b in the chunk of len numerators a₀, a₀ + G, …, by the sieve with the rows rs of b and the shifts sh moved by o.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For

                                                        The region #

                                                        The intervals of x that the Sturm step of ratpoints (Ratpoints.Sturm.computeSturm) keeps at the depth depth of the whole line, outside of which F(x, 1) < 0 assuming Sturm's theorem; none, no restriction, when the step does not apply (the degree 0 or at least 2 ^ 57, a height of at least 2 ^ 63, a depth beyond 55, or a polynomial that is not squarefree).

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For

                                                          An end of an interval of the region, for the numerators of absolute value at most H: the exact value n / q of the double in lowest terms (q a power of 2) when it is finite and of absolute value at most H, else H + 1 or -(H + 1) by its sign (an infinity too), which bound the values a / b with |a| ≤ H and b ≥ 1 as the end does, so that an interval beyond H keeps no numerator; none, read by Ratpoints.Fast.numRange as no bound, only for a NaN.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For

                                                            The region of the search, for the numerators of absolute value at most H: the intervals of Ratpoints.Fast.sturmIntervals with their ends as in Ratpoints.Fast.endFrac.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For

                                                              The numerators a with |a| ≤ H and a / b in the interval with the ends iv (as in Ratpoints.Fast.endFrac, an end none bounding nothing), from the first to the last; an end beyond H gives its bound without the product with b, which would leave the small integers.

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For

                                                                The numerators of the denominator b to search: from the first to the last a with |a| ≤ H and a / b in one of the intervals of reg (none when there is none), or all of -H, …, H without a region.

                                                                Equations
                                                                Instances For
                                                                  @[inline]
                                                                  def Ratpoints.Fast.part (G : Nat) (lo hi a₀ : Int) (len : Nat) :

                                                                  The part of the chunk of the len numerators a₀, a₀ + G, … that holds those from lo to hi, from the 64-bit word of the first to the end of the word of the last, or the whole chunk when that part leaves out fewer than len / 4 of its numerators (none when there is none): the bits t₁, …, t₁ + len₁ - 1 of the chunk as the pair (t₁, len₁), t₁ a multiple of 64.

                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For
                                                                    def Ratpoints.Fast.partPoints (c : List Int) (d G : Nat) (rs sh : Array Nat) (lo hi a₀ : Int) (len b : Nat) :

                                                                    The points with the denominator b in the part of the chunk of the len numerators a₀, a₀ + G, … that holds those from lo to hi (Ratpoints.Fast.part), by the sieve with the rows rs of b and the shifts sh of the chunk moved by the offset of the part.

                                                                    Equations
                                                                    • One or more equations did not get rendered due to their size.
                                                                    Instances For
                                                                      def Ratpoints.Fast.denPoints (c : List Int) (d H G : Nat) (fams : Array Family) (reg : Option (Array (Option (Int × Nat) × Option (Int × Nat)))) (sl : List (Nat × Int × Nat × Array Nat)) (b : Nat) :

                                                                      The points with the denominator b in the chunks sl, each given by its residue modulo G, its first numerator a₀, its length and the shifts of the families fams to a₀, by the sieve with the rows of b and the region reg: none when the row of b of some family is 0, which clears every chunk; else the chunks whole when the numerators of b in the region (Ratpoints.Fast.bounds) are all of -H, …, H, else their parts that hold these numerators (Ratpoints.Fast.partPoints).

                                                                      Equations
                                                                      • One or more equations did not get rendered due to their size.
                                                                      Instances For
                                                                        def Ratpoints.Fast.search (c : List Int) (d H G L : Nat) (fams : Array Family) (reg : Option (Array (Option (Int × Nat) × Option (Int × Nat)))) (dens : List Nat) :

                                                                        The points with the denominators dens (all positive), by the sieve with the step G, chunks of at most L numerators, the families fams and the region reg: for each denominator b, the chunks of the classes that the residue of b modulo G allows (Ratpoints.Fast.admitTable modulo G; these chunks are listed once for each residue), cut to the numerators of b in the region (Ratpoints.Fast.denPoints).

                                                                        Equations
                                                                        • One or more equations did not get rendered due to their size.
                                                                        Instances For

                                                                          The denominators #

                                                                          Whether k is a square.

                                                                          Equations
                                                                          Instances For

                                                                            The Jacobi symbol (a / n) times s for odd n, by Euclid's algorithm in at most fuel steps: the factors 2 of a are taken out with the sign of (2 / n), and for odd a the symbol becomes (n % a / a) with the sign of the reciprocity. The value is 0 when a and n have a common factor, and also when the steps run out, which they do not for n < 2 ^ 64 and fuel = 256; only the value -1 is used (Ratpoints.Fast.forbiddenDens).

                                                                            Equations
                                                                            Instances For

                                                                              The Jacobi symbol (a / n) for odd n (Ratpoints.Fast.jacobiNatAux), or 0.

                                                                              Equations
                                                                              Instances For
                                                                                @[irreducible]

                                                                                a with the bytes j, j + n, j + 2 n, … up to hi set to 1.

                                                                                Equations
                                                                                Instances For
                                                                                  @[irreducible]
                                                                                  def Ratpoints.Fast.forbiddenDensFrom (lc : Int) (hi : Nat) (comp forb : ByteArray) (p : Nat) :

                                                                                  The loop of Ratpoints.Fast.forbiddenDens over the odd numbers from p to hi, with the odd composites marked in comp and the forbidden denominators in forb so far: an unmarked p is a prime, whose odd multiples from p ^ 2 on are marked composite, and whose multiples are marked forbidden when (lc / p) = -1.

                                                                                  Equations
                                                                                  • One or more equations did not get rendered due to their size.
                                                                                  Instances For

                                                                                    The denominators up to hi that no point of a curve of even degree with the leading coefficient lc can have, marked 1: the multiples of an odd p with (lc / p) = -1 (Ratpoints.Fast.jacobiNat), p running over the odd primes up to hi, found by the sieve of Eratosthenes along the way (which p are tried affects the speed only).

                                                                                    Equations
                                                                                    Instances For

                                                                                      The numbers b from max lo 1 to n with m[b] = v, in ascending order, before acc.

                                                                                      Equations
                                                                                      Instances For
                                                                                        @[irreducible]

                                                                                        a with the bytes s j ^ 2 ≤ hi for j from k on (and j ≤ hi) set to 1.

                                                                                        Equations
                                                                                        Instances For

                                                                                          The numbers s k ^ 2 ≤ hi with s dividing n and k ≥ 1, marked 1 (for n ≥ 1).

                                                                                          Equations
                                                                                          • One or more equations did not get rendered due to their size.
                                                                                          Instances For
                                                                                            def Ratpoints.Fast.denominators (c : List Int) (d H : Nat) (bLow bHigh : Int) :

                                                                                            The denominators of the search, for the degree d, the height bound H and the range of bLow and bHigh: those b with max 1 bLow ≤ b ≤ H, and b ≤ bHigh unless bHigh < 1; for odd d only those of the form s k ^ 2 with s dividing the leading coefficient (Ratpoints.Fast.squareDens), for even d those that Ratpoints.Fast.forbiddenDens does not mark (all of them when the leading coefficient is a positive square, which Ratpoints.Fast.forbiddenDens would not mark); in ascending order.

                                                                                            Equations
                                                                                            • One or more equations did not get rendered due to their size.
                                                                                            Instances For

                                                                                              The choice of the parameters #

                                                                                              The fraction, in thousandths, of the pairs of residues modulo m that allow a point, from the table T = Ratpoints.Fast.admitTable c d m.

                                                                                              Equations
                                                                                              • One or more equations did not get rendered due to their size.
                                                                                              Instances For

                                                                                                The pairs (m, T) grouped into families: in ascending order of m, each family taking moduli while their product stays at most Mmax.

                                                                                                Equations
                                                                                                Instances For

                                                                                                  The parameters of the search: the step G, the chunk length L and the families, the most selective first.

                                                                                                  • G : Nat

                                                                                                    The step of the numerators in a chunk, a power of 2.

                                                                                                  • L : Nat

                                                                                                    The largest number of numerators in a chunk.

                                                                                                  • fams : Array Family

                                                                                                    The families.

                                                                                                  Instances For

                                                                                                    The moduli that the sieve may use.

                                                                                                    Equations
                                                                                                    Instances For

                                                                                                      The further moduli, for the searches in which many candidates are expected to survive the moduli of Ratpoints.Fast.candidateModuli (Ratpoints.Fast.takeModuli).

                                                                                                      Equations
                                                                                                      • One or more equations did not get rendered due to their size.
                                                                                                      Instances For

                                                                                                        The moduli m of ms with their tables Ratpoints.Fast.admitTable c d m, taken in turn while 64 m ^ 2 ≤ κ E, E the expected number of candidates that survive the moduli taken so far (each taken modulus keeps the share of them given by the density of its table): a modulus pays for its table, of cost about m ^ 2, only while many candidates are left.

                                                                                                        Equations
                                                                                                        Instances For

                                                                                                          Settings of the choice of the parameters (for tuning): the step G, the largest chunk length, the largest product of a family, the weight of the choice of the moduli and the depth of the Sturm step; 0 for the default.

                                                                                                          Instances For

                                                                                                            x, or dflt when x = 0.

                                                                                                            Equations
                                                                                                            Instances For

                                                                                                              The moduli of the sieve with their tables, for the coefficients c of degree d, the height bound H and B denominators: those of Ratpoints.Fast.candidateModuli and then of Ratpoints.Fast.furtherModuli that the expected number of survivors pays for (Ratpoints.Fast.takeModuli, from the (2 H + 1) B candidates).

                                                                                                              Equations
                                                                                                              • One or more equations did not get rendered due to their size.
                                                                                                              Instances For

                                                                                                                The moduli of Ratpoints.Fast.moduliTables grouped into families (Ratpoints.Fast.groupModuli), the most selective first.

                                                                                                                Equations
                                                                                                                • One or more equations did not get rendered due to their size.
                                                                                                                Instances For
                                                                                                                  def Ratpoints.Fast.choose (tu : Tuning) (c : List Int) (d H B : Nat) :

                                                                                                                  The parameters for the coefficients c of degree d, the height bound H and B denominators. The chunk length is tu.L (by default 8192), but at most the number of numerators of a class rounded up to a multiple of 64: by default a multiple of 64, so that the rows of the families are long enough at every shift for the fast path of Ratpoints.Mask.andRows in Ratpoints.FastMask.choose, which makes the same choice.

                                                                                                                  Equations
                                                                                                                  • One or more equations did not get rendered due to their size.
                                                                                                                  Instances For
                                                                                                                    def Ratpoints.Fast.sievePoints (c : List Int) (H : Nat) (bLow bHigh : Int) (tu : Tuning := { }) :

                                                                                                                    The normalized points of height at most H on y ^ 2 = F(x, z) whose denominator z is 0 or lies in the range of bLow and bHigh (bLow ≤ z, and z ≤ bHigh unless bHigh < 1), each once: the points at infinity, then those that the sieve finds (only such points in any case, and all of them assuming Sturm's theorem, on which the region of the numerators rests).

                                                                                                                    Equations
                                                                                                                    • One or more equations did not get rendered due to their size.
                                                                                                                    Instances For
                                                                                                                      def Ratpoints.Fast.reversed (c : List Int) (H : Nat) (bLow bHigh : Int) :

                                                                                                                      The coefficients to search with and whether they are reversed: the reversed coefficients when the reversal check of ratpoints (Ratpoints.Setup.reversal) reverses them and no range of the denominators restricts the search (bLow ≤ 1, and bHigh < 1 or H ≤ bHigh); otherwise, the refusal of the check included, c itself.

                                                                                                                      Equations
                                                                                                                      • One or more equations did not get rendered due to their size.
                                                                                                                      Instances For
                                                                                                                        def Ratpoints.Fast.fastPoints (c : List Int) (H : Nat) (bLow bHigh : Int) (tu : Tuning := { }) :

                                                                                                                        The normalized points of height at most H on y ^ 2 = F(x, z) whose denominator z is 0 or lies in the range of bLow and bHigh (bLow ≤ z, and z ≤ bHigh unless bHigh < 1), each once: those of Ratpoints.Fast.sievePoints, on the coefficients reversed as ratpoints reverses them when no range of the denominators is given (Ratpoints.Fast.reversed; the binary form with x and z exchanged), with the points mapped back by Ratpoints.Sieve.swapPoint (only such points in any case, and all of them assuming Sturm's theorem, on which the region of the numerators rests).

                                                                                                                        Equations
                                                                                                                        • One or more equations did not get rendered due to their size.
                                                                                                                        Instances For