diff --git a/multihashing.cc b/multihashing.cc index c65d4859..5d9d5368 100644 --- a/multihashing.cc +++ b/multihashing.cc @@ -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(_mm_malloc(sizeof(cryptonight_ctx), 16)); + ctx->memory = static_cast(_mm_malloc(xmrig::CRYPTONIGHT_HEAVY_MEMORY, 4096)); +} + #define THROW_ERROR_EXCEPTION(x) Nan::ThrowError(x) void callback(char* data, void* hint) { @@ -138,31 +146,28 @@ NAN_METHOD(CNLAsync) { NAN_METHOD(cryptonight_light) { - bool fast = false; + if (info.Length() < 1) return THROW_ERROR_EXCEPTION("You must provide one argument."); + + Local 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(info[1]).FromMaybe(0); } - - Local 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(reinterpret_cast(Buffer::Data(target)), Buffer::Length(target), reinterpret_cast(output), ctx); + break; + case 1: cryptonight_single_hash(reinterpret_cast(Buffer::Data(target)), Buffer::Length(target), reinterpret_cast(output), ctx); + break; + default: return THROW_ERROR_EXCEPTION("Unknown PoW variant"); + } v8::Local returnValue = Nan::CopyBuffer(output, 32).ToLocalChecked(); info.GetReturnValue().Set( @@ -170,7 +175,6 @@ NAN_METHOD(cryptonight_light) { ); } - NAN_MODULE_INIT(init) { Nan::Set(target, Nan::New("cryptonight").ToLocalChecked(), Nan::GetFunction(Nan::New(cryptonight)).ToLocalChecked()); Nan::Set(target, Nan::New("CNAsync").ToLocalChecked(), Nan::GetFunction(Nan::New(CNAsync)).ToLocalChecked());