From 71b52b32f6e0e9f1775a62b0c3a30017c29938f3 Mon Sep 17 00:00:00 2001 From: Dmitry Skryabin Date: Thu, 14 Jun 2018 23:23:33 +0300 Subject: [PATCH 1/2] Update multihashing.cc --- multihashing.cc | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/multihashing.cc b/multihashing.cc index c65d4859..41eecdd1 100644 --- a/multihashing.cc +++ b/multihashing.cc @@ -138,31 +138,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 +167,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()); From 0abd80f6a952ab3bb8fcc695f8094ca3d583b248 Mon Sep 17 00:00:00 2001 From: Dmitry Skryabin Date: Thu, 14 Jun 2018 23:38:03 +0300 Subject: [PATCH 2/2] Update multihashing.cc --- multihashing.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/multihashing.cc b/multihashing.cc index 41eecdd1..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) {