From d5af33da533c54342006b6f4d96c5455bb7ac2e5 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 15 Apr 2022 17:18:23 +0100 Subject: [PATCH] Utility function mp_resize. This reallocs an existing mp_int to have a different physical size, e.g. to make sure there's enough space at the top of it. Trivial, but I'm a little surprised I haven't needed it until now! --- crypto/mpint.c | 8 ++++++++ mpint.h | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/crypto/mpint.c b/crypto/mpint.c index 544a18b5..437c7e8c 100644 --- a/crypto/mpint.c +++ b/crypto/mpint.c @@ -82,6 +82,14 @@ mp_int *mp_new(size_t maxbits) return mp_make_sized(words); } +mp_int *mp_resize(mp_int *mp, size_t newmaxbits) +{ + mp_int *copy = mp_new(newmaxbits); + mp_copy_into(copy, mp); + mp_free(mp); + return copy; +} + mp_int *mp_from_integer(uintmax_t n) { mp_int *x = mp_make_sized( diff --git a/mpint.h b/mpint.h index ae09a24f..4ddc0e64 100644 --- a/mpint.h +++ b/mpint.h @@ -42,6 +42,13 @@ mp_int *mp_new(size_t maxbits); void mp_free(mp_int *); void mp_clear(mp_int *x); +/* + * Resize the physical size of existing mp_int, e.g. so that you have + * room to transform it in place to a larger value. Destroys the old + * mp_int in the process. + */ +mp_int *mp_resize(mp_int *, size_t newmaxbits); + /* * Create mp_ints from various sources: little- and big-endian binary * data, an ordinary C unsigned integer type, a decimal or hex string