Line data Source code
1 : /* gmp-glue.c 2 : 3 : Copyright (C) 2013 Niels Möller 4 : Copyright (C) 2013 Red Hat 5 : 6 : This file is part of GNU Nettle. 7 : 8 : GNU Nettle is free software: you can redistribute it and/or 9 : modify it under the terms of either: 10 : 11 : * the GNU Lesser General Public License as published by the Free 12 : Software Foundation; either version 3 of the License, or (at your 13 : option) any later version. 14 : 15 : or 16 : 17 : * the GNU General Public License as published by the Free 18 : Software Foundation; either version 2 of the License, or (at your 19 : option) any later version. 20 : 21 : or both in parallel, as here. 22 : 23 : GNU Nettle is distributed in the hope that it will be useful, 24 : but WITHOUT ANY WARRANTY; without even the implied warranty of 25 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 26 : General Public License for more details. 27 : 28 : You should have received copies of the GNU General Public License and 29 : the GNU Lesser General Public License along with this program. If 30 : not, see http://www.gnu.org/licenses/. 31 : */ 32 : 33 : #if HAVE_CONFIG_H 34 : # include "config.h" 35 : #endif 36 : 37 : #include "mpn-base256.h" 38 : 39 : void 40 29 : mpn_set_base256 (mp_limb_t *rp, mp_size_t rn, 41 : const uint8_t *xp, size_t xn) 42 : { 43 29 : size_t xi; 44 29 : mp_limb_t out; 45 29 : unsigned bits; 46 1125 : for (xi = xn, out = bits = 0; xi > 0 && rn > 0; ) 47 : { 48 1096 : mp_limb_t in = xp[--xi]; 49 1096 : out |= (in << bits) & GMP_NUMB_MASK; 50 1096 : bits += 8; 51 1096 : if (bits >= GMP_NUMB_BITS) 52 : { 53 133 : *rp++ = out; 54 133 : rn--; 55 : 56 133 : bits -= GMP_NUMB_BITS; 57 133 : out = in >> (8 - bits); 58 : } 59 : } 60 29 : if (rn > 0) 61 : { 62 9 : *rp++ = out; 63 9 : if (--rn > 0) 64 0 : mpn_zero (rp, rn); 65 : } 66 29 : } 67 : 68 : void 69 26 : mpn_get_base256 (uint8_t *rp, size_t rn, 70 : const mp_limb_t *xp, mp_size_t xn) 71 : { 72 26 : unsigned bits; 73 26 : mp_limb_t in; 74 884 : for (bits = in = 0; xn > 0 && rn > 0; ) 75 : { 76 858 : if (bits >= 8) 77 : { 78 728 : rp[--rn] = in; 79 728 : in >>= 8; 80 728 : bits -= 8; 81 : } 82 : else 83 : { 84 130 : uint8_t old = in; 85 130 : in = *xp++; 86 130 : xn--; 87 130 : rp[--rn] = old | (in << bits); 88 130 : in >>= (8 - bits); 89 130 : bits += GMP_NUMB_BITS - 8; 90 : } 91 : } 92 160 : while (rn > 0) 93 : { 94 134 : rp[--rn] = in; 95 134 : in >>= 8; 96 : } 97 26 : }