FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / java / security / util / Prime.java
blob06d059e1eebc11032a79ffbba2dc3b04d470e42a
1 /* Prime.java --- Prime number generation utilities
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.util;
40 import java.math.BigInteger;
41 import java.util.Random;
42 //import java.security.SecureRandom;
44 public final class Prime
48 See IEEE P1363 A.15.4 (10/05/98 Draft)
50 public static BigInteger generateRandomPrime( int pmin, int pmax, BigInteger f )
52 BigInteger d;
54 //Step 1 - generate prime
55 BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() );
56 if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmin ) ) <= 0 )
58 p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin ).subtract( p ) );
61 //Step 2 - test for even
62 if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0)
63 p.add( BigInteger.valueOf( 1 ) );
65 for(;;)
67 //Step 3
68 if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0)
70 //Step 3.1
71 p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) );
72 p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) );
73 p = p.subtract( BigInteger.valueOf( 1 ) );
75 //Step 3.2
76 // put step 2 code here so looping code is cleaner
77 //Step 2 - test for even
78 if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0)
79 p.add( BigInteger.valueOf( 1 ) );
80 continue;
83 //Step 4 - compute GCD
84 d = p.subtract( BigInteger.valueOf(1) );
85 d = d.gcd( f );
87 //Step 5 - test d
88 if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0)
90 //Step 5.1 - test primality
91 if( p.isProbablePrime( 1 ) == true )
93 //Step 5.2;
94 return p;
97 //Step 6
98 p = p.add( BigInteger.valueOf( 2 ) );
100 //Step 7
106 See IEEE P1363 A.15.5 (10/05/98 Draft)
108 public static BigInteger generateRandomPrime( BigInteger r, BigInteger a, int pmin, int pmax, BigInteger f )
110 BigInteger d, w;
112 //Step 1 - generate prime
113 BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() );
115 steptwo:{ //Step 2
116 w = p.mod( r.multiply( BigInteger.valueOf(2) ));
118 //Step 3
119 p = p.add( r.multiply( BigInteger.valueOf(2) ) );
120 p = p.subtract( w );
121 p = p.add(a);
123 //Step 4 - test for even
124 if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0)
125 p.add( r );
127 for(;;)
129 //Step 5
130 if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0)
132 //Step 5.1
133 p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) );
134 p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) );
135 p = p.subtract( BigInteger.valueOf( 1 ) );
137 //Step 5.2 - goto to Step 2
138 break steptwo;
141 //Step 6
142 d = p.subtract( BigInteger.valueOf(1) );
143 d = d.gcd( f );
145 //Step 7 - test d
146 if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0)
148 //Step 7.1 - test primality
149 if( p.isProbablePrime( 1 ) == true )
151 //Step 7.2;
152 return p;
155 //Step 8
156 p = p.add( r.multiply( BigInteger.valueOf(2) ) );
158 //Step 9
161 //Should never reach here but makes the compiler happy
162 return BigInteger.valueOf(0);