|
| 1 | +// Autarkysoft Benchmarks |
| 2 | +// Copyright (c) 2020 Autarkysoft |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file LICENCE or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +using Autarkysoft.Bitcoin.Cryptography; |
| 7 | +using BenchmarkDotNet.Attributes; |
| 8 | +using BenchmarkDotNet.Configs; |
| 9 | +using System; |
| 10 | +using System.Numerics; |
| 11 | + |
| 12 | +namespace Benchmarks.Bitcoin.Cryptography |
| 13 | +{ |
| 14 | + [InProcess] |
| 15 | + [RankColumn] |
| 16 | + [MemoryDiagnoser] |
| 17 | + [CategoriesColumn] |
| 18 | + [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 19 | + public class Rfc6979Bench |
| 20 | + { |
| 21 | + [GlobalSetup] |
| 22 | + public void Setup() |
| 23 | + { |
| 24 | + Random rng = new(23); |
| 25 | + rng.NextBytes(data); |
| 26 | + rng.NextBytes(key); |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + private readonly Rfc6979 rfc = new(); |
| 31 | + private readonly byte[] data = new byte[32]; |
| 32 | + private readonly byte[] key = new byte[32]; |
| 33 | + |
| 34 | + |
| 35 | + [Benchmark(Baseline = true), BenchmarkCategory("Single")] |
| 36 | + public BigInteger SingleTry_Old() |
| 37 | + { |
| 38 | + uint count = 1; |
| 39 | + byte[] extraEntropy = new byte[32]; |
| 40 | + extraEntropy[0] = (byte)count; |
| 41 | + extraEntropy[1] = (byte)(count >> 8); |
| 42 | + extraEntropy[2] = (byte)(count >> 16); |
| 43 | + extraEntropy[3] = (byte)(count >> 24); |
| 44 | + count++; |
| 45 | + |
| 46 | + return rfc.GetK(data, key, extraEntropy); |
| 47 | + } |
| 48 | + |
| 49 | + [Benchmark, BenchmarkCategory("Single")] |
| 50 | + public byte[] SingleTry_New() |
| 51 | + { |
| 52 | + rfc.Init(data, key); |
| 53 | + return rfc.Generate(); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + [Benchmark(Baseline = true), BenchmarkCategory("Double")] |
| 58 | + public BigInteger TwoTry_Old() |
| 59 | + { |
| 60 | + uint count = 1; |
| 61 | + byte[] extraEntropy = new byte[32]; |
| 62 | + BigInteger result; |
| 63 | + do |
| 64 | + { |
| 65 | + extraEntropy[0] = (byte)count; |
| 66 | + extraEntropy[1] = (byte)(count >> 8); |
| 67 | + extraEntropy[2] = (byte)(count >> 16); |
| 68 | + extraEntropy[3] = (byte)(count >> 24); |
| 69 | + count++; |
| 70 | + |
| 71 | + result = rfc.GetK(data, key, extraEntropy); |
| 72 | + |
| 73 | + } while (count < 3); |
| 74 | + |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark, BenchmarkCategory("Double")] |
| 79 | + public byte[] TwoTry_New() |
| 80 | + { |
| 81 | + rfc.Init(data, key); |
| 82 | + rfc.Generate(); |
| 83 | + return rfc.Generate(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments