Skip to content

Commit

Permalink
New random generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jonano614 committed Dec 18, 2018
1 parent 93fe96a commit 25a248c
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 27 deletions.
2 changes: 2 additions & 0 deletions automake/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sources = \
$(utils)/linenoise.c \
$(utils)/dirname.c \
$(utils)/string_utils.c \
$(utils)/random.c \
$(moving_statistics)/moving_average.c \
$(jsonrpc)/cJSON.c \
$(jsonrpc)/cJSON_Utils.c \
Expand Down Expand Up @@ -99,6 +100,7 @@ headers = \
$(utils)/utils.h \
$(utils)/linenoise.h \
$(utils)/string_utils.h \
$(utils)/random.h \
$(moving_statistics)/moving_average.h \
$(jsonrpc)/cJSON.h \
$(jsonrpc)/cJSON_Utils.h \
Expand Down
2 changes: 2 additions & 0 deletions client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sources = \
$(utils)/linenoise.c \
$(utils)/dirname.c \
$(utils)/string_utils.c \
$(utils)/random.c \
$(moving_statistics)/moving_average.c \
$(json-rpc)/cJSON.c \
$(json-rpc)/cJSON_Utils.c \
Expand Down Expand Up @@ -115,6 +116,7 @@ headers = \
$(utils)/linenoise.h \
$(utils)/dirname.h \
$(utils)/string_utils.h \
$(utils)/random.h \
$(moving_statistics)/moving_average.h \
$(json-rpc)/cJSON.h \
$(json-rpc)/cJSON_Utils.h \
Expand Down
3 changes: 2 additions & 1 deletion client/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "time.h"
#include "math.h"
#include "utils/atomic.h"
#include "utils/random.h"

#define MAX_WAITING_MAIN 1
#define MAIN_START_AMOUNT (1ll << 42)
Expand Down Expand Up @@ -1048,7 +1049,7 @@ int do_mining(struct xdag_block *block, struct block_internal **pretop, xtime_t
uint64_t taskIndex = g_xdag_pool_task_index + 1;
struct xdag_pool_task *task = &g_xdag_pool_task[taskIndex & 1];

xdag_generate_random_array(block[0].field[XDAG_BLOCK_FIELDS - 1].data, sizeof(xdag_hash_t));
GetRandBytes(block[0].field[XDAG_BLOCK_FIELDS - 1].data, sizeof(xdag_hash_t));

task->task_time = MAIN_TIME(send_time);

Expand Down
6 changes: 4 additions & 2 deletions client/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,14 +919,16 @@ int out_balances()
char address[33] = {0};
struct out_balances_data d;
unsigned i = 0;

xdag_set_log_level(0);
xdag_mem_init((xdag_get_frame() - xdag_get_start_frame()) << 17);
xdag_crypt_init(0);
xdag_crypt_init();
memset(&d, 0, sizeof(struct out_balances_data));
xdag_load_blocks(xdag_get_start_frame() << 16, xdag_get_frame() << 16, &i, &add_block_callback);
xdag_traverse_all_blocks(&d, out_balances_callback);

qsort(d.blocks, d.blocksCount, sizeof(struct xdag_field), out_sort_callback);
for(i = 0; i < d.blocksCount; ++i) {
for(int i = 0; i < d.blocksCount; ++i) {
xdag_hash2address(d.blocks[i].data, address);
printf("%s %20.9Lf\n", address, amount2xdags(d.blocks[i].amount));
}
Expand Down
10 changes: 2 additions & 8 deletions client/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ extern unsigned int xOPENSSL_ia32cap_P[4];
extern int xOPENSSL_ia32_cpuid(unsigned int *);

// initialization of the encryption system
int xdag_crypt_init(int withrandom)
int xdag_crypt_init()
{
if(withrandom) {
uint64_t buf[64];
xOPENSSL_ia32_cpuid(xOPENSSL_ia32cap_P);
xdag_generate_random_array(buf, sizeof(buf));
xdag_debug("Seed : [%s]", xdag_log_array(buf, sizeof(buf)));
RAND_seed(buf, sizeof(buf));
}
xOPENSSL_ia32_cpuid(xOPENSSL_ia32cap_P);

#if USE_OPTIMIZED_EC == 1 || USE_OPTIMIZED_EC == 2
ctx_noopenssl = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
Expand Down
2 changes: 1 addition & 1 deletion client/crypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
#endif

// initialization of the encryption system
extern int xdag_crypt_init(int withrandom);
extern int xdag_crypt_init();

// creates a new pair of private and public keys
extern void *xdag_create_key(xdag_hash_t privkey, xdag_hash_t pubkey, uint8_t *pubkey_bit);
Expand Down
9 changes: 7 additions & 2 deletions client/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "xdag_config.h"
#include "json-rpc/rpc_service.h"
#include "../dnet/dnet_crypt.h"
#include "utils/random.h"

#define ARG_EQUAL(a,b,c) strcmp(c, "") == 0 ? strcmp(a, b) == 0 : (strcmp(a, b) == 0 || strcmp(a, c) == 0)

Expand Down Expand Up @@ -273,11 +274,15 @@ int pre_init(void)
memset(&g_xdag_stats, 0, sizeof(g_xdag_stats));
memset(&g_xdag_extstats, 0, sizeof(g_xdag_extstats));

RandAddSeed();

xdag_mess("Initializing cryptography...");
if(xdag_crypt_init()) return -1;

//TODO: future xdag.wallet
xdag_mess("Reading wallet...");
if(dnet_key_init() < 0) return -1;
xdag_mess("Initializing cryptography...");
if(xdag_crypt_init(1)) return -1;

if(xdag_wallet_init()) return -1;

return 0;
Expand Down
3 changes: 2 additions & 1 deletion client/miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "algorithms/crc.h"
#include "utils/log.h"
#include "utils/utils.h"
#include "utils/random.h"

#define MINERS_PWD "minersgonnamine"
#define SECTOR0_BASE 0x1947f3acu
Expand Down Expand Up @@ -262,7 +263,7 @@ void *miner_net_thread(void *arg)
xdag_hash_update(task->ctx, data[1].data, sizeof(struct xdag_field));
xdag_hash_update(task->ctx, hash, sizeof(xdag_hashlow_t));

xdag_generate_random_array(task->nonce.data, sizeof(xdag_hash_t));
GetRandBytes(task->nonce.data, sizeof(xdag_hash_t));

memcpy(task->nonce.data, hash, sizeof(xdag_hashlow_t));
memcpy(task->lastfield.data, task->nonce.data, sizeof(xdag_hash_t));
Expand Down
12 changes: 3 additions & 9 deletions client/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "version.h"
#include "../dnet/dnet_main.h"
#include "utils/log.h"
#include "utils/random.h"

#define NEW_BLOCK_TTL 5
#define REQUEST_WAIT 64
Expand Down Expand Up @@ -303,13 +304,6 @@ int xdag_transport_start(int flags, int nthreads, const char *bindto, int npairs
return res;
}

/* generates an array with random data */
int xdag_generate_random_array(void *array, unsigned long size)
{
//TODO: do we need to generate random array based on DNET key?
return dnet_generate_random_array(array, size);
}

static int do_request(int type, xtime_t start_time, xtime_t end_time, void *data,
void *(*callback)(void *block, void *data))
{
Expand All @@ -323,7 +317,7 @@ static int do_request(int type, xtime_t start_time, xtime_t end_time, void *data
b.field[0].time = start_time;
b.field[0].end_time = end_time;

xdag_generate_random_array(&id, sizeof(uint64_t));
GetRandBytes(&id, sizeof(uint64_t));

memset(&b.field[1], 0, sizeof(struct xdag_field));
*(uint64_t*)b.field[1].hash = id;
Expand Down Expand Up @@ -457,7 +451,7 @@ static void *xdag_update_rip_thread(void *arg)
while(1) {
if (time(NULL) - last_change_time > REPLY_ID_PVT_TTL) {
time(&last_change_time);
xdag_generate_random_array(&reply_id_private, sizeof(uint64_t));
GetRandBytes(&reply_id_private, sizeof(uint64_t));
}
sleep(60);
}
Expand Down
3 changes: 0 additions & 3 deletions client/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ extern "C" {
*/
extern int xdag_transport_start(int flags, int nthreads, const char *bindto, int npairs, const char **addr_port_pairs);

/* generates an array with random data */
extern int xdag_generate_random_array(void *array, unsigned long size);

/* sends a new block to network */
extern int xdag_send_new_block(struct xdag_block *b);

Expand Down
72 changes: 72 additions & 0 deletions client/utils/random.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "random.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include <openssl/rand.h>
#include "log.h"

static void RandFailure()
{
xdag_fatal("Failed to read randomness, aborting\n");
abort();
}

static inline int64_t GetPerformanceCounter()
{
int64_t counter = 0;
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER*)&counter);
#else
timeval t;
gettimeofday(&t, NULL);
nCounter = (int64_t)(t.tv_sec * 1000000 + t.tv_usec);
#endif
return counter;
}

void RandAddSeed()
{
#ifdef _WIN32
// mix the contents of the screen into the generator.
RAND_screen();
#endif

// Seed with CPU performance counter
int64_t nCounter = GetPerformanceCounter();
RAND_add(&nCounter, sizeof(nCounter), 1.5);
}

void GetRandBytes(unsigned char* buf, int num)
{
if(RAND_bytes(buf, num) != 1) {
RandFailure();
}
}

uint64_t GetRand(uint64_t max)
{
if(max == 0) {
return 0;
}

// The range of the random source must be a multiple of the modulus
// to give every possible output value an equal possibility
uint64_t range = UINT64_MAX / max * max;
uint64_t randValue = 0;
do {
GetRandBytes((unsigned char*)&randValue, sizeof(randValue));
} while(randValue >= range);
return (randValue % max);
}

int GetRandInt(int max)
{
return GetRand(max);
}
21 changes: 21 additions & 0 deletions client/utils/random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef RANDOM_H
#define RANDOM_H

#include <stdint.h>

/* Seed OpenSSL PRNG with additional entropy data */
void RandAddSeed();

/**
* Functions to gather random data via the OpenSSL PRNG
*/
void GetRandBytes(unsigned char* buf, int num);
uint64_t GetRand(uint64_t nMax);
int GetRandInt(int nMax);

#endif // RANDOM_H
2 changes: 2 additions & 0 deletions win/xdaglib/xdaglib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<ClCompile Include="..\..\client\utils\atomic.c" />
<ClCompile Include="..\..\client\utils\log.c" />
<ClCompile Include="..\..\client\utils\moving_statistics\moving_average.c" />
<ClCompile Include="..\..\client\utils\random.c" />
<ClCompile Include="..\..\client\utils\string_utils.c" />
<ClCompile Include="..\..\client\utils\utils.c" />
<ClCompile Include="..\..\client\wallet.c" />
Expand Down Expand Up @@ -111,6 +112,7 @@
<ClInclude Include="..\..\client\utils\atomic.h" />
<ClInclude Include="..\..\client\utils\log.h" />
<ClInclude Include="..\..\client\utils\moving_statistics\moving_average.h" />
<ClInclude Include="..\..\client\utils\random.h" />
<ClInclude Include="..\..\client\utils\string_utils.h" />
<ClInclude Include="..\..\client\utils\utils.h" />
<ClInclude Include="..\..\client\version.h" />
Expand Down
6 changes: 6 additions & 0 deletions win/xdaglib/xdaglib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
<ClCompile Include="..\..\dfslib\dfsrsa.c">
<Filter>dfslib</Filter>
</ClCompile>
<ClCompile Include="..\..\client\utils\random.c">
<Filter>client\utils</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\client\address.h">
Expand Down Expand Up @@ -378,6 +381,9 @@
<ClInclude Include="..\..\dfslib\dfsrsa.h">
<Filter>dfslib</Filter>
</ClInclude>
<ClInclude Include="..\..\client\utils\random.h">
<Filter>client\utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\client\netdb-testnet.txt">
Expand Down

0 comments on commit 25a248c

Please sign in to comment.