Coverage Report

Created: 2025-03-18 06:55

/src/nettle/bignum.c
Line
Count
Source (jump to first uncovered line)
1
/* bignum.c
2
3
   Bignum operations that are missing from gmp.
4
5
   Copyright (C) 2001 Niels Möller
6
7
   This file is part of GNU Nettle.
8
9
   GNU Nettle is free software: you can redistribute it and/or
10
   modify it under the terms of either:
11
12
     * the GNU Lesser General Public License as published by the Free
13
       Software Foundation; either version 3 of the License, or (at your
14
       option) any later version.
15
16
   or
17
18
     * the GNU General Public License as published by the Free
19
       Software Foundation; either version 2 of the License, or (at your
20
       option) any later version.
21
22
   or both in parallel, as here.
23
24
   GNU Nettle is distributed in the hope that it will be useful,
25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
   General Public License for more details.
28
29
   You should have received copies of the GNU General Public License and
30
   the GNU Lesser General Public License along with this program.  If
31
   not, see http://www.gnu.org/licenses/.
32
*/
33
34
#if HAVE_CONFIG_H
35
# include "config.h"
36
#endif
37
38
#include <assert.h>
39
#include <string.h>
40
41
#include "bignum.h"
42
43
/* Two's complement negation means that -x = ~x + 1, ~x = -(x+1),
44
 * and we use that x = ~~x = ~(-x-1).
45
 *
46
 * Examples:
47
 *
48
 *   x  ~x = -x+1     ~~x = x
49
 *  -1          0          ff
50
 *  -2          1          fe
51
 * -7f         7e          81
52
 * -80         7f          80
53
 * -81         80        ff7f
54
 */
55
56
/* Including extra sign bit, if needed. Also one byte for zero. */
57
size_t
58
nettle_mpz_sizeinbase_256_s(const mpz_t x)
59
0
{
60
0
  if (mpz_sgn(x) >= 0)
61
0
    return 1 + mpz_sizeinbase(x, 2) / 8;
62
0
  else
63
0
    {
64
      /* We'll output ~~x, so we need as many bits as for ~x */
65
0
      size_t size;
66
0
      mpz_t c;
67
68
0
      mpz_init(c);
69
0
      mpz_com(c, x); /* Same as c = - x - 1 = |x| + 1 */
70
0
      size = 1 + mpz_sizeinbase(c,2) / 8;
71
0
      mpz_clear(c);
72
73
0
      return size;
74
0
    }
75
0
}
76
77
size_t
78
nettle_mpz_sizeinbase_256_u(const mpz_t x)
79
0
{
80
0
  return (mpz_sizeinbase(x,2) + 7) / 8;
81
0
}
82
83
static void
84
nettle_mpz_to_octets(size_t length, uint8_t *s,
85
         const mpz_t x, uint8_t sign)
86
0
{
87
0
  uint8_t *dst = s + length - 1;
88
0
  size_t size = mpz_size(x);
89
0
  size_t i;
90
  
91
0
  for (i = 0; i<size; i++)
92
0
    {
93
0
      mp_limb_t limb = mpz_getlimbn(x, i);
94
0
      size_t j;
95
96
0
      for (j = 0; length && j < sizeof(mp_limb_t); j++)
97
0
        {
98
0
          *dst-- = sign ^ (limb & 0xff);
99
0
          limb >>= 8;
100
0
          length--;
101
0
  }
102
0
    }
103
  
104
0
  if (length)
105
0
    memset(s, sign, length);
106
0
}
107
108
void
109
nettle_mpz_get_str_256(size_t length, uint8_t *s, const mpz_t x)
110
0
{
111
0
  if (!length)
112
0
    {
113
      /* x must be zero */
114
0
      assert(!mpz_sgn(x));
115
0
      return;
116
0
    }
117
118
0
  if (mpz_sgn(x) >= 0)
119
0
    {
120
0
      assert(nettle_mpz_sizeinbase_256_u(x) <= length);
121
0
      nettle_mpz_to_octets(length, s, x, 0);
122
0
    }
123
0
  else
124
0
    {
125
0
      mpz_t c;
126
0
      mpz_init(c);
127
0
      mpz_com(c, x);
128
129
0
      assert(nettle_mpz_sizeinbase_256_u(c) <= length);
130
0
      nettle_mpz_to_octets(length, s, c, 0xff);
131
132
0
      mpz_clear(c);
133
0
    }
134
0
}
135
136
/* Converting from strings */
137
138
/* mpz_import was introduced in GMP-4.1 */
139
#define nettle_mpz_from_octets(x, length, s) \
140
0
   mpz_import((x), (length), 1, 1, 0, 0, (s))
141
142
void
143
nettle_mpz_set_str_256_u(mpz_t x,
144
       size_t length, const uint8_t *s)
145
0
{
146
0
  nettle_mpz_from_octets(x, length, s);
147
0
}
148
149
void
150
nettle_mpz_init_set_str_256_u(mpz_t x,
151
            size_t length, const uint8_t *s)
152
0
{
153
0
  mpz_init(x);
154
0
  nettle_mpz_from_octets(x, length, s);
155
0
}
156
157
void
158
nettle_mpz_set_str_256_s(mpz_t x,
159
       size_t length, const uint8_t *s)
160
0
{
161
0
  if (!length)
162
0
    {
163
0
      mpz_set_ui(x, 0);
164
0
      return;
165
0
    }
166
  
167
0
  nettle_mpz_from_octets(x, length, s);
168
169
0
  if (s[0] & 0x80)
170
0
    {
171
0
      mpz_t t;
172
173
0
      mpz_init_set_ui(t, 1);
174
0
      mpz_mul_2exp(t, t, length*8);
175
0
      mpz_sub(x, x, t);
176
0
      mpz_clear(t);
177
0
    }
178
0
}
179
180
void
181
nettle_mpz_init_set_str_256_s(mpz_t x,
182
            size_t length, const uint8_t *s)
183
0
{
184
0
  mpz_init(x);
185
0
  nettle_mpz_set_str_256_s(x, length, s);
186
0
}