classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

java.security expert?


From: Tom Tromey
Subject: java.security expert?
Date: 03 Mar 2004 12:03:27 -0700

While debugging gcjx, I ran across some strange code in Prime.java.
I think the intention was for the code to resemble the result of the
appended patch.  The patch looks big, but it just wraps the body in a
`while (true)' and changes a `break' to `continue'.

Any comments on this?  I didn't look up the IEEE spec that this
references, for all I know the comments are the only part that are
wrong.

Tom

Index: ChangeLog
from  Tom Tromey  <address@hidden>

        * gnu/java/security/util/Prime.java (generateRandomPrime): Loop
        as intended.

Index: gnu/java/security/util/Prime.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/security/util/Prime.java,v
retrieving revision 1.3
diff -u -r1.3 Prime.java
--- gnu/java/security/util/Prime.java 11 Aug 2002 16:34:44 -0000 1.3
+++ gnu/java/security/util/Prime.java 3 Mar 2004 19:12:40 -0000
@@ -1,5 +1,5 @@
 /* Prime.java --- Prime number generation utilities
-   Copyright (C) 1999 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2004 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -105,60 +105,64 @@
   /*
     See IEEE P1363 A.15.5 (10/05/98 Draft)
   */
-  public static BigInteger generateRandomPrime( BigInteger r, BigInteger a, 
int pmin, int pmax, BigInteger f )
+  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) ));
+  steptwo:
+    while (true)
+      {
+       //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
+               continue steptwo;
+             }
+
+           //Step 6
+           d = p.subtract( BigInteger.valueOf(1) );
+           d = d.gcd( f );
 
-      //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);      
+           //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
+         }
+      }
   }
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]