Beispiel #1
0
        /**
         * Tests if the basis is valid.
         * @param h the polynomial h (either from the public key or from this basis)
         * @return <code>true</code> if the basis is valid, <code>false</code> otherwise
         */
        public bool isValid(IntegerPolynomial h)
        {
            if (f.ToIntegerPolynomial().Coeffs.Length != N)
                return false;
            if (fPrime.ToIntegerPolynomial().Coeffs.Length != N)
                return false;

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

            // determine F, G, g from f, fPrime, h using the eqn. fG-Fg=q
            IPolynomial FPoly = basisType == BasisType.STANDARD ? fPrime : f.Multiply(h, q);
            IntegerPolynomial F = FPoly.ToIntegerPolynomial();
            IntegerPolynomial fq = f.ToIntegerPolynomial().InvertFq(q);
            IPolynomial g;
            if (basisType == BasisType.STANDARD)
                g = f.Multiply(h, q);
            else
                g = fPrime;
            IntegerPolynomial G = g.Multiply(F);
            G.Coeffs[0] -= q;
            G = G.Multiply(fq, q);
            G.ModCenter(q);

            // check norms of F and G
            if (!new FGBasis(f, fPrime, h, F, G, q, polyType, basisType, keyNormBoundSq).isNormOk())
                return false;
            // check norms of f and g
            int factor = N / 24;
            if (f.ToIntegerPolynomial().CenteredNormSq(q) * factor >= F.CenteredNormSq(q))
                return false;
            if (g.ToIntegerPolynomial().CenteredNormSq(q) * factor >= G.CenteredNormSq(q))
                return false;

            // check ternarity
            if (polyType == TernaryPolynomialType.SIMPLE)
            {
                if (!f.ToIntegerPolynomial().IsTernary())
                    return false;
                if (!g.ToIntegerPolynomial().IsTernary())
                    return false;
            }
            else
            {
                if (!(f.GetType().IsAssignableFrom(typeof(ProductFormPolynomial))))
                    return false;
                if (!(g.GetType().IsAssignableFrom(typeof(ProductFormPolynomial))))
                    return false;
            }

            return true;
        }
Beispiel #2
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);
        }
Beispiel #3
0
        /**
         * Tests if the basis is valid.
         * @param h the polynomial h (either from the public key or from this basis)
         * @return <code>true</code> if the basis is valid, <code>false</code> otherwise
         */
        public bool isValid(IntegerPolynomial h)
        {
            if (f.ToIntegerPolynomial().Coeffs.Length != N)
            {
                return(false);
            }
            if (fPrime.ToIntegerPolynomial().Coeffs.Length != N)
            {
                return(false);
            }

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

            // determine F, G, g from f, fPrime, h using the eqn. fG-Fg=q
            IPolynomial       FPoly = basisType == BasisType.STANDARD ? fPrime : f.Multiply(h, q);
            IntegerPolynomial F     = FPoly.ToIntegerPolynomial();
            IntegerPolynomial fq    = f.ToIntegerPolynomial().InvertFq(q);
            IPolynomial       g;

            if (basisType == BasisType.STANDARD)
            {
                g = f.Multiply(h, q);
            }
            else
            {
                g = fPrime;
            }
            IntegerPolynomial G = g.Multiply(F);

            G.Coeffs[0] -= q;
            G            = G.Multiply(fq, q);
            G.ModCenter(q);

            // check norms of F and G
            if (!new FGBasis(f, fPrime, h, F, G, q, polyType, basisType, keyNormBoundSq).isNormOk())
            {
                return(false);
            }
            // check norms of f and g
            int factor = N / 24;

            if (f.ToIntegerPolynomial().CenteredNormSq(q) * factor >= F.CenteredNormSq(q))
            {
                return(false);
            }
            if (g.ToIntegerPolynomial().CenteredNormSq(q) * factor >= G.CenteredNormSq(q))
            {
                return(false);
            }

            // check ternarity
            if (polyType == TernaryPolynomialType.SIMPLE)
            {
                if (!f.ToIntegerPolynomial().IsTernary())
                {
                    return(false);
                }
                if (!g.ToIntegerPolynomial().IsTernary())
                {
                    return(false);
                }
            }
            else
            {
                if (!(f.GetType().IsAssignableFrom(typeof(ProductFormPolynomial))))
                {
                    return(false);
                }
                if (!(g.GetType().IsAssignableFrom(typeof(ProductFormPolynomial))))
                {
                    return(false);
                }
            }

            return(true);
        }