-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathequihashverify.cc
98 lines (73 loc) · 2.63 KB
/
equihashverify.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
#include <nan.h>
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <stdint.h>
#include "crypto/equihash.h"
#include <vector>
using namespace v8;
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
int verifyEH(const char *hdr, const std::vector<unsigned char> &soln, const char *personalizationString, unsigned int N, unsigned int K) {
// Hash state
crypto_generichash_blake2b_state state;
EhInitialiseState(N, K, state, personalizationString);
crypto_generichash_blake2b_update(&state, (const unsigned char*)hdr, 140);
bool isValid;
EhIsValidSolution(N, K, state, soln, isValid);
return isValid;
}
void Verify(const v8::FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
if (args.Length() < 4) {
isolate->ThrowException(
Exception::TypeError(String::NewFromUtf8(isolate, "Wrong number of arguments"))
);
return;
}
if (!args[3]->IsInt32() || !args[4]->IsInt32()) {
isolate->ThrowException(
Exception::TypeError(String::NewFromUtf8(isolate, "Fourth and fifth parameters should be equihash parameters (n, k)"))
);
return;
}
Local<Object> header = args[0]->ToObject();
Local<Object> solution = args[1]->ToObject();
if(!node::Buffer::HasInstance(header) || !node::Buffer::HasInstance(solution)) {
isolate->ThrowException(
Exception::TypeError(String::NewFromUtf8(isolate, "First two arguments should be buffer objects."))
);
return;
}
if (!args[2]->IsString()) {
isolate->ThrowException(
Exception::TypeError(String::NewFromUtf8(isolate, "Third argument should be the personalization string."))
);
return;
}
const char *hdr = node::Buffer::Data(header);
if(node::Buffer::Length(header) != 140) {
//invalid hdr length
args.GetReturnValue().Set(false);
return;
}
const char *soln = node::Buffer::Data(solution);
std::vector<unsigned char> vecSolution(soln, soln + node::Buffer::Length(solution));
String::Utf8Value str(args[2]);
const char* personalizationString = ToCString(str);
// Validate for N, K (4th and 5th parameters)
bool result = verifyEH(
hdr,
vecSolution,
personalizationString,
args[3].As<Uint32>()->Value(),
args[4].As<Uint32>()->Value()
);
args.GetReturnValue().Set(result);
}
void Init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "verify", Verify);
}
NODE_MODULE(equihashverify, Init)