From 20a9912c7c646fe12c175050e13f52ef55c487fa Mon Sep 17 00:00:00 2001
From: Simon Tatham <anakin@pobox.com>
Date: Wed, 19 Feb 2020 19:12:32 +0000
Subject: [PATCH] Add mp_copy_integer_into function.

Even simpler than the existing mp_add_integer_into.
---
 mpint.c            | 8 ++++++++
 mpint.h            | 3 ++-
 test/cryptsuite.py | 6 ++++--
 testcrypt.h        | 1 +
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/mpint.c b/mpint.c
index ddd214114..1c464c446 100644
--- a/mpint.c
+++ b/mpint.c
@@ -90,6 +90,14 @@ void mp_copy_into(mp_int *dest, mp_int *src)
     smemclr(dest->w + copy_nw, (dest->nw - copy_nw) * sizeof(BignumInt));
 }
 
+void mp_copy_integer_into(mp_int *r, uintmax_t n)
+{
+    for (size_t i = 0; i < r->nw; i++) {
+        r->w[i] = (BignumInt)n;
+        n = (BIGNUM_INT_BYTES < sizeof(n)) ? n >> BIGNUM_INT_BITS : 0;
+    }
+}
+
 /*
  * Conditional selection is done by negating 'which', to give a mask
  * word which is all 1s if which==1 and all 0s if which==0. Then you
diff --git a/mpint.h b/mpint.h
index bcbe442e1..5611a0074 100644
--- a/mpint.h
+++ b/mpint.h
@@ -176,9 +176,10 @@ mp_int *mp_max(mp_int *x, mp_int *y);
 void mp_dump(FILE *fp, const char *prefix, mp_int *x, const char *suffix);
 
 /*
- * Overwrite one mp_int with another.
+ * Overwrite one mp_int with another, or with a plain integer.
  */
 void mp_copy_into(mp_int *dest, mp_int *src);
+void mp_copy_integer_into(mp_int *dest, uintmax_t n);
 
 /*
  * Conditional selection. Overwrites dest with either src0 or src1,
diff --git a/test/cryptsuite.py b/test/cryptsuite.py
index c48a71fc6..190324efe 100755
--- a/test/cryptsuite.py
+++ b/test/cryptsuite.py
@@ -349,12 +349,14 @@ class mpint(MyTestBase):
 
         x = mp_new(mp_max_bits(initial) + 64)
 
-        # mp_{add,sub}_integer_into should be able to cope with any
-        # uintmax_t. Test a number that requires more than 32 bits.
+        # mp_{add,sub,copy}_integer_into should be able to cope with
+        # any uintmax_t. Test a number that requires more than 32 bits.
         mp_add_integer_into(x, initial, 123123123123123)
         self.assertEqual(int(x), 4444444444567567567567567)
         mp_sub_integer_into(x, initial, 123123123123123)
         self.assertEqual(int(x), 4444444444321321321321321)
+        mp_copy_integer_into(x, 123123123123123)
+        self.assertEqual(int(x), 123123123123123)
 
         # mp_mul_integer_into only takes a uint16_t integer input
         mp_mul_integer_into(x, initial, 10001)
diff --git a/testcrypt.h b/testcrypt.h
index 3eddc9bad..1eca3759c 100644
--- a/testcrypt.h
+++ b/testcrypt.h
@@ -41,6 +41,7 @@ FUNC3(void, mp_and_into, val_mpint, val_mpint, val_mpint)
 FUNC3(void, mp_or_into, val_mpint, val_mpint, val_mpint)
 FUNC3(void, mp_xor_into, val_mpint, val_mpint, val_mpint)
 FUNC3(void, mp_bic_into, val_mpint, val_mpint, val_mpint)
+FUNC2(void, mp_copy_integer_into, val_mpint, uint)
 FUNC3(void, mp_add_integer_into, val_mpint, val_mpint, uint)
 FUNC3(void, mp_sub_integer_into, val_mpint, val_mpint, uint)
 FUNC3(void, mp_mul_integer_into, val_mpint, val_mpint, uint)
-- 
GitLab