From cfa3f8b1929b6f954582d3820b246587878e728d Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 29 Feb 2020 06:47:56 +0000 Subject: [PATCH] PrimeCandidateSource: two extra query functions. pcs_get_upper_bound lets the holder of a PrimeCandidateSource ask what is the largest value it might ever generate. pcs_get_bits_remaining lets it ask how much extra entropy it's going to generate on top of the requirements that have already been input into it. Both of these will be needed by the upcoming provable-prime work to decide what sizes of subsidiary prime to generate. --- primecandidate.c | 15 +++++++++++++++ sshkeygen.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/primecandidate.c b/primecandidate.c index b7620fec..1f73307a 100644 --- a/primecandidate.c +++ b/primecandidate.c @@ -381,6 +381,21 @@ unsigned pcs_get_bits(PrimeCandidateSource *pcs) return pcs->bits; } +unsigned pcs_get_bits_remaining(PrimeCandidateSource *pcs) +{ + return mp_get_nbits(pcs->limit); +} + +mp_int *pcs_get_upper_bound(PrimeCandidateSource *pcs) +{ + /* Compute (limit-1) * factor + addend */ + mp_int *tmp = mp_mul(pcs->limit, pcs->factor); + mp_int *bound = mp_add(tmp, pcs->addend); + mp_free(tmp); + mp_sub_into(bound, bound, pcs->factor); + return bound; +} + mp_int **pcs_get_known_prime_factors(PrimeCandidateSource *pcs, size_t *nout) { *nout = pcs->nkps; diff --git a/sshkeygen.h b/sshkeygen.h index 6a9594f2..f8f3159f 100644 --- a/sshkeygen.h +++ b/sshkeygen.h @@ -70,6 +70,8 @@ void pcs_inspect(PrimeCandidateSource *pcs, mp_int **limit_out, /* Query functions for primegen to use */ unsigned pcs_get_bits(PrimeCandidateSource *pcs); +unsigned pcs_get_bits_remaining(PrimeCandidateSource *pcs); +mp_int *pcs_get_upper_bound(PrimeCandidateSource *pcs); mp_int **pcs_get_known_prime_factors(PrimeCandidateSource *pcs, size_t *nout); /* ----------------------------------------------------------------------