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);
        }