math/pgen.c: Have `steps' and `tests' count down as documented.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 28 Apr 2022 17:30:20 +0000 (18:30 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 1 May 2022 17:54:55 +0000 (18:54 +0100)
This was how they worked originally, but they were changed a long time
ago to count up from zero instead.  This makes it impossible for a
stepper or tester function to know in advance how many times it will be
invoked, which turns out to be important for a forthcoming change.

The reason it was changed wasn't explained in the commit
(283b9af095a5b24ae71b49a6d2dcbdcdaae47c40) that made it, but I deduce
that it was so that `pgen_test' could identify the first test round and
use the somewhat faster approach of using (the residue whose Montgomery
REDC representative is) 2, rather than a random witness.  That change
has now been undone, so we can restore the documented behaviour.

math/pgen.c

index f10c163..f90a3c7 100644 (file)
@@ -181,8 +181,8 @@ mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
     ev.m = MP_COPY(m);
   else
     ev.m = 0;
-  ev.steps = 0;
-  ev.tests = 0;
+  ev.steps = steps;
+  ev.tests = tests;
   ev.r = fibrand_create(0);
 
   /* --- Tell the event handler we're under way --- */
@@ -258,17 +258,17 @@ mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
     /* --- If decrementing counters is requested, do that --- */
 
     if ((act & A_STEP) && steps) {
-      ev.steps++;
-      if (ev.steps == steps) {
+      ev.steps--;
+      if (!ev.steps) {
        act |= A_EVENT | A_ENDSTEP | A_DONE;
        rc = PGEN_ABORT;
       }
-      ev.tests = 0;
+      ev.tests = tests;
     }
 
     if ((act & A_TEST) && tests) {
-      ev.tests++;
-      if (ev.tests == tests) {
+      ev.tests--;
+      if (!ev.tests) {
        act |= A_ENDTEST | A_ENDSTEP | A_DONE;
        rc = PGEN_DONE;
       }