package org.bouncycastle.crypto.generators;

import java.math.BigInteger;
import java.security.SecureRandom;

import org.bouncycastle.util.BigIntegers;

class DHParametersHelper
{
    private static final BigInteger ONE = BigInteger.valueOf(1);
    private static final BigInteger TWO = BigInteger.valueOf(2);

    // Finds a pair of prime BigInteger's {p, q: p = 2q + 1}
    static BigInteger[] generateSafePrimes(
        int             size,
        int             certainty,
        SecureRandom    random)
    {
        BigInteger p, q;
        int qLength = size - 1;

        for (;;)
        {
            q = new BigInteger(qLength, 2, random);

            // p <- 2q + 1
            p = q.shiftLeft(1).add(ONE);

            if (p.isProbablePrime(certainty)
                && (certainty <= 2 || q.isProbablePrime(certainty)))
            {
                    break;
            }
        }

        return new BigInteger[] { p, q };
    }

    // Select a high order element of the multiplicative group Zp*
    // p and q must be s.t. p = 2*q + 1, where p and q are prime
    static BigInteger selectGenerator(
        BigInteger      p,
        BigInteger      q,
        SecureRandom    random)
    {
        BigInteger pMinusTwo = p.subtract(TWO);
        BigInteger g;

        // Handbook of Applied Cryptography 4.86
        do
        {
            g = BigIntegers.createRandomInRange(TWO, pMinusTwo, random);
        }
        while (g.modPow(TWO, p).equals(ONE)
            || g.modPow(q, p).equals(ONE));

/*
        // RFC 2631 2.1.1 (and see Handbook of Applied Cryptography 4.81)
        do
        {
            BigInteger h = createInRange(TWO, pMinusTwo, random);

            g = h.modPow(TWO, p);
        }
        while (g.equals(ONE));
*/

        return g;
    }
}

@@ -309,17 +309,24 @@
 ;%0A%0A    /
-/
+*%0A     *
  Finds a
@@ -380,101 +380,128 @@
     
-static BigInteger%5B%5D generateSafePrimes(%0A        int             size,%0A        int            
+ * %0A     * (see: Handbook of Applied Cryptography 4.86)%0A     */%0A    static BigInteger%5B%5D generateSafePrimes(int size, int
  cer
@@ -507,24 +507,16 @@
 rtainty,
-%0A       
  SecureR
@@ -513,35 +513,32 @@
 y, SecureRandom 
-   
 random)%0A    %7B%0A  
@@ -786,32 +786,16 @@
 rtainty)
-%0A               
  && (cer
@@ -846,36 +846,32 @@
 )%0A            %7B%0A
-    
                 
@@ -956,17 +956,24 @@
 %7D%0A%0A    /
-/
+*%0A     *
  Select 
@@ -1029,18 +1029,26 @@
 Zp*%0A    
-//
+ * %0A     *
  p and q
@@ -1097,16 +1097,49 @@
 re prime
+ (see generateSafePrimes)%0A     */
 %0A    sta
@@ -1169,25 +1169,16 @@
 nerator(
-%0A        
 BigInteg
@@ -1184,50 +1184,24 @@
 ger 
-     p,%0A        BigInteger      q,%0A       
+p, BigInteger q,
  Sec
@@ -1210,19 +1210,16 @@
 eRandom 
-   
 random)%0A
@@ -1304,17 +1304,34 @@
        /
-/
+*%0A         * (see:
  Handboo
@@ -1359,17 +1359,30 @@
 aphy 4.8
-6
+0)%0A         */
 %0A       
@@ -1521,28 +1521,16 @@
 als(ONE)
-%0A           
  %7C%7C g.mo
@@ -1555,16 +1555,25 @@
 ONE));%0A%0A
+%0A        
 /*%0A     
@@ -1575,18 +1575,18 @@
         
-//
+ *
  RFC 263
@@ -1593,11 +1593,13 @@
 1 2.
-1
+2
 .1
+.2
  (an
@@ -1603,16 +1603,17 @@
 (and see
+:
  Handboo
@@ -1644,16 +1644,30 @@
 y 4.81)%0A
+         */%0A//
         
@@ -1661,32 +1661,34 @@
 */%0A//        do%0A
+//
         %7B%0A      
@@ -1673,32 +1673,34 @@
  do%0A//        %7B%0A
+//
             BigI
@@ -1710,22 +1710,40 @@
 ger h = 
-create
+BigIntegers.createRandom
 InRange(
@@ -1759,33 +1759,37 @@
 usTwo, random);%0A
-%0A
+//%0A//
             g = 
@@ -1806,16 +1806,18 @@
 WO, p);%0A
+//
         
@@ -1810,32 +1810,34 @@
 p);%0A//        %7D%0A
+//
         while (g
@@ -1851,18 +1851,16 @@
 (ONE));%0A
-*/
 %0A%0A      

Fix references in comments