-
Notifications
You must be signed in to change notification settings - Fork 2
/
hasherbeamhash.cc
109 lines (75 loc) · 3.38 KB
/
hasherbeamhash.cc
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <stdint.h>
#include <iostream>
#include "nan.h"
#include "src/equihashR.h"
#include "src/beamHashIII.h"
using namespace node;
using namespace v8;
#define THROW_ERROR_EXCEPTION(x) Nan::ThrowError(x)
static BeamHash_III BeamHashIII;
bool verifyPoWScheme(PoWScheme &scheme, const char *input_ptr, const char *nonce64_ptr, Local <Object> solution) {
const char *solution_ptr = (char *) Buffer::Data(solution);
blake2b_state state;
scheme.InitialiseState(state);
blake2b_update(&state, (const unsigned char *) input_ptr, 32);
blake2b_update(&state, (const unsigned char *) nonce64_ptr, 8);
std::vector<unsigned char> solution_vec(solution_ptr, solution_ptr + node::Buffer::Length(solution));
return scheme.IsValidSolution(state, solution_vec);
}
NAN_METHOD(verify1) {
if (info.Length() < 3) {
return THROW_ERROR_EXCEPTION("hasher-beamhash.verify1 - 3 arguments expected.");
}
const char* input_ptr = (char*)Buffer::Data(Nan::To<v8::Object>(info[0]).ToLocalChecked());
const char* nonce64_ptr = (char*)Buffer::Data(Nan::To<v8::Object>(info[1]).ToLocalChecked());
Local<Object> solution = Nan::To<v8::Object>(info[2]).ToLocalChecked();
bool isValid = verifyPoWScheme(BeamHashI, input_ptr, nonce64_ptr, solution);
if (isValid) {
info.GetReturnValue().Set(Nan::True());
}
else {
info.GetReturnValue().Set(Nan::False());
}
}
NAN_METHOD(verify2) {
if (info.Length() < 3) {
return THROW_ERROR_EXCEPTION("hasher-beamhash.verify2 - 3 arguments expected.");
}
const char* input_ptr = (char*)Buffer::Data(Nan::To<v8::Object>(info[0]).ToLocalChecked());
const char* nonce64_ptr = (char*)Buffer::Data(Nan::To<v8::Object>(info[1]).ToLocalChecked());
Local<Object> solution = Nan::To<v8::Object>(info[2]).ToLocalChecked();
bool isValid = verifyPoWScheme(BeamHashII, input_ptr, nonce64_ptr, solution);
if (isValid) {
info.GetReturnValue().Set(Nan::True());
}
else {
info.GetReturnValue().Set(Nan::False());
}
}
NAN_METHOD(verify3) {
if (info.Length() < 3) {
return THROW_ERROR_EXCEPTION("hasher-beamhash.verify3 - 3 arguments expected.");
}
const char* input_ptr = (char*)Buffer::Data(Nan::To<v8::Object>(info[0]).ToLocalChecked());
const char* nonce64_ptr = (char*)Buffer::Data(Nan::To<v8::Object>(info[1]).ToLocalChecked());
Local<Object> solution = Nan::To<v8::Object>(info[2]).ToLocalChecked();
bool isValid = verifyPoWScheme(BeamHashIII, input_ptr, nonce64_ptr, solution);
if (isValid) {
info.GetReturnValue().Set(Nan::True());
}
else {
info.GetReturnValue().Set(Nan::False());
}
}
NAN_MODULE_INIT(init) {
Nan::Set(target, Nan::New("verify1").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(verify1)).ToLocalChecked());
Nan::Set(target, Nan::New("verify2").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(verify2)).ToLocalChecked());
Nan::Set(target, Nan::New("verify3").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(verify3)).ToLocalChecked());
}
NODE_MODULE(hasherbeamhash, init)