Exemple #1
0
        private static BigInteger[] findRandomStrongPrime(uint primeBits, int orderBits, Rng random)
        {
            BigInteger one = new BigInteger(1);
            BigInteger u, aux, aux2;

            long[]     table_q, table_u, prime_table;
            PrimeSieve sieve = new PrimeSieve(16000);
            uint       table_count = sieve.AvailablePrimes() - 1;
            int        i, j;
            bool       flag;
            BigInteger prime = null, order = null;

            order = BigInteger.genPseudoPrime(orderBits, 20, random);

            prime_table = new long[table_count];
            table_q     = new long[table_count];
            table_u     = new long[table_count];

            i = 0;
            for (int pN = 2; pN != 0; pN = sieve.getNextPrime(pN), i++)
            {
                prime_table[i] = (long)pN;
            }

            for (i = 0; i < table_count; i++)
            {
                table_q[i] =
                    (((order % new BigInteger(prime_table[i])).LongValue()) *
                     (long)2) % prime_table[i];
            }

            while (true)
            {
                u = new BigInteger();
                u.genRandomBits((int)primeBits, random);
                u.setBit(primeBits - 1);
                aux  = order << 1;
                aux2 = u % aux;
                u    = u - aux2;
                u    = u + one;

                if (u.bitCount() <= (primeBits - 1))
                {
                    continue;
                }

                for (j = 0; j < table_count; j++)
                {
                    table_u[j] =
                        (u % new BigInteger(prime_table[j])).LongValue();
                }

                aux2 = order << 1;

                for (i = 0; i < (1 << 24); i++)
                {
                    long cur_p;
                    long value;

                    flag = true;
                    for (j = 1; j < table_count; j++)
                    {
                        cur_p = prime_table[j];
                        value = table_u[j];
                        if (value >= cur_p)
                        {
                            value -= cur_p;
                        }
                        if (value == 0)
                        {
                            flag = false;
                        }
                        table_u[j] = value + table_q[j];
                    }
                    if (!flag)
                    {
                        continue;
                    }

                    aux   = aux2 * new BigInteger(i);
                    prime = u + aux;

                    if (prime.bitCount() > primeBits)
                    {
                        continue;
                    }

                    if (prime.isProbablePrime(20))
                    {
                        break;
                    }
                }

                if (i < (1 << 24))
                {
                    break;
                }
            }

            return(new BigInteger[] { prime, order });
        }
Exemple #2
0
        private static BigInteger[] findRandomStrongPrime(uint primeBits, int orderBits, Rng random) {
            BigInteger one = new BigInteger(1);
            BigInteger u, aux, aux2;
            long[] table_q, table_u, prime_table;
            PrimeSieve sieve = new PrimeSieve(16000);
            uint table_count = sieve.AvailablePrimes() - 1;
            int i, j;
            bool flag;
            BigInteger prime = null, order = null;

            order = BigInteger.genPseudoPrime(orderBits, 20, random);

            prime_table = new long[table_count];
            table_q = new long[table_count];
            table_u = new long[table_count];

            i = 0;
            for (int pN = 2; pN != 0; pN = sieve.getNextPrime(pN), i++) {
                prime_table[i] = (long)pN;
            }

            for (i = 0; i < table_count; i++) {
                table_q[i] =
                    (((order % new BigInteger(prime_table[i])).LongValue()) *
                    (long)2) % prime_table[i];
            }

            while (true) {
                u = new BigInteger();
                u.genRandomBits((int)primeBits, random);
                u.setBit(primeBits - 1);
                aux = order << 1;
                aux2 = u % aux;
                u = u - aux2;
                u = u + one;

                if (u.bitCount() <= (primeBits - 1))
                    continue;

                for (j = 0; j < table_count; j++) {
                    table_u[j] =
                        (u % new BigInteger(prime_table[j])).LongValue();
                }

                aux2 = order << 1;

                for (i = 0; i < (1 << 24); i++) {
                    long cur_p;
                    long value;

                    flag = true;
                    for (j = 1; j < table_count; j++) {
                        cur_p = prime_table[j];
                        value = table_u[j];
                        if (value >= cur_p)
                            value -= cur_p;
                        if (value == 0)
                            flag = false;
                        table_u[j] = value + table_q[j];
                    }
                    if (!flag)
                        continue;

                    aux = aux2 * new BigInteger(i);
                    prime = u + aux;

                    if (prime.bitCount() > primeBits)
                        continue;

                    if (prime.isProbablePrime(20))
                        break;
                }

                if (i < (1 << 24))
                    break;
            }

            return new BigInteger[] { prime, order };
        }