apow-cli 0.9.1 → 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.
- package/.env.example +2 -0
- package/dist/build.js +229 -0
- package/dist/config.js +1 -0
- package/dist/grinder-native.js +56 -1
- package/dist/index.js +8 -0
- package/local/gpu/Makefile +39 -0
- package/local/gpu/grinder-cpu.c +241 -0
- package/local/gpu/grinder-cuda.cu +322 -0
- package/local/gpu/grinder.m +191 -0
- package/local/gpu/keccak.metal +143 -0
- package/package.json +7 -2
package/.env.example
CHANGED
|
@@ -29,8 +29,10 @@ LLM_MODEL=blockrun/eco
|
|
|
29
29
|
# SQUID_INTEGRATOR_ID=
|
|
30
30
|
|
|
31
31
|
# GPU Mining (optional — auto-detected, massive speed boost)
|
|
32
|
+
# Run `apow build-grinders` to compile native binaries automatically.
|
|
32
33
|
# GRINDER_MODE=auto # auto (default) or js (force JS-only)
|
|
33
34
|
# GPU_GRINDER_PATH=./gpu/grinder-gpu # Metal binary (macOS, auto-detected)
|
|
35
|
+
# CUDA_GRINDER_PATH=~/.apow/grinder-cuda # CUDA binary (NVIDIA GPU, auto-detected)
|
|
34
36
|
# CPU_GRINDER_PATH=./gpu/grinder-cpu # C binary (any platform, auto-detected)
|
|
35
37
|
# CPU_THREADS=16 # Threads for CPU-C grinder (default: all cores)
|
|
36
38
|
# Remote GPU via Vast.ai (see: local/vast-setup.sh)
|
package/dist/build.js
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Build native grinder binaries for GPU/CPU mining.
|
|
4
|
+
*
|
|
5
|
+
* Auto-detects available compilers and GPU architecture, then compiles
|
|
6
|
+
* whichever grinders are possible on the current platform:
|
|
7
|
+
* - CPU-C (any platform with clang or gcc)
|
|
8
|
+
* - CUDA (Linux/Windows with nvcc + NVIDIA GPU)
|
|
9
|
+
* - Metal (macOS with clang)
|
|
10
|
+
*
|
|
11
|
+
* Installs compiled binaries to ~/.apow/ where auto-detection will find them.
|
|
12
|
+
*/
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
+
}) : function(o, v) {
|
|
27
|
+
o["default"] = v;
|
|
28
|
+
});
|
|
29
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
30
|
+
var ownKeys = function(o) {
|
|
31
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
32
|
+
var ar = [];
|
|
33
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
34
|
+
return ar;
|
|
35
|
+
};
|
|
36
|
+
return ownKeys(o);
|
|
37
|
+
};
|
|
38
|
+
return function (mod) {
|
|
39
|
+
if (mod && mod.__esModule) return mod;
|
|
40
|
+
var result = {};
|
|
41
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
42
|
+
__setModuleDefault(result, mod);
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
})();
|
|
46
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
47
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.buildGrinders = buildGrinders;
|
|
51
|
+
const node_child_process_1 = require("node:child_process");
|
|
52
|
+
const node_fs_1 = require("node:fs");
|
|
53
|
+
const node_path_1 = require("node:path");
|
|
54
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
55
|
+
const ui = __importStar(require("./ui"));
|
|
56
|
+
const INSTALL_DIR = (0, node_path_1.join)(node_os_1.default.homedir(), ".apow");
|
|
57
|
+
function findSourceDir() {
|
|
58
|
+
// Check relative to this file's compiled location (dist/)
|
|
59
|
+
const fromDist = (0, node_path_1.join)((0, node_path_1.dirname)(__dirname), "local", "gpu");
|
|
60
|
+
if ((0, node_fs_1.existsSync)((0, node_path_1.join)(fromDist, "grinder-cpu.c")))
|
|
61
|
+
return fromDist;
|
|
62
|
+
// Check CWD (git clone case)
|
|
63
|
+
const fromCwd = (0, node_path_1.join)(process.cwd(), "local", "gpu");
|
|
64
|
+
if ((0, node_fs_1.existsSync)((0, node_path_1.join)(fromCwd, "grinder-cpu.c")))
|
|
65
|
+
return fromCwd;
|
|
66
|
+
const fromCwdGpu = (0, node_path_1.join)(process.cwd(), "gpu");
|
|
67
|
+
if ((0, node_fs_1.existsSync)((0, node_path_1.join)(fromCwdGpu, "grinder-cpu.c")))
|
|
68
|
+
return fromCwdGpu;
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
function which(cmd) {
|
|
72
|
+
try {
|
|
73
|
+
(0, node_child_process_1.execSync)(`which ${cmd}`, { stdio: "ignore" });
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function detectCudaArch() {
|
|
81
|
+
try {
|
|
82
|
+
const raw = (0, node_child_process_1.execSync)("nvidia-smi --query-gpu=compute_cap --format=csv,noheader", { encoding: "utf8", timeout: 5000 }).trim();
|
|
83
|
+
// e.g. "8.9" → "sm_89"
|
|
84
|
+
const first = raw.split("\n")[0].trim();
|
|
85
|
+
if (/^\d+\.\d+$/.test(first)) {
|
|
86
|
+
return "sm_" + first.replace(".", "");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch { }
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
function buildCpuC(sourceDir) {
|
|
93
|
+
const src = (0, node_path_1.join)(sourceDir, "grinder-cpu.c");
|
|
94
|
+
const out = (0, node_path_1.join)(INSTALL_DIR, "grinder-cpu");
|
|
95
|
+
const name = "CPU-C";
|
|
96
|
+
if (!(0, node_fs_1.existsSync)(src)) {
|
|
97
|
+
return { name, success: false, error: "source file not found" };
|
|
98
|
+
}
|
|
99
|
+
// Prefer clang, fall back to gcc
|
|
100
|
+
const cc = which("clang") ? "clang" : which("gcc") ? "gcc" : null;
|
|
101
|
+
if (!cc) {
|
|
102
|
+
return { name, success: false, error: "no C compiler found (need clang or gcc)" };
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
(0, node_child_process_1.execSync)(`${cc} -O2 -pthread "${src}" -o "${out}"`, {
|
|
106
|
+
stdio: "pipe",
|
|
107
|
+
timeout: 60000,
|
|
108
|
+
});
|
|
109
|
+
(0, node_fs_1.chmodSync)(out, 0o755);
|
|
110
|
+
return { name, success: true, path: out };
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
114
|
+
return { name, success: false, error: msg.split("\n")[0] };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function buildCuda(sourceDir, archOverride) {
|
|
118
|
+
const src = (0, node_path_1.join)(sourceDir, "grinder-cuda.cu");
|
|
119
|
+
const out = (0, node_path_1.join)(INSTALL_DIR, "grinder-cuda");
|
|
120
|
+
const name = "CUDA";
|
|
121
|
+
if (!(0, node_fs_1.existsSync)(src)) {
|
|
122
|
+
return { name, success: false, error: "source file not found" };
|
|
123
|
+
}
|
|
124
|
+
if (!which("nvcc")) {
|
|
125
|
+
return { name, success: false, error: "nvcc not found (install CUDA toolkit)" };
|
|
126
|
+
}
|
|
127
|
+
const arch = archOverride ?? detectCudaArch() ?? "sm_89";
|
|
128
|
+
try {
|
|
129
|
+
(0, node_child_process_1.execSync)(`nvcc "${src}" -o "${out}" -std=c++17 -O3 -arch=${arch}`, { stdio: "pipe", timeout: 120000 });
|
|
130
|
+
(0, node_fs_1.chmodSync)(out, 0o755);
|
|
131
|
+
return { name: `CUDA (${arch})`, success: true, path: out };
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
135
|
+
return { name, success: false, error: msg.split("\n")[0] };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function buildMetal(sourceDir) {
|
|
139
|
+
const src = (0, node_path_1.join)(sourceDir, "grinder.m");
|
|
140
|
+
const out = (0, node_path_1.join)(INSTALL_DIR, "grinder-gpu");
|
|
141
|
+
const name = "Metal GPU";
|
|
142
|
+
if (process.platform !== "darwin") {
|
|
143
|
+
return { name, success: false, error: "macOS only" };
|
|
144
|
+
}
|
|
145
|
+
if (!(0, node_fs_1.existsSync)(src)) {
|
|
146
|
+
return { name, success: false, error: "source file not found" };
|
|
147
|
+
}
|
|
148
|
+
if (!which("clang")) {
|
|
149
|
+
return { name, success: false, error: "clang not found (install Xcode CLI tools)" };
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
(0, node_child_process_1.execSync)(`clang -O2 -framework Metal -framework Foundation "${src}" -o "${out}"`, { stdio: "pipe", timeout: 60000 });
|
|
153
|
+
(0, node_fs_1.chmodSync)(out, 0o755);
|
|
154
|
+
return { name, success: true, path: out };
|
|
155
|
+
}
|
|
156
|
+
catch (e) {
|
|
157
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
158
|
+
return { name, success: false, error: msg.split("\n")[0] };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async function buildGrinders(opts) {
|
|
162
|
+
console.log("");
|
|
163
|
+
ui.banner(["Build Native Grinders"]);
|
|
164
|
+
console.log("");
|
|
165
|
+
// Find source files
|
|
166
|
+
const sourceDir = findSourceDir();
|
|
167
|
+
if (!sourceDir) {
|
|
168
|
+
ui.error("Grinder source files not found.");
|
|
169
|
+
ui.hint("Ensure local/gpu/ exists (included in npm package since v0.9.2)");
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
console.log(` Source: ${ui.dim(sourceDir)}`);
|
|
173
|
+
// Create install directory
|
|
174
|
+
if (!(0, node_fs_1.existsSync)(INSTALL_DIR)) {
|
|
175
|
+
(0, node_fs_1.mkdirSync)(INSTALL_DIR, { recursive: true });
|
|
176
|
+
}
|
|
177
|
+
console.log(` Install: ${ui.dim(INSTALL_DIR)}`);
|
|
178
|
+
console.log("");
|
|
179
|
+
// Build each grinder
|
|
180
|
+
const results = [];
|
|
181
|
+
const cpuSpinner = ui.spinner("Building CPU-C grinder...");
|
|
182
|
+
const cpuResult = buildCpuC(sourceDir);
|
|
183
|
+
results.push(cpuResult);
|
|
184
|
+
if (cpuResult.success) {
|
|
185
|
+
cpuSpinner.stop(`Building CPU-C grinder... ${ui.green("OK")}`);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
cpuSpinner.stop(`Building CPU-C grinder... ${ui.yellow("skipped")} (${cpuResult.error})`);
|
|
189
|
+
}
|
|
190
|
+
const cudaSpinner = ui.spinner("Building CUDA grinder...");
|
|
191
|
+
const cudaResult = buildCuda(sourceDir, opts.cudaArch);
|
|
192
|
+
results.push(cudaResult);
|
|
193
|
+
if (cudaResult.success) {
|
|
194
|
+
cudaSpinner.stop(`Building CUDA grinder... ${ui.green("OK")}`);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
cudaSpinner.stop(`Building CUDA grinder... ${ui.yellow("skipped")} (${cudaResult.error})`);
|
|
198
|
+
}
|
|
199
|
+
const metalSpinner = ui.spinner("Building Metal GPU grinder...");
|
|
200
|
+
const metalResult = buildMetal(sourceDir);
|
|
201
|
+
results.push(metalResult);
|
|
202
|
+
if (metalResult.success) {
|
|
203
|
+
metalSpinner.stop(`Building Metal GPU grinder... ${ui.green("OK")}`);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
metalSpinner.stop(`Building Metal GPU grinder... ${ui.yellow("skipped")} (${metalResult.error})`);
|
|
207
|
+
}
|
|
208
|
+
// Summary
|
|
209
|
+
console.log("");
|
|
210
|
+
const built = results.filter((r) => r.success);
|
|
211
|
+
if (built.length === 0) {
|
|
212
|
+
ui.warn("No grinders could be built on this system.");
|
|
213
|
+
console.log("");
|
|
214
|
+
console.log(` ${ui.dim("Requirements:")}`);
|
|
215
|
+
console.log(` ${ui.dim(" CPU-C: clang or gcc")}`);
|
|
216
|
+
console.log(` ${ui.dim(" CUDA: nvcc + NVIDIA GPU (nvidia-smi)")}`);
|
|
217
|
+
console.log(` ${ui.dim(" Metal: macOS + Xcode CLI tools")}`);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
ui.ok(`Built ${built.length} grinder${built.length > 1 ? "s" : ""}:`);
|
|
221
|
+
for (const r of built) {
|
|
222
|
+
console.log(` ${ui.green(r.name)} → ${ui.dim(r.path)}`);
|
|
223
|
+
}
|
|
224
|
+
console.log("");
|
|
225
|
+
console.log(` Grinders installed to ${ui.cyan("~/.apow/")} — auto-detected on next mine.`);
|
|
226
|
+
console.log(` Run ${ui.cyan("apow mine")} to start GPU mining.`);
|
|
227
|
+
}
|
|
228
|
+
console.log("");
|
|
229
|
+
}
|
package/dist/config.js
CHANGED
|
@@ -91,6 +91,7 @@ exports.config = {
|
|
|
91
91
|
grinderMode: (process.env.GRINDER_MODE === "js" ? "js" : "auto"),
|
|
92
92
|
gpuGrinderPath: process.env.GPU_GRINDER_PATH,
|
|
93
93
|
cpuGrinderPath: process.env.CPU_GRINDER_PATH,
|
|
94
|
+
cudaGrinderPath: process.env.CUDA_GRINDER_PATH,
|
|
94
95
|
vastIp: process.env.VAST_IP,
|
|
95
96
|
vastPort: process.env.VAST_PORT,
|
|
96
97
|
cpuGrinderThreads: parseInt(process.env.CPU_THREADS ?? String(node_os_1.default.cpus().length), 10),
|
package/dist/grinder-native.js
CHANGED
|
@@ -39,6 +39,7 @@ const config_1 = require("./config");
|
|
|
39
39
|
function detectGrinders() {
|
|
40
40
|
return {
|
|
41
41
|
gpu: detectLocalGpu(),
|
|
42
|
+
cuda: detectLocalCuda(),
|
|
42
43
|
cpu: detectLocalCpu(),
|
|
43
44
|
remoteGpu: !!(config_1.config.vastIp && config_1.config.vastPort),
|
|
44
45
|
};
|
|
@@ -61,6 +62,22 @@ function detectLocalGpu() {
|
|
|
61
62
|
}
|
|
62
63
|
return null;
|
|
63
64
|
}
|
|
65
|
+
function detectLocalCuda() {
|
|
66
|
+
if (config_1.config.cudaGrinderPath) {
|
|
67
|
+
return (0, node_fs_1.existsSync)(config_1.config.cudaGrinderPath) ? config_1.config.cudaGrinderPath : null;
|
|
68
|
+
}
|
|
69
|
+
const candidates = [
|
|
70
|
+
(0, node_path_1.join)(process.cwd(), "gpu", "grinder-cuda"),
|
|
71
|
+
(0, node_path_1.join)(process.cwd(), "local", "gpu", "grinder-cuda"),
|
|
72
|
+
(0, node_path_1.join)(process.cwd(), "grinder-cuda"),
|
|
73
|
+
(0, node_path_1.join)(node_os_1.default.homedir(), ".apow", "grinder-cuda"),
|
|
74
|
+
];
|
|
75
|
+
for (const p of candidates) {
|
|
76
|
+
if ((0, node_fs_1.existsSync)(p))
|
|
77
|
+
return p;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
64
81
|
function detectLocalCpu() {
|
|
65
82
|
if (config_1.config.cpuGrinderPath) {
|
|
66
83
|
return (0, node_fs_1.existsSync)(config_1.config.cpuGrinderPath) ? config_1.config.cpuGrinderPath : null;
|
|
@@ -78,12 +95,14 @@ function detectLocalCpu() {
|
|
|
78
95
|
return null;
|
|
79
96
|
}
|
|
80
97
|
function hasNativeGrinders(info) {
|
|
81
|
-
return !!(info.gpu || info.cpu || info.remoteGpu);
|
|
98
|
+
return !!(info.gpu || info.cuda || info.cpu || info.remoteGpu);
|
|
82
99
|
}
|
|
83
100
|
function grinderLabel(info) {
|
|
84
101
|
const parts = [];
|
|
85
102
|
if (info.remoteGpu)
|
|
86
103
|
parts.push("CUDA (remote)");
|
|
104
|
+
if (info.cuda)
|
|
105
|
+
parts.push("CUDA GPU");
|
|
87
106
|
if (info.gpu)
|
|
88
107
|
parts.push(process.platform === "darwin" ? "Metal GPU" : "GPU");
|
|
89
108
|
if (info.cpu)
|
|
@@ -189,6 +208,42 @@ async function grindNonceNative(challengeNumber, target, minerAddress, info, sig
|
|
|
189
208
|
});
|
|
190
209
|
proc.on("error", () => onFail());
|
|
191
210
|
}
|
|
211
|
+
// Local CUDA GPU
|
|
212
|
+
if (info.cuda) {
|
|
213
|
+
totalGrinders++;
|
|
214
|
+
// CUDA arg order: <challenge> <target> <address>
|
|
215
|
+
const proc = (0, node_child_process_1.spawn)(info.cuda, [challengeNumber, targetHex, minerAddress]);
|
|
216
|
+
processes.push(proc);
|
|
217
|
+
let buf = "";
|
|
218
|
+
proc.stdout.on("data", (d) => {
|
|
219
|
+
if (settled)
|
|
220
|
+
return;
|
|
221
|
+
buf += d.toString();
|
|
222
|
+
let nl;
|
|
223
|
+
while ((nl = buf.indexOf("\n")) !== -1) {
|
|
224
|
+
const line = buf.slice(0, nl).trim();
|
|
225
|
+
buf = buf.slice(nl + 1);
|
|
226
|
+
if (line.startsWith("F ")) {
|
|
227
|
+
const parts = line.split(/\s+/);
|
|
228
|
+
if (parts.length >= 4) {
|
|
229
|
+
const e = parseFloat(parts[3]);
|
|
230
|
+
onResult({
|
|
231
|
+
nonce: BigInt(parts[2]),
|
|
232
|
+
attempts: 0n,
|
|
233
|
+
elapsed: e,
|
|
234
|
+
hashrate: 0,
|
|
235
|
+
});
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
proc.on("close", () => {
|
|
242
|
+
if (!settled)
|
|
243
|
+
onFail();
|
|
244
|
+
});
|
|
245
|
+
proc.on("error", () => onFail());
|
|
246
|
+
}
|
|
192
247
|
// Local CPU-C
|
|
193
248
|
if (info.cpu) {
|
|
194
249
|
totalGrinders++;
|
package/dist/index.js
CHANGED
|
@@ -350,6 +350,14 @@ async function main() {
|
|
|
350
350
|
}
|
|
351
351
|
await (0, stats_1.displayStats)(tokenId);
|
|
352
352
|
});
|
|
353
|
+
program
|
|
354
|
+
.command("build-grinders")
|
|
355
|
+
.description("Compile native grinder binaries for GPU/CPU mining (~10-100x faster)")
|
|
356
|
+
.option("--cuda-arch <arch>", "CUDA architecture override (e.g., sm_89)")
|
|
357
|
+
.action(async (opts) => {
|
|
358
|
+
const { buildGrinders } = await Promise.resolve().then(() => __importStar(require("./build")));
|
|
359
|
+
await buildGrinders(opts);
|
|
360
|
+
});
|
|
353
361
|
const walletCmd = program
|
|
354
362
|
.command("wallet")
|
|
355
363
|
.description("Wallet generation and management");
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# APoW GPU Nonce Grinder — Build Targets
|
|
2
|
+
#
|
|
3
|
+
# Metal (macOS Apple Silicon): make metal
|
|
4
|
+
# CUDA (Vast.ai RTX 4090): make cuda
|
|
5
|
+
# CPU (reference/fallback): make cpu
|
|
6
|
+
|
|
7
|
+
.PHONY: all metal cuda cpu clean
|
|
8
|
+
|
|
9
|
+
# ── Metal (macOS only) ──
|
|
10
|
+
metal: grinder-gpu
|
|
11
|
+
|
|
12
|
+
grinder-gpu: grinder.m keccak.metal
|
|
13
|
+
clang -O2 -framework Metal -framework Foundation grinder.m -o grinder-gpu
|
|
14
|
+
|
|
15
|
+
# ── CUDA (Linux, requires nvcc + CUDA toolkit) ──
|
|
16
|
+
# sm_89 = Ada Lovelace (RTX 4090). Change for other GPUs:
|
|
17
|
+
# sm_86 = Ampere (RTX 3090), sm_90 = Hopper (H100)
|
|
18
|
+
CUDA_ARCH ?= sm_89
|
|
19
|
+
|
|
20
|
+
cuda: grinder-cuda
|
|
21
|
+
|
|
22
|
+
grinder-cuda: grinder-cuda.cu
|
|
23
|
+
nvcc grinder-cuda.cu -o grinder-cuda -std=c++17 -O3 -arch=$(CUDA_ARCH)
|
|
24
|
+
|
|
25
|
+
# ── CPU (portable reference) ──
|
|
26
|
+
cpu: grinder-cpu
|
|
27
|
+
|
|
28
|
+
grinder-cpu: grinder-cpu.c
|
|
29
|
+
clang -O2 -pthread grinder-cpu.c -o grinder-cpu
|
|
30
|
+
|
|
31
|
+
# ── Test harness (Metal, verifies GPU vs CPU keccak) ──
|
|
32
|
+
test: test_keccak
|
|
33
|
+
|
|
34
|
+
test_keccak: test_keccak.m
|
|
35
|
+
clang -O2 -framework Metal -framework Foundation test_keccak.m -o test_keccak
|
|
36
|
+
|
|
37
|
+
# ── Cleanup ──
|
|
38
|
+
clean:
|
|
39
|
+
rm -f grinder-gpu grinder-cuda grinder-cpu test_keccak *.metallib
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* APoW PoW Nonce Grinder — Multi-threaded keccak256
|
|
3
|
+
* Usage: ./grinder <challenge_hex> <address_hex> <target_hex> [threads]
|
|
4
|
+
* Output: <nonce_decimal> <attempts>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
#include <stdint.h>
|
|
11
|
+
#include <unistd.h>
|
|
12
|
+
#include <pthread.h>
|
|
13
|
+
#include <time.h>
|
|
14
|
+
|
|
15
|
+
/* ─── Keccak-256 (compact implementation) ──────────────────── */
|
|
16
|
+
|
|
17
|
+
#define KECCAK_ROUNDS 24
|
|
18
|
+
|
|
19
|
+
static const uint64_t RC[24] = {
|
|
20
|
+
0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
|
|
21
|
+
0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
|
|
22
|
+
0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
|
|
23
|
+
0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
|
|
24
|
+
0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
|
|
25
|
+
0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
|
|
26
|
+
0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
|
|
27
|
+
0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
static const int ROTC[24] = {
|
|
31
|
+
1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
static const int PILN[24] = {
|
|
35
|
+
10,7,11,17,18,3,5,16,8,21,24,4,15,23,19,13,12,2,20,14,22,9,6,1
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
static inline uint64_t rotl64(uint64_t x, int n) {
|
|
39
|
+
return (x << n) | (x >> (64 - n));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static void keccak_f1600(uint64_t st[25]) {
|
|
43
|
+
uint64_t bc[5];
|
|
44
|
+
for (int r = 0; r < KECCAK_ROUNDS; r++) {
|
|
45
|
+
// Theta
|
|
46
|
+
for (int i = 0; i < 5; i++)
|
|
47
|
+
bc[i] = st[i] ^ st[i+5] ^ st[i+10] ^ st[i+15] ^ st[i+20];
|
|
48
|
+
for (int i = 0; i < 5; i++) {
|
|
49
|
+
uint64_t t = bc[(i+4)%5] ^ rotl64(bc[(i+1)%5], 1);
|
|
50
|
+
for (int j = 0; j < 25; j += 5) st[j+i] ^= t;
|
|
51
|
+
}
|
|
52
|
+
// Rho + Pi
|
|
53
|
+
uint64_t t = st[1];
|
|
54
|
+
for (int i = 0; i < 24; i++) {
|
|
55
|
+
int j = PILN[i];
|
|
56
|
+
uint64_t tmp = st[j];
|
|
57
|
+
st[j] = rotl64(t, ROTC[i]);
|
|
58
|
+
t = tmp;
|
|
59
|
+
}
|
|
60
|
+
// Chi
|
|
61
|
+
for (int j = 0; j < 25; j += 5) {
|
|
62
|
+
uint64_t tmp[5];
|
|
63
|
+
for (int i = 0; i < 5; i++) tmp[i] = st[j+i];
|
|
64
|
+
for (int i = 0; i < 5; i++)
|
|
65
|
+
st[j+i] = tmp[i] ^ ((~tmp[(i+1)%5]) & tmp[(i+2)%5]);
|
|
66
|
+
}
|
|
67
|
+
// Iota
|
|
68
|
+
st[0] ^= RC[r];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static void keccak256(const uint8_t *in, size_t inlen, uint8_t *out) {
|
|
73
|
+
uint64_t st[25] = {0};
|
|
74
|
+
const int rate = 136; // (1600 - 256*2) / 8
|
|
75
|
+
|
|
76
|
+
// Absorb
|
|
77
|
+
while (inlen >= (size_t)rate) {
|
|
78
|
+
for (int i = 0; i < rate/8; i++)
|
|
79
|
+
st[i] ^= ((const uint64_t*)in)[i];
|
|
80
|
+
keccak_f1600(st);
|
|
81
|
+
in += rate;
|
|
82
|
+
inlen -= rate;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Final block (pad with aligned copy)
|
|
86
|
+
uint8_t tmp[136] = {0};
|
|
87
|
+
memcpy(tmp, in, inlen);
|
|
88
|
+
tmp[inlen] = 0x01; // Keccak padding (NOT SHA3 which uses 0x06)
|
|
89
|
+
tmp[rate - 1] |= 0x80;
|
|
90
|
+
|
|
91
|
+
for (int i = 0; i < rate/8; i++)
|
|
92
|
+
st[i] ^= ((uint64_t*)tmp)[i];
|
|
93
|
+
keccak_f1600(st);
|
|
94
|
+
|
|
95
|
+
// Squeeze (32 bytes)
|
|
96
|
+
memcpy(out, st, 32);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* ─── Hex parsing ──────────────────────────────────────────── */
|
|
100
|
+
|
|
101
|
+
static int hex2byte(char c) {
|
|
102
|
+
if (c >= '0' && c <= '9') return c - '0';
|
|
103
|
+
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
104
|
+
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
105
|
+
return -1;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static int parse_hex(const char *hex, uint8_t *out, int len) {
|
|
109
|
+
if (hex[0] == '0' && hex[1] == 'x') hex += 2;
|
|
110
|
+
for (int i = 0; i < len; i++) {
|
|
111
|
+
int hi = hex2byte(hex[i*2]);
|
|
112
|
+
int lo = hex2byte(hex[i*2+1]);
|
|
113
|
+
if (hi < 0 || lo < 0) return -1;
|
|
114
|
+
out[i] = (hi << 4) | lo;
|
|
115
|
+
}
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* ─── Worker thread ────────────────────────────────────────── */
|
|
120
|
+
|
|
121
|
+
typedef struct {
|
|
122
|
+
uint8_t base[84]; // challenge(32) + address(20) + nonce_template(32)
|
|
123
|
+
uint8_t target[32];
|
|
124
|
+
uint64_t start;
|
|
125
|
+
uint64_t range;
|
|
126
|
+
uint64_t found_nonce;
|
|
127
|
+
volatile int *global_found;
|
|
128
|
+
uint64_t attempts;
|
|
129
|
+
} WorkerArgs;
|
|
130
|
+
|
|
131
|
+
static void *grind_worker(void *arg) {
|
|
132
|
+
WorkerArgs *a = (WorkerArgs *)arg;
|
|
133
|
+
uint8_t buf[84];
|
|
134
|
+
uint8_t hash[32];
|
|
135
|
+
memcpy(buf, a->base, 52); // challenge + address
|
|
136
|
+
memset(buf + 52, 0, 24); // Zero first 24 bytes of nonce area
|
|
137
|
+
|
|
138
|
+
for (uint64_t n = a->start; n < a->start + a->range; n++) {
|
|
139
|
+
if (*a->global_found) return NULL;
|
|
140
|
+
|
|
141
|
+
// Write nonce as big-endian in last 8 bytes (buf[76..83])
|
|
142
|
+
buf[76] = (n >> 56) & 0xff;
|
|
143
|
+
buf[77] = (n >> 48) & 0xff;
|
|
144
|
+
buf[78] = (n >> 40) & 0xff;
|
|
145
|
+
buf[79] = (n >> 32) & 0xff;
|
|
146
|
+
buf[80] = (n >> 24) & 0xff;
|
|
147
|
+
buf[81] = (n >> 16) & 0xff;
|
|
148
|
+
buf[82] = (n >> 8) & 0xff;
|
|
149
|
+
buf[83] = n & 0xff;
|
|
150
|
+
|
|
151
|
+
keccak256(buf, 84, hash);
|
|
152
|
+
|
|
153
|
+
// Compare hash < target (MSB first)
|
|
154
|
+
int less = 0;
|
|
155
|
+
for (int i = 0; i < 32; i++) {
|
|
156
|
+
if (hash[i] < a->target[i]) { less = 1; break; }
|
|
157
|
+
if (hash[i] > a->target[i]) break;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (less) {
|
|
161
|
+
a->found_nonce = n;
|
|
162
|
+
a->attempts = n - a->start + 1;
|
|
163
|
+
__sync_val_compare_and_swap(a->global_found, 0, 1);
|
|
164
|
+
return NULL;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
a->attempts = a->range;
|
|
168
|
+
return NULL;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* ─── Main ─────────────────────────────────────────────────── */
|
|
172
|
+
|
|
173
|
+
int main(int argc, char *argv[]) {
|
|
174
|
+
if (argc < 4) {
|
|
175
|
+
fprintf(stderr, "Usage: %s <challenge_hex> <address_hex> <target_hex> [threads]\n", argv[0]);
|
|
176
|
+
return 1;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
uint8_t challenge[32], address[20], target[32];
|
|
180
|
+
if (parse_hex(argv[1], challenge, 32) < 0 ||
|
|
181
|
+
parse_hex(argv[2], address, 20) < 0 ||
|
|
182
|
+
parse_hex(argv[3], target, 32) < 0) {
|
|
183
|
+
fprintf(stderr, "Invalid hex input\n");
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
int nthreads = argc > 4 ? atoi(argv[4]) : 8;
|
|
188
|
+
if (nthreads < 1) nthreads = 1;
|
|
189
|
+
if (nthreads > 64) nthreads = 64;
|
|
190
|
+
|
|
191
|
+
// Build base buffer
|
|
192
|
+
uint8_t base[84] = {0};
|
|
193
|
+
memcpy(base, challenge, 32);
|
|
194
|
+
memcpy(base + 32, address, 20);
|
|
195
|
+
|
|
196
|
+
// Random start nonce
|
|
197
|
+
srand((unsigned)time(NULL) ^ (unsigned)getpid());
|
|
198
|
+
uint64_t start = ((uint64_t)rand() << 32) | rand();
|
|
199
|
+
uint64_t range_per_thread = 500000000ULL; // 500M per thread max
|
|
200
|
+
|
|
201
|
+
volatile int found = 0;
|
|
202
|
+
pthread_t threads[64];
|
|
203
|
+
WorkerArgs args[64];
|
|
204
|
+
|
|
205
|
+
struct timespec t0, t1;
|
|
206
|
+
clock_gettime(CLOCK_MONOTONIC, &t0);
|
|
207
|
+
|
|
208
|
+
for (int i = 0; i < nthreads; i++) {
|
|
209
|
+
args[i] = (WorkerArgs){
|
|
210
|
+
.start = start + i * range_per_thread,
|
|
211
|
+
.range = range_per_thread,
|
|
212
|
+
.found_nonce = 0,
|
|
213
|
+
.global_found = &found,
|
|
214
|
+
.attempts = 0
|
|
215
|
+
};
|
|
216
|
+
memcpy(args[i].base, base, 84);
|
|
217
|
+
memcpy(args[i].target, target, 32);
|
|
218
|
+
pthread_create(&threads[i], NULL, grind_worker, &args[i]);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Wait for completion
|
|
222
|
+
for (int i = 0; i < nthreads; i++)
|
|
223
|
+
pthread_join(threads[i], NULL);
|
|
224
|
+
|
|
225
|
+
clock_gettime(CLOCK_MONOTONIC, &t1);
|
|
226
|
+
double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;
|
|
227
|
+
|
|
228
|
+
// Find the winning thread
|
|
229
|
+
uint64_t total_attempts = 0;
|
|
230
|
+
for (int i = 0; i < nthreads; i++) {
|
|
231
|
+
total_attempts += args[i].attempts;
|
|
232
|
+
if (args[i].found_nonce && found) {
|
|
233
|
+
printf("%llu %llu %.3f\n", args[i].found_nonce, total_attempts, elapsed);
|
|
234
|
+
fflush(stdout);
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fprintf(stderr, "No nonce found after %llu attempts (%.1fs)\n", total_attempts, elapsed);
|
|
240
|
+
return 1;
|
|
241
|
+
}
|
|
@@ -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(¶ms, 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.
|
|
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",
|
|
@@ -31,7 +31,12 @@
|
|
|
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",
|