3
* bignum operations that are missing from gmp.
6
/* nettle, low-level cryptographics library
8
* Copyright (C) 2001 Niels M�ller
10
* The nettle library is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU Lesser General Public License as published by
12
* the Free Software Foundation; either version 2.1 of the License, or (at your
13
* option) any later version.
15
* The nettle library is distributed in the hope that it will be useful, but
16
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18
* License for more details.
20
* You should have received a copy of the GNU Lesser General Public License
21
* along with the nettle library; see the file COPYING.LIB. If not, write to
22
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37
/* Two's complement negation means that -x = ~x + 1, ~x = -(x+1),
38
* and we use that x = ~~x = ~(-x-1).
50
/* Including extra sign bit, if needed. Also one byte for zero. */
52
nettle_mpz_sizeinbase_256_s(const mpz_t x)
55
return 1 + mpz_sizeinbase(x, 2) / 8;
58
/* We'll output ~~x, so we need as many bits as for ~x */
63
mpz_com(c, x); /* Same as c = - x - 1 = |x| + 1 */
64
size = 1 + mpz_sizeinbase(c,2) / 8;
72
nettle_mpz_sizeinbase_256_u(const mpz_t x)
74
return (mpz_sizeinbase(x,2) + 7) / 8;
78
nettle_mpz_to_octets(unsigned length, uint8_t *s,
79
const mpz_t x, uint8_t sign)
81
uint8_t *dst = s + length - 1;
82
unsigned size = mpz_size(x);
85
for (i = 0; i<size; i++)
87
mp_limb_t limb = mpz_getlimbn(x, i);
90
for (j = 0; length && j < sizeof(mp_limb_t); j++)
92
*dst-- = sign ^ (limb & 0xff);
99
memset(s, sign, length);
103
nettle_mpz_get_str_256(unsigned length, uint8_t *s, const mpz_t x)
114
assert(nettle_mpz_sizeinbase_256_u(x) <= length);
115
nettle_mpz_to_octets(length, s, x, 0);
123
/* FIXME: A different trick is to complement all the limbs of c
124
* now. That way, nettle_mpz_to_octets need not complement each
126
assert(nettle_mpz_sizeinbase_256_u(c) <= length);
127
nettle_mpz_to_octets(length, s, c, 0xff);
133
/* Converting from strings */
135
nettle_mpz_from_octets(mpz_t x,
136
unsigned length, const uint8_t *s,
144
for (i = 0; i < length; i++)
146
mpz_set_ui(digit, sign ^ s[i]);
147
mpz_mul_2exp(digit, digit, (length - i - 1) * 8);
148
mpz_ior(x, x, digit);
154
nettle_mpz_set_str_256_u(mpz_t x,
155
unsigned length, const uint8_t *s)
158
nettle_mpz_from_octets(x, length, s, 0);
162
nettle_mpz_init_set_str_256_u(mpz_t x,
163
unsigned length, const uint8_t *s)
165
mpz_init_set_ui(x, 0);
166
nettle_mpz_from_octets(x, length, s, 0);
170
nettle_mpz_set_str_256_s(mpz_t x,
171
unsigned length, const uint8_t *s)
180
nettle_mpz_from_octets(x, length, s, 0xff);
184
nettle_mpz_from_octets(x, length, s, 0);
188
nettle_mpz_init_set_str_256_s(mpz_t x,
189
unsigned length, const uint8_t *s)
192
nettle_mpz_set_str_256_s(x, length, s);
195
#endif /* HAVE_LIBGMP */