Exemplo n.º 1
0
        public static BigInteger SolveDLP(ProjectivePoint P, ProjectivePoint Q)
        {
            var        R = P;
            BigInteger a = 1, b = 0;
            var        R2 = R;
            BigInteger a2 = a, b2 = b;
            //var MOD = P.E.P;
            var MOD = P.E.HasseTheorem();

            StepsCount = 1;
            for (int i = 1; i < MOD; i++)
            {
                R  = f(P, Q, R, ref a, ref b, MOD);
                R2 = f(P, Q, R2, ref a2, ref b2, MOD);
                R2 = f(P, Q, R2, ref a2, ref b2, MOD);
                if (R == R2)
                {
                    Console.WriteLine(R);
                    Console.WriteLine(R2);
                    break;
                }
                StepsCount++;
            }
            Console.WriteLine(MOD);
            Console.WriteLine(StepsCount);
            var dA = a2 - a;
            var dB = b - b2;

            dA = dA.ModPositive(MOD);
            dB = dB.ModPositive(MOD);
            var gcd = BigInteger.GreatestCommonDivisor(dB, MOD);

            Console.WriteLine(gcd);
            GCD = gcd;
            if (gcd == 1)
            {
                BigInteger x = dA * dB.ModInverse(MOD);
                return(x.ModPositive(MOD));
            }
            else
            {
                var reducedOrder = MOD / gcd;
                var x0           = (dB / gcd).ModInverse(reducedOrder);
                x0 = x0 * (dA / gcd);
                x0 = x0.ModPositive(reducedOrder);
                for (BigInteger m = 0; m < gcd; m++)
                {
                    var x = x0 + m * reducedOrder;
                    if ((x * P) == Q)
                    {
                        return(x);
                    }
                }
            }
            return(-1);
            // return (a2 - a) * BigIntegerExtension.ModInverse(b - b2, MOD);
        }
Exemplo n.º 2
0
        private static int chooseSet(ProjectivePoint R)
        {
            //var x = R.X;
            //int countSet = 3;
            //return (int)x % countSet;

            var y        = R.Y;
            int countSet = 3;

            return((int)y % countSet);
        }
        public static BigInteger SolveDLP(ProjectivePoint P, ProjectivePoint Q)
        {
            var startTime = System.Diagnostics.Stopwatch.StartNew();
            var x         = BabyStepGiantStep.SolveDLP(P, Q);

            startTime.Stop();
            var resultTime = startTime.Elapsed;

            TimeInSec   = resultTime.Hours * 3600 + resultTime.Minutes * 60 + resultTime.Seconds + (resultTime.Milliseconds * 1.0) / (1.0 * 100);
            ElapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", resultTime.Hours, resultTime.Minutes, resultTime.Seconds, resultTime.Milliseconds);
            return(x);
        }
Exemplo n.º 4
0
        private static ProjectivePoint f(ProjectivePoint P, ProjectivePoint Q, ProjectivePoint R, ref BigInteger a, ref BigInteger b, BigInteger MOD)
        {
            int set = chooseSet(R);

            if (set == 0)
            {
                b = (b + 1).ModPositive(MOD);
                return(R + Q);
            }
            else if (set == 1)
            {
                a = (2 * a).ModPositive(MOD);
                b = (2 * b).ModPositive(MOD);
                return(2 * R);  //ProjectivePoint.Double(R);
            }
            else
            {
                a = (a + 1).ModPositive(MOD);
                return(R + P);
            }
        }
        //public static BigInteger SolveDLP(AffinePoint P, AffinePoint Q)
        //{
        //    var order = P.E.HasseTheorem();
        //    var babySteps = new Dictionary<AffinePoint, BigInteger>();
        //    var m = BigIntegerExtension.Sqrt(order) + 1;
        //    AffinePoint babyStep;
        //    for (BigInteger i = 0; i < m; i++)
        //    {
        //        babyStep = (i * P.ToProjectivePoint()).ToAffinePoint();
        //        babySteps.Add(babyStep, i);
        //        Console.WriteLine(babyStep);
        //    }
        //    AffinePoint temp;
        //    ProjectivePoint giantStep = (m * P.ToProjectivePoint());
        //    for (BigInteger j = 0; j < m; j++)
        //    {
        //        temp = (Q.ToProjectivePoint() - j * giantStep).ToAffinePoint();
        //        Console.WriteLine(temp);
        //        BigInteger i = -1;
        //        try
        //        {
        //            i = babySteps[temp];
        //        }
        //        catch (KeyNotFoundException e) { }
        //        if (i != -1)
        //            return BigIntegerExtension.ModPositive(i + j * m, order);
        //    }
        //    return -1;
        //}

        public static BigInteger SolveDLP(ProjectivePoint P, ProjectivePoint Q)
        {
            var         order     = P.E.HasseTheorem();
            var         babySteps = new Dictionary <AffinePoint, BigInteger>();
            var         m         = BigIntegerExtension.Sqrt(order) + 1;
            AffinePoint babyStep;

            for (BigInteger i = 0; i <= m; i++)
            {
                babyStep = (i * P).ToAffinePoint();
                babySteps.Add(babyStep, i);
                //Console.WriteLine(babyStep);
            }
            AffinePoint     temp;
            ProjectivePoint giantStep = (m * P);

            //Console.WriteLine();
            //Console.WriteLine(giantStep.ToAffinePoint());
            //Console.WriteLine();
            for (BigInteger j = 0; j <= m; j++)
            {
                temp = (Q - j * giantStep).ToAffinePoint();
                // Console.WriteLine(temp);
                BigInteger i = -1;
                try
                {
                    i = babySteps[temp];
                }
                catch (KeyNotFoundException e) { }
                if (i != -1)
                {
                    //Console.WriteLine(temp + " dfsd");
                    return(BigIntegerExtension.ModPositive(i + j * m, order));
                }
            }
            //make modification with m/2 and +-iP
            return(-1);
        }