From d4a4111fec6a8fb5f1b9f9967bda7999950af6c3 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 21 Feb 2020 19:51:31 +0000 Subject: [PATCH] mpint: add mp_lshift_fixed(). This is a version of mp_lshift_fixed_into() which allocates the output number, which it can do because you know the size of the original number and are allowed to treat the shift count as non-secret. (By contrast, mp_lshift_safe() would be a nonsensical function - if you're trying to keep the shift count secret, you _can't_ use it as a parameter of memory allocation! In that situation you have no choice but to allocate memory based on a fixed upper bound.) --- mpint.c | 8 ++++++++ mpint.h | 1 + 2 files changed, 9 insertions(+) diff --git a/mpint.c b/mpint.c index cb10bd5f..b59d8464 100644 --- a/mpint.c +++ b/mpint.c @@ -1115,6 +1115,14 @@ void mp_rshift_fixed_into(mp_int *r, mp_int *a, size_t bits) } } +mp_int *mp_lshift_fixed(mp_int *x, size_t bits) +{ + size_t words = (bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS; + mp_int *r = mp_make_sized(x->nw + words); + mp_lshift_fixed_into(r, x, bits); + return r; +} + mp_int *mp_rshift_fixed(mp_int *x, size_t bits) { size_t words = bits / BIGNUM_INT_BITS; diff --git a/mpint.h b/mpint.h index 13ac9a51..0ed46b12 100644 --- a/mpint.h +++ b/mpint.h @@ -383,6 +383,7 @@ mp_int *mp_rshift_safe(mp_int *x, size_t shift); */ void mp_lshift_fixed_into(mp_int *r, mp_int *a, size_t shift); void mp_rshift_fixed_into(mp_int *r, mp_int *x, size_t shift); +mp_int *mp_lshift_fixed(mp_int *x, size_t shift); mp_int *mp_rshift_fixed(mp_int *x, size_t shift); /*