Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
nearly there
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul D Handy committed Dec 21, 2016
1 parent d7889ba commit f2c9f8c
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 576 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ libold/
*.cl.swp.h
*.cl.h
*.h.h
scripts/libccurl.so
2 changes: 1 addition & 1 deletion scripts/ccurl_pow
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
var ffi = require('ffi')

var libccurl = ffi.Library('/usr/lib/libccurl', {
var libccurl = ffi.Library('libccurl', {
ccurl_pow : [ 'string', [ 'string', 'int'] ]
})

Expand Down
131 changes: 80 additions & 51 deletions src/lib/PearlDiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
typedef struct {
States *states;
trit_t *trits;
int minWeightMagnitude;
int min_weight_magnitude;
int threadIndex;
PearlDiver *ctx;
} PDThread;
Expand All @@ -29,14 +29,14 @@ void interrupt(PearlDiver *ctx) {
pthread_mutex_unlock(&ctx->new_thread_search);
}

bool pd_search(PearlDiver *ctx, trit_t *const transactionTrits, int length, const int minWeightMagnitude, int numberOfThreads) {
bool pd_search(PearlDiver *ctx, trit_t *const transactionTrits, int length, const int min_weight_magnitude, int numberOfThreads) {

int k, thread_count;

if (length != TRANSACTION_LENGTH) {
return Invalid_transaction_trits_length;
}
if (minWeightMagnitude < 0 || minWeightMagnitude > HASH_LENGTH) {
if (min_weight_magnitude < 0 || min_weight_magnitude > HASH_LENGTH) {
return Invalid_min_weight_magnitude;
}

Expand Down Expand Up @@ -70,7 +70,7 @@ bool pd_search(PearlDiver *ctx, trit_t *const transactionTrits, int length, cons
PDThread pdthread = {
.states = states,
.trits = transactionTrits + TRANSACTION_LENGTH - HASH_LENGTH,
.minWeightMagnitude = minWeightMagnitude,
.min_weight_magnitude = min_weight_magnitude,
.threadIndex = numberOfThreads,
.ctx = ctx
};
Expand Down Expand Up @@ -100,17 +100,17 @@ void pd_search_init(States *states, trit_t *transactionTrits) {
for (j = 0; j < HASH_LENGTH; j++) {
switch (transactionTrits[offset++]) {
case 0: {
states->low[j] = HIGH_BITS;
states->high[j] = HIGH_BITS;
} break;
states->low[j] = HIGH_BITS;
states->high[j] = HIGH_BITS;
} break;
case 1: {
states->low[j] = LOW_BITS;
states->high[j] = HIGH_BITS;
} break;
states->low[j] = LOW_BITS;
states->high[j] = HIGH_BITS;
} break;
default: {
states->low[j] = HIGH_BITS;
states->high[j] = LOW_BITS;
}
states->low[j] = HIGH_BITS;
states->high[j] = LOW_BITS;
}
}
}

Expand All @@ -126,9 +126,32 @@ void pd_search_init(States *states, trit_t *transactionTrits) {
states->high[3] = 0b0000000000111111111111111111111111111111111111111111111111111111L;
}

int is_found(trit_t *low, trit_t *high, int index, int min_weight_magnitude) {
int i;
for (i = min_weight_magnitude; i-- > 0; ) {
if ((((trit_t)(low[HASH_LENGTH - 1 - i] )) & (1 << index))
!= (((trit_t)(high[HASH_LENGTH - 1 - i] )) & (1<< index))) {
return 0;
}
}
return 1;
}

int is_found_fast(trit_t *low, trit_t *high, int min_weight_magnitude) {
int i;
trit_t lastMeasurement = HIGH_BITS; /* (low[index] >> bitIndex & 1) != (high[index] >> bitIndex & 1) */
for (i = min_weight_magnitude; i-- > 0; ) {
lastMeasurement &= ~(low[HASH_LENGTH - 1 - i] ^ high[HASH_LENGTH - 1 - i]);
if(lastMeasurement == 0) {
return 0;
}
}
return lastMeasurement;
}

void *find_nonce(void *date){
trit_t midStateCopyLow[STATE_LENGTH],midStateCopyHigh[STATE_LENGTH];
int i,bitIndex;
int i,bit_index, nonce_probe;
PDThread *my_thread= (PDThread *)date;
trit_t *trits = my_thread->trits;

Expand All @@ -148,56 +171,37 @@ void *find_nonce(void *date){
memset(stateHigh, 0, STATE_LENGTH*sizeof(trit_t));
memset(scratchpadLow, 0, STATE_LENGTH*sizeof(trit_t));
memset(scratchpadHigh, 0, STATE_LENGTH*sizeof(trit_t));

bool skip;
int tries = 0;
while (!ctx->finished && !ctx->interrupted) {
tries++;
pd_increment(midStateCopyLow, midStateCopyHigh, (HASH_LENGTH / 3) * 2, HASH_LENGTH);
memcpy( stateLow, midStateCopyLow, STATE_LENGTH*sizeof(trit_t));
memcpy( stateHigh, midStateCopyHigh, STATE_LENGTH*sizeof(trit_t));
pd_transform(stateLow, stateHigh, scratchpadLow, scratchpadHigh);


for (bitIndex = 64; bitIndex-- > 0; ) {
skip = false;
if(ctx->finished) {return 0;}
for (i = my_thread->minWeightMagnitude; i-- > 0; ) {
if ((((trit_t)(stateLow[HASH_LENGTH - 1 - i] >> bitIndex)) & 1)
!= (((trit_t)(stateHigh[HASH_LENGTH - 1 - i] >> bitIndex)) & 1)) {
goto NEXT_BIT_INDEX;
/*
if ((((trit_t)(stateLow[HASH_LENGTH - 1 - i] )) & (1 << bitIndex))
!= (((trit_t)(stateHigh[HASH_LENGTH - 1 - i] )) & (1<< bitIndex))) {
*/
//skip = true;
//break;
}
}
if(skip) continue;

pthread_mutex_lock(&my_thread->ctx->new_thread_search);
if(ctx->finished) {
pthread_mutex_unlock(&my_thread->ctx->new_thread_search);
return 0;
}
ctx->finished = true;
for ( i = 0; i < HASH_LENGTH; i++) {
//my_thread->trits[TRANSACTION_LENGTH - HASH_LENGTH + i] =
trits[i] =
((((trit_t)(midStateCopyLow[i] >> bitIndex)) & 1) == 0) ?
1 : (((((trit_t)(midStateCopyHigh[i] >> bitIndex)) & 1) == 0) ? -1 : 0);
my_thread->ctx->nonceFound = true;
}
if((nonce_probe = is_found_fast(stateLow, stateHigh,
my_thread->min_weight_magnitude)) == 0)
continue;
pthread_mutex_lock(&my_thread->ctx->new_thread_search);
if(ctx->finished) {
pthread_mutex_unlock(&my_thread->ctx->new_thread_search);
sched_yield();
return 0;
NEXT_BIT_INDEX:
continue;
}
ctx->finished = true;
for ( i = 0; i < HASH_LENGTH; i++) {
trits[i] =
(((trit_t)(midStateCopyLow[i] ) & nonce_probe) == 0) ?
1 : ((((trit_t)(midStateCopyHigh[i] ) & nonce_probe) == 0) ? -1 : 0);
}
my_thread->ctx->nonceFound = true;
pthread_mutex_unlock(&my_thread->ctx->new_thread_search);
sched_yield();
return 0;

}
return 0;
}


void pd_transform( trit_t *const stateLow, trit_t *const stateHigh, trit_t *const scratchpadLow, trit_t *const scratchpadHigh) {

int scratchpadIndex = 0, round, stateIndex;
Expand Down Expand Up @@ -246,3 +250,28 @@ void pd_increment(trit_t *const midStateCopyLow, trit_t *const midStateCopyHigh,
}
}
}
/*
bool skip;
*/
/*
for (i = my_thread->min_weight_magnitude; i-- > 0; ) {
if ((((trit_t)(stateLow[HASH_LENGTH - 1 - i] >> bitIndex)) & 1)
!= (((trit_t)(stateHigh[HASH_LENGTH - 1 - i] >> bitIndex)) & 1)) {
goto NEXT_BIT_INDEX;
if ((((trit_t)(stateLow[HASH_LENGTH - 1 - i] )) & (1 << bitIndex))
!= (((trit_t)(stateHigh[HASH_LENGTH - 1 - i] )) & (1<< bitIndex))) {
skip = true;
break;
}
}
*/
/*
my_thread->trits[TRANSACTION_LENGTH - HASH_LENGTH + i] =
((((trit_t)(midStateCopyLow[i] >> bitIndex)) & 1) == 0) ?
1 : (((((trit_t)(midStateCopyHigh[i] >> bitIndex)) & 1) == 0) ? -1 : 0);
*/
/*
NEXT_BIT_INDEX:
continue;
*/

7 changes: 4 additions & 3 deletions src/lib/PearlDiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ typedef struct {

EXPORT void init_pearldiver(PearlDiver *ctx);
EXPORT void interrupt(PearlDiver *ctx);
EXPORT bool pd_search(PearlDiver *ctx, trit_t *const transactionTrits, int length, const int minWeightMagnitude, int numberOfThreads);
EXPORT bool pd_search(PearlDiver *ctx, trit_t *const transaction_trits, int length, const int min_weight_magnitude, int numberOfThreads);
EXPORT void pd_transform( trit_t *const stateLow, trit_t *const stateHigh, trit_t *const scratchpadLow, trit_t *const scratchpadHigh);
EXPORT void pd_increment(trit_t *const midStateCopyLow, trit_t *const midStateCopyHigh, const int fromIndex, const int toIndex);
EXPORT void pd_search_init(States *states, trit_t *transactionTrits);
EXPORT char *ccurl_pow(char *trytes, int minWeightMagnitude);
EXPORT void pd_search_init(States *states, trit_t *transaction_trits);
EXPORT char *ccurl_pow(char *trytes, int min_weight_magnitude);
EXPORT char *ccurl_digest_transaction(char *trytes);
/*
void trytes2trits(trit_t *trits, const char *trytes, const size_t len);
void trits2trytes(char *trytes, const trit_t *trits, const size_t len);
Expand Down
Loading

0 comments on commit f2c9f8c

Please sign in to comment.