public void TestSecp256k1ElGamalBinaryDoubleAndAdd()
        {
            TimeSpan[] setup_times = new TimeSpan[num_runs];
            TimeSpan[] key_generation_times = new TimeSpan[num_runs];
            TimeSpan[] encryption_times = new TimeSpan[num_runs];
            TimeSpan[] decryption_times = new TimeSpan[num_runs];

            for (int i = 0; i < num_runs; i++)
            {
                var stopwatch = Stopwatch.StartNew();

                var curve = CurveFactory.secp256k1;
                WeierstrassCurvePoint.Multiplier = new BinaryDoubleAndAddPointMultiplier();
                var encoder = new ProbabilisticWeierstrassMessageEncoder(curve, new BigInteger(7));
                var encryptor = new ElGamalEncryptor(curve, encoder);

                stopwatch.Stop();
                var t_setup = stopwatch.Elapsed;
                stopwatch.Restart();

                var keys = encryptor.GenerateKeyPair();

                stopwatch.Stop();
                var t_key_generation = stopwatch.Elapsed;
                stopwatch.Restart();

                var c = encryptor.Encrypt(keys.PublicKey, _m);

                stopwatch.Stop();
                var t_encryption = stopwatch.Elapsed;
                stopwatch.Restart();

                var m2 = encryptor.Decrypt(keys.PrivateKey, c);

                stopwatch.Stop();
                var t_decryption = stopwatch.Elapsed;
                var t_total = t_setup + t_key_generation + t_encryption + t_decryption;

                Assert.AreEqual(_m, m2);

                setup_times[i] = t_setup;
                key_generation_times[i] = t_key_generation;
                encryption_times[i] = t_encryption;
                decryption_times[i] = t_decryption;
            }

            var setup_avg = setup_times.Sum(x => x.Milliseconds) / (double)num_runs;
            var key_generation_avg = key_generation_times.Sum(x => x.Milliseconds) / (double)num_runs;
            var encryption_avg = encryption_times.Sum(x => x.Milliseconds) / (double)num_runs;
            var decryption_avg = decryption_times.Sum(x => x.Milliseconds) / (double)num_runs;

            var total_avg = setup_avg + key_generation_avg + encryption_avg + decryption_avg;

            throw new AssertFailedException("Runtime " + total_avg + "(" + key_generation_avg + "," + encryption_avg + "," + decryption_avg + ")");
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.ReadKey();
            var str = args[0];

            var curve = CurveFactory.secp256k1;
            var encoder = new ProbabilisticWeierstrassMessageEncoder(curve, new BigInteger(7));
            var encryptor = new ElGamalEncryptor(curve, encoder);
            var keys = encryptor.GenerateKeyPair();
            var plaintext = new Plaintext(str);

            var ciphertext = encryptor.Encrypt(keys.PublicKey, plaintext);
            var plaintext2 = encryptor.Decrypt(keys.PrivateKey, ciphertext);
        }
        public void TestElGamalEncryptDecrypt()
        {
            var curve = CurveFactory.secp256k1;
            var encoder = new ProbabilisticWeierstrassMessageEncoder(curve, new BigInteger(7));
            var encryptor = new ElGamalEncryptor(curve, encoder);
            var keys = encryptor.GenerateKeyPair();

            var m = new Plaintext("Hello, World");

            var c = encryptor.Encrypt(keys.PublicKey, m);

            var m2 = encryptor.Decrypt(keys.PrivateKey, c);

            Assert.AreEqual(m, m2);
        }
        public void TestEncoderCyclic()
        {
            var curve = CurveFactory.nistp384;
            var encoder = new ProbabilisticWeierstrassMessageEncoder(curve, new BigInteger(7));

            //Encode
            var m = new Plaintext("A");
            BigInteger encoding_key;
            var M = encoder.EncodeMessage(m);

            //Decode
            var m2 = encoder.DecodeMessage(M);

            Assert.AreEqual(m, m2);
        }
        public void CompareSecp256k1KeyGenerationRuntimes()
        {
            int num_runs = 100;

            long openecc_time_sum = 0L;
            long bc_time_sum = 0L;

            for (int i = 0; i < num_runs; i++)
            {
                ///
                /// OpenECC
                ///
                var openecc_curve = CurveFactory.secp256k1;

                var openecc_keygen_timer = Stopwatch.StartNew();

                var openecc_encoder = new ProbabilisticWeierstrassMessageEncoder(openecc_curve, new BigInteger(7));
                var openecc_encryptor = new ElGamalEncryptor(openecc_curve, openecc_encoder);
                var openecc_keys = openecc_encryptor.GenerateKeyPair();

                openecc_keygen_timer.Stop();
                openecc_time_sum += openecc_keygen_timer.Elapsed.Milliseconds;
            }

            for (int i = 0; i < num_runs; i++)
            {
                ///
                ///Bouncy Castle
                ///
                var bc_stuff = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

                var bc_keygen_timer = Stopwatch.StartNew();

                var bc_keygen = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();
                var bc_domain_params = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(bc_stuff.Curve, bc_stuff.G, bc_stuff.N);
                var bc_random = new Org.BouncyCastle.Security.SecureRandom();
                var bc_keygen_params = new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(bc_domain_params, bc_random);
                bc_keygen.Init(bc_keygen_params);
                var bc_keys = bc_keygen.GenerateKeyPair();

                bc_keygen_timer.Stop();
                bc_time_sum += bc_keygen_timer.Elapsed.Milliseconds;
            }

            throw new NotImplementedException("BC: " + (bc_time_sum/(double)num_runs) + "; OpenECC: " + (openecc_time_sum/(double)num_runs));
            //RuntimeAssert.LessThan(bc_time_sum / (double)num_runs, openecc_time_sum / (double)num_runs);
        }