summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2014-08-06 21:56:14 +0200
committerNiels Möller <nisse@lysator.liu.se>2014-08-06 21:56:14 +0200
commit2ac6d737f6100d3694ec0a0ad5c93641fcc6795d (patch)
treec962456ebe663f785b1238fea4e7632e036e7a3c
parentb6c445639015fb8dbe4058006dac7a7affcc7437 (diff)
downloadnettle-2ac6d737f6100d3694ec0a0ad5c93641fcc6795d.tar.gz
New functions mpn_set_base256_le and mpn_get_base256_le.
-rw-r--r--ChangeLog5
-rw-r--r--gmp-glue.c62
-rw-r--r--gmp-glue.h12
3 files changed, 78 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 9a4372b6..1e9876e9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2014-08-06 Niels Möller <nisse@lysator.liu.se>
+
+ * gmp-glue.c (mpn_set_base256_le, mpn_get_base256_le): New functions.
+ * gmp-glue.h: Declare them.
+
2014-08-02 Niels Möller <nisse@lysator.liu.se>
* testsuite/curve25519-dh-test.c (curve25519_sqrt): Fixed memory
diff --git a/gmp-glue.c b/gmp-glue.c
index 754a849b..013c4cfa 100644
--- a/gmp-glue.c
+++ b/gmp-glue.c
@@ -227,6 +227,68 @@ mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
}
}
+void
+mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
+ const uint8_t *xp, size_t xn)
+{
+ size_t xi;
+ mp_limb_t out;
+ unsigned bits;
+ for (xi = 0, out = bits = 0; xi < xn && rn > 0; )
+ {
+ mp_limb_t in = xp[xi++];
+ out |= (in << bits) & GMP_NUMB_MASK;
+ bits += 8;
+ if (bits >= GMP_NUMB_BITS)
+ {
+ *rp++ = out;
+ rn--;
+
+ bits -= GMP_NUMB_BITS;
+ out = in >> (8 - bits);
+ }
+ }
+ if (rn > 0)
+ {
+ *rp++ = out;
+ if (--rn > 0)
+ mpn_zero (rp, rn);
+ }
+}
+
+void
+mpn_get_base256_le (uint8_t *rp, size_t rn,
+ const mp_limb_t *xp, mp_size_t xn)
+{
+ unsigned bits;
+ mp_limb_t in;
+ for (bits = in = 0; xn > 0 && rn > 0; )
+ {
+ if (bits >= 8)
+ {
+ *rp++ = in;
+ rn--;
+ in >>= 8;
+ bits -= 8;
+ }
+ else
+ {
+ uint8_t old = in;
+ in = *xp++;
+ xn--;
+ *rp++ = old | (in << bits);
+ in >>= (8 - bits);
+ bits += GMP_NUMB_BITS - 8;
+ }
+ }
+ while (rn > 0)
+ {
+ *rp++ = in;
+ rn--;
+ in >>= 8;
+ }
+}
+
mp_limb_t *
gmp_alloc_limbs (mp_size_t n)
{
diff --git a/gmp-glue.h b/gmp-glue.h
index 69663de6..f9e149ad 100644
--- a/gmp-glue.h
+++ b/gmp-glue.h
@@ -71,6 +71,8 @@
#define mpz_limbs_copy _nettle_mpz_limbs_copy
#define mpz_set_n _nettle_mpz_set_n
#define mpn_set_base256 _nettle_mpn_set_base256
+#define mpn_set_base256_le _nettle_mpn_set_base256_le
+#define mpn_get_base256_le _nettle_mpn_get_base256_le
#define gmp_alloc_limbs _nettle_gmp_alloc_limbs
#define gmp_free_limbs _nettle_gmp_free_limbs
#define gmp_free _nettle_gmp_free
@@ -153,7 +155,7 @@ mpz_limbs_read_n (mpz_ptr x, mp_size_t n);
/* Copy limbs, with zero-padding. */
/* FIXME: Reorder arguments, on the theory that the first argument of
- an _mpz_* fucntion should be an mpz_t? Or rename to _mpz_get_limbs,
+ an _mpz_* function should be an mpz_t? Or rename to _mpz_get_limbs,
with argument order consistent with mpz_get_*. */
void
mpz_limbs_copy (mp_limb_t *xp, mpz_srcptr x, mp_size_t n);
@@ -167,6 +169,14 @@ void
mpn_set_base256 (mp_limb_t *rp, mp_size_t rn,
const uint8_t *xp, size_t xn);
+void
+mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn,
+ const uint8_t *xp, size_t xn);
+
+void
+mpn_get_base256_le (uint8_t *rp, size_t rn,
+ const mp_limb_t *xp, mp_size_t xn);
+
mp_limb_t *
gmp_alloc_limbs (mp_size_t n);