Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions multihashing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ extern "C" {
#include "cryptonight_light.h"
}

static struct cryptonight_ctx* ctx = NULL;

void init_ctx() {
if (ctx) return;
ctx = static_cast<cryptonight_ctx *>(_mm_malloc(sizeof(cryptonight_ctx), 16));
ctx->memory = static_cast<uint8_t *>(_mm_malloc(xmrig::CRYPTONIGHT_HEAVY_MEMORY, 4096));
}

#define THROW_ERROR_EXCEPTION(x) Nan::ThrowError(x)

void callback(char* data, void* hint) {
Expand Down Expand Up @@ -138,39 +146,35 @@ NAN_METHOD(CNLAsync) {

NAN_METHOD(cryptonight_light) {

bool fast = false;
if (info.Length() < 1) return THROW_ERROR_EXCEPTION("You must provide one argument.");

Local<Object> target = info[0]->ToObject();

if (info.Length() < 1)
return THROW_ERROR_EXCEPTION("You must provide one argument.");
if(!Buffer::HasInstance(target)) return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");

int variant = 0;

if (info.Length() >= 2) {
if(!info[1]->IsBoolean())
return THROW_ERROR_EXCEPTION("Argument 2 should be a boolean");
fast = info[1]->ToBoolean()->BooleanValue();
if (!info[1]->IsNumber()) return THROW_ERROR_EXCEPTION("Argument 2 should be a number");
variant = Nan::To<int>(info[1]).FromMaybe(0);
}

Local<Object> target = info[0]->ToObject();

if(!Buffer::HasInstance(target))
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");

char * input = Buffer::Data(target);

char output[32];

uint32_t input_len = Buffer::Length(target);

if(fast)
cryptonight_light_fast_hash(input, output, input_len);
else
cryptonight_light_hash(input, output, input_len);
init_ctx();
switch (variant) {
case 0: cryptonight_single_hash<AEON_ITER, AEON_MEMORY, AEON_MASK, false, 0>(reinterpret_cast<const uint8_t*>(Buffer::Data(target)), Buffer::Length(target), reinterpret_cast<uint8_t*>(output), ctx);
break;
case 1: cryptonight_single_hash<AEON_ITER, AEON_MEMORY, AEON_MASK, false, 1>(reinterpret_cast<const uint8_t*>(Buffer::Data(target)), Buffer::Length(target), reinterpret_cast<uint8_t*>(output), ctx);
break;
default: return THROW_ERROR_EXCEPTION("Unknown PoW variant");
}

v8::Local<v8::Value> returnValue = Nan::CopyBuffer(output, 32).ToLocalChecked();
info.GetReturnValue().Set(
returnValue
);
}


NAN_MODULE_INIT(init) {
Nan::Set(target, Nan::New("cryptonight").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(cryptonight)).ToLocalChecked());
Nan::Set(target, Nan::New("CNAsync").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(CNAsync)).ToLocalChecked());
Expand Down