public bool IsPointOnCurve(MyBigInteger x, MyBigInteger y)
        {
            var result = x.ModPow(3, Prime) + A * x + _b - y.ModPow(2, Prime);

            if (result % Prime == new MyBigInteger(0f))
            {
                return(true);
            }
            throw new Exception();
        }
Example #2
0
        public List <MyBigInteger> ModSqrt(MyBigInteger modulus)
        {
            if (modulus is null || modulus == 0 || modulus._value.Sign == -1)
            {
                return(null);
            }

            var result = new List <MyBigInteger>();

            //If the indicators value is "modulus - 1" then there is no modular square root
            var indicator = ModPow((modulus - 1) / 2, modulus);

            if (indicator == modulus - 1)
            {
                return(result);
            }

            //Special case for faster solution
            if (modulus % 4 == 3)
            {
                var exponant    = (modulus + 1) / 4;
                var firstResult = ModPow(exponant, modulus);

                if (firstResult * firstResult % modulus == this)
                {
                    result.Add(firstResult);
                    result.Add(firstResult.Negate());
                }

                return(result);
            }

            var          q = modulus - 1;
            MyBigInteger s = 0;

            while (q.IsEven)
            {
                q = q / 2;
                s++;
            }

            MyBigInteger z = 1;

            while (z.ModPow((modulus - 1) / 2, modulus) != modulus - 1)
            {
                z++;
            }

            var m = s;
            var c = z.ModPow(q, modulus);
            var t = ModPow(q, modulus);
            var r = ModPow((q + 1) / 2, modulus);

            while (true)
            {
                if (t == 0)
                {
                    return(result);
                }
                if (t == 1)
                {
                    if (r * r % modulus != this)
                    {
                        return(result);
                    }

                    result.Add(r);
                    result.Add(r.Negate());

                    return(result);
                }

                MyBigInteger i = 1;
                while (t.ModPow(MyConstants.Two ^ i, modulus) != 1 && i < m - 1)
                {
                    i++;
                }

                var b = c.ModPow(MyConstants.Two.ModPow(m - i - 1, modulus), modulus);
                m = i;
                c = b.ModPow(MyConstants.Two, modulus);
                t = t * c % modulus;
                r = r * b % modulus;
            }
        }