Beispiel #1
0
        public void testMult()
        {
            BigDecimalPolynomial a = new BigDecimalPolynomial(new BigIntPolynomial(new IntegerPolynomial(new int[] { 4, -1, 9, 2, 1, -5, 12, -7, 0, -9, 5 })));
            BigDecimalPolynomial b = new BigDecimalPolynomial(new BigIntPolynomial(new IntegerPolynomial(new int[] { -6, 0, 0, 13, 3, -2, -4, 10, 11, 2, -1 })));
            BigDecimalPolynomial c = a.Multiply(b);

            decimal[] expectedCoeffs = new BigDecimalPolynomial(new BigIntPolynomial(new IntegerPolynomial(new int[] { 2, -189, 77, 124, -29, 0, -75, 124, -49, 267, 34 }))).getCoeffs();

            decimal[] cCoeffs = c.getCoeffs();

            Assert.AreEqual(expectedCoeffs.Length, cCoeffs.Length);
            for (int i = 0; i != cCoeffs.Length; i++)
            {
                Assert.AreEqual(expectedCoeffs[i], cCoeffs[i]);
            }

            // multiply a polynomial by its inverse modulo 2048 and check that the result is 1
            SecureRandom      random = new SecureRandom();
            IntegerPolynomial d, dInv;

            do
            {
                d    = DenseTernaryPolynomial.GenerateRandom(1001, 333, 334, random);
                dInv = d.InvertFq(2048);
            }while (dInv == null);

            d.Mod(2048);
            BigDecimalPolynomial e = new BigDecimalPolynomial(new BigIntPolynomial(d));
            BigIntPolynomial     f = new BigIntPolynomial(dInv);
            IntegerPolynomial    g = new IntegerPolynomial(e.Multiply(f).round());

            g.ModPositive(2048);
            Assert.True(g.EqualsOne());
        }
Beispiel #2
0
        // tests if a*b=1 (mod modulus)
        private void verifyInverse(IntegerPolynomial a, IntegerPolynomial b, int modulus)
        {
            IntegerPolynomial c = a.Multiply(b, modulus);

            for (int i = 1; i < c.coeffs.Length; i++)
            {
                c.coeffs[i] %= modulus;
            }
            c.EnsurePositive(modulus);
            Assert.True(c.EqualsOne());
        }
        // tests if a*b=1 (mod modulus)
        private void VerifyInverse(IntegerPolynomial a, IntegerPolynomial b, int modulus)
        {
            IntegerPolynomial c = a.Multiply(b, modulus);

            for (int i = 1; i < c.Coeffs.Length; i++)
            {
                c.Coeffs[i] %= modulus;
            }

            c.EnsurePositive(modulus);
            if (!c.EqualsOne())
            {
                throw new Exception("IntegerPolynomialTest Modulus inverse test failed!");
            }
        }
        /// <summary>
        /// Test the validity of the BigDecimalPolynomial implementation
        /// </summary>
        ///
        /// <returns>State</returns>
        public string Test()
        {
            try
            {
                BigDecimalPolynomial a = CreateBigDecimalPolynomial(new int[] { 4, -1, 9, 2, 1, -5, 12, -7, 0, -9, 5 });
                BigIntPolynomial     b = new BigIntPolynomial(new IntegerPolynomial(new int[] { -6, 0, 0, 13, 3, -2, -4, 10, 11, 2, -1 }));
                BigDecimalPolynomial c = a.Multiply(b);
                if (!Compare.AreEqual(c.Coeffs, CreateBigDecimalPolynomial(new int[] { 2, -189, 77, 124, -29, 0, -75, 124, -49, 267, 34 }).Coeffs))
                {
                    throw new Exception("The BigDecimalPolynomial test failed!");
                }
                // multiply a polynomial by its inverse modulo 2048 and check that the result is 1
                IntegerPolynomial d, dInv;
                CSPRng            rng = new CSPRng();

                do
                {
                    d    = DenseTernaryPolynomial.GenerateRandom(1001, 333, 334, rng);
                    dInv = d.InvertFq(2048);
                } while (dInv == null);

                d.Mod(2048);
                BigDecimalPolynomial e = CreateBigDecimalPolynomial(d.Coeffs);
                BigIntPolynomial     f = new BigIntPolynomial(dInv);
                IntegerPolynomial    g = new IntegerPolynomial(e.Multiply(f).Round());
                g.ModPositive(2048);

                if (!g.EqualsOne())
                {
                    throw new Exception("The BigDecimalPolynomial test failed!");
                }
                OnProgress(new TestEventArgs("Passed BigDecimalPolynomial tests"));

                return(SUCCESS);
            }
            catch (Exception Ex)
            {
                string message = Ex.Message == null ? "" : Ex.Message;
                throw new Exception(FAILURE + message);
            }
        }