diff --git a/ChangeLog b/ChangeLog index 9a4372b67dfe099c0246acc7a49900bb98afa125..1e9876e99e2dcc9471e653da428b1a804356062b 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 754a849b5ec3b19bddeae70c24feccc80bc009c1..013c4cfa0dd60a8af3d12604538665749570fd4e 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 69663de695251e31004df83b69d33c3aa2ab172a..f9e149ada64b92b18ede13aeb81e39d332422aeb 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);