summaryrefslogtreecommitdiff
path: root/bootstrap.c
blob: 07eb416e382caceabbdd4b1a3115ed1d5a2c3d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Functions needed for bootstrapping the gmp build, based on mini-gmp.

Copyright 2001, 2002, 2004, 2011, 2012, 2015 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */


#define MINI_GMP_DONT_USE_FLOAT_H 1
#include "mini-gmp/mini-gmp.c"

#define MIN(l,o) ((l) < (o) ? (l) : (o))
#define PTR(x)   ((x)->_mp_d)
#define SIZ(x)   ((x)->_mp_size)

#define xmalloc gmp_default_alloc

int
isprime (unsigned long int t)
{
  unsigned long int q, r, d;

  if (t < 32)
    return (0xa08a28acUL >> t) & 1;
  if ((t & 1) == 0)
    return 0;

  if (t % 3 == 0)
    return 0;
  if (t % 5 == 0)
    return 0;
  if (t % 7 == 0)
    return 0;

  for (d = 11;;)
    {
      q = t / d;
      r = t - q * d;
      if (q < d)
	return 1;
      if (r == 0)
	break;
      d += 2;
      q = t / d;
      r = t - q * d;
      if (q < d)
	return 1;
      if (r == 0)
	break;
      d += 4;
    }
  return 0;
}

int
log2_ceil (int n)
{
  int  e;
  assert (n >= 1);
  for (e = 0; ; e++)
    if ((1 << e) >= n)
      break;
  return e;
}

/* Set inv to the inverse of d, in the style of invert_limb, ie. for
   udiv_qrnnd_preinv.  */
void
mpz_preinv_invert (mpz_t inv, const mpz_t d, int numb_bits)
{
  mpz_t  t;
  int    norm;
  assert (SIZ(d) > 0);

  norm = numb_bits - mpz_sizeinbase (d, 2);
  assert (norm >= 0);
  mpz_init (t);
  mpz_setbit (t, 2*numb_bits - norm);
  mpz_tdiv_q (inv, t, d);
  mpz_clrbit (inv, numb_bits);

  mpz_clear (t);
}

/* Calculate r satisfying r*a == 1 mod 2^n. */
void
mpz_invert_2exp (mpz_t r, const mpz_t a, unsigned long n)
{
  mpz_t  mod;

  assert (mpz_odd_p (a));

  mpz_init (mod);
  mpz_setbit (mod, n);

  mpz_invert (r, a, mod);

  mpz_clear (mod);
}

/* Calculate inv satisfying r*a == 1 mod 2^n. */
void
mpz_invert_ui_2exp (mpz_t r, unsigned long a, unsigned long n)
{
  mpz_t  az;

  mpz_init_set_ui (az, a);
  mpz_invert_2exp (r, az, n);
  mpz_clear (az);
}