Exemple #1
0
        private static void Test3Inner(int multipleCount)
        {
            const int d                      = 8;
            const int fieldOrder             = 13;
            var       projectiveEdwardsCurve = new ProjectiveEdwardsCurve(d, fieldOrder);
            var       pointsFactory          = new PointsFactory(projectiveEdwardsCurve);

            var initialPoint = pointsFactory.CreatePoint(6, 12, 2);
            var point1       = pointsFactory.CreatePoint(6, 12, 2);

            var calculator = new ProjectiveEdwardsCurvePointCalculator();

            for (var i = 0; i < multipleCount - 1; i++)
            {
                point1 = calculator.Sum(point1, initialPoint);
            }

            var point2 = calculator.Mult(multipleCount, initialPoint);

            if (point1.Equals(point2))
            {
                Console.Write(".");
                return;
            }

            throw new ArithmeticException("Точки, полученные сложением и умножение, отличаются при multipleCount = "
                                          + multipleCount);
        }
Exemple #2
0
        private static void Test4()
        {
            BigInteger fieldOrder = BigInteger.Parse("73928303") * BigInteger.Parse("73928293");
            BigInteger x          = 3;
            BigInteger y          = 2;
            BigInteger z          = 1;

            var d = ((x * x * z * z + y * y * z * z - z * z * z * z) * (x * x * y * y).Inverse(fieldOrder)).Mod(fieldOrder);

            const int multipleCount = 73928303;

            var projectiveEdwardsCurve = new ProjectiveEdwardsCurve(d, fieldOrder);
            var pointsFactory          = new PointsFactory(projectiveEdwardsCurve);

            var calculator   = new ProjectiveEdwardsCurvePointCalculator();
            var initialPoint = pointsFactory.CreatePoint(x, y, z);
            var point1       = pointsFactory.CreatePoint(x, y, z);

            for (var i = 0; i < multipleCount - 1; i++)
            {
                point1 = calculator.Sum(point1, initialPoint);

                var gcd = BigInteger.GreatestCommonDivisor(point1.ParameterZ, fieldOrder);
                if (gcd != BigInteger.One)
                {
                    Console.WriteLine("divider = " + gcd);
                    return;
                }

                //  Console.WriteLine(point1);
                //  Console.WriteLine(point1.ToEdwardsCurvePoint());
            }
        }
        public static void GetAllPoints()
        {
            BigInteger fieldOrder = BigInteger.Parse("113") * BigInteger.Parse("19");
            BigInteger x, y, d;
            var        random = new Random();

            do
            {
                x = BigIntegerExtensions.GetNextRandom(random, fieldOrder);
                y = BigIntegerExtensions.GetNextRandom(random, fieldOrder);
                d = ((x * x + y * y - 1) * (x * x * y * y).Inverse(fieldOrder)).Mod(fieldOrder);
            } while (d == 1 || d == 0);

            //     d = 1;
            Console.WriteLine($"x = {x}");
            Console.WriteLine($"y = {y}");

            var edwardsCurve = new AffineEdwardsCurve(d, fieldOrder);

            Console.WriteLine(edwardsCurve);

            var pointsFactory = new PointsFactory(edwardsCurve);

            var points = new List <AffineEdwardsCurvePoint>();

            for (var x1 = 0; x1 < fieldOrder; x1++)
            {
                for (var y1 = 0; y1 < fieldOrder; y1++)
                {
                    if (!pointsFactory.SoftCheckPointOnCurve(x1, y1))
                    {
                        continue;
                    }

                    points.Add(pointsFactory.CreatePoint(x1, y1));
                }
            }

            Console.WriteLine("Edwards curve has " + points.Count + " points");

            var calculator = new AffineEdwardsCurvePointCalculator();
            var pairs      = points.Join(points, p1 => 1, p2 => 1, (p1, p2) => new { p1, p2 }).ToArray();

            try
            {
                var sumPoints = pairs.AsParallel().Select(pair => calculator.Sum(pair.p1, pair.p2));
                Console.WriteLine("Sums were calculated without any exception. Sums count:" + sumPoints.Count());
            }
            catch (AggregateException e)
            {
                var gcdFoundException = e.InnerExceptions.First() as GcdFoundException;
                if (gcdFoundException == null)
                {
                    throw;
                }

                Console.WriteLine(gcdFoundException.Message);
                Console.WriteLine(gcdFoundException.GreatestCommonDivisor);
            }
        }
Exemple #4
0
 public MainForm()
 {
     InitializeComponent();
     pointsFactory = new PointsFactory();
     service       = new GenericService(new RatingFunction(), pointsFactory.CreateLinePoints());
     for (var i = 0; i < service.Points.Count; i++)
     {
         PointListBox.Items.Add($"Point {i}: {service.Points[i].ToString()}");
     }
 }
        public static void GetAllPointsSpecial()
        {
            BigInteger fieldOrder = BigInteger.Parse("113") * BigInteger.Parse("19");
            BigInteger x = 705, y = 232, d = 1577;
            var        edwardsCurve = new AffineEdwardsCurve(d, fieldOrder);

            var pointsFactory = new PointsFactory(edwardsCurve);
            var points        = new List <AffineEdwardsCurvePoint>();

            for (var x1 = 0; x1 < fieldOrder; x1++)
            {
                for (var y1 = 0; y1 < fieldOrder; y1++)
                {
                    if (!pointsFactory.SoftCheckPointOnCurve(x1, y1))
                    {
                        continue;
                    }

                    points.Add(pointsFactory.CreatePoint(x1, y1));
                }
            }

            Console.WriteLine("Edwards curve has " + points.Count + " points");

            var calculator = new AffineEdwardsCurvePointCalculator();
            var pairs      = points.Join(points, p1 => 1, p2 => 1, (p1, p2) => new { p1, p2 }).ToArray();


            try
            {
                var sumPoints = pairs.AsParallel().Select(pair => calculator.Sum(pair.p1, pair.p2));
                Console.WriteLine("Sums were calculated without any exception. Sums count:" + sumPoints.Count());
            }
            catch (AggregateException e)
            {
                var gcdFoundException = e.InnerExceptions.First() as GcdFoundException;
                if (gcdFoundException == null)
                {
                    throw;
                }

                Console.WriteLine(gcdFoundException.Message);
                Console.WriteLine(gcdFoundException.GreatestCommonDivisor);
            }
        }
Exemple #6
0
        private static void Test2()
        {
            const int d          = 8;
            const int fieldOrder = 13;

            var projectiveEdwardsCurve = new ProjectiveEdwardsCurve(d, fieldOrder);
            var pointsFactory          = new PointsFactory(projectiveEdwardsCurve);

            var point1 = pointsFactory.CreatePoint(6, 12, 2);
            var point2 = projectiveEdwardsCurve.NeitralPoint;

            var calculator = new ProjectiveEdwardsCurvePointCalculator();

            var sum = calculator.Sum(point1, point2);

            Console.WriteLine(sum);
            Console.WriteLine(sum.ToEdwardsCurvePoint());
        }
Exemple #7
0
        /// <summary> Тест на корректность суммирования </summary>
        private static void Test1()
        {
            var d          = 8;
            var fieldOrder = 13;

            var projectiveEdwardsCurve = new ProjectiveEdwardsCurve(d, fieldOrder);
            var pointsFactory          = new PointsFactory(projectiveEdwardsCurve);

            var point1 = pointsFactory.CreatePoint(3, 6, 1);
            var point2 = pointsFactory.CreatePoint(6, 3, 1);

            var calculator = new ProjectiveEdwardsCurvePointCalculator();

            var sum = calculator.Sum(point1, point2);

            Console.WriteLine(sum);
            Console.WriteLine(sum.ToEdwardsCurvePoint());
        }
        public static void CheckSum()
        {
            BigInteger fieldOrder    = 11;
            BigInteger d             = 2;
            var        edwardsCurve  = new AffineEdwardsCurve(d, fieldOrder);
            var        pointsFactory = new PointsFactory(edwardsCurve);

            var points = new[]
            {
                CreatePoint(0, 1, pointsFactory),
                CreatePoint(0, 10, pointsFactory),
                CreatePoint(1, 0, pointsFactory),
                CreatePoint(3, 4, pointsFactory),
                CreatePoint(3, 7, pointsFactory),
                CreatePoint(4, 3, pointsFactory),
                CreatePoint(4, 8, pointsFactory),
                CreatePoint(7, 3, pointsFactory),
                CreatePoint(7, 8, pointsFactory),
                CreatePoint(8, 4, pointsFactory),
                CreatePoint(8, 7, pointsFactory),
                CreatePoint(10, 0, pointsFactory)
            };

            for (int i = 0; i < points.Length; i++)
            {
                for (int j = 0; j < points.Length; j++)
                {
                    TestSum(points[i], points[j]);
                }
            }

//            return;
//            var sum = CreatePoint(7, 3, pointsFactory);
//
//            var calculator = new AffineEdwardsCurvePointCalculator();
//            var p3 = calculator.Sum(point1, point2);
//            CheckEquality(p3.ParameterX, p3.ParameterY, d, fieldOrder);
//
//            Console.WriteLine(point1);
//            Console.WriteLine(point2);
//            Console.WriteLine(p3);
        }
Exemple #9
0
        private static void Test5()
        {
            BigInteger fieldOrder = BigInteger.Parse("73928303") * BigInteger.Parse("73928293");
            BigInteger x          = 3;
            BigInteger y          = 2;
            BigInteger z          = 1;

            var d = ((x * x * z * z + y * y * z * z - z * z * z * z) * (x * x * y * y).Inverse(fieldOrder)).Mod(fieldOrder);

            var projectiveEdwardsCurve = new ProjectiveEdwardsCurve(d, fieldOrder);
            var pointsFactory          = new PointsFactory(projectiveEdwardsCurve);

            var calculator = new ProjectiveEdwardsCurvePointCalculator();
            var point1     = pointsFactory.CreatePoint(x, y, z);

            var        b1 = 73928303;
            BigInteger p  = 2;

            while (p < b1)
            {
                var pr = p;
                while (pr < b1)
                {
                    point1 = calculator.Mult(p, point1);

                    var gcd = BigInteger.GreatestCommonDivisor(point1.ParameterZ, fieldOrder);
                    if (gcd != BigInteger.One)
                    {
                        Console.WriteLine("divider = " + gcd);
                        return;
                    }

                    pr *= p;
                }
                p = BigIntegerExtensions.NextPrimaryMillerRabin(p);
            }
        }
        private static AffineEdwardsCurvePoint CreatePoint(BigInteger x, BigInteger y, PointsFactory pointsFactory)
        {
            var d          = pointsFactory.EdwardsCurve.ParameterD;
            var fieldOrder = pointsFactory.EdwardsCurve.FieldOrder;

            CheckEquality(x, y, d, fieldOrder);

            return(pointsFactory.CreatePoint(x, y));
        }
        public LenstraFactorizationResult GetDividerWithCancel(BigInteger n, Random random, CancellationToken token)
        {
            var startTime = DateTime.Now;

            BigInteger x, y, z, d;

            do
            {
                x = BigIntegerExtensions.GetNextRandom(random, n);
                y = BigIntegerExtensions.GetNextRandom(random, n);
                z = BigIntegerExtensions.GetNextRandom(random, n);
                d = ((x * x * z * z + y * y * z * z - z * z * z * z) * (x * x * y * y).Inverse(n)).Mod(n);
            } while (d == 1 || d == 0);

            var           projectiveEdwardsCurve = new ProjectiveEdwardsCurve(d, n);
            PointsFactory pointsFactory;

            try
            {
                pointsFactory = new PointsFactory(projectiveEdwardsCurve);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine("d = " + d);
                Console.WriteLine("x = " + x);
                Console.WriteLine("y = " + y);
                Console.WriteLine("z = " + z);
                throw;
            }

            var calculator = new ProjectiveEdwardsCurvePointCalculator();
            var point1     = pointsFactory.CreatePoint(x, y, z);

            BigInteger p         = 2;
            BigInteger step      = 1;
            long       iteration = 0;

            while (p < B1 && !token.IsCancellationRequested)
            {
                var pr = p;
                while (pr < B1 && !token.IsCancellationRequested)
                {
                    point1 = calculator.Mult(p, point1);

//                    if (iteration++ % step != 0)
//                    {
//                        pr *= p;
//                        continue;
//                    }

                    pr *= p;
                    if (point1.ParameterX == BigInteger.Zero)
                    {
                        continue;
                    }

                    // вычисляем НОД только через каждые step итераций, т.к. НОД уменьшает производительность
                    // пока это не будет реализовано
                    var gcd = BigInteger.GreatestCommonDivisor(point1.ParameterX, n);
                    if (gcd != BigInteger.One)
                    {
                        return(new LenstraFactorizationResult
                        {
                            EllepticCurve = projectiveEdwardsCurve,
                            TargetNumber = n,
                            Divider = gcd,
                            WastedTime = DateTime.Now - startTime,
                            EndType = EEndType.RunToComplete
                        });
                    }
                }

                p = BigIntegerExtensions.NextPrimaryMillerRabin(p);
            }

            return(new LenstraFactorizationResult
            {
                EllepticCurve = projectiveEdwardsCurve,
                TargetNumber = n,
                WastedTime = DateTime.Now - startTime,
                EndType = token.IsCancellationRequested ? EEndType.Cancelled : EEndType.RunToComplete
            });
        }
Exemple #12
0
        public LenstraFactorizationResult GetDividerWithCancel(BigInteger n, Random random, CancellationToken token)
        {
            var startTime = DateTime.Now;

            BigInteger x, y, d;

            do
            {
                x = BigIntegerExtensions.GetNextRandom(random, n);
                y = BigIntegerExtensions.GetNextRandom(random, n);
                d = ((x * x + y * y - 1) * (x * x * y * y).Inverse(n)).Mod(n);
            } while (d == 1 || d == 0);

            var edwardsCurve  = new AffineEdwardsCurve(d, n);
            var pointsFactory = new PointsFactory(edwardsCurve);

            var calculator = new AffineEdwardsCurvePointCalculator();
            var point1     = pointsFactory.CreatePoint(x, y);

            BigInteger p = 2;

            try
            {
                while (p < B1 && !token.IsCancellationRequested)
                {
                    var pr = p;
                    while (pr < B1 && !token.IsCancellationRequested)
                    {
                        point1 = calculator.Mult(p, point1);
                        var gcd = BigInteger.GreatestCommonDivisor(point1.ParameterX, n);
                        if (gcd != BigInteger.One)
                        {
                            throw new GcdFoundException(gcd);
                        }

                        pr *= p;
                    }

                    p = BigIntegerExtensions.NextPrimaryMillerRabin(p);
                }
            }
            catch (GcdFoundException exc)
            {
                return(new LenstraFactorizationResult
                {
                    EllepticCurve = edwardsCurve,
                    TargetNumber = n,
                    Divider = exc.GreatestCommonDivisor,
                    WastedTime = DateTime.Now - startTime,
                    EndType = EEndType.RunToComplete
                });
            }

            return(new LenstraFactorizationResult
            {
                EllepticCurve = edwardsCurve,
                TargetNumber = n,
                WastedTime = DateTime.Now - startTime,
                EndType = token.IsCancellationRequested ? EEndType.Cancelled : EEndType.RunToComplete
            });
        }