Example #1
0
        private void MultTest()
        {
            IntegerPolynomial i1 = new IntegerPolynomial(new int[] { 1368, 2047, 672, 871, 1662, 1352, 1099, 1608 });
            IntegerPolynomial i2 = new IntegerPolynomial(new int[] { 1729, 1924, 806, 179, 1530, 1381, 1695, 60 });
            LongPolynomial2   a  = new LongPolynomial2(i1);
            LongPolynomial2   b  = new LongPolynomial2(i2);
            IntegerPolynomial c1 = i1.Multiply(i2, 2048);
            IntegerPolynomial c2 = a.Multiply(b).ToIntegerPolynomial();

            if (!Compare.AreEqual(c1.Coeffs, c2.Coeffs))
            {
                throw new Exception("LongPolynomial2 multiply test failed!");
            }

            // test 10 random polynomials
            Random rng = new Random();

            for (int i = 0; i < 10; i++)
            {
                int N = 2 + rng.Next(2000);
                i1 = (IntegerPolynomial)PolynomialGeneratorForTesting.GenerateRandom(N, 2048);
                i2 = (IntegerPolynomial)PolynomialGeneratorForTesting.GenerateRandom(N, 2048);
                a  = new LongPolynomial2(i1);
                b  = new LongPolynomial2(i2);
                c1 = i1.Multiply(i2);
                c1.ModPositive(2048);
                c2 = a.Multiply(b).ToIntegerPolynomial();

                if (!Compare.AreEqual(c1.Coeffs, c2.Coeffs))
                {
                    throw new Exception("LongPolynomial2 multiply test failed!");
                }
            }
        }
        private void DivTest()
        {
            Random rng = new Random();

            for (int i = 0; i < 10; i++)
            {
                IntegerPolynomial poly = (IntegerPolynomial)PolynomialGeneratorForTesting.GenerateRandom(439, 10000);

                // test special case: division by 2048
                IntegerPolynomial a = poly.Clone();
                a.Divide(2048);
                for (int j = 0; j < poly.Coeffs.Length; j++)
                {
                    if (!Compare.Equals((int)Math.Floor((((double)poly.Coeffs[j]) / 2048) + 0.5), a.Coeffs[j]))
                    {
                        throw new Exception("IntegerPolynomialTest division test failed!");
                    }
                }
                // test the general case
                a = poly.Clone();
                int k = rng.Next(2047) + 1;
                a.Divide(k);
                for (int j = 0; j < poly.Coeffs.Length; j++)
                {
                    if (!Compare.Equals((int)Math.Floor(((double)poly.Coeffs[j] / k) + 0.5), a.Coeffs[j]))
                    {
                        throw new Exception("IntegerPolynomialTest division test failed!");
                    }
                }
            }
        }
        private void ToBinary4()
        {
            IntegerPolynomial a = (IntegerPolynomial)PolynomialGeneratorForTesting.GenerateRandom(743, 2048);

            if (!Compare.AreEqual(a.ToBinary(4), a.ToBinary4()))
            {
                throw new Exception("IntegerPolynomialTest ToBinary4 test failed!");
            }
        }
        private void MultTest()
        {
            CSPRng rng = new CSPRng();
            ProductFormPolynomial p1 = ProductFormPolynomial.GenerateRandom(_N, _df1, _df2, _df3, _df3 - 1, rng);
            IntegerPolynomial     p2 = PolynomialGeneratorForTesting.GenerateRandom(_N, _Q);
            IntegerPolynomial     p3 = p1.Multiply(p2);
            IntegerPolynomial     p4 = p1.ToIntegerPolynomial().Multiply(p2);

            if (!Compare.Equals(p3, p4))
            {
                throw new Exception("ProductFormPolynomial multiplication test failed!");
            }
        }
        private void AddTest()
        {
            NTRUParameters     param = NTRUParamSets.EES1087EP2;
            IntegerPolynomial  a     = PolynomialGeneratorForTesting.GenerateRandom(param.N, param.Q);
            ITernaryPolynomial b     = PolynomialGeneratorForTesting.generateRandom(1087);

            IntegerPolynomial c1 = a.Clone();

            c1.Add(b.ToIntegerPolynomial());

            IntegerPolynomial c2 = a.Clone();

            c2.Add(b);

            if (!Compare.Equals(c1, c2))
            {
                throw new Exception("IntegerPolynomialTest addition test failed!");
            }
        }