8000 :building_construction: Refactor Windows compatibility by francissantiago · Pull Request #423 · albertobsd/keyhunt · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@francissantiago
Copy link
  • Updated conditional compilation for Windows to exclude MinGW and Cygwin.
  • Introduced a compatibility layer for sockets and threading for MinGW.
  • Added os_getrandom implementation for portable random byte generation.
  • Refactored mutex and thread handling to use pthreads in MinGW.
  • Improved code organization and readability across multiple files.
  • Ensured consistent handling of random number generation across platforms.

- Updated conditional compilation for Windows to exclude MinGW and Cygwin.
- Introduced a compatibility layer for sockets and threading for MinGW.
- Added os_getrandom implementation for portable random byte generation.
- Refactored mutex and thread handling to use pthreads in MinGW.
- Improved code organization and readability across multiple files.
- Ensured consistent handling of random number generation across platforms.
Copilot AI review requested due to automatic review settings December 10, 2025 11:03
Copy link
Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a comprehensive compatibility layer for MinGW and Windows builds, refactoring platform-specific code to support cross-platform compilation. The main changes include introducing pthread-compatible wrappers for Windows threading APIs, abstracting socket operations, and providing portable random number generation.

Key changes:

  • New compatibility layer files (win_thread_compat.{h,c}, win_compat.{h,c}, os_random.{h,c}) that provide abstraction for threading, sockets, and random number generation
  • Updated conditional compilation macros throughout the codebase to explicitly exclude MinGW/Cygwin from native Windows code paths
  • Refactored random number generation to use a unified os_getrandom() API across all platforms

Reviewed changes

Copilot reviewed 21 out of 22 changed files in this pull request and generated 16 comments.

Show a summary per file
File Description
win_thread_compat.h Header defining pthread compatibility layer for MinGW, mapping WinAPI thread/mutex functions to pthreads
win_thread_compat.c Implementation of pthread compatibility wrappers for CreateThread, CreateMutex, WaitForSingleObject, etc.
win_compat.h Header for socket compatibility layer with cross-platform socket initialization
win_compat.c Implementation of socket wrappers (WSAStartup/closesocket on Windows, standard POSIX otherwise)
os_random.h Header for portable random byte generation API
os_random.c Implementation using BCryptGenRandom on Windows and /dev/urandom on POSIX systems
secp256k1/SECP256K1.cpp Updated __declspec(align) conditionals to exclude MinGW
secp256k1/Random.cpp Refactored to use os_getrandom() API and updated conditionals
secp256k1/Int.cpp Updated format specifiers and conditionals for MinGW compatibility
rmd160/rmd160.h Updated conditionals to exclude MinGW from Windows-specific paths
oldbloom/oldbloom.h Added pthread.h include for non-Windows platforms and updated conditionals
oldbloom/bloom.cpp Updated mutex usage conditionals and fixed indentation
keyhunt_legacy.cpp Mass update of conditional compilation macros and includes for compatibility layer
keyhunt.cpp Extensive refactoring of conditionals, random generation, and format specifiers for cross-platform support
hash/sha256_sse.cpp Updated SSE intrinsics access to handle MinGW's GCC-based compilation
hash/ripemd160_sse.cpp Updated SSE intrinsics and conditionals for MinGW compatibility
gmp256k1/Random.cpp Simplified random generation to use os_getrandom() wrapper
bsgsd.cpp Updated socket operations and random generation for compatibility layer
bloom/bloom.h Updated Windows header includes to exclude MinGW
base58/base58.c Updated network header selection to use Winsock on all Windows platforms
Makefile Added compilation rules for compatibility layer and new MinGW target
.gitignore Added .specstory/ directory to ignore list

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1786 to 1791
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
tid[j] = CreateThread(NULL, 0, thread_bPload, (void*) &bPload_temp_ptr[j], 0, &s);
#else
s = pthread_create(&tid[j],NULL,thread_bPload,(void*) &bPload_temp_ptr[j]);
pthread_detach(tid[j]);
#endif
Copy link
Copilot AI Dec 10, 2025
8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: preprocessor directives have mixed leading whitespace.

Copilot uses AI. Check for mistakes.
uint32_t threadid;
uint64_t from;
uint64_t to;
int osr = os_getrandom(&rseedvalue, sizeof(unsigned long));
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line appears to be a misplaced code fragment. It looks like a variable initialization int osr = os_getrandom(&rseedvalue, sizeof(unsigned long)); that should not be inside the struct definition. This will cause a compilation error as struct members cannot have code statements like function calls at the point of declaration.

Suggested change
int osr = os_getrandom(&rseedvalue, sizeof(unsigned long));
int osr;

Copilot uses AI. Check for mistakes.
fflush(stdout);
THREADOUTPUT = 0;
#ifdef _WIN64
#if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__)
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent conditional compilation: this line uses #if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__) while line 2241 uses #if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__), but line 2228 should include && !defined(__CYGWIN__) for consistency with the pattern used throughout the rest of the file (see lines 2176, 2241, etc.).

Suggested change
#if defined(_WIN64) && !defined(__MINGW32__) && !defined(__MINGW64__)
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)

Copilot uses AI. Check for mistakes.
Comment on lines +1265 to +1268
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
bloom_bPx3rd_mutex = (HANDLE*) calloc(256,sizeof(HANDLE));
#else
bloom_bPx3rd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
bloom_bPx3rd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: similar to lines 1238-1241, these lines use leading tabs that don't match the surrounding code style.

Copilot uses AI. Check for mistakes.
bloom_bPx3rd_mutex[i] = CreateMutex(NULL, FALSE, NULL);
#else
pthread_mutex_init(&bloom_bPx3rd_mutex[i],NULL);
#endif
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: preprocessor directives have mixed indentation that doesn't align with the surrounding code.

Suggested change
#endif
#endif

Copilot uses AI. Check for mistakes.
Comment on lines +1238 to +1241
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
bloom_bPx2nd_mutex = (HANDLE*) calloc(256,sizeof(HANDLE));
#else
bloom_bPx2nd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
bloom_bPx2nd_mutex = (pthread_mutex_t*) calloc(256,sizeof(pthread_mutex_t));
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: these lines use leading tabs instead of the consistent indentation pattern used in surrounding code. Lines 1239 and 1241 should align with the surrounding context.

Copilot uses AI. Check for mistakes.
#else
pthread_mutex_init(&bPload_mutex[j],NULL);
#endif
#endif
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: these lines have unusual leading whitespace that doesn't match the surrounding code pattern.

Suggested change
#endif
#endif

Copilot uses AI. Check for mistakes.
fprintf(stderr,"[E] Error os_getrandom() ?\n");
exit(EXIT_FAILURE);
rseed(clock() + time(NULL) + rand()*rand());
rseed(clock() + time(NULL));
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreachable code: line 469 calls exit(EXIT_FAILURE) but line 470 has rseed(clock() + time(NULL)) after it. This line will never execute because the program exits on line 469. This appears to be leftover code that should have been removed.

Suggested change
rseed(clock() + time(NULL));

Copilot uses AI. Check for mistakes.
free(h);
return 0;
} else if (h->type == 1) {
if(!h->detached) pthread_detach(h->u.thread);
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential double-detach issue: if CloseHandle is called on a thread that has already been joined via WaitForSingleObject, calling pthread_detach on an already-joined thread is undefined behavior. The code should track whether the thread has been joined and only detach if it hasn't been.

Copilot uses AI. Check for mistakes.
Comment on lines +1250 to +1254
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
bloom_bPx2nd_mutex[i] = CreateMutex(NULL, FALSE, NULL);
#else
pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL);
#endif
Copy link
Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: the preprocessor directives and their content have mixed indentation (lines 1250-1254). The indentation should be consistent with surrounding code blocks.

Suggested change
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
bloom_bPx2nd_mutex[i] = CreateMutex(NULL, FALSE, NULL);
#else
pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL);
#endif
#if defined(_WIN64) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
bloom_bPx2nd_mutex[i] = CreateMutex(NULL, FALSE, NULL);
#else
pthread_mutex_init(&bloom_bPx2nd_mutex[i],NULL);
#endif

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0