Example #1
0
        private void IsValid()
        {
            // test valid key pairs
            NtruSign         ntru = null;
            SignatureKeyPair kp   = null;

            SignatureParameters[] paramSets = new SignatureParameters[] { SignatureParameters.TEST157, SignatureParameters.TEST157_PROD };
            foreach (SignatureParameters param in paramSets)
            {
                ntru = new NtruSign(param);
                kp   = ntru.generateKeyPair();
                Assert.True(kp.isValid());
            }

            // test an invalid key pair
            int q = kp.pub.q;

            kp.pub.h.Multiply(101);   // make h invalid
            kp.pub.h.ModPositive(q);
            Assert.False(kp.isValid());
            int inv101 = IntEuclidean.Calculate(101, q).X;

            kp.pub.h.Multiply(inv101);   // restore h
            kp.pub.h.ModPositive(q);
            IntegerPolynomial f = kp.priv.getBasis(0).f.ToIntegerPolynomial();

            f.Multiply(3);   // make f invalid
            kp.priv.getBasis(0).f = f;
            Assert.False(kp.isValid());
        }
Example #2
0
        //private static volatile bool IS_64_BITNESS_KNOWN;
        //private static volatile bool IS_64_BIT_JVM;

        /**
         * Calculates the inverse of n mod modulus
         */
        public static int Invert(int n, int modulus)
        {
            n %= modulus;
            if (n < 0)
            {
                n += modulus;
            }
            return(IntEuclidean.Calculate(n, modulus).x);
        }
        public override void PerformTest()
        {
            IntEuclidean r = IntEuclidean.Calculate(120, 23);

            Assert.AreEqual(-9, r.x);
            Assert.AreEqual(47, r.y);
            Assert.AreEqual(1, r.gcd);

            r = IntEuclidean.Calculate(126, 231);
            Assert.AreEqual(2, r.x);
            Assert.AreEqual(-1, r.y);
            Assert.AreEqual(21, r.gcd);
        }
Example #4
0
        /// <summary>
        /// IntEuclidean tests
        /// </summary>
        ///
        /// <returns>State</returns>
        public string Test()
        {
            try
            {
                IntEuclidean r = IntEuclidean.Calculate(120, 23);
                if (!r.X.Equals(-9))
                {
                    throw new Exception("IntEuclidean failed r.X!");
                }
                if (!r.Y.Equals(47))
                {
                    throw new Exception("IntEuclidean failed r.Y!");
                }
                if (!r.GCD.Equals(1))
                {
                    throw new Exception("IntEuclidean failed r.GCD!");
                }
                OnProgress(new TestEventArgs("Passed round 1 X, Y and GCD value comparisons"));

                r = IntEuclidean.Calculate(126, 231);
                if (!r.X.Equals(2))
                {
                    throw new Exception("IntEuclidean failed r.X!");
                }
                if (!r.Y.Equals(-1))
                {
                    throw new Exception("IntEuclidean failed r.Y!");
                }
                if (!r.GCD.Equals(21))
                {
                    throw new Exception("IntEuclidean failed r.GCD!");
                }
                OnProgress(new TestEventArgs("Passed round 2 X, Y and GCD value comparisons"));

                return(SUCCESS);
            }
            catch (Exception Ex)
            {
                string message = Ex.Message == null ? "" : Ex.Message;
                throw new Exception(FAILURE + message);
            }
        }
Example #5
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);
        }