Ejemplo n.º 1
0
        /// <summary>
        /// Compare this polynomial to another for equality
        /// </summary>
        ///
        /// <param name="Obj">Object to compare</param>
        ///
        /// <returns>True if equal, otherwise false</returns>
        public override bool Equals(Object Obj)
        {
            if (this == Obj)
            {
                return(true);
            }
            if (Obj == null)
            {
                return(false);
            }

            SparseTernaryPolynomial other = (SparseTernaryPolynomial)Obj;

            if (m_N != other.m_N)
            {
                return(false);
            }
            if (!Compare.IsEqual(m_negOnes, other.m_negOnes))
            {
                return(false);
            }
            if (!Compare.IsEqual(m_ones, other.m_ones))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Generates a "sparse" or "dense" polynomial containing numOnes ints equal to 1,
 /// numNegOnes int equal to -1, and the rest equal to 0.
 /// </summary>
 ///
 /// <param name="N">Number of coeffeients</param>
 /// <param name="NumOnes">Number of ones</param>
 /// <param name="NumNegOnes">Number of negative ones</param>
 /// <param name="Sparse">Create a SparseTernaryPolynomial or DenseTernaryPolynomial</param>
 /// <param name="Rng">Random number generator</param>
 ///
 /// <returns>A ternary polynomial</returns>
 public static ITernaryPolynomial GenerateRandomTernary(int N, int NumOnes, int NumNegOnes, bool Sparse, IRandom Rng)
 {
     if (Sparse)
     {
         return(SparseTernaryPolynomial.GenerateRandom(N, NumOnes, NumNegOnes, Rng));
     }
     else
     {
         return(DenseTernaryPolynomial.GenerateRandom(N, NumOnes, NumNegOnes, Rng));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a <c>ProductFormPolynomial</c> from three random ternary polynomials.
        /// </summary>
        ///
        /// <param name="N">Number of coefficients</param>
        /// <param name="Df1">Number of ones in the first polynomial; also the number of negative ones</param>
        /// <param name="Df2">Number of ones in the second polynomial; also the number of negative ones</param>
        /// <param name="Df3Ones">Number of ones in the third polynomial</param>
        /// <param name="Df3NegOnes">Number of negative ones in the third polynomial</param>
        /// <param name="Rng">Random number generator</param>
        ///
        /// <returns>A random <c>ProductFormPolynomial</c></returns>
        public static ProductFormPolynomial GenerateRandom(int N, int Df1, int Df2, int Df3Ones, int Df3NegOnes, IRandom Rng)
        {
            SparseTernaryPolynomial f1 = null;
            SparseTernaryPolynomial f2 = null;
            SparseTernaryPolynomial f3 = null;

            f1 = SparseTernaryPolynomial.GenerateRandom(N, Df1, Df1, Rng);
            f2 = SparseTernaryPolynomial.GenerateRandom(N, Df2, Df2, Rng);
            f3 = SparseTernaryPolynomial.GenerateRandom(N, Df3Ones, Df3NegOnes, Rng);

            return(new ProductFormPolynomial(f1, f2, f3));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Decodes a polynomial encoded with ToBinary()
        /// </summary>
        ///
        /// <param name="InputStrem">An input stream containing an encoded polynomial</param>
        /// <param name="N">Number of coefficients in the polynomial</param>
        ///
        /// <returns>The decoded polynomial</returns>
        public static ProductFormPolynomial FromBinary(MemoryStream InputStrem, int N)
        {
            SparseTernaryPolynomial f1;

            try
            {
                f1 = SparseTernaryPolynomial.FromBinary(InputStrem, N);
                SparseTernaryPolynomial f2 = SparseTernaryPolynomial.FromBinary(InputStrem, N);
                SparseTernaryPolynomial f3 = SparseTernaryPolynomial.FromBinary(InputStrem, N);

                return(new ProductFormPolynomial(f1, f2, f3));
            }
            catch (IOException ex)
            {
                throw new CryptoAsymmetricException("ProductFormPolynomial:FromBinary", ex.Message, ex);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructs a new polynomial from three sparsely populated ternary polynomials
 /// </summary>
 /// 
 /// <param name="F1">F1 polynomial</param>
 /// <param name="F2">F2 polynomial</param>
 /// <param name="F3">F3 polynomial</param>
 public ProductFormPolynomial(SparseTernaryPolynomial F1, SparseTernaryPolynomial F2, SparseTernaryPolynomial F3)
 {
     this._f1 = F1;
     this._f2 = F2;
     this._f3 = F3;
 }
        private void Verify(SparseTernaryPolynomial poly)
        {
            // make sure ones and negative ones don't share indices
            int[] ones = poly.GetOnes();
            int[] nones = poly.GetNegOnes();

            for (int i = 0; i < ones.Length; i++)
            {
                for (int j = 0; j < nones.Length; j++)
                {
                    if (ones[i] == nones[j])
                        throw new Exception("SparseTernaryPolynomial GenerateRandom test failed!");
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Constructs a new polynomial from three sparsely populated ternary polynomials
 /// </summary>
 ///
 /// <param name="F1">F1 polynomial</param>
 /// <param name="F2">F2 polynomial</param>
 /// <param name="F3">F3 polynomial</param>
 public ProductFormPolynomial(SparseTernaryPolynomial F1, SparseTernaryPolynomial F2, SparseTernaryPolynomial F3)
 {
     m_f1 = F1;
     m_f2 = F2;
     m_f3 = F3;
 }