Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 56 lines (47 sloc) 1.185 kb
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_RAND_C
57354e1 added libtommath-0.12
Tom St Denis authored
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored
5 * LibTomMath is a library that provides multiple-precision
57354e1 added libtommath-0.12
Tom St Denis authored
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored
8 * The library was designed directly after the MPI library by
57354e1 added libtommath-0.12
Tom St Denis authored
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
333aebc added libtommath-0.41
Tom St Denis authored
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e1 added libtommath-0.12
Tom St Denis authored
16 */
17
18 /* makes a pseudo-random int of a given size */
19 int
20 mp_rand (mp_int * a, int digits)
21 {
b66471f added libtommath-0.13
Tom St Denis authored
22 int res;
23 mp_digit d;
57354e1 added libtommath-0.12
Tom St Denis authored
24
25 mp_zero (a);
26 if (digits <= 0) {
27 return MP_OKAY;
28 }
29
30 /* first place a random non-zero digit */
b66471f added libtommath-0.13
Tom St Denis authored
31 do {
fdfa2f4 added libtommath-0.35
Tom St Denis authored
32 d = ((mp_digit) abs (rand ())) & MP_MASK;
b66471f added libtommath-0.13
Tom St Denis authored
33 } while (d == 0);
57354e1 added libtommath-0.12
Tom St Denis authored
34
35 if ((res = mp_add_d (a, d, a)) != MP_OKAY) {
36 return res;
37 }
38
fdfa2f4 added libtommath-0.35
Tom St Denis authored
39 while (--digits > 0) {
b66471f added libtommath-0.13
Tom St Denis authored
40 if ((res = mp_lshd (a, 1)) != MP_OKAY) {
57354e1 added libtommath-0.12
Tom St Denis authored
41 return res;
42 }
b66471f added libtommath-0.13
Tom St Denis authored
43
44 if ((res = mp_add_d (a, ((mp_digit) abs (rand ())), a)) != MP_OKAY) {
57354e1 added libtommath-0.12
Tom St Denis authored
45 return res;
46 }
47 }
48
49 return MP_OKAY;
50 }
e549ccf added libtommath-0.32
Tom St Denis authored
51 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
52
53 /* $Source$ */
54 /* $Revision$ */
55 /* $Date$ */
Something went wrong with that request. Please try again.