示例#1
0
        /// <summary>
        /// Deterministically generates a blinding polynomial from a seed and a message representative
        /// </summary>
        ///
        /// <param name="Seed">The seed value</param>
        ///
        /// <returns>A blinding polynomial</returns>
        private IPolynomial GenerateBlindingPoly(byte[] Seed)
        {
            int            N  = _encParams.N;
            IndexGenerator ig = new IndexGenerator(Seed, _encParams);

            if (_encParams.PolyType == TernaryPolynomialType.PRODUCT) //.8, .6
            {
                SparseTernaryPolynomial r1 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR1);
                SparseTernaryPolynomial r2 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR2);
                SparseTernaryPolynomial r3 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR3);

                return(new ProductFormPolynomial(r1, r2, r3));
            }
            else
            {
                if (_encParams.Sparse)
                {
                    return(SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR));
                }
                else
                {
                    return(DenseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR));
                }
            }
        }
示例#2
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);
            }
        }
        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);
        }
示例#4
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);
        }
        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!");
                    }
                }
            }
        }
        /** 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!");
            }
        }