/// <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 (_N != other._N)
            {
                return(false);
            }
            if (!Compare.AreEqual(_negOnes, other._negOnes))
            {
                return(false);
            }
            if (!Compare.AreEqual(_ones, other._ones))
            {
                return(false);
            }

            return(true);
        }
 /// <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));
     }
 }
        /// <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));
        }
        /// <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 NTRUException("ProductFormPolynomial:FromBinary", ex.Message, ex);
            }
        }
Example #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;
 }
Example #6
0
        /// <summary>
        /// Read a Private key from a stream
        /// </summary>
        /// 
        /// <param name="KeyStream">The stream containing the key</param>
        /// 
        /// <returns>An initialized NTRUPrivateKey class</returns>
        /// 
        /// <exception cref="NTRUException">Thrown if the stream can not be read</exception>
        public static NTRUPrivateKey From(MemoryStream KeyStream)
        {
            BinaryReader dataStream = new BinaryReader(KeyStream);

            try
            {
                // ins.Position = 0; wrong here, ins pos is wrong
                int n = IntUtils.ReadShort(KeyStream);
                int q = IntUtils.ReadShort(KeyStream);
                byte flags = dataStream.ReadByte();
                bool sparse = (flags & 1) != 0;
                bool fastFp = (flags & 2) != 0;
                IPolynomial t;

                TernaryPolynomialType polyType = (flags & 4) == 0 ?
                    TernaryPolynomialType.SIMPLE :
                    TernaryPolynomialType.PRODUCT;

                if (polyType == TernaryPolynomialType.PRODUCT)
                {
                    t = ProductFormPolynomial.FromBinary(KeyStream, n);
                }
                else
                {
                    IntegerPolynomial fInt = IntegerPolynomial.FromBinary3Tight(KeyStream, n);

                    if (sparse)
                        t = new SparseTernaryPolynomial(fInt);
                    else
                        t = new DenseTernaryPolynomial(fInt);
                }

                // Initializes fp from t
                IntegerPolynomial fp;
                if (fastFp)
                {
                    fp = new IntegerPolynomial(n);
                    fp.Coeffs[0] = 1;
                }
                else
                {
                    fp = t.ToIntegerPolynomial().InvertF3();
                }

                return new NTRUPrivateKey(t, fp, n, q, sparse, fastFp, polyType);
            }
            catch (IOException ex)
            {
                throw new NTRUException("NTRUPrivateKey:From", ex.Message, ex);
            }
        }
 /// <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;
 }