1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-02 20:12:48 -05:00

Introduce a vtable system for prime generation.

The functions primegen() and primegen_add_progress_phase() are gone.
In their place is a small vtable system with two methods corresponding
to them, plus the usual admin of allocating and freeing contexts.

This API change is the starting point for being able to drop in
different prime generation algorithms at run time in response to user
configuration.
This commit is contained in:
Simon Tatham
2020-02-29 09:10:47 +00:00
parent 08a3547bc5
commit ece788240c
11 changed files with 163 additions and 61 deletions

View File

@ -10,9 +10,8 @@
#include "mpunsafe.h"
#include "sshkeygen.h"
/*
* This prime generation algorithm is pretty much cribbed from
* OpenSSL. The algorithm is:
/* ----------------------------------------------------------------------
* Standard probabilistic prime-generation algorithm:
*
* - invent a B-bit random number and ensure the top and bottom
* bits are set (so it's definitely B-bit, and it's definitely
@ -28,8 +27,22 @@
* - go back to square one if any M-R test fails.
*/
ProgressPhase primegen_add_progress_phase(ProgressReceiver *prog,
unsigned bits)
static PrimeGenerationContext *probprime_new_context(
const PrimeGenerationPolicy *policy)
{
PrimeGenerationContext *ctx = snew(PrimeGenerationContext);
ctx->vt = policy;
return ctx;
}
static void probprime_free_context(PrimeGenerationContext *ctx)
{
sfree(ctx);
}
static ProgressPhase probprime_add_progress_phase(
const PrimeGenerationPolicy *policy,
ProgressReceiver *prog, unsigned bits)
{
/*
* The density of primes near x is 1/(log x). When x is about 2^b,
@ -56,7 +69,9 @@ ProgressPhase primegen_add_progress_phase(ProgressReceiver *prog,
return progress_add_probabilistic(prog, cost, prob);
}
mp_int *primegen(PrimeCandidateSource *pcs, ProgressReceiver *prog)
static mp_int *probprime_generate(
PrimeGenerationContext *ctx,
PrimeCandidateSource *pcs, ProgressReceiver *prog)
{
pcs_ready(pcs);
@ -88,6 +103,12 @@ mp_int *primegen(PrimeCandidateSource *pcs, ProgressReceiver *prog)
}
}
const PrimeGenerationPolicy primegen_probabilistic = {
probprime_add_progress_phase,
probprime_new_context,
probprime_free_context,
probprime_generate,
};
/* ----------------------------------------------------------------------
* Reusable null implementation of the progress-reporting API.