Algorithms, hashing, and nonce search
Overview
This area owns the proof-of-work algorithm registrations, CPU nonce search implementations, OpenCL nonce result handling, and architecture-specific SHA256 acceleration used by the miner.
The registered mining algorithms live under malgo/. malgo/sha256d.c registers malgo_sha256d for POW_SHA256D; its hash_data reverses the block-header word swapping and calls gen_hash for the final double-SHA256 hash. When OpenCL is enabled, the SHA256d algorithm also supplies intensity/thread conversions and a default kernel selector: Mesa uses phatk, ATI SDK/driver version strings for pre-Tahiti devices use diablo, Tahiti or devices without bit-align use poclbm, and the remaining path uses phatk. malgo/scrypt.c registers malgo_scrypt for POW_SCRYPT; it contains the CPU scrypt implementation (PBKDF2_SHA256_80_128, salsa20_8, and scrypt_1024_1_1_256_sp), exposes scrypt_hash_data for generic nonce validation, and gives OpenCL the fixed scrypt kernel name. malgo/keccak.c registers malgo_keccak for POW_KECCAK; it implements a one-block Keccak permutation over the 80-byte header after word swapping and maps OpenCL work to the keccak kernel.
CPU mining chooses between generic hashing and specialized scan loops in driver-cpu.c, with the function implementations owned here. driver-cpu.h defines the compile-time availability gates: WANT_SSE2_4WAY, WANT_ALTIVEC_4WAY, WANT_X8632_SSE2, WANT_X8664_SSE2, WANT_X8664_SSE4, WANT_VIA_PADLOCK, and WANT_SCRYPT are derived from build options, architecture macros, YASM availability, and USE_SHA256D/USE_SCRYPT. driver-cpu.c then maps ALGO_* names to the scanhash functions. For SHA256d work with nonce difficulty at least 1, cpu_scanhash calls the selected optimized SHA256d function; for scrypt it calls scanhash_scrypt; all other algorithms, including Keccak, use the generic loop that calls the algorithm's hash_data_f from the malgo registration.
The plain C SHA256d path is sha256_generic.c. scanhash_c starts from work->midstate, hashes the second half of the 80-byte header with the changing nonce, then hashes hash1_init plus the first digest with sha256_init_state. It scans until hash32[7] == 0, a max nonce is reached, or thr->work_restart is set, and writes the accepted nonce back into work->data[76] in little-endian form. sha256_cryptopp.c provides the same external scan behavior with a Crypto++-style SHA256 transform, plus an optional 32-bit Crypto++ assembly-backed path scanhash_asm32 when WANT_CRYPTOPP_ASM32 is enabled.
The SIMD SHA256d paths batch multiple nonce candidates per transform. sha256_4way.c and sha256_altivec_4way.c implement 32-candidate loops using four-wide SSE2 or Altivec vectors in repeated groups; both inject sequential nonce lanes into the message word corresponding to the block header nonce, run double-SHA256, skip the last three rounds of the second SHA256 when only the high word zero test is needed, then copy a candidate digest back to work->hash for confirmation. sha256_sse2_i386.c, sha256_sse2_amd64.c, and sha256_sse4_amd64.c scan four candidates at a time and delegate the vectorized compression to assembly entry points. The 32-bit wrapper calls CalcSha256_x86 from x86_32/sha256_xmm.asm; the x86-64 SSE2 wrapper calls sha256_sse2_64_new from x86_64/sha256_xmm_amd64.asm; the x86-64 SSE4 wrapper calls CalcSha256_x64_sse4 from x86_64/sha256_sse4_amd64.asm. x86_32/Makefile.am and x86_64/Makefile.am build those YASM sources into private static libraries.
sha256_via.c is the VIA PadLock hardware SHA path. When WANT_VIA_PADLOCK is enabled on i386, scanhash_via uses the PadLock SHA instruction byte sequence via inline assembly, prepares data in the endianness expected by the hardware, double-hashes each nonce, and writes the found nonce back to the original work buffer with byte swapping.
OpenCL devices use the same malgo registrations but do nonce scanning in kernels outside this doc's coverage. The helpers in findnonce.c bridge host work to those kernels. For SHA256d, precalc_hash fills struct opencl_work_data with midstate words, header words, SHA256 constants, and precomputed schedule/round values so kernels can avoid repeating invariant work. After an OpenCL kernel returns, postcalc_hash_async copies the kernel result buffer (BUFFERSIZE for SHA256d/Keccak-style kernels or SCRYPT_BUFFERSIZE for scrypt), spawns a detached postcalc_hash thread, validates the returned count stored at FOUND or SCRYPT_FOUND, optionally byte-swaps nonces for KL_FULLHEADER, and submits each nonce through submit_nonce. driver-opencl.c uses these helpers by calling precalc_hash only for POW_SHA256D work before launch and postcalc_hash_async whenever the kernel's found-count slot is non-zero.
Coverage
malgo/**- findnonce.c
- findnonce.h
- sha256_*.c
- sha256_4way.c
- sha256_altivec_4way.c
- sha256_generic.c
- sha256_cryptopp.c
- sha256_sse2_amd64.c
- sha256_sse2_i386.c
- sha256_sse4_amd64.c
- sha256_via.c
x86_32/**x86_64/**
Coupling
- If a CPU scanhash implementation changes its prototype or nonce/writeback semantics, also update driver-cpu.c and driver-cpu.h: driver-cpu.c stores all specialized implementations behind the
sha256_funcsignature andcpu_scanhashexpects a found nonce to be recoverable fromwork->data[76]before callingsubmit_nonce. - If
enum sha256_algosorWANT_*availability changes in driver-cpu.h, update the correspondingalgo_namesandsha256_funcsentries in driver-cpu.c and the implementation guards in the owned sha256_*.c files; otherwise an advertised--algocan map to a null function or a compiled function can become unreachable. - If the vector assembly ABI changes, update both the C wrapper and the YASM source together: sha256_sse2_i386.c depends on
CalcSha256_x86fromx86_32/sha256_xmm.asm, sha256_sse2_amd64.c depends onsha256_sse2_64_newfromx86_64/sha256_xmm_amd64.asm, and sha256_sse4_amd64.c depends onCalcSha256_x64_sse4fromx86_64/sha256_sse4_amd64.asm. - If
struct opencl_work_data, OpenCL kernel result layout, orenum cl_kernelschanges in the OpenCL driver/kernel interface, update findnonce.c and findnonce.h:precalc_hashwrites named precomputed fields for SHA256d kernels, whilepostcalc_hash_asyncchoosesFOUNDversusSCRYPT_FOUNDand applies theKL_FULLHEADERnonce byte swap. - If a mining algorithm registration in malgo/*.c changes
algo,hash_data_f, OpenCL intensity conversion, or default kernel name selection, verify both CPU generic scanning in driver-cpu.c and OpenCL kernel lookup in driver-opencl.c; both dispatch through thestruct mining_algorithmpopulated by theLL_APPENDconstructors.
Risks
- Endianness is intentionally different across paths. sha256_generic.c, sha256_cryptopp.c, sha256_via.c, malgo/scrypt.c, and malgo/keccak.c each swap header, nonce, or digest words at different boundaries. If one path is changed to match another without tracing its input convention, valid shares can be rejected or invalid nonces submitted.
- The OpenCL result count is read from a shared nonce buffer in findnonce.c using
FOUND/SCRYPT_FOUNDconstants from findnonce.h. If a kernel writes more entries thanMAXBUFFERSorSCRYPT_MAXBUFFERS,postcalc_hashonly masks an invalid count after logging a hardware error; a mismatched kernel layout can silently drop nonces or submit wrong slots. - The optimized SIMD paths in sha256_4way.c and sha256_altivec_4way.c intentionally skip the last three SHA256 rounds after using
hash[7] == 0as the early test. If the acceptance predicate changes from that high-word zero check, these paths must be revisited or they can miss candidates that the full hash would accept.