예제 #1
0
 public void EuclideanLCM()
 {
     int first = 12, second = 6;
     Euclidean euclideanCalculator = new Euclidean();
     int lcm = euclideanCalculator.CalculateLCM(first, second);
     Assert.AreEqual<int>(12, lcm);
 }
예제 #2
0
 public void EuclideanGCD()
 {
     int first = 12, second = 6;
     Euclidean euclideanCalculator = new Euclidean();
     int gcd = euclideanCalculator.CalculateGCD(first, second);
     Assert.AreEqual<int>(6,gcd);
 }
예제 #3
0
        private BigInteger FindE(int size)
        {
            BigInteger e = Generate.BigInt(size) % Fi_N;

            while (Euclidean.Simple(e, Fi_N) != 1)
            {
                e--;
            }

            return(e);
        }
예제 #4
0
        private BigInteger FindD(int size)
        {
            BigInteger d = Euclidean.Extended(Fi_N, E)[2];

            if (d > 0)
            {
                return(d);
            }
            else
            {
                return(d + Fi_N);
            }
        }
        public static BigInteger Solve(BigInteger c, BigInteger d, BigInteger p, BigInteger q)
        {
            var c_1 = BigInteger.ModPow(c, BigInteger.ModPow(d, 1, p - 1), p);
            var c_2 = BigInteger.ModPow(c, BigInteger.ModPow(d, 1, q - 1), q);
            var M   = p * q;
            var M_1 = M / p;
            var M_2 = M / q;

            Console.WriteLine($"c1: {c_1}\nc2: {c_2}\nM: {M}\nM1: {M_1}\nM2: {M_2}\n");

            var y_1 = Euclidean.Extended(M_1, M_2)[1];
            var y_2 = Euclidean.Extended(M_1, M_2)[2];

            Console.WriteLine($"y1: {y_1}\ny2:{y_2}\n");

            var m = BigInteger.ModPow(c_1 * y_1 * M_1 + c_2 * y_2 * M_2, 1, M);

            Console.WriteLine(m);
            return(m);
        }
예제 #6
0
        public static bool IsPrime(BigInteger number)
        {
            // handle evens
            if (number == 2)
            {
                return(true);
            }
            else if (number % 2 == 0)
            {
                return(false);
            }

            // get S
            var S = 1;

            while (((number - 1) % BigInteger.Pow(2, ++S)) == 0)
            {
                ;
            }
            S--;

            // get d
            var d = (number - 1) / BigInteger.Pow(2, S);

            //Console.WriteLine($"{S}, {d}");

            Random rnd = new Random();
            int    randomBase;

            // repeat 8 times to be sure :)
            for (int i = 0; i < 8; i++)
            {
                // get random base that meets gcd(b,n)=1 condition
                do
                {
                    randomBase = rnd.Next();
                } while (Euclidean.Simple(randomBase, number) != 1);


                if (FastModExponentiation.DoModPow(randomBase, d, number) != 1)
                {
                    bool existR = false;
                    for (int r = 0; r < S; r++)
                    {
                        if (FastModExponentiation.DoModPow(randomBase, d * (BigInteger.Pow(2, r)), number) == number - 1)
                        {
                            existR = true;
                            break;
                        }
                    }
                    if (existR)
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }

                return(false);
            }

            return(true);
        }