Exemplo n.º 1
0
        /// <summary>
        /// Gets BigNum <paramref name="b"/>.
        /// Generates p and q, prime numbers.
        /// While b is not inverse number to p-1 and q-1, p and q re-generates.
        /// After finding good p and q, calculate a that is inverse of b mod ((p-1)(q-1)).
        /// n is calculated as p * q.
        /// Returns by reference the BigNum a, p, q, and n.
        /// </summary>
        /// <param name="b"></param>
        /// <param name="a"></param>
        /// <param name="q"></param>
        /// <param name="p"></param>
        /// <param name="nByRef"></param>
        public static void GenRSA(BigNum b, BigNum a, BigNum q, BigNum p, BigNum nByRef)
        {
            bool   isBInverseOfPMinus1AndQMinus1 = false;
            BigNum n = new BigNum();

            while (!isBInverseOfPMinus1AndQMinus1)
            {
                p.Set(GenPrime());
                q.Set(GenPrime());

                BigNum pMinus1 = new BigNum(p);
                pMinus1.SubNum(GlobalVariables.one);
                BigNum qMinus1 = new BigNum(q);
                qMinus1.SubNum(GlobalVariables.one);

                BigNum fiOfN = new BigNum(pMinus1);
                fiOfN.MultNum(qMinus1);
                BigNum gcd = new BigNum();
                ExtendedGCD(b, fiOfN, new BigNum(), new BigNum(), gcd);
                if (gcd.CompNumWithoutPrint(GlobalVariables.one))
                {
                    isBInverseOfPMinus1AndQMinus1 = true;
                    a.Set(Inverse(b, fiOfN));
                    if (a.IsNegative)
                    {
                        a.AddNum(fiOfN);
                    }
                    n = new BigNum(p);
                    n.MultNum(q);
                }
            }

            nByRef.Set(n);
        }
Exemplo n.º 2
0
        public static bool MillerRabinAlgorithm(BigNum n, BigNum t, BigNum u)
        {
            bool   isPrimeNumber   = true;
            BigNum randomNumber    = GenerateNumberFromOneToGivenTop(n);
            BigNum x0              = Power(randomNumber, u, n);
            BigNum x1              = new BigNum();
            BigNum iterationNumber = new BigNum(GlobalVariables.one);

            while (iterationNumber <= t)    // Iterates t times.
            {
                x1 = Power(x0, GlobalVariables.two, n);
                x0.Set(x1);
                if ((x1.CompNumWithoutPrint(GlobalVariables.one)) &&
                    (!x0.CompNumWithoutPrint(GlobalVariables.one)) &&
                    (!x0.CompNumWithoutPrint(GlobalVariables.minusOne)))
                {
                    isPrimeNumber = false;
                    break;
                }

                iterationNumber.AddNum(GlobalVariables.one);
            }

            if (isPrimeNumber)
            {
                if (!x1.CompNumWithoutPrint(GlobalVariables.one))
                {
                    isPrimeNumber = false;
                }
            }

            return(isPrimeNumber);
        }
Exemplo n.º 3
0
        public static void IIB()
        {
            Console.WriteLine("II.");
            Console.WriteLine("Finds the first five prime numbers with 100 binary digits");
            int countPrimeNumberFound = 0;
            int tryNumber             = 0;

            while (countPrimeNumberFound < 5)
            {
                List <int> positionsOf1s = new List <int> // Trusting that the number will have 100 binary digits.
                {
                    GlobalVariables.MAX_NUM - 100,
                    GlobalVariables.MAX_NUM - 1
                };
                int k = 20;

                BigNum primeNumberSuspect = new BigNum();
                primeNumberSuspect.ReadNum(positionsOf1s);
                Console.WriteLine($"try number: {++tryNumber}");
                if (MathOps.IsPrime(primeNumberSuspect, k))
                {
                    Console.WriteLine($"Prime {countPrimeNumberFound}: {primeNumberSuspect.BinaryValue}");
                    Console.WriteLine();
                }

                primeNumberSuspect.AddNum(GlobalVariables.two);
            }

            Console.WriteLine();
        }
Exemplo n.º 4
0
        /// <summary>
        /// D.
        /// Converts the big number from this instance to long number.
        /// Checks that the number can fit into long. If so, returns it.
        /// The biggest positive value that can be hold by long variable is: 2^63 -1,
        /// therfore has 63 bits.
        /// </summary>
        /// <returns>longNumber</returns>
        public long?BigToLong()
        {
            BigNum copyNumber = new BigNum(this);
            long?  longNumber;
            int    fromBase   = 2;
            bool   isNegative = false;

            if (IsNegative)
            {
                // Make copyNumber represented as positive number.
                copyNumber.Complement();
                copyNumber.AddNum(GlobalVariables.one);
                isNegative = true;
            }

            if (IsBigNumFitsIntoLongType(copyNumber))
            {
                longNumber = Convert.ToInt64(copyNumber.WriteNum(), fromBase);
                if (isNegative)
                {
                    longNumber = -longNumber;
                }
            }
            else
            {
                longNumber = null;
                Console.WriteLine($"Convertion from BigNum to long is not possible," +
                                  $"this BigNum has more then {GlobalVariables.MaxNumberOfDigitsInLongType} digits");
            }

            return(longNumber);
        }
Exemplo n.º 5
0
        /// <summary>
        /// I.
        /// Gets BigNum type and substract between the current value of the binary number with the given one.
        /// Let do substraction A = A-B. We know that A-B = A + (-B).
        /// Calculating -B: Doing complement and then add 1.
        /// </summary>
        /// <param name="numberToSubtract"></param>
        public void SubNum(BigNum numberToSubtract)
        {
            // numberToSubstract = B.
            BigNum modifiedNumberToSubtract = new BigNum(numberToSubtract);

            modifiedNumberToSubtract.Complement();                // numberToSubtract = -B - 1.
            modifiedNumberToSubtract.AddNum(GlobalVariables.one); // numberToSubtract = -B.
            AddNum(modifiedNumberToSubtract);                     // Allready updates <mMSB1Location>.
            UpdateMSB1Location();
        }
Exemplo n.º 6
0
        public static void E()
        {
            Console.WriteLine("(E) Using AddNum method.");
            Console.WriteLine("8 + 13 = 21.");
            BigNum bigNumE1 = new BigNum(8L);
            BigNum bigNumE2 = new BigNum(13L);

            bigNumE1.AddNum(bigNumE2);
            Console.WriteLine(bigNumE1.WriteNum());

            Console.WriteLine();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets a number m.
        /// Finds p >= 0 and k (k is odd), where m = 2^p * k.
        /// Return p and k by ref.
        ///
        /// p = 0;
        /// if (!IsEven(m))
        /// {
        ///     k = m;
        /// }
        /// else // m is even.
        /// {
        ///     while(IsEven(m))
        ///     {
        ///          m /= 2;
        ///          p++;
        ///     }
        ///     k = m;
        /// }
        /// </summary>
        /// <param name=""></param>
        /// <param name=""></param>
        public static void FindExpressionOfGivenNumberByPowerOfTwoTimesOddNumber(BigNum m, BigNum p, BigNum k)
        {
            if (!m.IsEven())
            {
                k.Set(m);
            }
            else
            {
                while (m.IsEven())
                {
                    DivNum(m, GlobalVariables.two, m, new BigNum());
                    p.AddNum(GlobalVariables.one);
                }

                k.Set(m);
            }
        }