1 /* $NetBSD: bn_mp_rand.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */
2
3 #include <tommath.h>
4 #ifdef BN_MP_RAND_C
5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
6 *
7 * LibTomMath is a library that provides multiple-precision
8 * integer arithmetic as well as number theoretic functionality.
9 *
10 * The library was designed directly after the MPI library by
11 * Michael Fromberger but has been written from scratch with
12 * additional optimizations in place.
13 *
14 * The library is free for all purposes without any express
15 * guarantee it works.
16 *
17 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18 */
19
20 /* makes a pseudo-random int of a given size */
21 int
mp_rand(mp_int * a,int digits)22 mp_rand (mp_int * a, int digits)
23 {
24 int res;
25 mp_digit d;
26
27 mp_zero (a);
28 if (digits <= 0) {
29 return MP_OKAY;
30 }
31
32 /* first place a random non-zero digit */
33 do {
34 d = ((mp_digit) labs (rand ())) & MP_MASK;
35 } while (d == 0);
36
37 if ((res = mp_add_d (a, d, a)) != MP_OKAY) {
38 return res;
39 }
40
41 while (--digits > 0) {
42 if ((res = mp_lshd (a, 1)) != MP_OKAY) {
43 return res;
44 }
45
46 if ((res = mp_add_d (a, ((mp_digit) labs (rand ())), a)) != MP_OKAY) {
47 return res;
48 }
49 }
50
51 return MP_OKAY;
52 }
53 #endif
54
55 /* Source: /cvs/libtom/libtommath/bn_mp_rand.c,v */
56 /* Revision: 1.4 */
57 /* Date: 2006/12/28 01:25:13 */
58