Skip to content

Commit a3bfc44

Browse files
committed
Allow varying input length with scrypt, scryptn and scryptjane
1 parent 814d47a commit a3bfc44

17 files changed

+106
-507
lines changed

blake.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "sha3/sph_blake.h"
88

99

10-
void blake_hash(const char* input, char* output, unsigned int len)
10+
void blake_hash(const char* input, char* output, uint32_t len)
1111
{
1212
sph_blake256_context ctx_blake;
1313
sph_blake256_init(&ctx_blake);

blake.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
extern "C" {
66
#endif
77

8-
void blake_hash(const char* input, char* output, unsigned int len);
8+
#include <stdint.h>
9+
10+
void blake_hash(const char* input, char* output, uint32_t len);
911

1012
#ifdef __cplusplus
1113
}

groestl.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "sha3/sph_groestl.h"
88
#include "sha256.h"
99

10-
void groestl_hash(const char* input, char* output, unsigned int len)
10+
void groestl_hash(const char* input, char* output, uint32_t len)
1111
{
1212
char* hash1 = (char*) malloc(64);
1313
char* hash2 = (char*) malloc(64);
@@ -27,7 +27,7 @@ void groestl_hash(const char* input, char* output, unsigned int len)
2727
free(hash2);
2828
}
2929

30-
void groestl_myriad_hash(const char* input, char* output, unsigned int len)
30+
void groestl_myriad_hash(const char* input, char* output, uint32_t len)
3131
{
3232
char* temp = (char*) malloc(64);
3333

groestl.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
extern "C" {
66
#endif
77

8-
void groestl_hash(const char* input, char* output, unsigned int len);
9-
void groestl_myriad_hash(const char* input, char* output, unsigned int len);
8+
#include <stdint.h>
9+
10+
void groestl_hash(const char* input, char* output, uint32_t len);
11+
void groestl_myriad_hash(const char* input, char* output, uint32_t len);
1012

1113
#ifdef __cplusplus
1214
}

multihashing.cc

+16-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <node.h>
22
#include <node_buffer.h>
33
#include <v8.h>
4+
#include <stdint.h>
45

56
extern "C" {
67
#include "bcrypt.h"
@@ -13,48 +14,6 @@ extern "C" {
1314
#include "x11.h"
1415
#include "groestl.h"
1516
#include "blake.h"
16-
17-
18-
#define max(a,b) (((a) > (b)) ? (a) : (b))
19-
#define min(a,b) (((a) < (b)) ? (a) : (b))
20-
unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax) {
21-
22-
const unsigned char minNfactor = nMin;//4;
23-
const unsigned char maxNfactor = nMax;//30;
24-
25-
int l = 0, s, n;
26-
unsigned char N;
27-
28-
if (nTimestamp <= nChainStartTime)
29-
return 4;
30-
31-
s = nTimestamp - nChainStartTime;
32-
while ((s >> 1) > 3) {
33-
l += 1;
34-
s >>= 1;
35-
}
36-
37-
s &= 3;
38-
39-
n = (l * 170 + s * 25 - 2320) / 100;
40-
41-
if (n < 0) n = 0;
42-
43-
if (n > 255)
44-
printf("GetNfactor(%d) - something wrong(n == %d)\n", nTimestamp, n);
45-
46-
N = (unsigned char)n;
47-
//printf("GetNfactor: %d -> %d %d : %d / %d\n", nTimestamp - nChainStartTime, l, s, n, min(max(N, minNfactor), maxNfactor));
48-
49-
return min(max(N, minNfactor), maxNfactor);
50-
}
51-
52-
void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor)
53-
{
54-
return scrypt((const unsigned char*)input, inputlen,
55-
(const unsigned char*)input, inputlen,
56-
Nfactor, 0, 0, (unsigned char*)res, 32);
57-
}
5817
}
5918

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

81-
unsigned int input_len = Buffer::Length(target);
40+
uint32_t input_len = Buffer::Length(target);
8241

8342
quark_hash(input, output, input_len);
8443

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

103-
unsigned int input_len = Buffer::Length(target);
62+
uint32_t input_len = Buffer::Length(target);
10463

10564
x11_hash(input, output, input_len);
10665

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

125-
scrypt_1024_1_1_256(input, output);
84+
uint32_t input_len = Buffer::Length(target);
85+
86+
scrypt_1024_1_1_256(input, output, input_len);
12687

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

111+
uint32_t input_len = Buffer::Length(target);
112+
150113
//unsigned int N = 1 << (getNfactor(input) + 1);
151114
unsigned int N = 1 << nFactor;
152115

153-
scrypt_N_1_1_256(input, output, N);
116+
scrypt_N_1_1_256(input, output, N, input_len);
154117

155118

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

186-
scryptjane_hash(input, 80, (uint32_t *)output, GetNfactorJane(timestamp, nChainStartTime, nMin, nMax));
149+
uint32_t input_len = Buffer::Length(target);
150+
151+
scryptjane_hash(input, input_len, (uint32_t *)output, GetNfactorJane(timestamp, nChainStartTime, nMin, nMax));
187152

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

249-
unsigned int input_len = Buffer::Length(target);
214+
uint32_t input_len = Buffer::Length(target);
250215

251216
skein_hash(input, output, input_len);
252217

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

272-
unsigned int input_len = Buffer::Length(target);
237+
uint32_t input_len = Buffer::Length(target);
273238

274239
groestl_hash(input, output, input_len);
275240

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

295-
unsigned int input_len = Buffer::Length(target);
260+
uint32_t input_len = Buffer::Length(target);
296261

297262
groestl_myriad_hash(input, output, input_len);
298263

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

318-
unsigned int input_len = Buffer::Length(target);
283+
uint32_t input_len = Buffer::Length(target);
319284

320285
blake_hash(input, output, input_len);
321286

quark.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ le32enc(void *pp, uint32_t x)
8585
* (unsigned char) in big-endian form. Assumes len is a multiple of 4.
8686
*/
8787
static void
88-
be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
88+
be32enc_vect(unsigned char *dst, const uint32_t *src, uint32_t len)
8989
{
9090
size_t i;
9191

@@ -98,15 +98,15 @@ be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
9898
* len/4 vector of (uint32_t). Assumes len is a multiple of 4.
9999
*/
100100
static void
101-
be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
101+
be32dec_vect(uint32_t *dst, const unsigned char *src, uint32_t len)
102102
{
103103
size_t i;
104104

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

109-
void quark_hash(const char* input, char* output, unsigned int len)
109+
void quark_hash(const char* input, char* output, uint32_t len)
110110
{
111111
sph_blake512_context ctx_blake;
112112
sph_bmw512_context ctx_bmw;

quark.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#ifndef QUARK_H
22
#define QUARK_H
33

4-
void quark_hash(const char* input, char* output, unsigned int len);
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdint.h>
9+
10+
void quark_hash(const char* input, char* output, uint32_t len);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
515

616
#endif

scrypt.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
223223
/* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
224224
scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
225225
*/
226-
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad)
226+
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad, size_t len)
227227
{
228228
uint8_t * B;
229229
uint32_t * V;
@@ -239,7 +239,7 @@ void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad)
239239
V = (uint32_t *)(B + (128 * r * p) + (256 * r + 64));
240240

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

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

254-
void scrypt_1024_1_1_256(const char* input, char* output)
254+
void scrypt_1024_1_1_256(const char* input, char* output, size_t len)
255255
{
256256
char scratchpad[131583];
257-
scrypt_1024_1_1_256_sp(input, output, scratchpad);
257+
scrypt_1024_1_1_256_sp(input, output, scratchpad, len);
258258
}

scrypt.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef SCRYPT_H
22
#define SCRYPT_H
33

4-
void scrypt_1024_1_1_256(const char* input, char* output);
5-
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad);
4+
#include <stddef.h>
5+
6+
void scrypt_1024_1_1_256(const char* input, char* output, size_t len);
7+
void scrypt_1024_1_1_256_sp(const char* input, char* output, char* scratchpad, size_t len);
68
#define scrypt_scratchpad_size 131583;
79

810
#endif

scryptjane.c

+41
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,44 @@ scrypt(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t
180180
scrypt_free(&V);
181181
scrypt_free(&YX);
182182
}
183+
184+
#define max(a,b) (((a) > (b)) ? (a) : (b))
185+
#define min(a,b) (((a) < (b)) ? (a) : (b))
186+
unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax) {
187+
188+
const unsigned char minNfactor = nMin;//4;
189+
const unsigned char maxNfactor = nMax;//30;
190+
191+
int l = 0, s, n;
192+
unsigned char N;
193+
194+
if (nTimestamp <= nChainStartTime)
195+
return 4;
196+
197+
s = nTimestamp - nChainStartTime;
198+
while ((s >> 1) > 3) {
199+
l += 1;
200+
s >>= 1;
201+
}
202+
203+
s &= 3;
204+
205+
n = (l * 170 + s * 25 - 2320) / 100;
206+
207+
if (n < 0) n = 0;
208+
209+
if (n > 255)
210+
printf("GetNfactor(%d) - something wrong(n == %d)\n", nTimestamp, n);
211+
212+
N = (unsigned char)n;
213+
//printf("GetNfactor: %d -> %d %d : %d / %d\n", nTimestamp - nChainStartTime, l, s, n, min(max(N, minNfactor), maxNfactor));
214+
215+
return min(max(N, minNfactor), maxNfactor);
216+
}
217+
218+
void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor)
219+
{
220+
return scrypt((const unsigned char*)input, inputlen,
221+
(const unsigned char*)input, inputlen,
222+
Nfactor, 0, 0, (unsigned char*)res, 32);
223+
}

scryptjane.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef SCRYPT_JANE_H
22
#define SCRYPT_JANE_H
33

4+
#include <stdint.h>
45

56
#define SCRYPT_KECCAK512
67
#define SCRYPT_CHACHA
@@ -29,4 +30,7 @@ void scrypt_set_fatal_error(scrypt_fatal_errorfn fn);
2930

3031
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);
3132

33+
unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax);
34+
void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor);
35+
3236
#endif /* SCRYPT_JANE_H */

0 commit comments

Comments
 (0)