public void FromBigEndianByteArrayTest_BigValue1()
        {
            var array = new byte[] { 0x3f, 0x88, 0xc9, 0x8c, 0x42, 0xe4, 0x79, 0xde };
            var value = BigIntegerExtension.FromBigEndianByteArray(array);

            Assert.AreEqual(4578130625476983262, value);
        }
        public void FromBigEndianByteArrayTest_BigValue2()
        {
            var array = new byte[] { 0xba, 0xad, 0x87, 0xcb, 0x64, 0x27, 0x29, 0xf0 };
            var value = BigIntegerExtension.FromBigEndianByteArray(array);

            Assert.AreEqual(-4995187104055612944, value);
        }
        public void ModularInverseTest_NotRelativelyPrime()
        {
            var value   = new BigInteger(18);
            var modulus = new BigInteger(60);

            BigIntegerExtension.ModularInverse(value, modulus);
        }
        public void FromBigEndianByteArrayTest_One()
        {
            var array = new byte[] { 0x01 };
            var value = BigIntegerExtension.FromBigEndianByteArray(array);

            Assert.AreEqual(1, value);
        }
 public void GroupGeneratorByDivision()
 {
     foreach (var number in numbers)
     {
         BigIntegerExtension.GetGroupGenerator(number);
     }
 }
        public void ExtendedEuclidTest_VNotPositive()
        {
            var u = new BigInteger(1);
            var v = new BigInteger(0);

            BigIntegerExtension.ExtendedEuclid(u, v);
        }
        // мое из крендэла померанца, не уверен что работает
        public static AffinePoint operator +(AffinePoint P, AffinePoint Q)
        {
            BigInteger X1 = P.X, Y1 = P.Y, Z1 = P.Z;
            BigInteger X2 = Q.X, Y2 = Q.Y, Z2 = Q.Z;
            BigInteger MOD = P.E.P, A = P.E.A;
            BigInteger m;

            if (Z1 == 0)
            {
                return(Q);
            }
            if (Z2 == 0)
            {
                return(P);
            }
            if (X1 == X2)
            {
                if (Y1 + Y2 == 0)
                {
                    return(GetInfinitePointForCurve(P.E));
                }
                m = (3 * X1 * X1 + A) * BigIntegerExtension.ModInverse(2 * Y1, MOD);
            }
            else
            {
                m = (Y2 - Y1) * BigIntegerExtension.ModInverse((X2 - X1), MOD);
            }
            BigInteger X3 = m * m - X1 - X2;
            BigInteger Y3 = m * (X1 - X3) - Y1;

            return(new AffinePoint(X3.ModPositive(MOD), Y3.ModPositive(MOD), P.E));
        }
        public void ModularInverseTest_OneZero()
        {
            var value   = new BigInteger(1);
            var modulus = new BigInteger(0);

            BigIntegerExtension.ModularInverse(value, modulus);
        }
        public static BigInteger SolveDLP(BigInteger g, BigInteger h, BigInteger p)
        {
            var order         = p - 1;
            var input         = new DLPInput(g, h, p, order);
            var factorBase    = BigIntegerExtension.GetFactorBase(FactorBaseSize);
            var coefficients  = new List <List <BigInteger> >();
            var constantTerms = new List <BigInteger>();

            FirstStep(input, factorBase, ref coefficients, ref constantTerms);
            Console.WriteLine("First step done");
            var factorBaseLogs = SecondStep(input, ref factorBase, Converter.ToTwoDimensionalBigIntegerArray(coefficients), constantTerms.ToArray());

            Console.WriteLine("Second step done");
            if (factorBaseLogs == null)
            {
                return(-1);
            }
            for (int i = 0; i < factorBaseLogs.Length; i++)
            {
                var        l   = factorBaseLogs[i];
                BigInteger pow = -1;
                if (l != -1)
                {
                    pow = BigInteger.ModPow(g, l, p);
                }
                var pr = factorBase[i];
                Console.WriteLine(pow + " = " + pr);
            }
            var x = ThirdStep(input, factorBase, factorBaseLogs);

            Console.WriteLine("Third step done");
            ReducedFactorBaseSize = factorBase.Length;
            return(x);
        }
        public void ToBigEndianByteArrayTest_One()
        {
            var value = new BigInteger(1);
            var array = BigIntegerExtension.ToBigEndianByteArray(value);

            Assert.IsTrue(array.Length == 1);
            Assert.IsTrue(array[0] == 1);
        }
        public void ModularInverseTest_RelativelyPrime()
        {
            var value   = new BigInteger(12);
            var modulus = new BigInteger(25);
            var result  = BigIntegerExtension.ModularInverse(value, modulus);

            Assert.AreEqual(new BigInteger(23), result);
        }
        public void ModularInverseTest_Prime()
        {
            var value   = new BigInteger(881);
            var modulus = new BigInteger(991);
            var result  = BigIntegerExtension.ModularInverse(value, modulus);

            Assert.AreEqual(new BigInteger(9), result);
        }
        public static BigInteger SolveDLP(BigInteger g, BigInteger h, BigInteger p)
        {
            var order = p - 1;

            if (h == 1)
            {
                return(0);
            }
            if (h == g)
            {
                return(1);
            }
            var giantSteps = new Dictionary <BigInteger, BigInteger>();
            var m          = BigIntegerExtension.Sqrt(order) + 1;
            var temp       = BigInteger.ModPow(g, m, p);
            var y          = temp;

            //Console.WriteLine("g = {0}, h = {1}, p = {2}, realOrder = {3}, m = {4}",g, h, p, BigIntegerExtension.FindElementOrder(g, p), m);
            //Console.WriteLine();
            //Console.WriteLine("Table of y = g^(m*i) mod p");
            for (int i = 1; i <= m; i++)
            {
                //babySteps.Add(i, y);

                //Console.WriteLine("y = {0}, k*i = {1}", y, i * m);
                try
                {
                    giantSteps.Add(y, i);
                }
                catch (Exception e)
                {
                    //if (BigIntegerExtension.FindElementOrder(g, p) == order)
                    //    Console.WriteLine("dublicates with generators mod" + p);
                    //babySteps[y] = i;
                }
                y = (y * temp).ModPositive(p);
            }
            temp = (h * g).ModPositive(p);
            for (int j = 1; j <= m; j++)
            {
                //if (babySteps.ContainsValue(temp))
                //    return (m * babySteps[temp] - j).ModPositive(order);

                BigInteger i = -1;
                try
                {
                    i = giantSteps[temp];
                }
                catch (KeyNotFoundException e) { }
                if (i != -1)
                {
                    return((m * i - j).ModPositive(order));
                }
                temp = (temp * g).ModPositive(p);
            }
            return(-1);
        }
        public void ExtendedEuclidTest_TwoPrimes()
        {
            var u = new BigInteger(7823);
            var v = new BigInteger(7919);

            var result = BigIntegerExtension.ExtendedEuclid(u, v);

            Assert.AreEqual(3877, result.Item1);
            Assert.AreEqual(-3830, result.Item2);
            Assert.AreEqual(1, result.Item3);
        }
        public void ExtendedEuclidTest_TwoTwo()
        {
            var u = new BigInteger(2);
            var v = new BigInteger(2);

            var result = BigIntegerExtension.ExtendedEuclid(u, v);

            Assert.AreEqual(0, result.Item1);
            Assert.AreEqual(1, result.Item2);
            Assert.AreEqual(2, result.Item3);
        }
Exemple #16
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);
        }
Exemple #17
0
        private void OnGenerateGBtnClick(object sender, RoutedEventArgs e)
        {
            int  p       = 1;
            bool isValid = int.TryParse(pTxtBox.Text, out p);

            if (isValid)
            {
                var g = BigIntegerExtension.PrimitiveRoot(p);
                gTxtBox.Text = g.ToString();
            }
        }
        public void ExtendedEuclidTest_TwoProductsOfPrimes()
        {
            // Let 7703, 7607 and 7541 be three prime numbers.
            var u = new BigInteger(58088323); // 7703 * 7541
            var v = new BigInteger(57364387); // 7607 * 7541

            var result = BigIntegerExtension.ExtendedEuclid(u, v);

            Assert.AreEqual(1981, result.Item1);
            Assert.AreEqual(-2006, result.Item2);
            Assert.AreEqual(7541, result.Item3);
        }
        public void ExtendedEuclidTest_DontComputeU2()
        {
            // Let 7703, 7607 and 7541 be three prime numbers.
            var u = new BigInteger(58088323); // 7703 * 7541
            var v = new BigInteger(57364387); // 7607 * 7541

            var result = BigIntegerExtension.ExtendedEuclid(u, v, false);

            Assert.AreEqual(1981, result.Item1);
            Assert.AreEqual(0, result.Item2);
            Assert.AreEqual(7541, result.Item3);
        }
        public void Init()
        {
            const int bytesCount = 10;
            const int primCount  = 10;

            numbers = new(primCount);

            for (int i = 0; i < primCount; ++i)
            {
                numbers.Add(BigIntegerExtension.GetRandomPrime(bytesCount, (b) => b.SolovayStrassenTest(1000)));
            }
        }
        public void ToBigEndianByteArrayTest_BigValue2()
        {
            var value = new BigInteger(904637443760610831);
            var array = BigIntegerExtension.ToBigEndianByteArray(value);

            Assert.IsTrue(array.Length == 8);
            Assert.IsTrue(array[0] == 0x0C);
            Assert.IsTrue(array[1] == 0x8D);
            Assert.IsTrue(array[2] == 0xEA);
            Assert.IsTrue(array[3] == 0xF6);
            Assert.IsTrue(array[4] == 0x4D);
            Assert.IsTrue(array[5] == 0xDE);
            Assert.IsTrue(array[6] == 0x5E);
            Assert.IsTrue(array[7] == 0x0F);
        }
        public void ToBigEndianByteArrayTest_BigValue1()
        {
            var value = new BigInteger(-8726769014922317791);
            var array = BigIntegerExtension.ToBigEndianByteArray(value);

            Assert.IsTrue(array.Length == 8);
            Assert.IsTrue(array[0] == 0x86);
            Assert.IsTrue(array[1] == 0xE4);
            Assert.IsTrue(array[2] == 0x49);
            Assert.IsTrue(array[3] == 0xD1);
            Assert.IsTrue(array[4] == 0x3C);
            Assert.IsTrue(array[5] == 0xAB);
            Assert.IsTrue(array[6] == 0x70);
            Assert.IsTrue(array[7] == 0x21);
        }
Exemple #23
0
        public AffinePoint GetRandomAffinePoint()
        {
            BigIntegerRandom rand = new BigIntegerRandom();
            var        start      = rand.Next(0, P);
            var        x          = start;
            BigInteger t;

            do
            {
                t = (x * (x * x + A) + B).ModPositive(P);
                if (BigIntegerExtension.JacobiSymbol(t, P) != -1)
                {
                    return(new AffinePoint(x, BigIntegerExtension.Sqrt(t).ModPositive(P), this));
                }
                if (x > P - 1)
                {
                    x = -1;
                }
                x++;
            }while (x != start);
            return(GetInfiniteAffinePoint());
        }
        //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);
        }
Exemple #25
0
        private static void GenerateGenerators(string filePath, BigInteger[] primes)
        {
            var generators = BigIntegerExtension.GetPrimitiveRoots(primes);

            FileUtility.WriteArrayInFile(filePath, generators);
        }
Exemple #26
0
 public BigInteger HasseTheorem()
 {
     return(P + 1 + (BigIntegerExtension.Sqrt(P) + 1));
 }
 public void FromBigEndianByteArrayTest_ThrowsArgumentNullException()
 {
     BigIntegerExtension.FromBigEndianByteArray(null);
 }
Exemple #28
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);
        }