Beispiel #1
0
        public void testInvertFq()
        {
            SecureRandom random = new SecureRandom();
            // Verify an example from the NTRU tutorial
            IntegerPolynomial a = new IntegerPolynomial(new int[] { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 });
            IntegerPolynomial b = a.InvertFq(32);

            assertEqualsMod(new int[] { 5, 9, 6, 16, 4, 15, 16, 22, 20, 18, 30 }, b.coeffs, 32);
            verifyInverse(a, b, 32);

            // test 3 random polynomials
            int numInvertible = 0;

            while (numInvertible < 3)
            {
                a = DenseTernaryPolynomial.GenerateRandom(853, random);
                b = a.InvertFq(2048);
                if (b != null)
                {
                    numInvertible++;
                    verifyInverse(a, b, 2048);
                }
            }

            // test a non-invertible polynomial
            a = new IntegerPolynomial(new int[] { -1, 0, 1, 1, 0, 0, -1, 0, -1, 0, 1 });
            b = a.InvertFq(32);
            Assert.IsNull(b);
        }
        private void InvertFq()
        {
            // Verify an example from the NTRU tutorial
            IntegerPolynomial a = new IntegerPolynomial(new int[] { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 });
            IntegerPolynomial b = a.InvertFq(32);

            AssertEqualsMod(new int[] { 5, 9, 6, 16, 4, 15, 16, 22, 20, 18, 30 }, b.Coeffs, 32);
            VerifyInverse(a, b, 32);

            // test 3 random polynomials
            int numInvertible = 0;

            while (numInvertible < 3)
            {
                a = (IntegerPolynomial)PolynomialGeneratorForTesting.generateRandom(853);
                b = a.InvertFq(2048);
                if (b != null)
                {
                    numInvertible++;
                    VerifyInverse(a, b, 2048);
                }
            }

            // test a non-invertible polynomial
            a = new IntegerPolynomial(new int[] { -1, 0, 1, 1, 0, 0, -1, 0, -1, 0, 1 });
            b = a.InvertFq(32);

            if (b != null)
            {
                throw new Exception("IntegerPolynomialTest InvertFq test failed!");
            }
        }