From 96946f633a839757e3a40e0f9020be722e4a0c42 Mon Sep 17 00:00:00 2001
From: Andreas Enge <andreas.enge@inria.fr>
Date: Fri, 24 Feb 2023 13:08:09 +0100
Subject: [PATCH] Correct logic in class polynomial factoring.

* lib/pari.c (mpzx_oneroot_split_mod): Make sure that factors of factors
  are also read by only treating one factor per loop round.
---
 lib/pari.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/lib/pari.c b/lib/pari.c
index e7f7e6f..ec26772 100644
--- a/lib/pari.c
+++ b/lib/pari.c
@@ -524,7 +524,7 @@ void mpzx_oneroot_split_mod (mpz_ptr root, mpzx_srcptr f, mpz_srcptr p,
    /* Compute in root a root of the polynomial f over the prime field
       of characteristic p, assuming that f splits completely. */
 {
-   mpzx_t F, factor, factorloc;
+   mpzx_t F, factor;
    mpz_t inv;
    int i;
    cm_timer_t clock;
@@ -548,22 +548,18 @@ void mpzx_oneroot_split_mod (mpz_ptr root, mpzx_srcptr f, mpz_srcptr p,
 
    while (F->deg != 1) {
       /* Try to read a factor of F from a checkpointing file. */
-      if (tmpdir != NULL) {
-         mpzx_init (factorloc, -1);
-         if (cm_file_read_factor (tmpdir, factorloc, F, p)) {
-            mpzx_set (F, factorloc);
-            if (debug)
-               cm_file_printf ("    Read factor of degree %i\n", F->deg);
-         }
-         mpzx_clear (factorloc);
+      if (tmpdir != NULL && cm_file_read_factor (tmpdir, factor, F, p)) {
+         if (debug)
+            cm_file_printf ("    Read factor of degree %i\n", factor->deg);
       }
+      else {
+         /* Find a factor. */
+         mpzx_onefactor_split_mod (factor, F, p, debug);
 
-      /* Find a smaller factor. */
-      mpzx_onefactor_split_mod (factor, F, p, debug);
-
-      /* Write the factor to a checkpointing file. */
-      if (tmpdir != NULL)
-         cm_file_write_factor (tmpdir, factor, F, p);
+         /* Write the factor to a checkpointing file. */
+         if (tmpdir != NULL)
+            cm_file_write_factor (tmpdir, factor, F, p);
+      }
 
       /* Replace F by the factor. */
       mpzx_set (F, factor);
-- 
GitLab