示例#1
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));
        }
示例#2
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}");
        }
示例#3
0
        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();
        }