Exemplo n.º 1
0
        public static BigInteger2 getProbablePrime(int n)
        {
            BigInteger2 num     = new BigInteger2(n);
            BigInteger2 zero    = BigInteger2.Zero();
            BigInteger2 temp    = new BigInteger2(1, 0);
            BigInteger2 nem     = ((BigInteger2)num.Clone()) + BigInteger2.T50();
            bool        success = true;
            int         ni      = 0;

            while (true)
            {
                ni++;
                Console.Out.WriteLine(ni);
                if (num < nem || num == nem)
                {
                    foreach (int i in mySet)
                    {
                        bool[] val = BigInteger2.ConvertToBinary(i, 11);
                        bool[] t   = new bool[val.Length];
                        for (int x = 0; x < val.Length; x++)
                        {
                            t[x] = (val[x] ? true : false);
                        }
                        temp.bitlength = t;
                        if (BigIntegerExtensions.DivideBy(num, temp)[1] == zero)
                        {
                            success = false;
                            break;
                        }
                    }
                }
                if (success && BigIntegerExtensions.MillerRabinIsPrime(num))
                {
                    break;
                }
                if (num == nem)
                {
                    success = true;
                    var skip = DateTime.Now.Millisecond % 5;
                    switch (skip)
                    {
                    case 0:

                        num = num + BigInteger2.TWO();
                        nem = nem + BigInteger2.T50();
                        break;

                    case 1:
                        num = num + BigInteger2.FOUR();
                        nem = nem + BigInteger2.T100();
                        break;

                    case 2:
                        num = num + BigInteger2.T16();
                        nem = nem + BigInteger2.T100();
                        break;

                    case 3:
                        num = num + BigInteger2.T32();
                        nem = nem + BigInteger2.T100();
                        break;

                    case 4:
                        num = num + BigInteger2.T50();
                        nem = nem + BigInteger2.T100();
                        break;
                    }
                }
                else
                {
                    success = true;
                    num     = num + BigInteger2.TWO();
                }
            }
            return(num);
        }
Exemplo n.º 2
0
        //MillerRabinTest
        public static bool MillerRabinIsPrime(BigInteger2 numb)
        {
            //Use Rabin's Test suite a.modPower(d,numb)== 1 : then numb is prime else numb composite

            BigInteger2 n         = (BigInteger2)numb.Clone();
            BigInteger2 numbLess1 = n - BigInteger2.ONE();

            BigInteger2[] testSuite = new BigInteger2[3];

            //Fill testSuite with BigInteger2 values:
            //Including 5, 11, and 61

            testSuite[0]           = new BigInteger2(1, 0);
            testSuite[0].bitlength = BigInteger2.ConvertToBinary(61, 6);
            testSuite[1]           = new BigInteger2(1, 0);
            testSuite[1].bitlength = BigInteger2.ConvertToBinary(31, 6);
            testSuite[2]           = new BigInteger2(1, 0);
            testSuite[2].bitlength = BigInteger2.ConvertToBinary(11, 4);

            // Determine two.power(r) factor of q:
            // By: using q is least set bit referenced from zero
            int i = 0, j = 0;

            for (i = numbLess1.bitlength.Length - 1, j = 0; i >= 2; i--, j++)
            {
                if (numbLess1.bitlength[i])
                {
                    break;
                }
            }
            //Console.Out.WriteLine("j: {0}", j.ToString());
            //Form component two.power(r) = twoPowerR
            BigInteger2 twoPowerS = BigInteger2.TWO().power(j);

            //Calculate d:
            BigInteger2 d         = BigIntegerExtensions.DivideBy(numbLess1, twoPowerS)[0];

            //Use testSuite to eliminate Composites:
            bool isPrime = true;

            foreach (BigInteger2 integer in testSuite)
            {
                bool        prime  = false;
                BigInteger2 result = BigIntegerExtensions.modPower(integer, d, numb);
                if (result == BigInteger2.ONE())
                {
                    prime = true;
                    continue;
                }
                else
                {
                    for (i = 0; i < j; i++)
                    {
                        if ((numb - result) == BigInteger2.ONE())
                        {
                            prime = true;
                            break;
                        }
                        result = BigIntegerExtensions.modPower(result, BigInteger2.TWO(), numb);
                    }
                    if (prime)
                    {
                        continue;
                    }
                    else
                    {
                        isPrime = false;
                        break;
                    }
                }
            }
            return(isPrime);
        }