예제 #1
0
        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);
            }
        }
예제 #2
0
        public static void TestSum(AffineEdwardsCurvePoint point1, AffineEdwardsCurvePoint point2)
        {
            var calculator = new AffineEdwardsCurvePointCalculator();
            var p3         = calculator.Sum(point1, point2);

            var fieldOrder = point1.EdwardsCurve.FieldOrder;
            var d          = point1.EdwardsCurve.ParameterD;

            CheckEquality(p3.ParameterX, p3.ParameterY, d, fieldOrder);

            point2 = new AffineEdwardsCurvePoint((-point2.ParameterX).Mod(fieldOrder), point2.ParameterY, point2.EdwardsCurve);
            p3     = new AffineEdwardsCurvePoint((-p3.ParameterX).Mod(fieldOrder), p3.ParameterY, p3.EdwardsCurve);

            var sum2 = calculator.Sum(point1, p3);

            CheckEquality(sum2.ParameterX, sum2.ParameterY, d, fieldOrder);

            if (sum2.ToString() == point2.ToString())
            {
                return;
            }

            throw new Exception("123");
        }
예제 #3
0
        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);
            }
        }