diff options
| author | Paul Fisher <rao@gnu.org> | 2000-03-17 00:25:38 +0000 |
|---|---|---|
| committer | Paul Fisher <rao@gnu.org> | 2000-03-17 00:25:38 +0000 |
| commit | f1ba39f5e0f310d63b354aa6716d6e2e4c4fffb9 (patch) | |
| tree | 9be9c1908d4b571d0a6e54e399cbbc75723daf81 /gnu/java/security/util/Prime.java | |
| parent | d27e26052f33a4c3784183c130975a83fd01905e (diff) | |
| download | classpath-f1ba39f5e0f310d63b354aa6716d6e2e4c4fffb9.tar.gz | |
Change licensing terms to libgcc-like.
Change DOS EOLs to Unix EOLs.
Diffstat (limited to 'gnu/java/security/util/Prime.java')
| -rw-r--r-- | gnu/java/security/util/Prime.java | 298 |
1 files changed, 153 insertions, 145 deletions
diff --git a/gnu/java/security/util/Prime.java b/gnu/java/security/util/Prime.java index abcaf076f..437c8cc82 100644 --- a/gnu/java/security/util/Prime.java +++ b/gnu/java/security/util/Prime.java @@ -1,145 +1,153 @@ -/* Prime.java --- Prime number generation utilities
-
- Copyright (c) 1999 by Free Software Foundation, Inc.
- Written by Mark Benvenuto <ivymccough@worldnet.att.net>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Library General Public License as published
- by the Free Software Foundation, version 2. (see COPYING.LIB)
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. */
-
-package gnu.java.security.util;
-import java.math.BigInteger;
-import java.util.Random;
-//import java.security.SecureRandom;
-
-public final class Prime
-{
-
-/*
- See IEEE P1363 A.15.4 (10/05/98 Draft)
-*/
-public static BigInteger generateRandomPrime( int pmin, int pmax, BigInteger f )
-{
- BigInteger d;
-
- //Step 1 - generate prime
- BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() );
- if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmin ) ) <= 0 )
- {
- p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin ).subtract( p ) );
- }
-
- //Step 2 - test for even
- if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0)
- p.add( BigInteger.valueOf( 1 ) );
-
- for(;;)
- {
- //Step 3
- if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0)
- {
- //Step 3.1
- p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) );
- p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) );
- p = p.subtract( BigInteger.valueOf( 1 ) );
-
- //Step 3.2
- // put step 2 code here so looping code is cleaner
- //Step 2 - test for even
- if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0)
- p.add( BigInteger.valueOf( 1 ) );
- continue;
- }
-
- //Step 4 - compute GCD
- d = p.subtract( BigInteger.valueOf(1) );
- d = d.gcd( f );
-
- //Step 5 - test d
- if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0)
- {
- //Step 5.1 - test primality
- if( p.isProbablePrime( 1 ) == true )
- {
- //Step 5.2;
- return p;
- }
- }
- //Step 6
- p = p.add( BigInteger.valueOf( 2 ) );
-
- //Step 7
- }
-}
-
-
-/*
- See IEEE P1363 A.15.5 (10/05/98 Draft)
-*/
-public static BigInteger generateRandomPrime( BigInteger r, BigInteger a, int pmin, int pmax, BigInteger f )
-{
- BigInteger d, w;
-
- //Step 1 - generate prime
- BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() );
-
-steptwo:{ //Step 2
- w = p.mod( r.multiply( BigInteger.valueOf(2) ));
-
- //Step 3
- p = p.add( r.multiply( BigInteger.valueOf(2) ) );
- p = p.subtract( w );
- p = p.add(a);
-
- //Step 4 - test for even
- if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0)
- p.add( r );
-
- for(;;)
- {
- //Step 5
- if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0)
- {
- //Step 5.1
- p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) );
- p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) );
- p = p.subtract( BigInteger.valueOf( 1 ) );
-
- //Step 5.2 - goto to Step 2
- break steptwo;
- }
-
- //Step 6
- d = p.subtract( BigInteger.valueOf(1) );
- d = d.gcd( f );
-
- //Step 7 - test d
- if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0)
- {
- //Step 7.1 - test primality
- if( p.isProbablePrime( 1 ) == true )
- {
- //Step 7.2;
- return p;
- }
- }
- //Step 8
- p = p.add( r.multiply( BigInteger.valueOf(2) ) );
-
- //Step 9
- }
- }
- //Should never reach here but makes the compiler happy
- return BigInteger.valueOf(0);
-}
-
-}
\ No newline at end of file +/* Prime.java --- Prime number generation utilities + Copyright (C) 1999 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +As a special exception, if you link this library with other files to +produce an executable, this library does not by itself cause the +resulting executable to be covered by the GNU General Public License. +This exception does not however invalidate any other reasons why the +executable file might be covered by the GNU General Public License. */ + + +package gnu.java.security.util; +import java.math.BigInteger; +import java.util.Random; +//import java.security.SecureRandom; + +public final class Prime +{ + + /* + See IEEE P1363 A.15.4 (10/05/98 Draft) + */ + public static BigInteger generateRandomPrime( int pmin, int pmax, BigInteger f ) + { + BigInteger d; + + //Step 1 - generate prime + BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() ); + if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmin ) ) <= 0 ) + { + p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin ).subtract( p ) ); + } + + //Step 2 - test for even + if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0) + p.add( BigInteger.valueOf( 1 ) ); + + for(;;) + { + //Step 3 + if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0) + { + //Step 3.1 + p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) ); + p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) ); + p = p.subtract( BigInteger.valueOf( 1 ) ); + + //Step 3.2 + // put step 2 code here so looping code is cleaner + //Step 2 - test for even + if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0) + p.add( BigInteger.valueOf( 1 ) ); + continue; + } + + //Step 4 - compute GCD + d = p.subtract( BigInteger.valueOf(1) ); + d = d.gcd( f ); + + //Step 5 - test d + if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0) + { + //Step 5.1 - test primality + if( p.isProbablePrime( 1 ) == true ) + { + //Step 5.2; + return p; + } + } + //Step 6 + p = p.add( BigInteger.valueOf( 2 ) ); + + //Step 7 + } + } + + + /* + See IEEE P1363 A.15.5 (10/05/98 Draft) + */ + public static BigInteger generateRandomPrime( BigInteger r, BigInteger a, int pmin, int pmax, BigInteger f ) + { + BigInteger d, w; + + //Step 1 - generate prime + BigInteger p = new BigInteger( (pmax + pmin)/2, new Random() ); + + steptwo:{ //Step 2 + w = p.mod( r.multiply( BigInteger.valueOf(2) )); + + //Step 3 + p = p.add( r.multiply( BigInteger.valueOf(2) ) ); + p = p.subtract( w ); + p = p.add(a); + + //Step 4 - test for even + if( p.mod( BigInteger.valueOf(2) ).compareTo( BigInteger.valueOf( 0 )) == 0) + p.add( r ); + + for(;;) + { + //Step 5 + if( p.compareTo( BigInteger.valueOf( 1 ).shiftLeft( pmax)) > 0) + { + //Step 5.1 + p = p.subtract( BigInteger.valueOf( 1 ).shiftLeft( pmax) ); + p = p.add( BigInteger.valueOf( 1 ).shiftLeft( pmin) ); + p = p.subtract( BigInteger.valueOf( 1 ) ); + + //Step 5.2 - goto to Step 2 + break steptwo; + } + + //Step 6 + d = p.subtract( BigInteger.valueOf(1) ); + d = d.gcd( f ); + + //Step 7 - test d + if( d.compareTo( BigInteger.valueOf( 1 ) ) == 0) + { + //Step 7.1 - test primality + if( p.isProbablePrime( 1 ) == true ) + { + //Step 7.2; + return p; + } + } + //Step 8 + p = p.add( r.multiply( BigInteger.valueOf(2) ) ); + + //Step 9 + } + } + //Should never reach here but makes the compiler happy + return BigInteger.valueOf(0); + } +} |
