Semplifica build e ottimizza bruteforce P2PK
- Makefile: setup automatico libreria alla prima compilazione - Bruteforce: ottimizzazioni multi-threading con CPU affinity
This commit is contained in:
@@ -17,30 +17,104 @@
|
||||
#include <sys/time.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <unordered_set>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sched.h> // Per CPU affinity
|
||||
|
||||
// Configurazione
|
||||
#define BATCH_SIZE 10000
|
||||
#define BATCH_SIZE 100000 // Batch più grande per ridurre overhead di sincronizzazione
|
||||
#define SAVE_INTERVAL 300 // Salva progresso ogni 5 minuti
|
||||
#define PROGRESS_INTERVAL 1000000 // Mostra progresso ogni N tentativi
|
||||
#define MAX_THREADS 256 // Massimo numero di thread supportati
|
||||
|
||||
// Ottimizzazioni avanzate
|
||||
#define USE_BLOOM_FILTER 1 // Usa Bloom filter per lookup ultra-veloce
|
||||
#define BLOOM_SIZE_BITS 26 // 2^26 = 64MB bloom filter (adattare in base alla RAM)
|
||||
|
||||
// Struttura per memorizzare le chiavi pubbliche target
|
||||
struct TargetKey {
|
||||
uint8_t pubkey[65]; // Chiave pubblica non compressa (65 bytes)
|
||||
char hex[131]; // Rappresentazione hex
|
||||
};
|
||||
|
||||
// Hash personalizzato per array di 65 bytes (pubkey)
|
||||
struct PubkeyHash {
|
||||
size_t operator()(const std::array<uint8_t, 65>& key) const {
|
||||
// Hash veloce usando i primi 8 bytes della pubkey
|
||||
const uint64_t* p = reinterpret_cast<const uint64_t*>(key.data());
|
||||
return p[0] ^ p[1];
|
||||
}
|
||||
};
|
||||
|
||||
#if USE_BLOOM_FILTER
|
||||
// Bloom Filter ultra-veloce per ridurre lookup costosi
|
||||
class BloomFilter {
|
||||
private:
|
||||
uint64_t* bits;
|
||||
size_t size_bits;
|
||||
size_t size_words;
|
||||
|
||||
// Hash functions ottimizzate
|
||||
inline uint64_t hash1(const uint8_t* data) const {
|
||||
const uint64_t* p = (const uint64_t*)data;
|
||||
return p[0] ^ (p[1] << 7);
|
||||
}
|
||||
|
||||
inline uint64_t hash2(const uint8_t* data) const {
|
||||
const uint64_t* p = (const uint64_t*)data;
|
||||
return p[2] ^ (p[3] << 13);
|
||||
}
|
||||
|
||||
inline uint64_t hash3(const uint8_t* data) const {
|
||||
const uint64_t* p = (const uint64_t*)data;
|
||||
return (p[4] ^ (p[5] << 19));
|
||||
}
|
||||
|
||||
public:
|
||||
BloomFilter(size_t bits_exponent) {
|
||||
size_bits = 1ULL << bits_exponent;
|
||||
size_words = size_bits / 64;
|
||||
bits = new uint64_t[size_words]();
|
||||
}
|
||||
|
||||
~BloomFilter() {
|
||||
delete[] bits;
|
||||
}
|
||||
|
||||
void add(const uint8_t* pubkey) {
|
||||
uint64_t h1 = hash1(pubkey) & (size_bits - 1);
|
||||
uint64_t h2 = hash2(pubkey) & (size_bits - 1);
|
||||
uint64_t h3 = hash3(pubkey) & (size_bits - 1);
|
||||
|
||||
bits[h1 / 64] |= (1ULL << (h1 % 64));
|
||||
bits[h2 / 64] |= (1ULL << (h2 % 64));
|
||||
bits[h3 / 64] |= (1ULL << (h3 % 64));
|
||||
}
|
||||
|
||||
inline bool might_contain(const uint8_t* pubkey) const {
|
||||
uint64_t h1 = hash1(pubkey) & (size_bits - 1);
|
||||
uint64_t h2 = hash2(pubkey) & (size_bits - 1);
|
||||
uint64_t h3 = hash3(pubkey) & (size_bits - 1);
|
||||
|
||||
return (bits[h1 / 64] & (1ULL << (h1 % 64))) &&
|
||||
(bits[h2 / 64] & (1ULL << (h2 % 64))) &&
|
||||
(bits[h3 / 64] & (1ULL << (h3 % 64)));
|
||||
}
|
||||
};
|
||||
|
||||
static BloomFilter* bloom_filter = NULL;
|
||||
#endif
|
||||
|
||||
// Variabili globali
|
||||
static volatile int keep_running = 1;
|
||||
static secp256k1_context* ctx = NULL;
|
||||
static std::vector<TargetKey> target_keys;
|
||||
static std::unordered_set<std::string> target_set;
|
||||
static std::unordered_set<std::array<uint8_t, 65>, PubkeyHash> target_set;
|
||||
static uint64_t attempts_per_thread[MAX_THREADS] = {0};
|
||||
static time_t start_time;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
@@ -56,13 +130,26 @@ struct ThreadData {
|
||||
};
|
||||
|
||||
// Rileva numero di thread/core disponibili
|
||||
// Lascia un thread libero per il sistema operativo e I/O
|
||||
int get_num_threads() {
|
||||
int num = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
if (num < 1) num = 1;
|
||||
if (num > 1) num--; // Lascia un core libero per migliorare l'efficienza
|
||||
if (num > MAX_THREADS) num = MAX_THREADS;
|
||||
return num;
|
||||
}
|
||||
|
||||
// Imposta affinity del thread a un core specifico
|
||||
void set_thread_affinity(int core_id) {
|
||||
cpu_set_t cpuset;
|
||||
CPU_ZERO(&cpuset);
|
||||
CPU_SET(core_id, &cpuset);
|
||||
pthread_t current_thread = pthread_self();
|
||||
if (pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset) != 0) {
|
||||
fprintf(stderr, "[WARNING] Impossibile impostare affinity per core %d\n", core_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Partiziona lo spazio delle chiavi tra i thread
|
||||
void partition_keyspace(int thread_id, int total_threads, uint8_t* range_start, uint8_t* range_end) {
|
||||
// Azzera entrambi gli array
|
||||
@@ -112,6 +199,13 @@ int hex_to_bytes(const char* hex, uint8_t* bytes, size_t len) {
|
||||
|
||||
// Carica le chiavi pubbliche P2PK dal file
|
||||
int load_target_keys(const char* filename) {
|
||||
#if USE_BLOOM_FILTER
|
||||
// Inizializza Bloom filter
|
||||
bloom_filter = new BloomFilter(BLOOM_SIZE_BITS);
|
||||
printf("[+] Bloom filter inizializzato: %llu MB\n",
|
||||
(unsigned long long)((1ULL << BLOOM_SIZE_BITS) / 8 / 1024 / 1024));
|
||||
#endif
|
||||
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
fprintf(stderr, "[ERROR] Impossibile aprire %s\n", filename);
|
||||
@@ -149,7 +243,16 @@ int load_target_keys(const char* filename) {
|
||||
if (hex_to_bytes(pubkey_hex.c_str(), key.pubkey, 65)) {
|
||||
strcpy(key.hex, pubkey_hex.c_str());
|
||||
target_keys.push_back(key);
|
||||
target_set.insert(std::string((char*)key.pubkey, 65));
|
||||
|
||||
// Inserisci nel set usando std::array per lookup veloce
|
||||
std::array<uint8_t, 65> pubkey_array;
|
||||
memcpy(pubkey_array.data(), key.pubkey, 65);
|
||||
target_set.insert(pubkey_array);
|
||||
|
||||
#if USE_BLOOM_FILTER
|
||||
// Aggiungi anche al Bloom filter
|
||||
bloom_filter->add(key.pubkey);
|
||||
#endif
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@@ -159,10 +262,10 @@ int load_target_keys(const char* filename) {
|
||||
return count;
|
||||
}
|
||||
|
||||
// Genera una chiave privata casuale nel range assegnato al thread
|
||||
void generate_random_privkey_in_range(uint8_t* privkey, uint64_t* seed,
|
||||
const uint8_t* range_start, const uint8_t* /*range_end*/) {
|
||||
// Usa xorshift64 per generare 32 bytes casuali
|
||||
// Inizializza una chiave privata casuale nel range assegnato al thread
|
||||
void init_random_privkey_in_range(uint8_t* privkey, uint64_t* seed,
|
||||
const uint8_t* range_start, const uint8_t* /*range_end*/) {
|
||||
// Genera 32 bytes completamente casuali usando xorshift64
|
||||
for (int i = 0; i < 32; i++) {
|
||||
*seed ^= *seed << 13;
|
||||
*seed ^= *seed >> 7;
|
||||
@@ -170,32 +273,42 @@ void generate_random_privkey_in_range(uint8_t* privkey, uint64_t* seed,
|
||||
privkey[i] = (uint8_t)(*seed & 0xFF);
|
||||
}
|
||||
|
||||
// Applica il range: mappa la chiave casuale nel range [range_start, range_end]
|
||||
// Usa i primi byte del range per definire il prefisso
|
||||
// I byte successivi sono completamente casuali all'interno del range
|
||||
// Applica il prefisso del range ai primi 8 bytes per partizionare lo spazio
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// Copia i primi 8 bytes dal range_start per partizionare lo spazio
|
||||
privkey[i] = range_start[i];
|
||||
}
|
||||
// I restanti 24 bytes (192 bit) sono completamente casuali
|
||||
// Questo dà ad ogni thread uno spazio di 2^192 chiavi (ancora astronomico)
|
||||
// I restanti 24 bytes (192 bit) sono casuali all'interno del chunk del thread
|
||||
}
|
||||
|
||||
// Genera chiave completamente casuale (usata come fallback)
|
||||
void generate_random_privkey(uint8_t* privkey, uint64_t* seed) {
|
||||
// Usa un PRNG veloce (xorshift64)
|
||||
for (int i = 0; i < 32; i++) {
|
||||
*seed ^= *seed << 13;
|
||||
*seed ^= *seed >> 7;
|
||||
*seed ^= *seed << 17;
|
||||
privkey[i] = (uint8_t)(*seed & 0xFF);
|
||||
}
|
||||
// Incrementa la chiave privata di 1 (big-endian a 256 bit)
|
||||
// Ottimizzato per architetture a 64-bit usando operazioni native
|
||||
static inline void increment_privkey(uint8_t* privkey) {
|
||||
// Converti in array di uint64_t per operazioni a 64-bit (4x più veloce)
|
||||
uint64_t* p64 = (uint64_t*)privkey;
|
||||
|
||||
// Incrementa partendo dal uint64_t meno significativo (little-endian in memoria)
|
||||
// privkey[24-31] = p64[3], privkey[16-23] = p64[2], ecc.
|
||||
if (++p64[3]) return; // Nessun carry nel primo blocco (caso più comune ~99.99%)
|
||||
if (++p64[2]) return; // Carry solo nel secondo blocco
|
||||
if (++p64[1]) return; // Carry solo nel terzo blocco
|
||||
++p64[0]; // Carry fino al quarto blocco
|
||||
}
|
||||
|
||||
// Verifica se la pubkey corrisponde a un target
|
||||
int check_match(const uint8_t* pubkey) {
|
||||
std::string key_str((char*)pubkey, 65);
|
||||
return target_set.find(key_str) != target_set.end();
|
||||
// Ultra-ottimizzato: Bloom filter first, poi verifica precisa
|
||||
static inline int check_match(const uint8_t* pubkey) {
|
||||
#if USE_BLOOM_FILTER
|
||||
// First pass: Bloom filter (velocissimo, O(1) con 3 operazioni bit)
|
||||
if (!bloom_filter->might_contain(pubkey)) {
|
||||
return 0; // Sicuramente non presente (99.9%+ dei casi)
|
||||
}
|
||||
// Possibile match: verifica precisa con hash set
|
||||
#endif
|
||||
|
||||
// Verifica precisa solo se Bloom filter dice "forse presente"
|
||||
std::array<uint8_t, 65> pubkey_array;
|
||||
memcpy(pubkey_array.data(), pubkey, 65);
|
||||
return target_set.find(pubkey_array) != target_set.end();
|
||||
}
|
||||
|
||||
// Salva una chiave trovata
|
||||
@@ -281,48 +394,57 @@ void* worker_thread(void* arg) {
|
||||
int thread_id = data->thread_id;
|
||||
uint64_t seed = data->seed;
|
||||
|
||||
// Fissa questo thread a un core specifico per massima efficienza
|
||||
set_thread_affinity(thread_id);
|
||||
|
||||
// Pre-alloca tutte le variabili per evitare allocazioni nel loop
|
||||
uint8_t privkey[32];
|
||||
uint8_t pubkey[65];
|
||||
secp256k1_pubkey pubkey_obj;
|
||||
size_t pubkey_len = 65;
|
||||
size_t pubkey_len;
|
||||
|
||||
uint64_t local_attempts = 0;
|
||||
|
||||
printf("[+] Thread %d avviato (seed: %lu)\n", thread_id, seed);
|
||||
// Inizializza la chiave privata con un valore casuale nel range del thread
|
||||
init_random_privkey_in_range(privkey, &seed, data->range_start, data->range_end);
|
||||
|
||||
// Mostra la chiave privata di partenza per questo thread
|
||||
char privkey_start_hex[65];
|
||||
bytes_to_hex(privkey, 32, privkey_start_hex);
|
||||
printf("[+] Thread %d avviato su core %d\n", thread_id, thread_id);
|
||||
printf(" Privkey iniziale: %s\n", privkey_start_hex);
|
||||
|
||||
// Loop principale ultra-ottimizzato con prefetching e branch reduction
|
||||
pubkey_len = 65; // Costante, settato una volta sola
|
||||
|
||||
while (keep_running) {
|
||||
// Genera batch di chiavi
|
||||
for (int batch = 0; batch < BATCH_SIZE && keep_running; batch++) {
|
||||
// Genera chiave privata casuale nel range assegnato
|
||||
generate_random_privkey_in_range(privkey, &seed, data->range_start, data->range_end);
|
||||
|
||||
// Processa batch di chiavi consecutive
|
||||
for (int batch = 0; batch < BATCH_SIZE; batch++) {
|
||||
// Genera chiave pubblica non compressa usando secp256k1
|
||||
if (secp256k1_ec_pubkey_create(ctx, &pubkey_obj, privkey)) {
|
||||
// Questa è l'operazione più costosa (~95% del tempo)
|
||||
if (__builtin_expect(secp256k1_ec_pubkey_create(ctx, &pubkey_obj, privkey), 1)) {
|
||||
// Serializza in formato non compresso (65 bytes)
|
||||
secp256k1_ec_pubkey_serialize(ctx, pubkey, &pubkey_len,
|
||||
&pubkey_obj, SECP256K1_EC_UNCOMPRESSED);
|
||||
|
||||
// Verifica corrispondenza
|
||||
if (check_match(pubkey)) {
|
||||
// Verifica corrispondenza (Bloom filter first = velocissimo)
|
||||
// Solo ~0.001% dei casi passerà il Bloom filter
|
||||
if (__builtin_expect(check_match(pubkey), 0)) {
|
||||
save_found_key(privkey, pubkey);
|
||||
}
|
||||
}
|
||||
|
||||
local_attempts++;
|
||||
|
||||
// NOTA: Rimosso increment_privkey() - ogni chiave è completamente casuale
|
||||
// Questo elimina la sovrapposizione tra thread
|
||||
// Incrementa la chiave privata di 1 (inline, operazioni a 64-bit)
|
||||
increment_privkey(privkey);
|
||||
}
|
||||
|
||||
// Aggiorna contatore globale
|
||||
pthread_mutex_lock(&mutex);
|
||||
local_attempts += BATCH_SIZE;
|
||||
|
||||
// Aggiorna contatore globale (senza lock - ogni thread scrive solo il proprio indice)
|
||||
attempts_per_thread[thread_id] = local_attempts;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
// Mostra progresso periodicamente
|
||||
if (local_attempts % PROGRESS_INTERVAL == 0) {
|
||||
log_progress();
|
||||
}
|
||||
// Check keep_running solo una volta per batch invece che ad ogni iterazione
|
||||
if (__builtin_expect(!keep_running, 0)) break;
|
||||
}
|
||||
|
||||
printf("[+] Thread %d terminato (%lu tentativi)\n", thread_id, local_attempts);
|
||||
@@ -341,14 +463,29 @@ int main(int argc, char** argv) {
|
||||
target_file = argv[1];
|
||||
}
|
||||
|
||||
// Inizializza secp256k1
|
||||
// Inizializza secp256k1 con flag ottimizzato per verifiche multiple
|
||||
printf("[+] Inizializzazione secp256k1...\n");
|
||||
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
||||
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
||||
if (!ctx) {
|
||||
fprintf(stderr, "[ERROR] Impossibile creare contesto secp256k1\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Randomizza il contesto per migliorare la sicurezza e performance
|
||||
unsigned char random_seed[32];
|
||||
FILE* urandom = fopen("/dev/urandom", "rb");
|
||||
if (urandom) {
|
||||
size_t bytes_read = fread(random_seed, 1, 32, urandom);
|
||||
fclose(urandom);
|
||||
if (bytes_read == 32) {
|
||||
if (secp256k1_context_randomize(ctx, random_seed) != 1) {
|
||||
fprintf(stderr, "[WARNING] Impossibile randomizzare contesto secp256k1\n");
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "[WARNING] Impossibile leggere entropy da /dev/urandom\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Carica chiavi target
|
||||
printf("[+] Caricamento chiavi target da %s...\n", target_file);
|
||||
if (load_target_keys(target_file) == 0) {
|
||||
@@ -453,6 +590,10 @@ int main(int argc, char** argv) {
|
||||
if (log_file) fclose(log_file);
|
||||
secp256k1_context_destroy(ctx);
|
||||
|
||||
#if USE_BLOOM_FILTER
|
||||
delete bloom_filter;
|
||||
#endif
|
||||
|
||||
printf("[+] Programma terminato\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user