Skip to content

Commit 3858027

Browse files
Add an Add bench!
1 parent eafa4e9 commit 3858027

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

Src/Benchmarks/Bitcoin/Cryptography/EllipticCurve/Scalar8x32Alt.cs

+60
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,66 @@ public bool IsHigh
125125
}
126126
}
127127

128+
129+
public Scalar8x32Alt Add(in Scalar8x32Alt other, out bool overflow)
130+
{
131+
ulong t = (ulong)b0 + other.b0;
132+
uint r0 = (uint)t; t >>= 32;
133+
t += (ulong)b1 + other.b1;
134+
uint r1 = (uint)t; t >>= 32;
135+
t += (ulong)b2 + other.b2;
136+
uint r2 = (uint)t; t >>= 32;
137+
t += (ulong)b3 + other.b3;
138+
uint r3 = (uint)t; t >>= 32;
139+
t += (ulong)b4 + other.b4;
140+
uint r4 = (uint)t; t >>= 32;
141+
t += (ulong)b5 + other.b5;
142+
uint r5 = (uint)t; t >>= 32;
143+
t += (ulong)b6 + other.b6;
144+
uint r6 = (uint)t; t >>= 32;
145+
t += (ulong)b7 + other.b7;
146+
uint r7 = (uint)t; t >>= 32;
147+
148+
int yes = 0;
149+
int no = 0;
150+
no |= (r7 < N7 ? 1 : 0);
151+
no |= (r6 < N6 ? 1 : 0);
152+
no |= (r5 < N5 ? 1 : 0);
153+
no |= (r4 < N4 ? 1 : 0);
154+
yes |= (r4 > N4 ? 1 : 0) & ~no;
155+
no |= (r3 < N3 ? 1 : 0) & ~yes;
156+
yes |= (r3 > N3 ? 1 : 0) & ~no;
157+
no |= (r2 < N2 ? 1 : 0) & ~yes;
158+
yes |= (r2 > N2 ? 1 : 0) & ~no;
159+
no |= (r1 < N1 ? 1 : 0) & ~yes;
160+
yes |= (r1 > N1 ? 1 : 0) & ~no;
161+
yes |= (r0 >= N0 ? 1 : 0) & ~no;
162+
163+
uint of = (uint)yes + (uint)t;
164+
overflow = of != 0;
165+
166+
Debug.Assert(of == 0 || of == 1);
167+
168+
t = (ulong)r0 + (of * NC0);
169+
r0 = (uint)t; t >>= 32;
170+
t += (ulong)r1 + (of * NC1);
171+
r1 = (uint)t; t >>= 32;
172+
t += (ulong)r2 + (of * NC2);
173+
r2 = (uint)t; t >>= 32;
174+
t += (ulong)r3 + (of * NC3);
175+
r3 = (uint)t; t >>= 32;
176+
t += (ulong)r4 + (of * NC4);
177+
r4 = (uint)t; t >>= 32;
178+
t += r5;
179+
r5 = (uint)t; t >>= 32;
180+
t += r6;
181+
r6 = (uint)t; t >>= 32;
182+
t += r7;
183+
r7 = (uint)t;
184+
185+
return new Scalar8x32Alt(r0, r1, r2, r3, r4, r5, r6, r7);
186+
}
187+
128188
public Scalar8x32Alt Multiply(in Scalar8x32Alt b)
129189
{
130190
uint[] l = new uint[16];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.EllipticCurve;
7+
using BenchmarkDotNet.Attributes;
8+
using System;
9+
10+
namespace Benchmarks.Bitcoin.Cryptography.EllipticCurve
11+
{
12+
[InProcess]
13+
[RankColumn]
14+
public class Scalar8x32_AddBench
15+
{
16+
[GlobalSetup]
17+
public void Setup()
18+
{
19+
byte[] ba1 = new byte[32];
20+
byte[] ba2 = new byte[32];
21+
Random rng = new(17);
22+
rng.NextBytes(ba1);
23+
rng.NextBytes(ba2);
24+
25+
lib1 = new(ba1, out bool of1);
26+
lib2 = new(ba1, out bool of2);
27+
if (of1 || of2)
28+
{
29+
throw new Exception("Unexpected overflow.");
30+
}
31+
alt1 = new(ba1);
32+
alt2 = new(ba1);
33+
34+
Scalar8x32 res1 = lib1.Add(lib2, out _);
35+
Scalar8x32Alt res2 = alt1.Add(alt2, out _);
36+
37+
if (!res2.Equals(res1))
38+
{
39+
throw new Exception("Not equal.");
40+
}
41+
}
42+
43+
Scalar8x32 lib1, lib2;
44+
Scalar8x32Alt alt1, alt2;
45+
46+
47+
[Benchmark(Baseline = true)]
48+
public Scalar8x32 LibAdd() => lib1.Add(lib2, out _);
49+
50+
[Benchmark]
51+
public Scalar8x32Alt AltAdd() => alt1.Add(alt2, out _);
52+
}
53+
}

0 commit comments

Comments
 (0)