public ProjectivePoint ToProjectivePoint()
 {
     if (this == AffinePoint.GetInfinitePointForCurve(E))
     {
         return(new ProjectivePoint(0, 1, 0, E));
     }
     return(new ProjectivePoint(X, Y, 1, E));
 }
Beispiel #2
0
        public static void SolveDLPRange(int startBitLength, int finishBitLength, int count)
        {
            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook    workBook;
            Excel.Worksheet   workSheet;
            workBook         = excelApp.Workbooks.Add();
            workSheet        = (Excel.Worksheet)workBook.Worksheets.get_Item(1);
            excelApp.Visible = true;

            int row = 1;

            for (int i = startBitLength; i <= finishBitLength; i++)
            {
                var primes = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Сurves\p{0}bits.txt", i));
                var arrA   = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Сurves\A{0}bits.txt", i));
                var arrB   = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Сurves\B{0}bits.txt", i));

                var arrX = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Points\X{0}bits.txt", i));
                var arrY = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Points\Y{0}bits.txt", i));
                //var arrOrder = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Points\order{0}bits.txt", i));
                //var arrOrderbitslength = FileUtility.ReadArrayFromFile(String.Format(@"C:\Utility\ECDLPUtility\Points\orderbitslength{0}bits.txt", i));

                var A     = arrA[0];
                var B     = arrB[0];
                var p     = primes[0];
                var curve = new EllipticCurveUtility.EllipticCurve(A, B, p);
                var X     = arrX[0];
                var Y     = arrY[0];
                //var order = arrOrder[0];
                //var orderBitsLength = arrOrderbitslength[0];
                var point = new EllipticCurveUtility.AffinePoint(X, Y, curve);

                var rand = new BigIntegerRandom();
                for (int j = 0; j < count; j++)
                {
                    var n   = rand.Next(0, p - 1);
                    var P   = point.ToProjectivePoint();
                    var Q   = (n * P);
                    var log = SolveDLP(P, Q);

                    workSheet.Cells[row, 1] = i.ToString();
                    workSheet.Cells[row, 2] = curve.ToString();
                    workSheet.Cells[row, 3] = P.ToString();
                    workSheet.Cells[row, 4] = Q.ToString();
                    //  workSheet.Cells[row, 5] = order.ToString();
                    //    workSheet.Cells[row, 6] = orderBitsLength.ToString();
                    workSheet.Cells[row, 7]  = TimeInSec.ToString();
                    workSheet.Cells[row, 8]  = n.ToString();
                    workSheet.Cells[row, 9]  = log.ToString();
                    workSheet.Cells[row, 10] = StepsCount.ToString();
                    //workSheet.Cells[row, 11] = RhoPollard.GCD.ToString();

                    row++;
                }
            }
        }
Beispiel #3
0
        public bool IsOnCurve(AffinePoint P)
        {
            var x = P.X;
            var t = (x * (x * x + A) + B).ModPositive(this.P);

            if (BigIntegerExtension.JacobiSymbol(t, this.P) == -1)
            {
                return(false);
            }
            return(true);
        }
Beispiel #4
0
        ////Функция нахождения порядка кривой в афинных координатах
        //public static BigInteger FindOrder(EllipticCurve E)
        //{
        //    var Z = E.P;
        //    var rand = new Random();
        //    var startPoint = BasePoint.RandomPoint<PointAffine>(E);
        //    // Найдем границы по теореме Хассе
        //    var leftBorder = Z + 1 - 2 * (FieldsOperations.Sqrt(Z));
        //    var rightBorder = Z + 1 + 2 * (FieldsOperations.Sqrt(Z));
        //    for (int c = 0; c < NUMBER_OF_ATTEMPTS; c++)
        //    {
        //        //Сгенерируем точку на кривой и найдем ее порядок
        //        var P = BasePoint.RandomPoint<PointAffine>(E);
        //        var M = E.FindPointOrder(P);
        //        startPoint = P;
        //        if (M == 0)
        //            continue;
        //        int countOfDevided = 0;
        //        var order = BigInteger.Zero;
        //        //Найдем число делителей порядка точки в границах
        //        for (var i = leftBorder; i < rightBorder; )
        //        {
        //            if (i % M == 0)
        //            {
        //                order = i;
        //                i += M;
        //                countOfDevided++;
        //                if (countOfDevided > 1)
        //                    break;
        //            }
        //            else
        //            {
        //                i++;
        //            }
        //        }
        //        //Если оно равно единице, то порядок кривой - делитель порядка точки
        //        if (countOfDevided == 1)
        //        {
        //            E.Order = order;
        //            return order;
        //        }
        //    }
        //    return 0;
        //}

        public static BigInteger FindPointOrder(AffinePoint P)
        {
            var point = P;
            //var rand = new Random();
            var        rand = new BigIntegerRandom();
            BigInteger M    = 0;
            //Создаем список точек
            var A = new List <AffinePoint>();
            var Q = (P.E.P + 1) * point;

            A.Add(new AffinePoint());
            A[0].X = 0;
            A[0].Y = 0;
            A[0].Z = 1;
            A[0].E = P.E;
            A.Add(point);
            var max   = BigInteger.One;
            int count = 0;

            for (; count < P.E.P; count++)
            {
                M = 0;
                // генерерируем число m
                var m = BigIntegerExtension.Sqrt(BigIntegerExtension.Sqrt(P.E.P)) + 1 + rand.Next(0, P.E.P);
                if (m > max)
                {
                    //дозаполняем список
                    for (BigInteger i = 0; i < m - max; i++)
                    {
                        A.Add(point + A[A.Count - 1]);
                    }
                    max = m;
                }
                var  k     = -m - 1;
                int  j     = 0;
                bool flag  = true;
                var  twoMP = (2 * m) * point;
                var  temp  = Q + (k * twoMP);
                k++;
                //вычисляем параметры k и j
                for (; k < m && flag; k++)
                {
                    if (k < 0 && k != -m)
                    {
                        temp = temp - twoMP;
                    }
                    else
                    {
                        temp = temp + twoMP;
                    }

                    for (j = 0; j < A.Count && flag; j++)
                    {
                        if (temp == A[j])
                        {
                            j    = -j;
                            flag = false;
                            break;
                        }
                        if (temp == (-A[j]))
                        {
                            flag = false;
                            break;
                        }
                    }
                }
                k--;
                M = (P.E.P + 1 + 2 * m * k + j);
                if ((M * point) == P.E.GetInfiniteAffinePoint() && M != 0)
                {
                    break;
                }
            }
            if (count == P.E.P)
            {
                return(0);
            }
            // Раскладываем число M ро-методом Полларда
            List <BigInteger> factorsM;

            try
            {
                factorsM = BigIntegerExtension.PollardsAlg(BigInteger.Abs(M));
            }
            catch (Exception)
            {
                return(0);
            }
            // Проверяем простые делители числа M
            for (int i = 0; i < factorsM.Count;)
            {
                BigInteger temp = M / factorsM[i];
                if (M != factorsM[i] && M % factorsM[i] == 0 && (temp * point) == P.E.GetInfiniteAffinePoint())
                {
                    M = temp;
                    i = 0;
                }
                else
                {
                    i++;
                }
            }
            //P.Order = M;
            return(M);
        }
 public static AffinePoint Double(AffinePoint P)
 {
     return(P + P);
 }
        //чувака
        private static AffinePoint X2(AffinePoint point)
        {
            BigInteger A = 3 * point.X * point.X + point.E.A * point.Z * point.Z;

            A = BigInteger.Remainder(A, point.E.P);
            if (A < 0)
            {
                while (A < 0)
                {
                    A += point.E.P;
                }
            }


            BigInteger B = 2 * point.Y * point.Z;

            B = BigInteger.Remainder(B, point.E.P);
            if (B < 0)
            {
                while (B < 0)
                {
                    B += point.E.P;
                }
            }


            BigInteger X = B * (A * A - 4 * point.X * point.Y * B);

            X = BigInteger.Remainder(X, point.E.P);
            if (X < 0)
            {
                while (X < 0)
                {
                    X += point.E.P;
                }
            }


            BigInteger Y = A * (6 * point.Y * point.X * B - A * A) - 2 * point.Y * point.Y * B * B;

            Y = BigInteger.Remainder(Y, point.E.P);
            if (Y < 0)
            {
                while (Y < 0)
                {
                    Y += point.E.P;
                }
            }


            BigInteger Z = B * B * B;

            Z = BigInteger.Remainder(Z, point.E.P);
            if (Z < 0)
            {
                while (Z < 0)
                {
                    Z += point.E.P;
                }
            }
            return(new AffinePoint(X, Y, Z, point.E));
        }