Ejemplo n.º 1
0
        public void TestComputeProofOfWork()
        {
            var d = new ProofOfWorkDetails
            {
                difficulty = 15,
                challenge  = ByteHelper.GetRandom(31)
            };

            var res = DiffieHellmanHelper.ComputeProofOfWork(d, out byte[] proof);

            Assert.IsTrue(res);

            VerifyPow(d, proof);
        }
Ejemplo n.º 2
0
        public void TestPerformances()
        {
            var difficulties = new int[] { 15, 16, 17, 18, 19, 20, 21, 22 };
            var results      = new long[difficulties.Length * 2];
            var loops        = 50L;

            for (var k = 0; k < loops; k++)
            {
                var d = new ProofOfWorkDetails
                {
                    challenge = ByteHelper.GetRandom(31)
                };

                for (var i = 0; i < difficulties.Length; i++)
                {
                    var sw1 = Stopwatch.StartNew();

                    d.difficulty = (byte)difficulties[i];

                    var res = DiffieHellmanHelper.ComputeProofOfWork(d, out byte[] proof);
                    Assert.IsTrue(res);

                    sw1.Stop();

                    var sw2 = Stopwatch.StartNew();

                    VerifyPow(d, proof);

                    sw2.Stop();

                    results[i * 2]     += sw1.ElapsedMilliseconds;
                    results[i * 2 + 1] += sw2.ElapsedMilliseconds;
                }
            }

            for (var i = 0; i < difficulties.Length; i++)
            {
                var msg = string.Format("Difficulty {0}, proof {1}ms, verification {2}ms", difficulties[i], results[i * 2] / loops, results[i * 2 + 1] / loops);
                TestContext.WriteLine(msg);
            }
        }
Ejemplo n.º 3
0
        private void VerifyPow(ProofOfWorkDetails details, byte[] proof)
        {
            var res = new ProofOfWork <SHA256>(HashHelper.Sha256, details.difficulty, details.challenge).Verify(proof);

            Assert.IsTrue(res);
        }
Ejemplo n.º 4
0
        public static bool ComputeProofOfWork(this ProofOfWorkDetails details, out byte[] proof)
        {
            var computer = new ProofOfWork <SHA256>(HashHelper.Sha256, details.difficulty, details.challenge);

            return(computer.Compute(out proof));
        }