Browse Source

Merge pull request #21 from SOUx4cx4fx56x69/master

Correctly threads?
pull/22/head
orignal 7 years ago committed by GitHub
parent
commit
3c9c58c4da
  1. 48
      vanity.hpp
  2. 73
      vanitygen.cpp

48
vanity.hpp

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
#pragma once
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <openssl/rand.h>
#include "Crypto.h"
#include "Identity.h"
#include "common/key.hpp"
#include <thread>
#include <unistd.h>
#include <vector>
#include <mutex>
// MARCO
#define INIT_CPUS(count_cpu){\
cpu = CPU_ALLOC(count_cpu-1);\
if (cpu == NULL) {\
std::cout << "CPU_ALLOC error" << std::endl;\
return 1;\
}\
}
static std::mutex thread_mutex;
static i2p::data::SigningKeyType type;
static i2p::data::PrivateKeys keys;
static bool finded=false;
static size_t padding_size;
static uint8_t * KeyBuf;
static uint8_t * PaddingBuf;
static unsigned long long hash;
static cpu_set_t * cpu;
//Functions visible and don't need.
/*
it does not need, but maybe in future we are can create.
namespace i2p{
namespace tools{
namespace vain{
}
}
}
*/

73
vanitygen.cpp

@ -1,24 +1,9 @@ @@ -1,24 +1,9 @@
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <openssl/rand.h>
#include "Crypto.h"
#include "Identity.h"
#include "common/key.hpp"
#include <thread>
#include <unistd.h>
#include <vector>
#include <mutex>
static std::mutex thread_mutex;
static i2p::data::SigningKeyType type;
static i2p::data::PrivateKeys keys;
static bool finded=false;
static size_t padding_size;
static uint8_t * KeyBuf;
static uint8_t * PaddingBuf;
static unsigned long long hash;
#include "vanity.hpp"
#define CPU_ONLY
static bool check_prefix(const char * buf){
unsigned short size_str=0;
while(*buf)
@ -35,10 +20,10 @@ while(*buf) @@ -35,10 +20,10 @@ while(*buf)
size_str++;
*buf++;
}
return true;
}
#ifdef CPU_ONLY
static inline bool NotThat(const char * a, const char *b){
while(*b)
@ -46,7 +31,7 @@ while(*b) @@ -46,7 +31,7 @@ while(*b)
return false;
}
inline void twist_cpu(uint8_t * buf,size_t * l0){
inline void twist_cpu(uint8_t * buf){
//TODO: NORMAL IMPLEMENTATION,\
As in miner...
@ -57,26 +42,35 @@ As in miner... @@ -57,26 +42,35 @@ As in miner...
// XXX: make this faster
static inline void mutate_keys_cpu(
uint8_t * buf,
uint8_t * padding,
size_t * l0)
uint8_t * padding)
{
twist_cpu(padding,l0);
twist_cpu(padding);
thread_mutex.lock();
keys.RecalculateIdentHash(buf);
thread_mutex.unlock();
}
void thread_find(const char * prefix){
static void thread_find(const char * prefix,int id_thread){
sched_setaffinity(0, sizeof(cpu), cpu);
cpu++;
std::cout << "Thread " << id_thread << " binded" << std::endl;
while(NotThat(keys.GetPublic()->GetIdentHash().ToBase32().c_str(),prefix) and !finded)
{
size_t l0 = 0;
//size_t l0 = 0; \
in future.
mutate_keys_cpu(KeyBuf,PaddingBuf, (size_t*)&l0);
mutate_keys_cpu(KeyBuf,PaddingBuf);
hash++;
}
}
#endif
int main (int argc, char * argv[])
{
if ( argc < 3 )
@ -141,25 +135,24 @@ TODO: @@ -141,25 +135,24 @@ TODO:
<orignal> без всех это pthread
* orignal has quit (Quit: Leaving)
*/
//
KeyBuf = new uint8_t[keys.GetFullLen()];
PaddingBuf = keys.GetPadding();
//
unsigned int count_cpu = sysconf(_SC_NPROCESSORS_ONLN);
std::vector<std::thread> threads(count_cpu);
INIT_CPUS(count_cpu);
//
std::cout << "Start vanity generator in " << count_cpu << " threads" << std::endl;
for ( unsigned int j = count_cpu;j--;){
threads[j] = std::thread(thread_find,argv[2]);
sched_param sch;
int policy;
pthread_getschedparam(threads[j].native_handle(), &policy, &sch);
sch.sched_priority = 10;
if (pthread_setschedparam(threads[j].native_handle(), SCHED_FIFO, &sch)) {
std::cout << "Failed to setschedparam" << std::endl;
return 1;
}
}
///
for ( unsigned int j = count_cpu;j--;)
threads[j] = std::thread(thread_find,argv[2],j);
// SET AFFINITY NOW IN FUNCTION
for(unsigned int j = 0; j < count_cpu;j++)
threads[j].join();
///
std::cout << "Hashes: " << hash << std::endl;
std::ofstream f (argv[1], std::ofstream::binary | std::ofstream::out);

Loading…
Cancel
Save