-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscryptmodule.c
More file actions
90 lines (76 loc) · 2.16 KB
/
scryptmodule.c
File metadata and controls
90 lines (76 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "scrypt.h"
static unsigned char getNfactor(char* blockheader) {
int n,l = 0;
unsigned long nTimestamp = *(unsigned int*)(&blockheader[68]);
unsigned char minNfactor = 10;
unsigned char maxNfactor = 30;
unsigned char N;
uint64_t s;
if (nTimestamp <= 1389306217) {
return minNfactor;
}
s = nTimestamp - 1389306217;
while ((s >> 1) > 3) {
l += 1;
s >>= 1;
}
s &= 3;
n = (l * 158 + s * 28 - 2670) / 100;
if (n < 0) n = 0;
N = (unsigned char) n;
n = N > minNfactor ? N : minNfactor;
N = n < maxNfactor ? n : maxNfactor;
return N;
}
static PyObject *scrypt_getpowhash(PyObject *self, PyObject *args)
{
char *output;
PyObject *value;
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
unsigned int N;
if (!PyArg_ParseTuple(args, "S", &input))
return NULL;
Py_INCREF(input);
output = PyMem_Malloc(32);
#if PY_MAJOR_VERSION >= 3
N = 1 << (getNfactor((char *)PyBytes_AsString((PyObject*) input)) + 1);
scrypt_N_1_1_256((char *)PyBytes_AsString((PyObject*) input), output, N);
#else
N = 1 << (getNfactor((char *)PyString_AsString((PyObject*) input)) + 1);
scrypt_N_1_1_256((char *)PyString_AsString((PyObject*) input), output, N);
#endif
Py_DECREF(input);
#if PY_MAJOR_VERSION >= 3
value = Py_BuildValue("y#", output, 32);
#else
value = Py_BuildValue("s#", output, 32);
#endif
PyMem_Free(output);
return value;
}
static PyMethodDef ScryptMethods[] = {
{ "getPoWHash", scrypt_getpowhash, METH_VARARGS, "Returns the proof of work hash using scrypt" },
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef VTCScryptModule = {
PyModuleDef_HEAD_INIT,
"vtc_scrypt_new",
"...",
-1,
ScryptMethods
};
PyMODINIT_FUNC PyInit_vtc_scrypt_new(void) {
return PyModule_Create(&VTCScryptModule);
}
#else
PyMODINIT_FUNC initvtc_scrypt_new(void) {
(void) Py_InitModule("vtc_scrypt_new", ScryptMethods);
}
#endif