apow-cli 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,322 @@
1
+ /*
2
+ * APoW GPU Nonce Grinder — CUDA (RTX 4090) v4: Multi-Address Concurrent
3
+ *
4
+ * Grinds nonces for up to 8 addresses simultaneously in one kernel launch.
5
+ * Threads are partitioned across addresses — all addresses grind in parallel.
6
+ * Found nonces are streamed to stdout as they're discovered.
7
+ *
8
+ * Usage: ./grinder-cuda <challenge_hex> <target_hex> <addr1_hex> [addr2_hex] ...
9
+ * Output (streamed): F <addr_index> <nonce_decimal>
10
+ * Final line: D <total_attempts> <elapsed_seconds>
11
+ *
12
+ * Build: nvcc grinder-cuda.cu -o grinder-cuda -std=c++17 -O3 -arch=sm_89
13
+ */
14
+
15
+ #include <cstdio>
16
+ #include <cstdlib>
17
+ #include <cstring>
18
+ #include <cstdint>
19
+ #include <ctime>
20
+ #include <chrono>
21
+ #include <unistd.h>
22
+
23
+ #define MAX_ADDRS 8
24
+
25
+ // ── Keccak round constants ──
26
+
27
+ __constant__ uint64_t RC[24] = {
28
+ 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
29
+ 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
30
+ 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
31
+ 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
32
+ 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
33
+ 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
34
+ 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
35
+ 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
36
+ };
37
+
38
+ __device__ __forceinline__ uint64_t rotl64(uint64_t x, int n) {
39
+ return (x << n) | (x >> (64 - n));
40
+ }
41
+
42
+ __device__ __forceinline__ uint64_t bswap64(uint64_t x) {
43
+ uint32_t lo = (uint32_t)x;
44
+ uint32_t hi = (uint32_t)(x >> 32);
45
+ return ((uint64_t)__byte_perm(lo, 0, 0x0123) << 32) |
46
+ (uint64_t)__byte_perm(hi, 0, 0x0123);
47
+ }
48
+
49
+ #define CHI(a0, a1, a2, a3, a4) { \
50
+ uint64_t c0=a0, c1=a1, c2=a2, c3=a3, c4=a4; \
51
+ a0 = c0 ^ (~c1 & c2); \
52
+ a1 = c1 ^ (~c2 & c3); \
53
+ a2 = c2 ^ (~c3 & c4); \
54
+ a3 = c3 ^ (~c4 & c0); \
55
+ a4 = c4 ^ (~c0 & c1); \
56
+ }
57
+
58
+ // ── Structures ──
59
+
60
+ struct GrindParams {
61
+ uint64_t pre_state[MAX_ADDRS][25]; // Pre-absorbed state per address
62
+ uint64_t target_be[4]; // Same target for all
63
+ uint64_t start_nonce;
64
+ uint32_t num_addrs;
65
+ uint32_t threads_per_addr[MAX_ADDRS]; // Per-address thread count (for weighting)
66
+ uint32_t thread_offset[MAX_ADDRS]; // Cumulative offset: sum of threads_per_addr[0..i-1]
67
+ };
68
+
69
+ struct GrindResult {
70
+ uint32_t found[MAX_ADDRS]; // Per-address found flag
71
+ uint64_t nonce[MAX_ADDRS]; // Per-address winning nonce
72
+ };
73
+
74
+ // ── GPU kernel: threads partitioned across addresses ──
75
+
76
+ __global__ void grind_kernel(const GrindParams* __restrict__ params, GrindResult* result) {
77
+ uint64_t tid = (uint64_t)blockIdx.x * blockDim.x + threadIdx.x;
78
+
79
+ // Find which address this thread belongs to via cumulative offsets
80
+ uint32_t addr_idx = 0;
81
+ uint32_t n = params->num_addrs;
82
+ for (uint32_t i = 0; i < n; i++) {
83
+ if (tid < params->thread_offset[i] + params->threads_per_addr[i]) {
84
+ addr_idx = i;
85
+ break;
86
+ }
87
+ }
88
+
89
+ uint64_t local_tid = tid - params->thread_offset[addr_idx];
90
+ if (local_tid >= params->threads_per_addr[addr_idx]) return;
91
+ if (result->found[addr_idx]) return;
92
+
93
+ uint64_t nonce = params->start_nonce + tid; // Global unique nonce per thread
94
+
95
+ // Load pre-absorbed state for this address
96
+ const uint64_t* ps = params->pre_state[addr_idx];
97
+ uint64_t s00 = ps[0], s01 = ps[1], s02 = ps[2], s03 = ps[3], s04 = ps[4],
98
+ s05 = ps[5], s06 = ps[6], s07 = ps[7], s08 = ps[8], s09 = ps[9],
99
+ s10 = ps[10], s11 = ps[11], s12 = ps[12], s13 = ps[13], s14 = ps[14],
100
+ s15 = ps[15], s16 = ps[16], s17 = ps[17], s18 = ps[18], s19 = ps[19],
101
+ s20 = ps[20], s21 = ps[21], s22 = ps[22], s23 = ps[23], s24 = ps[24];
102
+
103
+ // XOR nonce into words 9 and 10
104
+ uint32_t nhi = (uint32_t)(nonce >> 32);
105
+ uint32_t nlo = (uint32_t)(nonce);
106
+ s09 ^= (uint64_t)__byte_perm(nhi, 0, 0x0123) << 32;
107
+ s10 ^= (uint64_t)__byte_perm(nlo, 0, 0x0123);
108
+
109
+ // ── Keccak-f1600: 24 rounds ──
110
+ for (int r = 0; r < 24; r++) {
111
+ uint64_t bc0 = s00^s05^s10^s15^s20, bc1 = s01^s06^s11^s16^s21,
112
+ bc2 = s02^s07^s12^s17^s22, bc3 = s03^s08^s13^s18^s23,
113
+ bc4 = s04^s09^s14^s19^s24;
114
+ uint64_t t0 = bc4^rotl64(bc1,1), t1 = bc0^rotl64(bc2,1),
115
+ t2 = bc1^rotl64(bc3,1), t3 = bc2^rotl64(bc4,1),
116
+ t4 = bc3^rotl64(bc0,1);
117
+ s00^=t0; s05^=t0; s10^=t0; s15^=t0; s20^=t0;
118
+ s01^=t1; s06^=t1; s11^=t1; s16^=t1; s21^=t1;
119
+ s02^=t2; s07^=t2; s12^=t2; s17^=t2; s22^=t2;
120
+ s03^=t3; s08^=t3; s13^=t3; s18^=t3; s23^=t3;
121
+ s04^=t4; s09^=t4; s14^=t4; s19^=t4; s24^=t4;
122
+
123
+ uint64_t tmp, t = s01;
124
+ tmp=s10; s10=rotl64(t, 1); t=tmp; tmp=s07; s07=rotl64(t, 3); t=tmp;
125
+ tmp=s11; s11=rotl64(t, 6); t=tmp; tmp=s17; s17=rotl64(t,10); t=tmp;
126
+ tmp=s18; s18=rotl64(t,15); t=tmp; tmp=s03; s03=rotl64(t,21); t=tmp;
127
+ tmp=s05; s05=rotl64(t,28); t=tmp; tmp=s16; s16=rotl64(t,36); t=tmp;
128
+ tmp=s08; s08=rotl64(t,45); t=tmp; tmp=s21; s21=rotl64(t,55); t=tmp;
129
+ tmp=s24; s24=rotl64(t, 2); t=tmp; tmp=s04; s04=rotl64(t,14); t=tmp;
130
+ tmp=s15; s15=rotl64(t,27); t=tmp; tmp=s23; s23=rotl64(t,41); t=tmp;
131
+ tmp=s19; s19=rotl64(t,56); t=tmp; tmp=s13; s13=rotl64(t, 8); t=tmp;
132
+ tmp=s12; s12=rotl64(t,25); t=tmp; tmp=s02; s02=rotl64(t,43); t=tmp;
133
+ tmp=s20; s20=rotl64(t,62); t=tmp; tmp=s14; s14=rotl64(t,18); t=tmp;
134
+ tmp=s22; s22=rotl64(t,39); t=tmp; tmp=s09; s09=rotl64(t,61); t=tmp;
135
+ tmp=s06; s06=rotl64(t,20); t=tmp; s01=rotl64(t,44);
136
+
137
+ CHI(s00,s01,s02,s03,s04); CHI(s05,s06,s07,s08,s09);
138
+ CHI(s10,s11,s12,s13,s14); CHI(s15,s16,s17,s18,s19);
139
+ CHI(s20,s21,s22,s23,s24);
140
+
141
+ s00 ^= RC[r];
142
+ }
143
+
144
+ // ── Compare hash < target ──
145
+ uint64_t h, tgt;
146
+ h = bswap64(s00); tgt = params->target_be[0];
147
+ if (h > tgt) return; if (h < tgt) goto found;
148
+ h = bswap64(s01); tgt = params->target_be[1];
149
+ if (h > tgt) return; if (h < tgt) goto found;
150
+ h = bswap64(s02); tgt = params->target_be[2];
151
+ if (h > tgt) return; if (h < tgt) goto found;
152
+ h = bswap64(s03); tgt = params->target_be[3];
153
+ if (h > tgt) return; if (h < tgt) goto found;
154
+ return;
155
+
156
+ found:
157
+ if (atomicCAS(&result->found[addr_idx], 0u, 1u) == 0u) {
158
+ result->nonce[addr_idx] = nonce;
159
+ }
160
+ }
161
+
162
+ // ── Host code ──
163
+
164
+ static int hex2byte(char c) {
165
+ if (c >= '0' && c <= '9') return c - '0';
166
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
167
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
168
+ return -1;
169
+ }
170
+
171
+ static int parse_hex(const char* hex, uint8_t* out, int len) {
172
+ if (hex[0] == '0' && hex[1] == 'x') hex += 2;
173
+ for (int i = 0; i < len; i++) {
174
+ int hi = hex2byte(hex[i*2]);
175
+ int lo = hex2byte(hex[i*2+1]);
176
+ if (hi < 0 || lo < 0) return -1;
177
+ out[i] = (hi << 4) | lo;
178
+ }
179
+ return 0;
180
+ }
181
+
182
+ int main(int argc, char* argv[]) {
183
+ // Usage: grinder-cuda <challenge> <target> [Wn] <addr1> [Wn] [addr2] ...
184
+ // Wn before an address sets its weight (default 1). E.g. W2 = 2x threads.
185
+ if (argc < 4) {
186
+ fprintf(stderr, "Usage: %s <challenge_hex> <target_hex> [W<weight>] <addr1_hex> ...\n", argv[0]);
187
+ return 1;
188
+ }
189
+
190
+ uint8_t challenge[32], target[32];
191
+ if (parse_hex(argv[1], challenge, 32) < 0 || parse_hex(argv[2], target, 32) < 0) {
192
+ fprintf(stderr, "Invalid hex input\n");
193
+ return 1;
194
+ }
195
+
196
+ int num_addrs = 0;
197
+ uint8_t addresses[MAX_ADDRS][20];
198
+ int weights[MAX_ADDRS];
199
+ int next_weight = 1;
200
+
201
+ for (int i = 3; i < argc && num_addrs < MAX_ADDRS; i++) {
202
+ if (argv[i][0] == 'W' || argv[i][0] == 'w') {
203
+ next_weight = atoi(argv[i] + 1);
204
+ if (next_weight < 1) next_weight = 1;
205
+ if (next_weight > 4) next_weight = 4;
206
+ continue;
207
+ }
208
+ if (parse_hex(argv[i], addresses[num_addrs], 20) < 0) {
209
+ fprintf(stderr, "Invalid address hex: %s\n", argv[i]);
210
+ return 1;
211
+ }
212
+ weights[num_addrs] = next_weight;
213
+ next_weight = 1;
214
+ num_addrs++;
215
+ }
216
+
217
+ // ── Pre-absorb state for each address ──
218
+ uint64_t pre_states[MAX_ADDRS][25];
219
+ for (int a = 0; a < num_addrs; a++) {
220
+ uint8_t input[136] = {0};
221
+ memcpy(input, challenge, 32);
222
+ memcpy(input + 32, addresses[a], 20);
223
+ input[84] = 0x01;
224
+ input[135] = 0x80;
225
+
226
+ memset(pre_states[a], 0, sizeof(pre_states[a]));
227
+ for (int i = 0; i < 17; i++) {
228
+ uint64_t w = 0;
229
+ for (int b = 0; b < 8; b++)
230
+ w |= (uint64_t)input[i*8 + b] << (b * 8);
231
+ pre_states[a][i] = w;
232
+ }
233
+ }
234
+
235
+ // Target as big-endian uint64_t
236
+ uint64_t target_be[4];
237
+ for (int i = 0; i < 4; i++) {
238
+ uint64_t w = 0;
239
+ for (int b = 0; b < 8; b++)
240
+ w = (w << 8) | target[i*8 + b];
241
+ target_be[i] = w;
242
+ }
243
+
244
+ // ── Allocate unified memory ──
245
+ GrindParams* params;
246
+ GrindResult* result;
247
+ cudaMallocManaged(&params, sizeof(GrindParams));
248
+ cudaMallocManaged(&result, sizeof(GrindResult));
249
+
250
+ memcpy(params->pre_state, pre_states, sizeof(pre_states));
251
+ memcpy(params->target_be, target_be, sizeof(target_be));
252
+ params->num_addrs = num_addrs;
253
+
254
+ srand((unsigned)time(nullptr) ^ (unsigned)getpid());
255
+ uint64_t baseNonce = ((uint64_t)rand() << 32) | (uint64_t)rand();
256
+
257
+ // ── Dispatch config (weighted) ──
258
+ const int BLOCK_SIZE = 256;
259
+ const uint64_t RAW_BATCH = 1ULL << 26; // 64M
260
+
261
+ // Compute total weight and per-address thread counts
262
+ int total_weight = 0;
263
+ for (int a = 0; a < num_addrs; a++) total_weight += weights[a];
264
+
265
+ uint64_t BATCH_SIZE = 0;
266
+ uint32_t cum_offset = 0;
267
+ for (int a = 0; a < num_addrs; a++) {
268
+ uint64_t tpa = (RAW_BATCH * weights[a] / total_weight / BLOCK_SIZE) * BLOCK_SIZE;
269
+ if (tpa < (uint64_t)BLOCK_SIZE) tpa = BLOCK_SIZE;
270
+ params->threads_per_addr[a] = (uint32_t)tpa;
271
+ params->thread_offset[a] = cum_offset;
272
+ cum_offset += (uint32_t)tpa;
273
+ BATCH_SIZE += tpa;
274
+ }
275
+ int GRID_SIZE = (int)(BATCH_SIZE / BLOCK_SIZE);
276
+
277
+ const int MAX_BATCHES = 1 << 20; // ~64T hashes — never exhausts before abort
278
+ bool found_all[MAX_ADDRS] = {false};
279
+ int found_count = 0;
280
+
281
+ auto t0 = std::chrono::steady_clock::now();
282
+ uint64_t totalAttempts = 0;
283
+
284
+ for (int batch = 0; batch < MAX_BATCHES && found_count < num_addrs; batch++) {
285
+ params->start_nonce = baseNonce + (uint64_t)batch * BATCH_SIZE;
286
+
287
+ // Reset unfound addresses
288
+ for (int a = 0; a < num_addrs; a++) {
289
+ if (!found_all[a]) {
290
+ result->found[a] = 0;
291
+ result->nonce[a] = 0;
292
+ }
293
+ }
294
+
295
+ grind_kernel<<<GRID_SIZE, BLOCK_SIZE>>>(params, result);
296
+ cudaDeviceSynchronize();
297
+
298
+ totalAttempts += BATCH_SIZE;
299
+
300
+ // Check for newly found nonces — stream to stdout immediately
301
+ for (int a = 0; a < num_addrs; a++) {
302
+ if (!found_all[a] && result->found[a]) {
303
+ auto t1 = std::chrono::steady_clock::now();
304
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
305
+ printf("F %d %llu %.3f\n", a,
306
+ (unsigned long long)result->nonce[a], elapsed);
307
+ fflush(stdout);
308
+ found_all[a] = true;
309
+ found_count++;
310
+ }
311
+ }
312
+ }
313
+
314
+ auto t1 = std::chrono::steady_clock::now();
315
+ double elapsed = std::chrono::duration<double>(t1 - t0).count();
316
+ printf("D %llu %.3f\n", (unsigned long long)totalAttempts, elapsed);
317
+ fflush(stdout);
318
+
319
+ cudaFree(params);
320
+ cudaFree(result);
321
+ return (found_count > 0) ? 0 : 1;
322
+ }
@@ -0,0 +1,191 @@
1
+ /*
2
+ * APoW GPU Nonce Grinder — Metal compute on Apple Silicon
3
+ * Usage: ./grinder-gpu <challenge_hex> <address_hex> <target_hex>
4
+ * Output: <nonce_decimal> <attempts> <elapsed_seconds>
5
+ *
6
+ * Dispatches millions of nonces per batch to the GPU.
7
+ * M3 Max (40 GPU cores) should hit 500M-2B+ H/s.
8
+ */
9
+
10
+ #import <Foundation/Foundation.h>
11
+ #import <Metal/Metal.h>
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+ #include <stdint.h>
16
+ #include <time.h>
17
+
18
+ // Must match the Metal shader structs exactly (natural alignment)
19
+ typedef struct {
20
+ uint8_t base[84];
21
+ uint8_t target[32];
22
+ uint32_t _pad; // align start_nonce to 8 bytes (84+32=116, +4=120)
23
+ uint64_t start_nonce;
24
+ } GrindParams;
25
+
26
+ typedef struct {
27
+ uint32_t found; // atomic in shader, plain here
28
+ uint32_t _pad; // alignment
29
+ uint64_t nonce;
30
+ uint32_t thread_id;
31
+ } GrindResult;
32
+
33
+ static int hex2byte(char c) {
34
+ if (c >= '0' && c <= '9') return c - '0';
35
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
36
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
37
+ return -1;
38
+ }
39
+
40
+ static int parse_hex(const char *hex, uint8_t *out, int len) {
41
+ if (hex[0] == '0' && hex[1] == 'x') hex += 2;
42
+ for (int i = 0; i < len; i++) {
43
+ int hi = hex2byte(hex[i*2]);
44
+ int lo = hex2byte(hex[i*2+1]);
45
+ if (hi < 0 || lo < 0) return -1;
46
+ out[i] = (hi << 4) | lo;
47
+ }
48
+ return 0;
49
+ }
50
+
51
+ int main(int argc, char *argv[]) {
52
+ @autoreleasepool {
53
+ if (argc < 4) {
54
+ fprintf(stderr, "Usage: %s <challenge_hex> <address_hex> <target_hex>\n", argv[0]);
55
+ return 1;
56
+ }
57
+
58
+ uint8_t challenge[32], address[20], target[32];
59
+ if (parse_hex(argv[1], challenge, 32) < 0 ||
60
+ parse_hex(argv[2], address, 20) < 0 ||
61
+ parse_hex(argv[3], target, 32) < 0) {
62
+ fprintf(stderr, "Invalid hex input\n");
63
+ return 1;
64
+ }
65
+
66
+ // ── Metal setup ──
67
+ id<MTLDevice> device = MTLCreateSystemDefaultDevice();
68
+ if (!device) {
69
+ fprintf(stderr, "No Metal device found\n");
70
+ return 1;
71
+ }
72
+
73
+ // Load shader from .metallib (pre-compiled) or compile from source
74
+ NSError *error = nil;
75
+ NSString *shaderPath = [[[NSString stringWithUTF8String:argv[0]]
76
+ stringByDeletingLastPathComponent]
77
+ stringByAppendingPathComponent:@"keccak.metallib"];
78
+
79
+ id<MTLLibrary> library = nil;
80
+ if ([[NSFileManager defaultManager] fileExistsAtPath:shaderPath]) {
81
+ library = [device newLibraryWithFile:shaderPath error:&error];
82
+ }
83
+
84
+ if (!library) {
85
+ // Fallback: compile from source
86
+ NSString *srcDir = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
87
+ NSString *srcPath = [srcDir stringByAppendingPathComponent:@"keccak.metal"];
88
+ NSString *source = [NSString stringWithContentsOfFile:srcPath encoding:NSUTF8StringEncoding error:&error];
89
+ if (!source) {
90
+ fprintf(stderr, "Cannot read shader: %s\n", [[error localizedDescription] UTF8String]);
91
+ return 1;
92
+ }
93
+ MTLCompileOptions *opts = [[MTLCompileOptions alloc] init];
94
+ opts.fastMathEnabled = YES;
95
+ library = [device newLibraryWithSource:source options:opts error:&error];
96
+ if (!library) {
97
+ fprintf(stderr, "Shader compile error: %s\n", [[error localizedDescription] UTF8String]);
98
+ return 1;
99
+ }
100
+ }
101
+
102
+ id<MTLFunction> function = [library newFunctionWithName:@"grind_nonce"];
103
+ if (!function) {
104
+ fprintf(stderr, "Cannot find grind_nonce kernel\n");
105
+ return 1;
106
+ }
107
+
108
+ id<MTLComputePipelineState> pipeline = [device newComputePipelineStateWithFunction:function error:&error];
109
+ if (!pipeline) {
110
+ fprintf(stderr, "Pipeline error: %s\n", [[error localizedDescription] UTF8String]);
111
+ return 1;
112
+ }
113
+
114
+ id<MTLCommandQueue> queue = [device newCommandQueue];
115
+
116
+ // ── Buffers ──
117
+ id<MTLBuffer> paramsBuf = [device newBufferWithLength:sizeof(GrindParams)
118
+ options:MTLResourceStorageModeShared];
119
+ id<MTLBuffer> resultBuf = [device newBufferWithLength:sizeof(GrindResult)
120
+ options:MTLResourceStorageModeShared];
121
+
122
+ GrindParams *params = (GrindParams *)paramsBuf.contents;
123
+ memset(params, 0, sizeof(GrindParams));
124
+ memcpy(params->base, challenge, 32);
125
+ memcpy(params->base + 32, address, 20);
126
+ memcpy(params->target, target, 32);
127
+
128
+ // Random start nonce
129
+ srand((unsigned)time(NULL) ^ (unsigned)getpid());
130
+ uint64_t baseNonce = ((uint64_t)rand() << 32) | rand();
131
+
132
+ // ── Dispatch config ──
133
+ // Each batch tests BATCH_SIZE nonces on the GPU
134
+ // M3 Max: 40 cores * ~1024 threads/core = ~40k concurrent
135
+ // We dispatch much more to keep the GPU saturated
136
+ const uint64_t BATCH_SIZE = 1 << 24; // 16M nonces per batch
137
+ const int MAX_BATCHES = 1 << 20; // ~16T nonces — never exhausts before abort
138
+
139
+ NSUInteger threadGroupSize = pipeline.maxTotalThreadsPerThreadgroup;
140
+ if (threadGroupSize > 1024) threadGroupSize = 1024;
141
+
142
+ struct timespec t0, t1;
143
+ clock_gettime(CLOCK_MONOTONIC, &t0);
144
+
145
+ uint64_t totalAttempts = 0;
146
+
147
+ for (int batch = 0; batch < MAX_BATCHES; batch++) {
148
+ params->start_nonce = baseNonce + (uint64_t)batch * BATCH_SIZE;
149
+
150
+ // Reset result
151
+ GrindResult *result = (GrindResult *)resultBuf.contents;
152
+ result->found = 0;
153
+ result->nonce = 0;
154
+ result->thread_id = 0;
155
+
156
+ // Dispatch
157
+ id<MTLCommandBuffer> cmdBuf = [queue commandBuffer];
158
+ id<MTLComputeCommandEncoder> encoder = [cmdBuf computeCommandEncoder];
159
+ [encoder setComputePipelineState:pipeline];
160
+ [encoder setBuffer:paramsBuf offset:0 atIndex:0];
161
+ [encoder setBuffer:resultBuf offset:0 atIndex:1];
162
+
163
+ MTLSize gridSize = MTLSizeMake(BATCH_SIZE, 1, 1);
164
+ MTLSize groupSize = MTLSizeMake(threadGroupSize, 1, 1);
165
+ [encoder dispatchThreads:gridSize threadsPerThreadgroup:groupSize];
166
+ [encoder endEncoding];
167
+
168
+ [cmdBuf commit];
169
+ [cmdBuf waitUntilCompleted];
170
+
171
+ totalAttempts += BATCH_SIZE;
172
+
173
+ // Check result
174
+ result = (GrindResult *)resultBuf.contents;
175
+ if (result->found) {
176
+ clock_gettime(CLOCK_MONOTONIC, &t1);
177
+ double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;
178
+ printf("%llu %llu %.3f\n", (unsigned long long)result->nonce,
179
+ (unsigned long long)totalAttempts, elapsed);
180
+ fflush(stdout);
181
+ return 0;
182
+ }
183
+ }
184
+
185
+ clock_gettime(CLOCK_MONOTONIC, &t1);
186
+ double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;
187
+ fprintf(stderr, "No nonce found after %llu attempts (%.1fs)\n",
188
+ (unsigned long long)totalAttempts, elapsed);
189
+ return 1;
190
+ }
191
+ }
@@ -0,0 +1,143 @@
1
+ /*
2
+ * Keccak-256 GPU nonce grinder — Apple Metal compute shader
3
+ * Each GPU thread tests one nonce independently.
4
+ */
5
+
6
+ #include <metal_stdlib>
7
+ using namespace metal;
8
+
9
+ // ── Keccak-256 round constants ──
10
+
11
+ constant uint64_t RC[24] = {
12
+ 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
13
+ 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
14
+ 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
15
+ 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
16
+ 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
17
+ 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
18
+ 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
19
+ 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
20
+ };
21
+
22
+ constant int ROTC[24] = {
23
+ 1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44
24
+ };
25
+
26
+ constant int PILN[24] = {
27
+ 10,7,11,17,18,3,5,16,8,21,24,4,15,23,19,13,12,2,20,14,22,9,6,1
28
+ };
29
+
30
+ static inline uint64_t rotl64(uint64_t x, int n) {
31
+ return (x << n) | (x >> (64 - n));
32
+ }
33
+
34
+ static void keccak_f1600(thread uint64_t st[25]) {
35
+ uint64_t bc[5];
36
+ for (int r = 0; r < 24; r++) {
37
+ for (int i = 0; i < 5; i++)
38
+ bc[i] = st[i] ^ st[i+5] ^ st[i+10] ^ st[i+15] ^ st[i+20];
39
+ for (int i = 0; i < 5; i++) {
40
+ uint64_t t = bc[(i+4)%5] ^ rotl64(bc[(i+1)%5], 1);
41
+ for (int j = 0; j < 25; j += 5) st[j+i] ^= t;
42
+ }
43
+ uint64_t t = st[1];
44
+ for (int i = 0; i < 24; i++) {
45
+ int j = PILN[i];
46
+ uint64_t tmp = st[j];
47
+ st[j] = rotl64(t, ROTC[i]);
48
+ t = tmp;
49
+ }
50
+ for (int j = 0; j < 25; j += 5) {
51
+ uint64_t tmp[5];
52
+ for (int i = 0; i < 5; i++) tmp[i] = st[j+i];
53
+ for (int i = 0; i < 5; i++)
54
+ st[j+i] = tmp[i] ^ ((~tmp[(i+1)%5]) & tmp[(i+2)%5]);
55
+ }
56
+ st[0] ^= RC[r];
57
+ }
58
+ }
59
+
60
+ // ── Input structure (set by host) ──
61
+
62
+ struct GrindParams {
63
+ // Pre-built 84-byte input: challenge(32) + address(20) + zero_pad(32)
64
+ // We only modify the last 8 bytes (nonce) per thread
65
+ uint8_t base[84];
66
+ uint8_t target[32];
67
+ uint32_t _pad; // align start_nonce to 8 bytes
68
+ uint64_t start_nonce;
69
+ };
70
+
71
+ // ── Result buffer ──
72
+
73
+ struct GrindResult {
74
+ atomic_uint found; // 0 = not found, 1 = found
75
+ uint64_t nonce; // winning nonce
76
+ uint32_t thread_id; // which thread found it
77
+ };
78
+
79
+ // ── GPU kernel: each thread tests one nonce ──
80
+
81
+ kernel void grind_nonce(
82
+ device const GrindParams& params [[buffer(0)]],
83
+ device GrindResult& result [[buffer(1)]],
84
+ uint tid [[thread_position_in_grid]]
85
+ ) {
86
+ // Early exit if someone already found it
87
+ if (atomic_load_explicit(&result.found, memory_order_relaxed)) return;
88
+
89
+ uint64_t nonce = params.start_nonce + tid;
90
+
91
+ // Build input: copy base, write nonce big-endian into last 8 bytes
92
+ uint8_t buf[84];
93
+ for (int i = 0; i < 76; i++) buf[i] = params.base[i];
94
+ buf[76] = (nonce >> 56) & 0xff;
95
+ buf[77] = (nonce >> 48) & 0xff;
96
+ buf[78] = (nonce >> 40) & 0xff;
97
+ buf[79] = (nonce >> 32) & 0xff;
98
+ buf[80] = (nonce >> 24) & 0xff;
99
+ buf[81] = (nonce >> 16) & 0xff;
100
+ buf[82] = (nonce >> 8) & 0xff;
101
+ buf[83] = nonce & 0xff;
102
+
103
+ // Keccak-256: build padded 136-byte block, absorb, permute
104
+ uint64_t st[25] = {0};
105
+
106
+ uint8_t padded[136] = {0};
107
+ for (int i = 0; i < 84; i++) padded[i] = buf[i];
108
+ padded[84] = 0x01;
109
+ padded[135] |= 0x80;
110
+
111
+ for (int i = 0; i < 17; i++) {
112
+ uint64_t w = 0;
113
+ for (int b = 0; b < 8; b++)
114
+ w |= uint64_t(padded[i*8 + b]) << (b * 8);
115
+ st[i] ^= w;
116
+ }
117
+
118
+ keccak_f1600(st);
119
+
120
+ // Extract 32-byte hash
121
+ uint8_t hash[32];
122
+ for (int i = 0; i < 4; i++) {
123
+ for (int b = 0; b < 8; b++)
124
+ hash[i*8 + b] = (st[i] >> (b * 8)) & 0xff;
125
+ }
126
+
127
+ // Big-endian comparison: hash < target
128
+ bool less = false;
129
+ for (int i = 0; i < 32; i++) {
130
+ if (hash[i] < params.target[i]) { less = true; break; }
131
+ if (hash[i] > params.target[i]) break;
132
+ }
133
+
134
+ if (less) {
135
+ // Atomically claim the result
136
+ uint expected = 0;
137
+ if (atomic_compare_exchange_weak_explicit(&result.found, &expected, 1u,
138
+ memory_order_relaxed, memory_order_relaxed)) {
139
+ result.nonce = nonce;
140
+ result.thread_id = tid;
141
+ }
142
+ }
143
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apow-cli",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Mine AGENT tokens on Base L2 with Agentic Proof of Work",
5
5
  "keywords": [
6
6
  "apow",
@@ -24,14 +24,19 @@
24
24
  "apow-cli": "dist/index.js"
25
25
  },
26
26
  "engines": {
27
- "node": ">=18.17.0"
27
+ "node": ">=20.0.0"
28
28
  },
29
29
  "files": [
30
30
  "dist",
31
31
  "skill.md",
32
32
  ".env.example",
33
33
  "README.md",
34
- "LICENSE"
34
+ "LICENSE",
35
+ "local/gpu/grinder-cpu.c",
36
+ "local/gpu/grinder-cuda.cu",
37
+ "local/gpu/grinder.m",
38
+ "local/gpu/keccak.metal",
39
+ "local/gpu/Makefile"
35
40
  ],
36
41
  "scripts": {
37
42
  "build": "tsc",
@@ -39,6 +44,7 @@
39
44
  "prepublishOnly": "npm run build"
40
45
  },
41
46
  "dependencies": {
47
+ "@blockrun/clawrouter": "^0.12.81",
42
48
  "@quicknode/x402": "^0.1.2",
43
49
  "@solana/web3.js": "^1.98.0",
44
50
  "commander": "^14.0.0",