public void testFromToBinary()
        {
            SecureRandom            random      = new SecureRandom();
            SparseTernaryPolynomial poly1       = SparseTernaryPolynomial.GenerateRandom(1000, 100, 101, random);
            MemoryStream            poly1Stream = new MemoryStream(poly1.ToBinary());
            SparseTernaryPolynomial poly2       = SparseTernaryPolynomial.FromBinary(poly1Stream, 1000, 100, 101);

            Assert.AreEqual(poly1.GetOnes(), poly2.GetOnes());
            Assert.AreEqual(poly1.GetNegOnes(), poly2.GetNegOnes());
            Assert.AreEqual(poly1.ToIntegerPolynomial().coeffs, poly2.ToIntegerPolynomial().coeffs);
        }
Пример #2
0
 /**
  * Generates a "sparse" or "dense" polynomial containing numOnes ints equal to 1,
  * numNegOnes int equal to -1, and the rest equal to 0.
  *
  * @param N
  * @param numOnes
  * @param numNegOnes
  * @param sparse     whether to create a {@link SparseTernaryPolynomial} or {@link DenseTernaryPolynomial}
  * @return a ternary polynomial
  */
 public static ITernaryPolynomial GenerateRandomTernary(int N, int numOnes, int numNegOnes, bool sparse, SecureRandom random)
 {
     if (sparse)
     {
         return(SparseTernaryPolynomial.GenerateRandom(N, numOnes, numNegOnes, random));
     }
     else
     {
         return(DenseTernaryPolynomial.GenerateRandom(N, numOnes, numNegOnes, random));
     }
 }
        private void FromToBinary()
        {
            CSPRng rng = new CSPRng();
            int    N   = 1000;
            SparseTernaryPolynomial poly1       = SparseTernaryPolynomial.GenerateRandom(N, 100, 101, rng);
            MemoryStream            poly1Stream = new MemoryStream(poly1.ToBinary());
            SparseTernaryPolynomial poly2       = SparseTernaryPolynomial.FromBinary(poly1Stream, N);

            if (!Compare.Equals(poly1, poly2))
            {
                throw new Exception("SparseTernaryPolynomial FromToBinary test failed!");
            }
        }
        private void GenerateRandom()
        {
            CSPRng rng = new CSPRng();

            Verify(SparseTernaryPolynomial.GenerateRandom(743, 248, 248, rng));

            for (int i = 0; i < 10; i++)
            {
                int N          = rng.Next(2000) + 10;
                int numOnes    = rng.Next(N);
                int numNegOnes = rng.Next(N - numOnes);
                Verify(SparseTernaryPolynomial.GenerateRandom(N, numOnes, numNegOnes, rng));
            }
        }
        /**
         * tests mult(IntegerPolynomial) and mult(BigIntPolynomial)
         */
        public void testMult()
        {
            SecureRandom            random = new SecureRandom();
            SparseTernaryPolynomial p1     = SparseTernaryPolynomial.GenerateRandom(1000, 500, 500, random);
            IntegerPolynomial       p2     = DenseTernaryPolynomial.GenerateRandom(1000, random);

            IntegerPolynomial prod1 = p1.Multiply(p2);
            IntegerPolynomial prod2 = p1.Multiply(p2);

            Assert.AreEqual(prod1.coeffs, prod2.coeffs);

            BigIntPolynomial p3    = new BigIntPolynomial(p2);
            BigIntPolynomial prod3 = p1.Multiply(p3);

            Assert.AreEqual((new BigIntPolynomial(prod1)).coeffs, prod3.coeffs);
        }
        /** tests mult(IntegerPolynomial) and mult(BigIntPolynomial) */
        private void MultTest()
        {
            CSPRng rng = new CSPRng();
            SparseTernaryPolynomial p1 = SparseTernaryPolynomial.GenerateRandom(1000, 500, 500, rng);
            IntegerPolynomial       p2 = PolynomialGeneratorForTesting.generateRandom(1000);

            IntegerPolynomial prod1 = p1.Multiply(p2);

            prod1 = p1.Multiply(p2);
            IntegerPolynomial prod2 = p1.Multiply(p2);

            if (!Compare.Equals(prod1, prod2))
            {
                throw new Exception("SparseTernaryPolynomial multiplication test failed!");
            }

            BigIntPolynomial p3    = new BigIntPolynomial(p2);
            BigIntPolynomial prod3 = p1.Multiply(p3);

            if (!Compare.Equals(new BigIntPolynomial(prod1), prod3))
            {
                throw new Exception("SparseTernaryPolynomial multiplication test failed!");
            }
        }