Skip to content
This repository has been archived by the owner on Apr 20, 2020. It is now read-only.

Configurable check for valid Equihash <n,k> solutions #2

Merged
merged 5 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ nodejs native binding to check for valid Equihash solutions
sudo apt-get install build-essential libsodium-dev libboost-system-dev
````

# Usage:
# Usage
````javascript
var ev = require('bindings')('equihashverify.node');

var header = new Buffer(..., 'hex');
var solution = new Buffer(..., 'hex'); //do not include byte size preamble "fd4005"

ev.verify(header, solution);
ev.verify(header, solution, n, k);
//returns boolean
````

# Backward compatibility
````javascript
ev.verify(header, solution);
````

# Test Suite:
````
sudo npm install -g mocha
Expand Down
35 changes: 29 additions & 6 deletions crypto/equihash.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2016 Jack Grigg
// Copyright (c) 2016 The Zcash developers
// Copyright (c) 2017-2018 The LitecoinZ developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -18,14 +19,15 @@

#include "crypto/common.h"
#include "crypto/equihash.h"
#include "../util.h"

#include <algorithm>
#include <iostream>
#include <stdexcept>

#include <boost/optional.hpp>
#include "../util.h"

EhSolverCancelledException solver_cancelled;
static EhSolverCancelledException solver_cancelled;

template<unsigned int N, unsigned int K>
int Equihash<N,K>::InitialiseState(eh_HashState& base_state)
Expand Down Expand Up @@ -718,13 +720,10 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
}
#endif // ENABLE_MINING



template<unsigned int N, unsigned int K>
bool Equihash<N,K>::IsValidSolution(const eh_HashState& base_state, std::vector<unsigned char> soln)
{

if (soln.size() != SolutionWidth) {
if (soln.size() != SolutionWidth) {
LogPrint("pow", "Invalid solution length: %d (expected %d)\n",
soln.size(), SolutionWidth);
return false;
Expand Down Expand Up @@ -816,3 +815,27 @@ template bool Equihash<48,5>::OptimisedSolve(const eh_HashState& base_state,
const std::function<bool(EhSolverCancelCheck)> cancelled);
#endif
template bool Equihash<48,5>::IsValidSolution(const eh_HashState& base_state, std::vector<unsigned char> soln);

// Explicit instantiations for Equihash<144,5>
template int Equihash<144,5>::InitialiseState(eh_HashState& base_state);
#ifdef ENABLE_MINING
template bool Equihash<144,5>::BasicSolve(const eh_HashState& base_state,
const std::function<bool(std::vector<unsigned char>)> validBlock,
const std::function<bool(EhSolverCancelCheck)> cancelled);
template bool Equihash<144,5>::OptimisedSolve(const eh_HashState& base_state,
const std::function<bool(std::vector<unsigned char>)> validBlock,
const std::function<bool(EhSolverCancelCheck)> cancelled);
#endif
template bool Equihash<144,5>::IsValidSolution(const eh_HashState& base_state, std::vector<unsigned char> soln);

// Explicit instantiations for Equihash<192,7>
template int Equihash<192,7>::InitialiseState(eh_HashState& base_state);
#ifdef ENABLE_MINING
template bool Equihash<192,7>::BasicSolve(const eh_HashState& base_state,
const std::function<bool(std::vector<unsigned char>)> validBlock,
const std::function<bool(EhSolverCancelCheck)> cancelled);
template bool Equihash<192,7>::OptimisedSolve(const eh_HashState& base_state,
const std::function<bool(std::vector<unsigned char>)> validBlock,
const std::function<bool(EhSolverCancelCheck)> cancelled);
#endif
template bool Equihash<192,7>::IsValidSolution(const eh_HashState& base_state, std::vector<unsigned char> soln);
41 changes: 40 additions & 1 deletion crypto/equihash.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2016 Jack Grigg
// Copyright (c) 2016 The Zcash developers
// Copyright (c) 2017-2018 The LitecoinZ developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -20,7 +21,6 @@

#include <boost/static_assert.hpp>


typedef crypto_generichash_blake2b_state eh_HashState;
typedef uint32_t eh_index;
typedef uint8_t eh_trunc;
Expand Down Expand Up @@ -200,6 +200,8 @@ static Equihash<96,3> Eh96_3;
static Equihash<200,9> Eh200_9;
static Equihash<96,5> Eh96_5;
static Equihash<48,5> Eh48_5;
static Equihash<144,5> Eh144_5;
static Equihash<192,7> Eh192_7;

#define EhInitialiseState(n, k, base_state) \
if (n == 96 && k == 3) { \
Expand All @@ -210,6 +212,10 @@ static Equihash<48,5> Eh48_5;
Eh96_5.InitialiseState(base_state); \
} else if (n == 48 && k == 5) { \
Eh48_5.InitialiseState(base_state); \
} else if (n == 144 && k == 5) { \
Eh144_5.InitialiseState(base_state); \
} else if (n == 192 && k == 7) { \
Eh192_7.InitialiseState(base_state); \
} else { \
throw std::invalid_argument("Unsupported Equihash parameters"); \
}
Expand All @@ -227,6 +233,10 @@ inline bool EhBasicSolve(unsigned int n, unsigned int k, const eh_HashState& bas
return Eh96_5.BasicSolve(base_state, validBlock, cancelled);
} else if (n == 48 && k == 5) {
return Eh48_5.BasicSolve(base_state, validBlock, cancelled);
} else if (n == 144 && k == 5) {
return Eh144_5.BasicSolve(base_state, validBlock, cancelled);
} else if (n == 192 && k == 7) {
return Eh192_7.BasicSolve(base_state, validBlock, cancelled);
} else {
throw std::invalid_argument("Unsupported Equihash parameters");
}
Expand All @@ -251,6 +261,10 @@ inline bool EhOptimisedSolve(unsigned int n, unsigned int k, const eh_HashState&
return Eh96_5.OptimisedSolve(base_state, validBlock, cancelled);
} else if (n == 48 && k == 5) {
return Eh48_5.OptimisedSolve(base_state, validBlock, cancelled);
} else if (n == 144 && k == 5) {
return Eh144_5.OptimisedSolve(base_state, validBlock, cancelled);
} else if (n == 192 && k == 7) {
return Eh192_7.OptimisedSolve(base_state, validBlock, cancelled);
} else {
throw std::invalid_argument("Unsupported Equihash parameters");
}
Expand All @@ -273,8 +287,33 @@ inline bool EhOptimisedSolveUncancellable(unsigned int n, unsigned int k, const
ret = Eh96_5.IsValidSolution(base_state, soln); \
} else if (n == 48 && k == 5) { \
ret = Eh48_5.IsValidSolution(base_state, soln); \
} else if (n == 144 && k == 5) { \
ret = Eh144_5.IsValidSolution(base_state, soln); \
} else if (n == 192 && k == 7) { \
ret = Eh192_7.IsValidSolution(base_state, soln); \
} else { \
throw std::invalid_argument("Unsupported Equihash parameters"); \
}

inline unsigned int EhSolutionWidth(int n, int k)
{
unsigned int ret;
if (n == 96 && k == 3) {
ret = Eh96_3.SolutionWidth;
} else if (n == 200 && k == 9) {
ret = Eh200_9.SolutionWidth;
} else if (n == 144 && k == 5) {
ret = Eh144_5.SolutionWidth;
} else if (n == 192 && k == 7) {
ret = Eh192_7.SolutionWidth;
} else if (n == 96 && k == 5) {
ret = Eh96_5.SolutionWidth;
} else if (n == 48 && k == 5) {
ret = Eh48_5.SolutionWidth;
} else {
throw std::invalid_argument("Unsupported Equihash parameters");
}
return ret;
}

#endif // BITCOIN_EQUIHASH_H
32 changes: 26 additions & 6 deletions equihashverify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@
#include <vector>
using namespace v8;

int verifyEH(const char *hdr, const std::vector<unsigned char> &soln){
unsigned int n = 200;
unsigned int k = 9;

int verifyEH(const char *hdr, const std::vector<unsigned char> &soln, unsigned int n = 200, unsigned int k = 9){
// Hash state
crypto_generichash_blake2b_state state;
EhInitialiseState(n, k, state);

crypto_generichash_blake2b_update(&state, (const unsigned char*)hdr, 140);

bool isValid = Eh200_9.IsValidSolution(state, soln);
bool isValid;
if (n == 96 && k == 3) {
isValid = Eh96_3.IsValidSolution(state, soln);
} else if (n == 200 && k == 9) {
isValid = Eh200_9.IsValidSolution(state, soln);
} else if (n == 144 && k == 5) {
isValid = Eh144_5.IsValidSolution(state, soln);
} else if (n == 192 && k == 7) {
isValid = Eh192_7.IsValidSolution(state, soln);
} else if (n == 96 && k == 5) {
isValid = Eh96_5.IsValidSolution(state, soln);
} else if (n == 48 && k == 5) {
isValid = Eh48_5.IsValidSolution(state, soln);
} else {
throw std::invalid_argument("Unsupported Equihash parameters");
}

return isValid;
}
Expand All @@ -28,6 +40,9 @@ void Verify(const v8::FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

unsigned int n = 200;
unsigned int k = 9;

if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
Expand All @@ -37,6 +52,11 @@ void Verify(const v8::FunctionCallbackInfo<Value>& args) {
Local<Object> header = args[0]->ToObject();
Local<Object> solution = args[1]->ToObject();

if (args.Length() == 4) {
n = args[2]->Uint32Value();
k = args[3]->Uint32Value();
}

if(!node::Buffer::HasInstance(header) || !node::Buffer::HasInstance(solution)) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Arguments should be buffer objects.")));
Expand All @@ -53,7 +73,7 @@ void Verify(const v8::FunctionCallbackInfo<Value>& args) {

std::vector<unsigned char> vecSolution(soln, soln + node::Buffer::Length(solution));

bool result = verifyEH(hdr, vecSolution);
bool result = verifyEH(hdr, vecSolution, n, k);
args.GetReturnValue().Set(result);

}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "equihashverify",
"version": "0.0.2",
"version": "0.0.3",
"description": "function to check whether an Equihash solution is valid",
"main": "equihashverify",
"scripts": {
Expand All @@ -17,7 +17,8 @@
"author": "Joseph Nicholas R. Alcantara",
"contributors": [
"Alberto Garoffolo",
"cronic"
"cronic",
"LitecoinZ"
],
"license": "MIT",
"bugs": {
Expand Down
18 changes: 13 additions & 5 deletions test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading