Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpz/urandomm.c
Line
Count
Source (jump to first uncovered line)
1
/* mpz_urandomm (rop, state, n) -- Generate a uniform pseudorandom
2
   integer in the range 0 to N-1, using STATE as the random state
3
   previously initialized by a call to gmp_randinit().
4
5
Copyright 2000, 2002, 2012, 2015 Free Software Foundation, Inc.
6
7
This file is part of the GNU MP Library.
8
9
The GNU MP Library is free software; you can redistribute it and/or modify
10
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 Software
19
    Foundation; either version 2 of the License, or (at your option) any
20
    later version.
21
22
or both in parallel, as here.
23
24
The GNU MP Library is distributed in the hope that it will be useful, but
25
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
for more details.
28
29
You should have received copies of the GNU General Public License and the
30
GNU Lesser General Public License along with the GNU MP Library.  If not,
31
see https://www.gnu.org/licenses/.  */
32
33
#include "gmp-impl.h"
34
#include "longlong.h" /* for count_leading_zeros */
35
36
37
1.01k
#define MAX_URANDOMM_ITER  80
38
39
void
40
mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n)
41
1.02k
{
42
1.02k
  mp_ptr rp, np;
43
1.02k
  mp_size_t nbits, size;
44
1.02k
  mp_limb_t nh;
45
1.02k
  int count;
46
1.02k
  int pow2;
47
1.02k
  int cmp;
48
1.02k
  TMP_DECL;
49
50
1.02k
  size = ABSIZ (n);
51
1.02k
  if (UNLIKELY (size == 0))
52
0
    DIVIDE_BY_ZERO;
53
54
1.02k
  np = PTR (n);
55
1.02k
  nh = np[size - 1];
56
57
  /* Detect whether n is a power of 2.  */
58
1.02k
  pow2 = POW2_P (nh) && (size == 1 || mpn_zero_p (np, size - 1));
59
60
1.02k
  count_leading_zeros (count, nh);
61
1.02k
  nbits = size * GMP_NUMB_BITS - (count - GMP_NAIL_BITS) - pow2;
62
1.02k
  if (nbits == 0)    /* nbits == 0 means that n was == 1.  */
63
1
    {
64
1
      SIZ (rop) = 0;
65
1
      return;
66
1
    }
67
68
1.01k
  TMP_MARK;
69
1.01k
  if (rop == n)
70
0
    {
71
0
      mp_ptr tp;
72
0
      tp = TMP_ALLOC_LIMBS (size);
73
0
      MPN_COPY (tp, np, size);
74
0
      np = tp;
75
0
    }
76
77
  /* Here the allocated size can be one too much if n is a power of
78
     (2^GMP_NUMB_BITS) but it's convenient for using mpn_cmp below.  */
79
1.01k
  rp = MPZ_NEWALLOC (rop, size);
80
  /* Clear last limb to prevent the case in which size is one too much.  */
81
1.01k
  rp[size - 1] = 0;
82
83
1.01k
  count = MAX_URANDOMM_ITER; /* Set iteration count limit.  */
84
1.01k
  do
85
1.91k
    {
86
1.91k
      _gmp_rand (rp, rstate, nbits);
87
1.91k
      MPN_CMP (cmp, rp, np, size);
88
1.91k
    }
89
1.91k
  while (cmp >= 0 && --count != 0);
90
91
1.01k
  if (count == 0)
92
    /* Too many iterations; return result mod n == result - n */
93
0
    mpn_sub_n (rp, rp, np, size);
94
95
1.01k
  MPN_NORMALIZE (rp, size);
96
1.01k
  SIZ (rop) = size;
97
1.01k
  TMP_FREE;
98
1.01k
}