Beispiel #1
0
        static double Decrypt(int Iterations, NTRUParameters Param)
        {
            NTRUKeyGenerator   mkgen = new NTRUKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(64);
            byte[]    rtext = new byte[64];
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (NTRUEncrypt mpe = new NTRUEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);
                ctext = mpe.Encrypt(ptext);
                mpe.Initialize(akp);

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                {
                    rtext = mpe.Decrypt(ctext);
                }
                runTimer.Stop();
            }

            return(runTimer.Elapsed.TotalMilliseconds);
        }
Beispiel #2
0
        static double Decrypt(int Iterations, RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(Param.N >> 3);
            byte[]    rtext = new byte[Param.N >> 3];
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);
                ctext = mpe.Encrypt(ptext);
                mpe.Initialize(akp.PrivateKey);

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                {
                    rtext = mpe.Decrypt(ctext);
                }
                runTimer.Stop();
            }

            //if (!Compare.AreEqual(ptext, rtext))
            //    throw new Exception("Encryption test: decryption failure!");

            return(runTimer.Elapsed.TotalMilliseconds);
        }
Beispiel #3
0
        // tests key pair generation from a passphrase
        private void GenerateKeyPair()
        {
            NTRUParameters param = (NTRUParameters)NTRUParamSets.EES1087EP2FAST.DeepCopy();

            byte[] passphrase = Encoding.Unicode.GetBytes("password123");
            byte[] salt       = new CSPRng().GetBytes(16);

            NTRUKeyPair kp1;

            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false)) // note: parallel must be turned off with passphrase prng
                kp1 = (NTRUKeyPair)kg.GenerateKeyPair(passphrase, salt);
            NTRUKeyPair kp2;

            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false))
                kp2 = (NTRUKeyPair)kg.GenerateKeyPair(passphrase, salt);

            NTRUEncrypt ntru  = new NTRUEncrypt(param);
            NTRUEncrypt ntru2 = new NTRUEncrypt(NTRUParamSets.EES1087EP2FAST);

            if (!kp1.Equals(kp2))
            {
                throw new Exception("NtruEncryptTest: key pair generation test failed!");
            }

            salt = ntru.GenerateSalt();
            NTRUKeyPair kp3;

            using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param, false))
                kp3 = (NTRUKeyPair)kg.GenerateKeyPair(passphrase, salt);

            if (!Compare.False(kp1.Equals(kp3)))
            {
                throw new Exception("NtruEncryptTest: key pair generation test failed!");
            }
        }
        private void FromToBinary()
        {
            CSPRng rng = new CSPRng();
            ProductFormPolynomial p1 = ProductFormPolynomial.GenerateRandom(_N, _df1, _df2, _df3, _df3 - 1, rng);

            byte[] bin1 = p1.ToBinary();
            ProductFormPolynomial p2 = ProductFormPolynomial.FromBinary(bin1, _N);

            if (!Compare.Equals(p1, p2))
            {
                throw new Exception("ProductFormPolynomial FromToBinary test failed!");
            }
        }
        private void MultTest()
        {
            CSPRng rng = new CSPRng();
            ProductFormPolynomial p1 = ProductFormPolynomial.GenerateRandom(_N, _df1, _df2, _df3, _df3 - 1, rng);
            IntegerPolynomial     p2 = PolynomialGeneratorForTesting.GenerateRandom(_N, _Q);
            IntegerPolynomial     p3 = p1.Multiply(p2);
            IntegerPolynomial     p4 = p1.ToIntegerPolynomial().Multiply(p2);

            if (!Compare.Equals(p3, p4))
            {
                throw new Exception("ProductFormPolynomial multiplication test failed!");
            }
        }
        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));
            }
        }
        /// <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);
            }
        }
Beispiel #9
0
        private void DigestTest()
        {
            CSPRng         rng   = new CSPRng();
            NTRUParameters param = (NTRUParameters)NTRUParamSets.EES1087EP2.DeepCopy();

            for (int i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    param.Digest = Digests.Blake512;//blake512
                }
                else if (i == 1)
                {
                    param.Digest = Digests.Keccak512;//keccak512
                }
                else
                {
                    param.Digest = Digests.Skein512;//skein512
                }
                NTRUKeyPair kp;
                using (NTRUKeyGenerator kg = new NTRUKeyGenerator(param))
                    kp = (NTRUKeyPair)kg.GenerateKeyPair();

                using (NTRUEncrypt ntru = new NTRUEncrypt(param))
                {
                    byte[] plainText = rng.GetBytes(32);
                    ntru.Initialize(kp.PublicKey);
                    byte[] encrypted = ntru.Encrypt(plainText);
                    ntru.Initialize(kp);
                    byte[] decrypted = ntru.Decrypt(encrypted);

                    if (!Compare.AreEqual(plainText, decrypted))
                    {
                        throw new Exception("NtruEncryptTest: digest test failed!");
                    }
                }
            }
        }
Beispiel #10
0
        static double Encrypt(int Iterations, RLWEParameters Param)
        {
            RLWEKeyGenerator   mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp   = mkgen.GenerateKeyPair();

            byte[]    ptext = new CSPRng().GetBytes(Param.N >> 3);
            byte[]    ctext;
            Stopwatch runTimer = new Stopwatch();

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                {
                    ctext = mpe.Encrypt(ptext);
                }
                runTimer.Stop();
            }

            return(runTimer.Elapsed.TotalMilliseconds);
        }
        /// <summary>
        /// Test the validity of the DenseTernaryPolynomial implementation
        /// </summary>
        ///
        /// <returns>State</returns>
        public string Test()
        {
            try
            {
                CheckTernarity(PolynomialGeneratorForTesting.generateRandom(1499));

                CSPRng rng = new CSPRng();
                for (int i = 0; i < 10; i++)
                {
                    int N          = rng.Next(2000) + 10;
                    int numOnes    = rng.Next(N);
                    int numNegOnes = rng.Next(N - numOnes);
                    CheckTernarity(DenseTernaryPolynomial.GenerateRandom(N, numOnes, numNegOnes, rng));
                }
                OnProgress(new TestEventArgs("Passed DenseTernaryPolynomial Ternarity"));

                return(SUCCESS);
            }
            catch (Exception Ex)
            {
                string message = Ex.Message == null ? "" : Ex.Message;
                throw new Exception(FAILURE + message);
            }
        }
        /** 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!");
            }
        }