From 1222a44ad62e577af2f1af6f7aee831e89793965 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 28 Apr 2022 18:30:20 +0100 Subject: [PATCH] math/pgen.c: Have `steps' and `tests' count down as documented. 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 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/math/pgen.c b/math/pgen.c index f10c163e..f90a3c78 100644 --- a/math/pgen.c +++ b/math/pgen.c @@ -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; } -- 2.11.0