-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmine_commit.sh
More file actions
executable file
·356 lines (307 loc) · 9.97 KB
/
mine_commit.sh
File metadata and controls
executable file
·356 lines (307 loc) · 9.97 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
#
# mine_commit.sh - Mine a git commit hash with leading zeros
#
# Usage: mine_commit.sh [mode] [args...]
#
# mine_commit.sh [target_zeros] [threads]
# Mine for N leading hex zeros (default: 7).
#
# mine_commit.sh infinite [threads]
# Run forever finding the lowest possible hash.
# Press Ctrl+C to stop and apply the best result found so far.
#
# mine_commit.sh incremental [prefix_length] [threads]
# Auto-generate a sequential hex prefix based on commit number.
# First commit -> 0000000, second -> 0000001, ..., 11th -> 000000a, etc.
# prefix_length: number of hex digits (default: 7)
#
# threads: CPU threads / device ID to use (default: auto-detect)
#
# Must be run from within a git repository after making a commit.
# The most recent commit will be re-created with a nonce that produces
# a hash matching the target.
#
# For GPG-signed commits: the nonce is placed in a PGP armor Comment
# header, which does NOT invalidate the cryptographic signature but
# DOES affect the git SHA-1 hash. This allows mining and signing
# to coexist.
#
# For unsigned commits: the nonce is appended to the commit message.
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ── Parse mode and arguments ──
MODE="zeros"
TARGET_ZEROS=7
TARGET_PREFIX=""
case "${1:-7}" in
infinite)
MODE="infinite"
TARGET_ZEROS=0
THREADS=${2:-$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)}
;;
incremental)
MODE="incremental"
TARGET_ZEROS=0
PREFIX_LENGTH=${2:-7}
THREADS=${3:-$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)}
;;
*)
TARGET_ZEROS=${1:-7}
THREADS=${2:-$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)}
;;
esac
NONCE_PLACEHOLDER="aaaaaaaaaa"
NONCE_LEN=${#NONCE_PLACEHOLDER}
# ── Auto-detect best available miner ──
IS_CUDA=0
if [ -x "$SCRIPT_DIR/gitminer" ]; then
MINER="$SCRIPT_DIR/gitminer"
IS_CUDA=1
echo "Using CUDA GPU miner"
elif [ -x "$SCRIPT_DIR/gitminer_vulkan" ]; then
MINER="$SCRIPT_DIR/gitminer_vulkan"
echo "Using Vulkan GPU miner"
elif [ -x "$SCRIPT_DIR/gitminer_metal" ]; then
MINER="$SCRIPT_DIR/gitminer_metal"
echo "Using Metal GPU miner"
elif [ -x "$SCRIPT_DIR/gitminer_opencl" ]; then
MINER="$SCRIPT_DIR/gitminer_opencl"
echo "Using OpenCL GPU miner"
elif [ -x "$SCRIPT_DIR/gitminer_cpu" ]; then
MINER="$SCRIPT_DIR/gitminer_cpu"
echo "Using CPU miner"
else
echo "Error: no miner found. Run 'make gitminer_metal', 'make gitminer_opencl', or 'make gitminer_cpu' first." >&2
exit 1
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: not inside a git repository." >&2
exit 1
fi
COMMIT_HASH=$(git rev-parse HEAD)
echo "Mining commit: $COMMIT_HASH"
# ── For incremental mode, compute target prefix from commit number ──
if [ "$MODE" = "incremental" ]; then
COMMIT_INDEX=$(( $(git rev-list --count HEAD) - 1 ))
TARGET_PREFIX=$(printf "%0${PREFIX_LENGTH}x" "$COMMIT_INDEX")
echo "Incremental mode: commit #$COMMIT_INDEX -> prefix $TARGET_PREFIX"
fi
# Check if commit is GPG-signed
IS_SIGNED=0
if git cat-file commit HEAD | grep -q '^gpgsig '; then
IS_SIGNED=1
fi
if [ "$IS_SIGNED" = "1" ]; then
echo "Commit is GPG-signed. Mining via PGP armor Comment nonce."
# Inject Comment: nonce=aaaaaaaaaa into the gpgsig armor block.
# This line is NOT part of the cryptographic signature (GPG ignores
# armor headers during verification), but IS part of the git object hash.
python3 -c "
import sys
content = open('.git/' + open('.git/HEAD').read().strip().split(': ')[1], 'rb').read() if False else None
# Read the raw commit content via git
import subprocess
raw = subprocess.check_output(['git', 'cat-file', 'commit', 'HEAD'])
lines = raw.split(b'\n')
result = []
nonce_line = b' Comment: nonce=${NONCE_PLACEHOLDER}'
found_gpgsig = False
comment_replaced = False
for line in lines:
# Look for existing Comment: nonce= line and replace it
if found_gpgsig and line.startswith(b' Comment: nonce='):
result.append(nonce_line)
comment_replaced = True
continue
result.append(line)
if line.startswith(b'gpgsig -----BEGIN'):
found_gpgsig = True
if not comment_replaced:
# Insert Comment line right after BEGIN line
result.append(nonce_line)
comment_replaced = True
if not comment_replaced:
print('Error: could not find gpgsig block', file=sys.stderr)
sys.exit(1)
modified = b'\n'.join(result)
# Build full git object
header = b'commit ' + str(len(modified)).encode() + b'\x00'
full_object = header + modified
open('base.txt', 'wb').write(full_object)
# Find nonce position
nonce_marker = b'nonce=${NONCE_PLACEHOLDER}'
pos = full_object.find(nonce_marker)
if pos == -1:
print('Error: nonce not found in object', file=sys.stderr)
sys.exit(1)
# The actual nonce starts after 'nonce='
nonce_pos = pos + len(b'nonce=')
print(nonce_pos)
"
NONCE_POS=$?
# Re-run to capture output (previous was exit code check)
NONCE_POS=$(python3 -c "
import subprocess
raw = subprocess.check_output(['git', 'cat-file', 'commit', 'HEAD'])
lines = raw.split(b'\n')
result = []
nonce_line = b' Comment: nonce=${NONCE_PLACEHOLDER}'
found_gpgsig = False
comment_replaced = False
for line in lines:
if found_gpgsig and line.startswith(b' Comment: nonce='):
result.append(nonce_line)
comment_replaced = True
continue
result.append(line)
if line.startswith(b'gpgsig -----BEGIN'):
found_gpgsig = True
if not comment_replaced:
result.append(nonce_line)
comment_replaced = True
modified = b'\n'.join(result)
header = b'commit ' + str(len(modified)).encode() + b'\x00'
full_object = header + modified
open('base.txt', 'wb').write(full_object)
nonce_marker = b'nonce=${NONCE_PLACEHOLDER}'
pos = full_object.find(nonce_marker)
nonce_pos = pos + len(b'nonce=')
print(nonce_pos)
")
else
echo "Commit is unsigned. Mining via message nonce."
ORIG_MSG=$(git log -1 --format='%B')
if echo "$ORIG_MSG" | grep -q '^nonce:'; then
NEW_MSG=$(echo "$ORIG_MSG" | sed "s/^nonce:.*$/nonce:${NONCE_PLACEHOLDER}/")
else
NEW_MSG=$(printf '%s\nnonce:%s\n' "$(echo "$ORIG_MSG" | sed -e 's/[[:space:]]*$//')" "$NONCE_PLACEHOLDER")
fi
AUTHOR_DATE=$(git log -1 --format='%aI')
COMMITTER_DATE=$(git log -1 --format='%cI')
GIT_AUTHOR_DATE="$AUTHOR_DATE" GIT_COMMITTER_DATE="$COMMITTER_DATE" \
git commit --amend -m "$NEW_MSG" --allow-empty >/dev/null 2>&1
COMMIT_CONTENT=$(git cat-file commit HEAD)
CONTENT_LEN=$(printf '%s' "$COMMIT_CONTENT" | wc -c | tr -d ' ')
printf "commit %d\0%s" "$CONTENT_LEN" "$COMMIT_CONTENT" > base.txt
NONCE_POS=$(python3 -c "
data = open('base.txt', 'rb').read()
pos = data.find(b'${NONCE_PLACEHOLDER}')
print(pos if pos != -1 else -1)
")
fi
if [ "$NONCE_POS" = "-1" ] || [ -z "$NONCE_POS" ]; then
echo "Error: could not find nonce placeholder in commit object." >&2
exit 1
fi
NONCE_END=$((NONCE_POS + NONCE_LEN))
echo "Nonce position: [$NONCE_POS, $NONCE_END)"
# ── Display target info ──
case "$MODE" in
infinite)
echo "Mode: infinite (finding lowest hash, Ctrl+C to stop)"
;;
incremental)
echo "Mode: incremental (target prefix: $TARGET_PREFIX)"
;;
*)
echo "Target: $TARGET_ZEROS leading hex zeros"
;;
esac
echo "Threads: $THREADS"
echo "Mining..."
# ── Build miner arguments ──
MINER_ARGS=("$THREADS" /dev/stderr result.txt "$NONCE_POS" "$NONCE_END" "$TARGET_ZEROS")
if [ -n "$TARGET_PREFIX" ]; then
if [ "$IS_CUDA" = "1" ]; then
MINER_ARGS+=(1024 "$TARGET_PREFIX")
else
MINER_ARGS+=("$TARGET_PREFIX")
fi
fi
# ── Run the miner ──
if [ "$MODE" = "infinite" ]; then
# Run in background so we can trap Ctrl+C and apply best result
"$MINER" "${MINER_ARGS[@]}" 2>&1 &
MINER_PID=$!
cleanup() {
echo ""
echo "Stopping miner (PID $MINER_PID)..."
kill "$MINER_PID" 2>/dev/null || true
wait "$MINER_PID" 2>/dev/null || true
}
trap cleanup INT TERM
wait "$MINER_PID" 2>/dev/null || true
trap - INT TERM
else
"$MINER" "${MINER_ARGS[@]}" 2>&1
fi
# ── Process result ──
if [ ! -f result.txt ]; then
if [ "$MODE" = "infinite" ]; then
echo "No result found (miner was stopped before finding any hash)." >&2
rm -f base.txt
exit 1
fi
echo "Error: miner did not produce result.txt" >&2
exit 1
fi
MINED_NONCE=$(dd if=result.txt bs=1 skip="$NONCE_POS" count="$NONCE_LEN" 2>/dev/null)
echo "Mined nonce: $MINED_NONCE"
# Extract commit content from result.txt (strip "commit <len>\0" header)
python3 -c "
data = open('result.txt', 'rb').read()
null_pos = data.index(b'\x00')
open('commit_content.tmp', 'wb').write(data[null_pos+1:])
"
NEW_HASH=$(git hash-object -t commit -w commit_content.tmp)
echo "New commit hash: $NEW_HASH"
ZEROS=$(python3 -c "h='$NEW_HASH'; print(len(h) - len(h.lstrip('0')))")
echo "Leading zeros: $ZEROS"
APPLY_RESULT=0
case "$MODE" in
infinite)
# Always apply in infinite mode (best result found)
APPLY_RESULT=1
echo "Best hash found during mining session."
;;
incremental)
# Verify the prefix matches
ACTUAL_PREFIX=$(python3 -c "print('$NEW_HASH'[:${PREFIX_LENGTH}])")
if [ "$ACTUAL_PREFIX" = "$TARGET_PREFIX" ]; then
APPLY_RESULT=1
else
echo "Warning: hash prefix '$ACTUAL_PREFIX' does not match target '$TARGET_PREFIX'" >&2
fi
;;
*)
if [ "$ZEROS" -ge "$TARGET_ZEROS" ]; then
APPLY_RESULT=1
else
echo "Warning: hash only has $ZEROS leading zeros (target: $TARGET_ZEROS)" >&2
fi
;;
esac
if [ "$APPLY_RESULT" = "1" ]; then
BRANCH=$(git branch --show-current)
if [ -n "$BRANCH" ]; then
git update-ref "refs/heads/$BRANCH" "$NEW_HASH"
echo "Success! Branch '$BRANCH' now points to $NEW_HASH"
else
git update-ref HEAD "$NEW_HASH"
echo "Success! HEAD now points to $NEW_HASH"
fi
# Verify signature if signed
if [ "$IS_SIGNED" = "1" ]; then
echo "Verifying GPG signature..."
if git verify-commit "$NEW_HASH" 2>&1; then
echo "GPG signature: VALID"
else
echo "WARNING: GPG signature verification failed!" >&2
fi
fi
fi
# Cleanup
rm -f base.txt result.txt commit_content.tmp log.txt
echo "Done! Final commit: $(git rev-parse HEAD)"