2

I have to implement DSA for university and I have a problem with finding number q which is prime factor of p - 1 where p is prime number. I was trying to write some wierd loops but it only worked for small p values. With 512 bit long prime it would take ages I guess. I implement using Java and BigInteger library.

EDIT:

   public BigInteger[] generatePAndQ(){

    BigInteger q = BigInteger.probablePrime(160, new Random());
    BigInteger k = BigInteger.valueOf(2); // k = 2

    BigInteger probablyPrime = q.multiply(k).add(BigInteger.ONE); // probablyPrime = q * k + 1
    while(!isPrime(probablyPrime)){
        q = BigInteger.probablePrime(160, new Random());
        probablyPrime = q.multiply(k).add(BigInteger.ONE);
    }

    BigInteger[] qAndP = new BigInteger[2];
    qAndP[0] = q;
    qAndP[1] = probablyPrime;

    return  qAndP;
}
Ayaka
  • 59
  • 6
  • this might help https://crypto.stackexchange.com/questions/64743/dsa-how-to-calculate-224-bit-q-for-2048-bit-p – kelalaka Dec 14 '18 at 13:13
  • One way to do this is to start with q. First find a prime q of the right size, then check the value p = 2*q + 1 for to see if it is prime. Expected running time is O(log^2 p) I believe. You really need to show your code if you want debugging help. – President James K. Polk Dec 14 '18 at 13:43
  • I edited post. @JamesKPolk did you ment something like this? – Ayaka Dec 14 '18 at 14:23
  • Well, it works but still is slow. For 30-bit q it search about 5 minutes and I meet 160-bit one – Ayaka Dec 14 '18 at 14:40
  • Yes, well if you want q to be 160 bits then k must be approximately p/q. So for 512 bit p k would need to be around 352 bits in size. – President James K. Polk Dec 14 '18 at 14:40
  • 1
    Don't create a new Random instance inside the loop. Instead create a single SecureRandom instance **outside** the loop and use that single instance for all random. – President James K. Polk Dec 14 '18 at 14:42
  • Meh, I've changed k to random 352-bit long number and made one instance of Random but I don't know how long is it going to search D: – Ayaka Dec 14 '18 at 14:52

1 Answers1

2

I'm not sure what you're doing but this code illustrates my comments. It typically runs in less than 0.5 seconds on my laptop.

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

public class Main {

    public static BigInteger[] generatePAndQ() {
        SecureRandom random = new SecureRandom();

        final int pSizeInBits = 512;
        final int qSizeInBits = 160;
        BigInteger q = BigInteger.probablePrime(qSizeInBits, random);
        BigInteger k = BigInteger.ONE.shiftLeft(pSizeInBits - qSizeInBits); // k = 2**(pSizeInBits - qSizeInBits);

        BigInteger probablyPrime = q.multiply(k).add(BigInteger.ONE); // probablyPrime = q * k + 1
        while (!probablyPrime.isProbablePrime(50)) {
            q = BigInteger.probablePrime(qSizeInBits, random);
            probablyPrime = q.multiply(k).add(BigInteger.ONE);
        }

        BigInteger[] qAndP = new BigInteger[2];
        qAndP[0] = q;
        qAndP[1] = probablyPrime;

        return qAndP;
    }

    public static void main(String[] args) {
        long start = System.nanoTime();
        final BigInteger[] pAndQ = generatePAndQ();
        double elapsed = (System.nanoTime() - start) / 1e9;
        System.out.printf("q=%d%np=%d%nTime: %f (seconds)%n", pAndQ[0], pAndQ[1], elapsed);
    }
}

The bounds on q, p, and k are quick and dirty and should be cleaned up.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • Oh God I figure out why it was so slow.... It's becasue of function 'isPrime(probablyPrime)'.... Anyway thank you very much. I was looking for how to caculate q and p for two days. Once again thank you very helpful! – Ayaka Dec 14 '18 at 15:07