Skip to content

Commit

Permalink
Merge pull request #190 from skalenetwork/feature/SKALE-3334-separate…
Browse files Browse the repository at this point in the history
…-response

Feature/skale 3334 separate response
  • Loading branch information
olehnikolaiev authored Oct 3, 2020
2 parents e075e5e + 3229806 commit 2c73c2d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
36 changes: 31 additions & 5 deletions DKGCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ string gen_dkg_poly(int _t) {
return result;
}

vector <vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int n) {
vector <vector<string>> get_verif_vect(const string& encryptedPolyHex, int t, int n) {

CHECK_STATE(encryptedPolyHex);
auto encryptedPolyHexPtr = encryptedPolyHex.c_str();

CHECK_STATE(encryptedPolyHexPtr);

vector<char> errMsg(BUF_LEN, 0);

Expand All @@ -166,7 +168,7 @@ vector <vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int

vector <uint8_t> encrDKGPoly(2 * BUF_LEN, 0);

if (!hex2carray(encryptedPolyHex, &encLen, encrDKGPoly.data(), 6100)) {
if (!hex2carray(encryptedPolyHexPtr, &encLen, encrDKGPoly.data(), 6100)) {
throw SGXException(INVALID_HEX, "Invalid encryptedPolyHex");
}

Expand All @@ -182,15 +184,39 @@ vector <vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

vector <string> g2Strings = splitString(pubShares.data(), ',');
vector <vector<string>> pubSharesVect;
vector <vector<string>> pubSharesVect(t);
for (uint64_t i = 0; i < g2Strings.size(); i++) {
vector <string> coeffStr = splitString(g2Strings.at(i).c_str(), ':');
pubSharesVect.push_back(coeffStr);
pubSharesVect[i] = coeffStr;
}

return pubSharesVect;
}

vector <vector<string>> getVerificationVectorMult(const std::string& encryptedPolyHex, int t, int n, size_t ind) {
auto verificationVector = get_verif_vect(encryptedPolyHex, t, n);

vector<vector<string>> result(t);

for (size_t i = 0; i < t; ++i) {
libff::alt_bn128_G2 current_coefficient;
current_coefficient.X.c0 = libff::alt_bn128_Fq(verificationVector[i][0].c_str());
current_coefficient.X.c1 = libff::alt_bn128_Fq(verificationVector[i][1].c_str());
current_coefficient.Y.c0 = libff::alt_bn128_Fq(verificationVector[i][2].c_str());
current_coefficient.Y.c1 = libff::alt_bn128_Fq(verificationVector[i][3].c_str());
current_coefficient.Z = libff::alt_bn128_Fq2::one();

current_coefficient = libff::power(libff::alt_bn128_Fr(ind + 1), i) * current_coefficient;
current_coefficient.to_affine_coordinates();

auto g2_str = convertG2ToString(current_coefficient);

result[i] = splitString(g2_str.c_str(), ':');
}

return result;
}

string
getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const vector <string> &_publicKeys,
int _t,
Expand Down
4 changes: 3 additions & 1 deletion DKGCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ using namespace std;

string gen_dkg_poly( int _t);

vector <vector<string>> get_verif_vect(const char* encryptedPolyHex, int t, int n);
vector <vector<string>> get_verif_vect(const string& encryptedPolyHex, int t, int n);

vector <vector<string>> getVerificationVectorMult(const std::string& encryptedPolyHex, int t, int n, size_t ind);

vector<string> splitString(const char* coeffs, const char symbol);

Expand Down
32 changes: 21 additions & 11 deletions SGXWalletServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName,

shared_ptr <string> encrPoly = readFromDb(_polyName);

verifVector = get_verif_vect(encrPoly->c_str(), _t, _n);
verifVector = get_verif_vect(*encrPoly, _t, _n);

for (int i = 0; i < _t; i++) {
vector <string> currentCoef = verifVector.at(i);
Expand Down Expand Up @@ -586,7 +586,7 @@ Json::Value SGXWalletServer::calculateAllBLSPublicKeysImpl(const Json::Value& pu
RETURN_SUCCESS(result);
}

Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) {
Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _t, int _n, int _ind) {
spdlog::info("Entering {}", __FUNCTION__);
INIT_RESULT(result)

Expand All @@ -603,13 +603,23 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int
result["share*G2"] = *shareG2_ptr;
result["dhKey"] = DHKey;

// TODO: delete dh keys
// for (int i = 0; i < _n; i++) {
// string name = _polyName + "_" + to_string(i) + ":";
// LevelDB::getLevelDb()->deleteDHDKGKey(name);
// string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":";
// LevelDB::getLevelDb()->deleteKey(shareG2_name);
// }
shared_ptr <string> encrPoly = readFromDb(_polyName);

auto verificationVectorMult = getVerificationVectorMult(encrPoly->c_str(), _t, _n, _ind);

for (int i = 0; i < _t; i++) {
vector <string> currentCoef = verificationVectorMult.at(i);
for (int j = 0; j < 4; j++) {
result["verificationVectorMult"][i][j] = currentCoef.at(j);
}
}

for (int i = 0; i < _n; i++) {
string name = _polyName + "_" + to_string(i) + ":";
LevelDB::getLevelDb()->deleteDHDKGKey(name);
string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":";
LevelDB::getLevelDb()->deleteKey(shareG2_name);
}
LevelDB::getLevelDb()->deleteKey(_polyName);

string encryptedSecretShareName = "encryptedSecretShare:" + _polyName;
Expand Down Expand Up @@ -737,8 +747,8 @@ Json::Value SGXWalletServer::blsSignMessageHash(const string &_keyShareName, con
return blsSignMessageHashImpl(_keyShareName, _messageHash, _t, _n);
}

Json::Value SGXWalletServer::complaintResponse(const string &polyName, int ind) {
return complaintResponseImpl(polyName, ind);
Json::Value SGXWalletServer::complaintResponse(const string &polyName, int t, int n, int ind) {
return complaintResponseImpl(polyName, t, n, ind);
}

Json::Value SGXWalletServer::multG2(const string &x) {
Expand Down
4 changes: 2 additions & 2 deletions SGXWalletServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SGXWalletServer : public AbstractStubServer {

virtual Json::Value calculateAllBLSPublicKeys(const Json::Value& publicShares, int t, int n);

virtual Json::Value complaintResponse(const string &polyName, int ind);
virtual Json::Value complaintResponse(const string &polyName, int t, int n, int ind);

virtual Json::Value multG2(const string &x);

Expand Down Expand Up @@ -126,7 +126,7 @@ class SGXWalletServer : public AbstractStubServer {

static Json::Value calculateAllBLSPublicKeysImpl(const Json::Value& publicShares, int t, int n);

static Json::Value complaintResponseImpl(const string &_polyName, int _ind);
static Json::Value complaintResponseImpl(const string &_polyName, int t, int n, int _ind);

static Json::Value multG2Impl(const string &_x);

Expand Down
6 changes: 3 additions & 3 deletions abstractstubserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
this->bindAndAddMethod(jsonrpc::Procedure("createBLSPrivateKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING, "ethKeyName",jsonrpc::JSON_STRING, "polyName", jsonrpc::JSON_STRING, "secretShare",jsonrpc::JSON_STRING,"t", jsonrpc::JSON_INTEGER,"n",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::createBLSPrivateKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("getBLSPublicKeyShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getBLSPublicKeyShareI);
this->bindAndAddMethod(jsonrpc::Procedure("calculateAllBLSPublicKeys", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "publicShares", jsonrpc::JSON_ARRAY, "n", jsonrpc::JSON_INTEGER, "t", jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::calculateAllBLSPublicKeysI);
this->bindAndAddMethod(jsonrpc::Procedure("complaintResponse", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"ind",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::complaintResponseI);
this->bindAndAddMethod(jsonrpc::Procedure("complaintResponse", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, "n",jsonrpc::JSON_INTEGER, "ind",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::complaintResponseI);
this->bindAndAddMethod(jsonrpc::Procedure("multG2", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "x",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::multG2I);
this->bindAndAddMethod(jsonrpc::Procedure("isPolyExists", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::isPolyExistsI);

Expand Down Expand Up @@ -111,7 +111,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
}
inline virtual void complaintResponseI(const Json::Value &request, Json::Value &response)
{
response = this->complaintResponse( request["polyName"].asString(), request["ind"].asInt());
response = this->complaintResponse( request["polyName"].asString(), request["t"].asInt(), request["n"].asInt(), request["ind"].asInt());
}
inline virtual void multG2I(const Json::Value &request, Json::Value &response)
{
Expand Down Expand Up @@ -152,7 +152,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value createBLSPrivateKey(const std::string & blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n) = 0;
virtual Json::Value getBLSPublicKeyShare(const std::string & blsKeyName) = 0;
virtual Json::Value calculateAllBLSPublicKeys(const Json::Value& publicShares, int t, int n) = 0;
virtual Json::Value complaintResponse(const std::string& polyName, int ind) = 0;
virtual Json::Value complaintResponse(const std::string& polyName, int t, int n, int ind) = 0;
virtual Json::Value multG2(const std::string & x) = 0;
virtual Json::Value isPolyExists(const std::string& polyName) = 0;

Expand Down
4 changes: 3 additions & 1 deletion stubclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ class StubClient : public jsonrpc::Client
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}

Json::Value complaintResponse(const std::string& polyName, int ind)
Json::Value complaintResponse(const std::string& polyName, int t, int n,int ind)
{
Json::Value p;
p["polyName"] = polyName;
p["t"] = t;
p["n"] = n;
p["ind"] = ind;
Json::Value result = this->CallMethod("complaintResponse",p);
if (result.isObject())
Expand Down
2 changes: 1 addition & 1 deletion testw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG test", "[aes-dkg]") {
REQUIRE(res);
}

Json::Value complaintResponse = c.complaintResponse(polyNames[1], 0);
Json::Value complaintResponse = c.complaintResponse(polyNames[1], t, n, 0);
REQUIRE(complaintResponse["status"] == 0);

BLSSigShareSet sigShareSet(t, n);
Expand Down

0 comments on commit 2c73c2d

Please sign in to comment.