/
GpuJob.cs
137 lines (114 loc) · 5.39 KB
/
GpuJob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Cloo;
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace VanityAddrGen
{
public sealed class GpuJob : Job
{
public new sealed class Params : Job.Params
{
public int WorkSize;
public int PlatformIndex;
}
public const int MaxWorkSize = 100_000;
private readonly int workSize;
private readonly int platformIndex;
public GpuJob(Params @params)
: base(@params)
{
workSize = @params.WorkSize;
platformIndex = @params.PlatformIndex;
}
public override void Run(object? arg)
{
string prefix1 = string.Concat(AddressPrefix, "3", keyword);
string prefix2 = string.Concat(AddressPrefix, "1", keyword);
string suffix1 = keyword;
int workSize = this.workSize;
byte[] bigPublicKeyBytes = new byte[32 * workSize];
byte[] bigChecksumBytes = new byte[5 * workSize];
byte[] bigSeedBytes = new byte[31 + workSize];
AddressBuffer addressBuffer = new(AddressPrefix.Length + 60);
bool canMatchPrefix = this.canMatchPrefix;
bool canMatchSuffix = this.canMatchSuffix;
CancellationToken cancellationToken = this.cancellationToken;
GCHandle hPublicKey = GCHandle.Alloc(bigPublicKeyBytes, GCHandleType.Pinned);
GCHandle hChecksum = GCHandle.Alloc(bigChecksumBytes, GCHandleType.Pinned);
GCHandle hSeed = GCHandle.Alloc(bigSeedBytes, GCHandleType.Pinned);
ComputePlatform platform = ComputePlatform.Platforms[platformIndex];
ComputeContext context = new(
ComputeDeviceTypes.Gpu,
new ComputeContextPropertyList(platform),
null, IntPtr.Zero);
ComputeProgram program = new(context, new string[]
{
OpenCl.Blake2b,
OpenCl.Curve25519Constants,
OpenCl.Curve25519Constants2,
OpenCl.Curve25519,
OpenCl.Entry,
});
program.Build(null, null, null, IntPtr.Zero);
ComputeKernel kernel = program.CreateKernel("generate_pubkey");
ComputeCommandQueue queue = new(context, context.Devices[0], ComputeCommandQueueFlags.None);
ComputeBuffer<byte> argPublicKey = new(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, bigPublicKeyBytes);
ComputeBuffer<byte> argChecksum = new(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, bigChecksumBytes);
ComputeBuffer<byte> argSeed = new(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, bigSeedBytes);
addressBuffer.Append(AddressPrefix);
while (!cancellationToken.IsCancellationRequested)
{
random.GetBytes(bigSeedBytes);
queue.Write(argSeed, true, 0, bigSeedBytes.Length, hSeed.AddrOfPinnedObject(), null);
kernel.SetMemoryArgument(0, argPublicKey);
kernel.SetMemoryArgument(1, argChecksum);
kernel.SetMemoryArgument(2, argSeed);
queue.Execute(kernel, null, new long[] { workSize }, null, null);
queue.Read(argPublicKey, true, 0, bigPublicKeyBytes.Length, hPublicKey.AddrOfPinnedObject(), null);
queue.Read(argChecksum, true, 0, bigChecksumBytes.Length, hChecksum.AddrOfPinnedObject(), null);
queue.Finish();
string foundSeed = null;
for (int i = 0; i < workSize; ++i)
{
ArraySegment<byte> seed = new(bigSeedBytes, i, 32);
ArraySegment<byte> publicKey = new(bigPublicKeyBytes, i * 32, 32);
ArraySegment<byte> checksum = new(bigChecksumBytes, i * 5, 5);
Reverse(checksum);
NanoBase32(publicKey, ref addressBuffer);
NanoBase32(checksum, ref addressBuffer);
bool isMatched = false;
if (canMatchPrefix)
isMatched = addressBuffer.StartsWith(prefix1) || addressBuffer.StartsWith(prefix2);
if (!isMatched && canMatchSuffix)
isMatched = addressBuffer.EndsWith(suffix1);
if (isMatched)
{
var address = addressBuffer.ToString();
if (resultCallback != null)
{
resultCallback.Invoke(HexUtils.HexFromByteArray(seed), address);
}
else
{
foundSeed = HexUtils.HexFromByteArray(seed);
FoundSeed = foundSeed;
FoundAddress = address;
attempts += i;
break;
}
}
addressBuffer.Length = AddressPrefix.Length;
}
if (foundSeed != null)
break;
attempts += workSize;
}
argPublicKey.Dispose();
argChecksum.Dispose();
argSeed.Dispose();
hPublicKey.Free();
hChecksum.Free();
hSeed.Free();
}
}
}