Skip to content

Commit

Permalink
Allow varying input length with scrypt, scryptn and scryptjane
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasjones committed Apr 19, 2014
1 parent 814d47a commit a3bfc44
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 507 deletions.
2 changes: 1 addition & 1 deletion blake.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "sha3/sph_blake.h"


void blake_hash(const char* input, char* output, unsigned int len)
void blake_hash(const char* input, char* output, uint32_t len)
{
sph_blake256_context ctx_blake;
sph_blake256_init(&ctx_blake);
Expand Down
4 changes: 3 additions & 1 deletion blake.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
extern "C" {
#endif

void blake_hash(const char* input, char* output, unsigned int len);
#include <stdint.h>

void blake_hash(const char* input, char* output, uint32_t len);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions groestl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "sha3/sph_groestl.h"
#include "sha256.h"

void groestl_hash(const char* input, char* output, unsigned int len)
void groestl_hash(const char* input, char* output, uint32_t len)
{
char* hash1 = (char*) malloc(64);
char* hash2 = (char*) malloc(64);
Expand All @@ -27,7 +27,7 @@ void groestl_hash(const char* input, char* output, unsigned int len)
free(hash2);
}

void groestl_myriad_hash(const char* input, char* output, unsigned int len)
void groestl_myriad_hash(const char* input, char* output, uint32_t len)
{
char* temp = (char*) malloc(64);

Expand Down
6 changes: 4 additions & 2 deletions groestl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
extern "C" {
#endif

void groestl_hash(const char* input, char* output, unsigned int len);
void groestl_myriad_hash(const char* input, char* output, unsigned int len);
#include <stdint.h>

void groestl_hash(const char* input, char* output, uint32_t len);
void groestl_myriad_hash(const char* input, char* output, uint32_t len);

#ifdef __cplusplus
}
Expand Down
67 changes: 16 additions & 51 deletions multihashing.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <stdint.h>

extern "C" {
#include "bcrypt.h"
Expand All @@ -13,48 +14,6 @@ extern "C" {
#include "x11.h"
#include "groestl.h"
#include "blake.h"


#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax) {

const unsigned char minNfactor = nMin;//4;
const unsigned char maxNfactor = nMax;//30;

int l = 0, s, n;
unsigned char N;

if (nTimestamp <= nChainStartTime)
return 4;

s = nTimestamp - nChainStartTime;
while ((s >> 1) > 3) {
l += 1;
s >>= 1;
}

s &= 3;

n = (l * 170 + s * 25 - 2320) / 100;

if (n < 0) n = 0;

if (n > 255)
printf("GetNfactor(%d) - something wrong(n == %d)\n", nTimestamp, n);

N = (unsigned char)n;
//printf("GetNfactor: %d -> %d %d : %d / %d\n", nTimestamp - nChainStartTime, l, s, n, min(max(N, minNfactor), maxNfactor));

return min(max(N, minNfactor), maxNfactor);
}

void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor)
{
return scrypt((const unsigned char*)input, inputlen,
(const unsigned char*)input, inputlen,
Nfactor, 0, 0, (unsigned char*)res, 32);
}
}

using namespace node;
Expand All @@ -78,7 +37,7 @@ Handle<Value> quark(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

unsigned int input_len = Buffer::Length(target);
uint32_t input_len = Buffer::Length(target);

quark_hash(input, output, input_len);

Expand All @@ -100,7 +59,7 @@ Handle<Value> x11(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

unsigned int input_len = Buffer::Length(target);
uint32_t input_len = Buffer::Length(target);

x11_hash(input, output, input_len);

Expand All @@ -122,7 +81,9 @@ Handle<Value> scrypt(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

scrypt_1024_1_1_256(input, output);
uint32_t input_len = Buffer::Length(target);

scrypt_1024_1_1_256(input, output, input_len);

Buffer* buff = Buffer::New(output, 32);
return scope.Close(buff->handle_);
Expand All @@ -147,10 +108,12 @@ Handle<Value> scryptn(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

uint32_t input_len = Buffer::Length(target);

//unsigned int N = 1 << (getNfactor(input) + 1);
unsigned int N = 1 << nFactor;

scrypt_N_1_1_256(input, output, N);
scrypt_N_1_1_256(input, output, N, input_len);


Buffer* buff = Buffer::New(output, 32);
Expand Down Expand Up @@ -183,7 +146,9 @@ Handle<Value> scryptjane(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

scryptjane_hash(input, 80, (uint32_t *)output, GetNfactorJane(timestamp, nChainStartTime, nMin, nMax));
uint32_t input_len = Buffer::Length(target);

scryptjane_hash(input, input_len, (uint32_t *)output, GetNfactorJane(timestamp, nChainStartTime, nMin, nMax));

Buffer* buff = Buffer::New(output, 32);
return scope.Close(buff->handle_);
Expand Down Expand Up @@ -246,7 +211,7 @@ Handle<Value> skein(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

unsigned int input_len = Buffer::Length(target);
uint32_t input_len = Buffer::Length(target);

skein_hash(input, output, input_len);

Expand All @@ -269,7 +234,7 @@ Handle<Value> groestl(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

unsigned int input_len = Buffer::Length(target);
uint32_t input_len = Buffer::Length(target);

groestl_hash(input, output, input_len);

Expand All @@ -292,7 +257,7 @@ Handle<Value> groestl_myriad(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

unsigned int input_len = Buffer::Length(target);
uint32_t input_len = Buffer::Length(target);

groestl_myriad_hash(input, output, input_len);

Expand All @@ -315,7 +280,7 @@ Handle<Value> blake(const Arguments& args) {
char * input = Buffer::Data(target);
char * output = new char[32];

unsigned int input_len = Buffer::Length(target);
uint32_t input_len = Buffer::Length(target);

blake_hash(input, output, input_len);

Expand Down
6 changes: 3 additions & 3 deletions quark.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ le32enc(void *pp, uint32_t x)
* (unsigned char) in big-endian form. Assumes len is a multiple of 4.
*/
static void
be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
be32enc_vect(unsigned char *dst, const uint32_t *src, uint32_t len)
{
size_t i;

Expand All @@ -98,15 +98,15 @@ be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
* len/4 vector of (uint32_t). Assumes len is a multiple of 4.
*/
static void
be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
be32dec_vect(uint32_t *dst, const unsigned char *src, uint32_t len)
{
size_t i;

for (i = 0; i < len / 4; i++)
dst[i] = be32dec(src + i * 4);
}

void quark_hash(const char* input, char* output, unsigned int len)
void quark_hash(const char* input, char* output, uint32_t len)
{
sph_blake512_context ctx_blake;
sph_bmw512_context ctx_bmw;
Expand Down
12 changes: 11 additions & 1 deletion quark.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#ifndef QUARK_H
#define QUARK_H

void quark_hash(const char* input, char* output, unsigned int len);
#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

void quark_hash(const char* input, char* output, uint32_t len);

#ifdef __cplusplus
}
#endif

#endif
8 changes: 4 additions & 4 deletions scrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
/* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
*/
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad)
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad, size_t len)
{
uint8_t * B;
uint32_t * V;
Expand All @@ -239,7 +239,7 @@ void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad)
V = (uint32_t *)(B + (128 * r * p) + (256 * r + 64));

/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
PBKDF2_SHA256((const uint8_t*)input, 80, (const uint8_t*)input, 80, 1, B, p * 128 * r);
PBKDF2_SHA256((const uint8_t*)input, len, (const uint8_t*)input, len, 1, B, p * 128 * r);

/* 2: for i = 0 to p - 1 do */
for (i = 0; i < p; i++) {
Expand All @@ -251,8 +251,8 @@ void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad)
PBKDF2_SHA256((const uint8_t*)input, 80, B, p * 128 * r, 1, (uint8_t*)output, 32);
}

void scrypt_1024_1_1_256(const char* input, char* output)
void scrypt_1024_1_1_256(const char* input, char* output, size_t len)
{
char scratchpad[131583];
scrypt_1024_1_1_256_sp(input, output, scratchpad);
scrypt_1024_1_1_256_sp(input, output, scratchpad, len);
}
6 changes: 4 additions & 2 deletions scrypt.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef SCRYPT_H
#define SCRYPT_H

void scrypt_1024_1_1_256(const char* input, char* output);
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad);
#include <stddef.h>

void scrypt_1024_1_1_256(const char* input, char* output, size_t len);
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad, size_t len);
#define scrypt_scratchpad_size 131583;

#endif
41 changes: 41 additions & 0 deletions scryptjane.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,44 @@ scrypt(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t
scrypt_free(&V);
scrypt_free(&YX);
}

#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax) {

const unsigned char minNfactor = nMin;//4;
const unsigned char maxNfactor = nMax;//30;

int l = 0, s, n;
unsigned char N;

if (nTimestamp <= nChainStartTime)
return 4;

s = nTimestamp - nChainStartTime;
while ((s >> 1) > 3) {
l += 1;
s >>= 1;
}

s &= 3;

n = (l * 170 + s * 25 - 2320) / 100;

if (n < 0) n = 0;

if (n > 255)
printf("GetNfactor(%d) - something wrong(n == %d)\n", nTimestamp, n);

N = (unsigned char)n;
//printf("GetNfactor: %d -> %d %d : %d / %d\n", nTimestamp - nChainStartTime, l, s, n, min(max(N, minNfactor), maxNfactor));

return min(max(N, minNfactor), maxNfactor);
}

void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor)
{
return scrypt((const unsigned char*)input, inputlen,
(const unsigned char*)input, inputlen,
Nfactor, 0, 0, (unsigned char*)res, 32);
}
4 changes: 4 additions & 0 deletions scryptjane.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SCRYPT_JANE_H
#define SCRYPT_JANE_H

#include <stdint.h>

#define SCRYPT_KECCAK512
#define SCRYPT_CHACHA
Expand Down Expand Up @@ -29,4 +30,7 @@ void scrypt_set_fatal_error(scrypt_fatal_errorfn fn);

void scrypt(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor, unsigned char *out, size_t bytes);

unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax);
void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor);

#endif /* SCRYPT_JANE_H */
Loading

0 comments on commit a3bfc44

Please sign in to comment.