dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / common / mpi / mpprime.c
blob4f0356dfe1e1a53d37ba1f8c0cea40857d2176a0
1 /*
2 * mpprime.c
4 * Utilities for finding and working with prime and pseudo-prime
5 * integers
7 * ***** BEGIN LICENSE BLOCK *****
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 * The contents of this file are subject to the Mozilla Public License Version
11 * 1.1 (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 * for the specific language governing rights and limitations under the
18 * License.
20 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
22 * The Initial Developer of the Original Code is
23 * Michael J. Fromberger.
24 * Portions created by the Initial Developer are Copyright (C) 1997
25 * the Initial Developer. All Rights Reserved.
27 * Contributor(s):
28 * Netscape Communications Corporation
30 * Alternatively, the contents of this file may be used under the terms of
31 * either the GNU General Public License Version 2 or later (the "GPL"), or
32 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 * in which case the provisions of the GPL or the LGPL are applicable instead
34 * of those above. If you wish to allow use of your version of this file only
35 * under the terms of either the GPL or the LGPL, and not to allow others to
36 * use your version of this file under the terms of the MPL, indicate your
37 * decision by deleting the provisions above and replace them with the notice
38 * and other provisions required by the GPL or the LGPL. If you do not delete
39 * the provisions above, a recipient may use your version of this file under
40 * the terms of any one of the MPL, the GPL or the LGPL.
42 * ***** END LICENSE BLOCK ***** */
44 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
45 * Use is subject to license terms.
47 * Sun elects to use this software under the MPL license.
50 #pragma ident "%Z%%M% %I% %E% SMI"
52 #include "mpi-priv.h"
53 #include "mpprime.h"
54 #include "mplogic.h"
55 #ifndef _KERNEL
56 #include <stdlib.h>
57 #include <string.h>
58 #else
59 #include <sys/random.h>
60 #endif
62 #define SMALL_TABLE 0 /* determines size of hard-wired prime table */
64 #ifndef _KERNEL
65 #define RANDOM() rand()
66 #else
67 #define RANDOM() foo_rand()
69 static int
70 foo_rand()
72 int r;
73 random_get_pseudo_bytes((uchar_t *)&r, sizeof (r));
74 return (r);
76 #endif
79 mpp_random(a)
81 Assigns a random value to a. This value is generated using the
82 standard C library's rand() function, so it should not be used for
83 cryptographic purposes, but it should be fine for primality testing,
84 since all we really care about there is good statistical properties.
86 As many digits as a currently has are filled with random digits.
89 mp_err mpp_random(mp_int *a)
92 mp_digit next = 0;
93 unsigned int ix, jx;
95 ARGCHK(a != NULL, MP_BADARG);
97 for(ix = 0; ix < USED(a); ix++) {
98 for(jx = 0; jx < sizeof(mp_digit); jx++) {
99 next = (next << CHAR_BIT) | (RANDOM() & UCHAR_MAX);
101 DIGIT(a, ix) = next;
104 return MP_OKAY;
106 } /* end mpp_random() */
108 /* }}} */
110 /* {{{ mpp_random_size(a, prec) */
112 mp_err mpp_random_size(mp_int *a, mp_size prec)
114 mp_err res;
116 ARGCHK(a != NULL && prec > 0, MP_BADARG);
118 if((res = s_mp_pad(a, prec)) != MP_OKAY)
119 return res;
121 return mpp_random(a);
123 } /* end mpp_random_size() */