Exemplo n.º 1
0
        public bool TryFindY(BigInteger x, byte firstByte, out BigInteger y)
        {
            if (firstByte != 2 && firstByte != 3)
            {
                y = 0;
                return(false);
            }
            if (x.Sign < 1)
            {
                y = 0;
                return(false);
            }


            // y2 = x3 + ax + b (mod p)
            BigInteger right = (BigInteger.Pow(x, 3) + BigInteger.Multiply(curve.A, x) + curve.B) % curve.P;

            try
            {
                y = SquareRoot.FindSquareRoot(right, curve.P);
            }
            catch (ArithmeticException)
            {
                y = 0;
                return(false);
            }

            if (firstByte == 2 && !y.IsEven)
            {
                y = PointNegChecked(new EllipticCurvePoint(x, y)).Y;
                return(true);
            }
            else if (firstByte == 3 && y.IsEven)
            {
                y = PointNegChecked(new EllipticCurvePoint(x, y)).Y;
                return(true);
            }
            else
            {
                return(true);
            }
        }