Beispiel #1
0
        /// <summary>
        /// Tests if the key pair is valid.
        /// <para>See IEEE 1363.1 section 9.2.4.1.</para>
        /// </summary>
        ///
        /// <returns>if the key pair is valid, <c>true</c> otherwise false</returns>
        public bool IsValid()
        {
            int N = ((NTRUPrivateKey)PrivateKey).N;
            int q = ((NTRUPrivateKey)PrivateKey).Q;
            TernaryPolynomialType polyType = ((NTRUPrivateKey)PrivateKey).PolyType;

            if (((NTRUPublicKey)PublicKey).N != N)
            {
                return(false);
            }
            if (((NTRUPublicKey)PublicKey).Q != q)
            {
                return(false);
            }
            if (((NTRUPrivateKey)PrivateKey).T.ToIntegerPolynomial().Coeffs.Length != N)
            {
                return(false);
            }

            IntegerPolynomial h = ((NTRUPublicKey)PublicKey).H.ToIntegerPolynomial();

            if (h.Coeffs.Length != N)
            {
                return(false);
            }
            if (!h.IsReduced(q))
            {
                return(false);
            }

            IntegerPolynomial f = ((NTRUPrivateKey)PrivateKey).T.ToIntegerPolynomial();

            if (polyType == TernaryPolynomialType.SIMPLE && !f.IsTernary())
            {
                return(false);
            }
            // if t is a ProductFormPolynomial, ternarity of f1,f2,f3 doesn't need to be verified
            if (polyType == TernaryPolynomialType.PRODUCT && !(((NTRUPrivateKey)PrivateKey).T.GetType().Equals(typeof(ProductFormPolynomial))))
            {
                return(false);
            }

            if (polyType == TernaryPolynomialType.PRODUCT)
            {
                f.Multiply(3);
                f.Coeffs[0] += 1;
                f.ModPositive(q);
            }

            // the key generator pre-multiplies h by 3, so divide by 9 instead of 3
            int inv9 = IntEuclidean.Calculate(9, q).X;   // 9^-1 mod q

            IntegerPolynomial g = f.Multiply(h, q);

            g.Multiply(inv9);
            g.ModCenter(q);

            if (!g.IsTernary())
            {
                return(false);
            }

            int dg = N / 3;   // see EncryptionParameters.Initialize()

            if (g.Count(1) != dg)
            {
                return(false);
            }
            if (g.Count(-1) != dg - 1)
            {
                return(false);
            }

            return(true);
        }