diff --git a/DKGCrypto.cpp b/DKGCrypto.cpp index c92ddc53..7d37afc0 100644 --- a/DKGCrypto.cpp +++ b/DKGCrypto.cpp @@ -152,9 +152,11 @@ string gen_dkg_poly(int _t) { return result; } -vector > get_verif_vect(const char *encryptedPolyHex, int t, int n) { +vector > get_verif_vect(const string& encryptedPolyHex, int t, int n) { - CHECK_STATE(encryptedPolyHex); + auto encryptedPolyHexPtr = encryptedPolyHex.c_str(); + + CHECK_STATE(encryptedPolyHexPtr); vector errMsg(BUF_LEN, 0); @@ -166,7 +168,7 @@ vector > get_verif_vect(const char *encryptedPolyHex, int t, int vector 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"); } @@ -182,15 +184,39 @@ vector > get_verif_vect(const char *encryptedPolyHex, int t, int HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data()); vector g2Strings = splitString(pubShares.data(), ','); - vector > pubSharesVect; + vector > pubSharesVect(t); for (uint64_t i = 0; i < g2Strings.size(); i++) { vector coeffStr = splitString(g2Strings.at(i).c_str(), ':'); - pubSharesVect.push_back(coeffStr); + pubSharesVect[i] = coeffStr; } return pubSharesVect; } +vector > getVerificationVectorMult(const std::string& encryptedPolyHex, int t, int n, size_t ind) { + auto verificationVector = get_verif_vect(encryptedPolyHex, t, n); + + vector> 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 &_publicKeys, int _t, diff --git a/DKGCrypto.h b/DKGCrypto.h index 5ec9eb61..1dcd032b 100644 --- a/DKGCrypto.h +++ b/DKGCrypto.h @@ -33,7 +33,9 @@ using namespace std; string gen_dkg_poly( int _t); -vector > get_verif_vect(const char* encryptedPolyHex, int t, int n); +vector > get_verif_vect(const string& encryptedPolyHex, int t, int n); + +vector > getVerificationVectorMult(const std::string& encryptedPolyHex, int t, int n, size_t ind); vector splitString(const char* coeffs, const char symbol); diff --git a/SGXWalletServer.cpp b/SGXWalletServer.cpp index 8c87ae7c..65816398 100644 --- a/SGXWalletServer.cpp +++ b/SGXWalletServer.cpp @@ -388,7 +388,7 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, shared_ptr 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 currentCoef = verifVector.at(i); @@ -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) @@ -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 encrPoly = readFromDb(_polyName); + + auto verificationVectorMult = getVerificationVectorMult(encrPoly->c_str(), _t, _n, _ind); + + for (int i = 0; i < _t; i++) { + vector 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; @@ -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) { diff --git a/SGXWalletServer.hpp b/SGXWalletServer.hpp index 816cfab0..f7b801d6 100644 --- a/SGXWalletServer.hpp +++ b/SGXWalletServer.hpp @@ -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); @@ -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); diff --git a/abstractstubserver.h b/abstractstubserver.h index ce43bfa2..580b8445 100644 --- a/abstractstubserver.h +++ b/abstractstubserver.h @@ -50,7 +50,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer 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); @@ -111,7 +111,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer } 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) { @@ -152,7 +152,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer 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; diff --git a/stubclient.h b/stubclient.h index 632a2b9b..7bcad015 100644 --- a/stubclient.h +++ b/stubclient.h @@ -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()) diff --git a/testw.cpp b/testw.cpp index f4175aa8..95666a63 100644 --- a/testw.cpp +++ b/testw.cpp @@ -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);