コード例 #1
0
        public void GenerateRandomCurve()
        {
            // 1 - generowanie liczby pierwszej przystającej do 3 modulo 4
            p = PrimeHelper.GenerateRandomPrimeForElipticCurve(primeSize);

            pMinus1 = BigInteger.Subtract(p, BigInteger.One);
            while (true)
            {
                // 2 - losowanie a, b, chi
                a = PrimeHelper.GenerateRandom(RandomGenerator.Next(2, primeSize - 1));
                b = PrimeHelper.GenerateRandom(RandomGenerator.Next(2, primeSize - 1));

                // 2.1 - jeśli delta przystaje do 0 to skok do punktu 2
                delta = CalculateDelta(a, b);
                if (delta.Mod(p) != 0)
                {
                    // Krzywa gotowa, można szukać punktu należącego do krzywej
                    break;
                }
            }


            Console.WriteLine($"p:\t{p}");
            Console.WriteLine($"a:\t{a}");
            Console.WriteLine($"b:\t{b}");
            Console.WriteLine($"delta:\t{delta}");
        }
コード例 #2
0
        public Point GenerateRandomPointOnCurve()
        {
            BigInteger x, y, f, legendreFP;

            while (true)
            {
                x = PrimeHelper.GenerateRandom(RandomGenerator.Next(2, primeSize - 1));

                f          = CalculateF(a, b, x, p);
                legendreFP = CalculateLegendreSymbol(f, p);
                if (legendreFP == BigInteger.One)
                {
                    y = BigInteger.ModPow(f, (p + 1) / 4, p);
                    BigInteger left, right;
                    left  = BigInteger.ModPow(y, 2, p);
                    right = (BigInteger.Pow(x, 3) + a * x + b).Mod(p);
                    bool test = left == right;
                    if (test)
                    {
                        break;
                    }
                }
            }

            return(new Point(x, y, a, p));
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Guziec94/ElipticCurve
        static void Main(string[] args)
        {
            ElipticCurve elipticCurve = new ElipticCurve(256);

            elipticCurve.GenerateRandomCurve();

            Point randomPoint = elipticCurve.GenerateRandomPointOnCurve();

            BigInteger nA = PrimeHelper.GenerateRandom(64);
            BigInteger nB = PrimeHelper.GenerateRandom(64);

            Point qA = elipticCurve.ScalarMultiplication(randomPoint, nA);
            Point qB = elipticCurve.ScalarMultiplication(randomPoint, nB);

            if (qA == qB)
            {
                Console.WriteLine("Ok");
            }
            else
            {
                Console.WriteLine("Nie ok");
            }

            Console.ReadKey();
        }
コード例 #4
0
        public Point ScalarMultiplication(Point P, BigInteger n)
        {
            // 1)
            Point Q = P;

            // 2)
            string binaryString = PrimeHelper.BigintegerToBinaryString(n);

            Point R = binaryString[0] == '0'
                ? Point.NeutralElement(P.a, P.p)
                : P;

            // 3)
            for (int i = 1; i < binaryString.Length; i++)
            {
                Q = Q + Q;
                if (binaryString[i] == '1')
                {
                    R = R + Q;
                }
            }

            return(R);
        }