Sqrt() public abstract method

public abstract Sqrt ( ) : ECFieldElement
return ECFieldElement
Exemplo n.º 1
0
        protected override ECPoint DecompressPoint(
            int yTilde,
            BigInteger X1)
        {
            ECFieldElement x     = FromBigInteger(X1);
            ECFieldElement alpha = x.Multiply(x.Square().Add(a)).Add(b);
            ECFieldElement beta  = alpha.Sqrt();

            //
            // if we can't find a sqrt we haven't got a point on the
            // curve - run!
            //
            if (beta == null)
            {
                throw new ArithmeticException("Invalid point compression");
            }

            BigInteger betaValue = beta.ToBigInteger();
            int        bit0      = betaValue.TestBit(0) ? 1 : 0;

            if (bit0 != yTilde)
            {
                // Use the other root
                beta = FromBigInteger(q.Subtract(betaValue));
            }

            return(new FpPoint(this, x, beta, true));
        }
Exemplo n.º 2
0
        /**
         * Decode a point on this curve from its ASN.1 encoding. The different
         * encodings are taken account of, including point compression for
         * <code>F<sub>p</sub><code> (X9.62 s 4.2.1 pg 17).
         * @return The decoded point.
         */
        public override ECPoint DecodePoint(
            byte[] encoded)
        {
            ECPoint p = null;

            switch (encoded[0])
            {
            // compressed
            case 0x02:
            case 0x03:
                int    ytilde = encoded[0] & 1;
                byte[] i      = new byte[encoded.Length - 1];

                Array.Copy(encoded, 1, i, 0, i.Length);

                ECFieldElement x     = new FpFieldElement(this.q, new BigInteger(1, i));
                ECFieldElement alpha = x.Multiply(x.Square()).Add(x.Multiply(a).Add(b));
                ECFieldElement beta  = alpha.Sqrt();

                //
                // if we can't find a sqrt we haven't got a point on the
                // curve - run!
                //
                if (beta == null)
                {
                    throw new ArithmeticException("Invalid point compression");
                }

                BigInteger betaValue = beta.ToBigInteger();
                int        bit0      = betaValue.TestBit(0) ? 1 : 0;

                if (bit0 != ytilde)
                {
                    // Use the other root
                    beta = new FpFieldElement(q, q.Subtract(betaValue));
                }

                p = new FpPoint(this, x, beta, true);
                break;

            case 0x04:
                byte[] xEnc = new byte[(encoded.Length - 1) / 2];
                byte[] yEnc = new byte[(encoded.Length - 1) / 2];

                Array.Copy(encoded, 1, xEnc, 0, xEnc.Length);
                Array.Copy(encoded, xEnc.Length + 1, yEnc, 0, yEnc.Length);

                p = new FpPoint(this,
                                new FpFieldElement(this.q, new BigInteger(1, xEnc)),
                                new FpFieldElement(this.q, new BigInteger(1, yEnc)));
                break;

            default:
                throw new FormatException("Invalid point encoding " + encoded[0]);
            }

            return(p);
        }
Exemplo n.º 3
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            ECFieldElement eCFieldElement  = this.FromBigInteger(X1);
            ECFieldElement eCFieldElement2 = eCFieldElement.Square().Add(this.A).Multiply(eCFieldElement).Add(this.B);
            ECFieldElement eCFieldElement3 = eCFieldElement2.Sqrt();

            if (eCFieldElement3 == null)
            {
                throw new ArgumentException("Invalid point compression");
            }
            if (eCFieldElement3.TestBitZero() != (yTilde == 1))
            {
                eCFieldElement3 = eCFieldElement3.Negate();
            }
            return(this.CreateRawPoint(eCFieldElement, eCFieldElement3, true));
        }
Exemplo n.º 4
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            //IL_003a: Unknown result type (might be due to invalid IL or missing references)
            ECFieldElement eCFieldElement  = FromBigInteger(X1);
            ECFieldElement eCFieldElement2 = eCFieldElement.Square().Add(A).Multiply(eCFieldElement)
                                             .Add(B);
            ECFieldElement eCFieldElement3 = eCFieldElement2.Sqrt();

            if (eCFieldElement3 == null)
            {
                throw new ArgumentException("Invalid point compression");
            }
            if (eCFieldElement3.TestBitZero() != (yTilde == 1))
            {
                eCFieldElement3 = eCFieldElement3.Negate();
            }
            return(CreateRawPoint(eCFieldElement, eCFieldElement3, withCompression: true));
        }
Exemplo n.º 5
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            ECFieldElement x   = FromBigInteger(X1);
            ECFieldElement rhs = x.Square().Add(A).Multiply(x).Add(B);
            ECFieldElement y   = rhs.Sqrt();

            /*
             * If y is not a square, then we haven't got a point on the curve
             */
            if (y == null)
            {
                throw new ArgumentException("Invalid point compression");
            }

            if (y.TestBitZero() != (yTilde == 1))
            {
                // Use the other root
                y = y.Negate();
            }

            return(CreateRawPoint(x, y, true));
        }
Exemplo n.º 6
0
        protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
        {
            ECFieldElement x     = FromBigInteger(X1);
            ECFieldElement alpha = x.Square().Add(m_a).Multiply(x).Add(m_b);
            ECFieldElement beta  = alpha.Sqrt();

            //
            // if we can't find a sqrt we haven't got a point on the
            // curve - run!
            //
            if (beta == null)
            {
                throw new ArithmeticException("Invalid point compression");
            }

            if (beta.TestBitZero() != (yTilde == 1))
            {
                // Use the other root
                beta = beta.Negate();
            }

            return(new FpPoint(this, x, beta, true));
        }