From ebf576eb1a32255a6111e43d24e6d3b2007c2f02 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Mon, 7 Feb 2022 13:53:29 +0200 Subject: [PATCH] SKALE-4954 change getDecryptionShare to getDecryptionShares --- SGXWalletServer.cpp | 26 ++++++++----- SGXWalletServer.hpp | 4 +- abstractstubserver.h | 8 ++-- stubclient.h | 6 +-- testw.cpp | 88 +++++++++++++++++++++++++++++++----------- zmq_src/ReqMessage.cpp | 4 +- zmq_src/RspMessage.h | 2 +- zmq_src/ZMQClient.cpp | 4 +- zmq_src/ZMQClient.h | 2 +- 9 files changed, 98 insertions(+), 46 deletions(-) diff --git a/SGXWalletServer.cpp b/SGXWalletServer.cpp index e47dfbcf..f00725ee 100644 --- a/SGXWalletServer.cpp +++ b/SGXWalletServer.cpp @@ -985,7 +985,7 @@ SGXWalletServer::createBLSPrivateKeyV2Impl(const string &_blsKeyName, const stri RETURN_SUCCESS(result); } -Json::Value SGXWalletServer::getDecryptionShareImpl(const std::string& blsKeyName, const std::string& publicDecryptionValue) { +Json::Value SGXWalletServer::getDecryptionSharesImpl(const std::string& blsKeyName, const Json::Value& publicDecryptionValues) { spdlog::info("Entering {}", __FUNCTION__); INIT_RESULT(result) @@ -994,15 +994,23 @@ Json::Value SGXWalletServer::getDecryptionShareImpl(const std::string& blsKeyNam throw SGXException(BLS_SIGN_INVALID_KS_NAME, string(__FUNCTION__) + ":Invalid BLSKey name"); } - if ( publicDecryptionValue.length() < 7 || publicDecryptionValue.length() > 78 * 4 ) { - throw SGXException(INVALID_DECRYPTION_VALUE_FORMAT, string(__FUNCTION__) + ":Invalid publicDecryptionValue format"); + if (!publicDecryptionValues.isArray()) { + throw SGXException(INVALID_DECRYPTION_VALUE_FORMAT, + string(__FUNCTION__) + ":Public decryption values should be an array"); } - shared_ptr encryptedKeyHex_ptr = readFromDb(blsKeyName); + for (int i = 0; i < publicDecryptionValues.size(); ++i) { + std::string publicDecryptionValue = publicDecryptionValues[i].asString(); + if ( publicDecryptionValue.length() < 7 || publicDecryptionValue.length() > 78 * 4 ) { + throw SGXException(INVALID_DECRYPTION_VALUE_FORMAT, string(__FUNCTION__) + ":Invalid publicDecryptionValue format"); + } + + shared_ptr encryptedKeyHex_ptr = readFromDb(blsKeyName); - vector decryptionValueVector = calculateDecryptionShare(encryptedKeyHex_ptr->c_str(), publicDecryptionValue); - for (uint8_t i = 0; i < 4; ++i) { - result["decryptionShare"][i] = decryptionValueVector.at(i); + vector decryptionValueVector = calculateDecryptionShare(encryptedKeyHex_ptr->c_str(), publicDecryptionValue); + for (uint8_t j = 0; j < 4; ++j) { + result["decryptionShares"][i][j] = decryptionValueVector.at(j); + } } } HANDLE_SGX_EXCEPTION(result) @@ -1109,8 +1117,8 @@ SGXWalletServer::createBLSPrivateKeyV2(const string &blsKeyName, const string &e return createBLSPrivateKeyV2Impl(blsKeyName, ethKeyName, polyName, SecretShare, t, n); } -Json::Value SGXWalletServer::getDecryptionShare(const std::string& blsKeyName, const std::string& publicDecryptionValue) { - return getDecryptionShareImpl(blsKeyName, publicDecryptionValue); +Json::Value SGXWalletServer::getDecryptionShares(const std::string& blsKeyName, const Json::Value& publicDecryptionValues) { + return getDecryptionSharesImpl(blsKeyName, publicDecryptionValues); } shared_ptr SGXWalletServer::readFromDb(const string &name, const string &prefix) { diff --git a/SGXWalletServer.hpp b/SGXWalletServer.hpp index bcec3f59..c11c1af4 100644 --- a/SGXWalletServer.hpp +++ b/SGXWalletServer.hpp @@ -113,7 +113,7 @@ class SGXWalletServer : public AbstractStubServer { virtual Json::Value createBLSPrivateKeyV2(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n); - virtual Json::Value getDecryptionShare(const std::string& blsKeyName, const std::string& publicDecryptionValue); + virtual Json::Value getDecryptionShares(const std::string& blsKeyName, const Json::Value& publicDecryptionValues); static shared_ptr readFromDb(const string &name, const string &prefix = ""); @@ -173,7 +173,7 @@ class SGXWalletServer : public AbstractStubServer { static Json::Value createBLSPrivateKeyV2Impl(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n); - static Json::Value getDecryptionShareImpl(const std::string& KeyName, const std::string& publicDecryptionValue); + static Json::Value getDecryptionSharesImpl(const std::string& KeyName, const Json::Value& publicDecryptionValues); static void printDB(); diff --git a/abstractstubserver.h b/abstractstubserver.h index e6d3d0cf..1be018b7 100644 --- a/abstractstubserver.h +++ b/abstractstubserver.h @@ -63,7 +63,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer this->bindAndAddMethod(jsonrpc::Procedure("dkgVerificationV2", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "publicShares",jsonrpc::JSON_STRING, "ethKeyName",jsonrpc::JSON_STRING, "secretShare",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, "n",jsonrpc::JSON_INTEGER, "index",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::dkgVerificationV2I); this->bindAndAddMethod(jsonrpc::Procedure("createBLSPrivateKeyV2", 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::createBLSPrivateKeyV2I); - this->bindAndAddMethod(jsonrpc::Procedure("getDecryptionShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING,"publicDecryptionValue",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getDecryptionShareI); + this->bindAndAddMethod(jsonrpc::Procedure("getDecryptionShares", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING,"publicDecryptionValues",jsonrpc::JSON_ARRAY, NULL), &AbstractStubServer::getDecryptionSharesI); } inline virtual void importBLSKeyShareI(const Json::Value &request, Json::Value &response) @@ -163,9 +163,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer response = this->createBLSPrivateKeyV2(request["blsKeyName"].asString(), request["ethKeyName"].asString(), request["polyName"].asString(),request["secretShare"].asString(),request["t"].asInt(), request["n"].asInt()); } - inline virtual void getDecryptionShareI(const Json::Value &request, Json::Value &response) + inline virtual void getDecryptionSharesI(const Json::Value &request, Json::Value &response) { - response = this->getDecryptionShare(request["blsKeyName"].asString(), request["publicDecryptionValue"].asString()); + response = this->getDecryptionShares(request["blsKeyName"].asString(), request["publicDecryptionValues"]); } virtual Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName) = 0; @@ -194,7 +194,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer virtual Json::Value dkgVerificationV2( const std::string& publicShares, const std::string& ethKeyName, const std::string& SecretShare, int t, int n, int index) = 0; virtual Json::Value createBLSPrivateKeyV2(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n) = 0; - virtual Json::Value getDecryptionShare(const std::string& KeyName, const std::string& publicDecryptionValue) = 0; + virtual Json::Value getDecryptionShares(const std::string& KeyName, const Json::Value& publicDecryptionValues) = 0; }; #endif //JSONRPC_CPP_STUB_ABSTRACTSTUBSERVER_H_ diff --git a/stubclient.h b/stubclient.h index b2334ed5..7a5cf703 100644 --- a/stubclient.h +++ b/stubclient.h @@ -214,13 +214,13 @@ class StubClient : public jsonrpc::Client throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value getDecryptionShare(const std::string& blsKeyName, const std::string& publicDecryptionValue) + Json::Value getDecryptionShares(const std::string& blsKeyName, const Json::Value& publicDecryptionValues) { Json::Value p; p["blsKeyName"] = blsKeyName; - p["publicDecryptionValue"] = publicDecryptionValue; + p["publicDecryptionValues"] = publicDecryptionValues["publicDecryptionValues"]; - Json::Value result = this->CallMethod("getDecryptionShare",p); + Json::Value result = this->CallMethod("getDecryptionShares",p); if (result.isObject()) return result; else diff --git a/testw.cpp b/testw.cpp index 005791bb..7f240561 100644 --- a/testw.cpp +++ b/testw.cpp @@ -1248,21 +1248,43 @@ TEST_CASE_METHOD(TestFixture, "Test decryption share for threshold encryption", libff::alt_bn128_Fr key = libff::alt_bn128_Fr( "6507625568967977077291849236396320012317305261598035438182864059942098934847"); - libff::alt_bn128_G2 decryption_value = libff::alt_bn128_G2::random_element(); - decryption_value.to_affine_coordinates(); + libff::alt_bn128_G2 decryption_value1 = libff::alt_bn128_G2::random_element(); + libff::alt_bn128_G2 decryption_value2 = libff::alt_bn128_G2::random_element(); - auto decrytion_value_str = convertG2ToString( decryption_value, ':' ); - auto decryption_share = c.getDecryptionShare( name, decrytion_value_str )["decryptionShare"]; + decryption_value1.to_affine_coordinates(); + decryption_value2.to_affine_coordinates(); - libff::alt_bn128_G2 share; - share.Z = libff::alt_bn128_Fq2::one(); + auto decrytion_value_str1 = convertG2ToString( decryption_value1, ':' ); + auto decrytion_value_str2 = convertG2ToString( decryption_value2, ':' ); - share.X.c0 = libff::alt_bn128_Fq( decryption_share[0].asCString() ); - share.X.c1 = libff::alt_bn128_Fq( decryption_share[1].asCString() ); - share.Y.c0 = libff::alt_bn128_Fq( decryption_share[2].asCString() ); - share.Y.c1 = libff::alt_bn128_Fq( decryption_share[3].asCString() ); + Json::Value publicDecryptionValues; + publicDecryptionValues["publicDecryptionValues"][0] = decrytion_value_str1; + publicDecryptionValues["publicDecryptionValues"][1] = decrytion_value_str2; - REQUIRE( share == key * decryption_value ); + auto decryptionShares = c.getDecryptionShares( name, publicDecryptionValues ); + + auto decryption_share1 = decryptionShares["decryptionShares"][0]; + auto decryption_share2 = decryptionShares["decryptionShares"][1]; + + libff::alt_bn128_G2 share1; + share1.Z = libff::alt_bn128_Fq2::one(); + + share1.X.c0 = libff::alt_bn128_Fq( decryption_share1[0].asCString() ); + share1.X.c1 = libff::alt_bn128_Fq( decryption_share1[1].asCString() ); + share1.Y.c0 = libff::alt_bn128_Fq( decryption_share1[2].asCString() ); + share1.Y.c1 = libff::alt_bn128_Fq( decryption_share1[3].asCString() ); + + REQUIRE( share1 == key * decryption_value1 ); + + libff::alt_bn128_G2 share2; + share2.Z = libff::alt_bn128_Fq2::one(); + + share2.X.c0 = libff::alt_bn128_Fq( decryption_share2[0].asCString() ); + share2.X.c1 = libff::alt_bn128_Fq( decryption_share2[1].asCString() ); + share2.Y.c0 = libff::alt_bn128_Fq( decryption_share2[2].asCString() ); + share2.Y.c1 = libff::alt_bn128_Fq( decryption_share2[3].asCString() ); + + REQUIRE( share2 == key * decryption_value2 ); } TEST_CASE_METHOD(TestFixture, "Test decryption share for threshold encryption via zmq", "[te-decryption-share-zmq]") { @@ -1277,21 +1299,43 @@ TEST_CASE_METHOD(TestFixture, "Test decryption share for threshold encryption vi libff::alt_bn128_Fr key = libff::alt_bn128_Fr( "6507625568967977077291849236396320012317305261598035438182864059942098934847"); - libff::alt_bn128_G2 decryption_value = libff::alt_bn128_G2::random_element(); - decryption_value.to_affine_coordinates(); + libff::alt_bn128_G2 decryption_value1 = libff::alt_bn128_G2::random_element(); + libff::alt_bn128_G2 decryption_value2 = libff::alt_bn128_G2::random_element(); + + decryption_value1.to_affine_coordinates(); + decryption_value2.to_affine_coordinates(); + + auto decrytion_value_str1 = convertG2ToString( decryption_value1, ':' ); + auto decrytion_value_str2 = convertG2ToString( decryption_value2, ':' ); + + Json::Value publicDecryptionValues; + publicDecryptionValues["publicDecryptionValues"][0] = decrytion_value_str1; + publicDecryptionValues["publicDecryptionValues"][1] = decrytion_value_str2; + + auto decryptionShares = client->getDecryptionShares( name, publicDecryptionValues ); + + auto decryption_share1 = decryptionShares[0]; + auto decryption_share2 = decryptionShares[1]; + + libff::alt_bn128_G2 share1; + share1.Z = libff::alt_bn128_Fq2::one(); + + share1.X.c0 = libff::alt_bn128_Fq( decryption_share1[0].asCString() ); + share1.X.c1 = libff::alt_bn128_Fq( decryption_share1[1].asCString() ); + share1.Y.c0 = libff::alt_bn128_Fq( decryption_share1[2].asCString() ); + share1.Y.c1 = libff::alt_bn128_Fq( decryption_share1[3].asCString() ); - auto decrytion_value_str = convertG2ToString( decryption_value, ':' ); - auto decryption_share = client->getDecryptionShare( name, decrytion_value_str ); + REQUIRE( share1 == key * decryption_value1 ); - libff::alt_bn128_G2 share; - share.Z = libff::alt_bn128_Fq2::one(); + libff::alt_bn128_G2 share2; + share2.Z = libff::alt_bn128_Fq2::one(); - share.X.c0 = libff::alt_bn128_Fq( decryption_share[0].asCString() ); - share.X.c1 = libff::alt_bn128_Fq( decryption_share[1].asCString() ); - share.Y.c0 = libff::alt_bn128_Fq( decryption_share[2].asCString() ); - share.Y.c1 = libff::alt_bn128_Fq( decryption_share[3].asCString() ); + share2.X.c0 = libff::alt_bn128_Fq( decryption_share2[0].asCString() ); + share2.X.c1 = libff::alt_bn128_Fq( decryption_share2[1].asCString() ); + share2.Y.c0 = libff::alt_bn128_Fq( decryption_share2[2].asCString() ); + share2.Y.c1 = libff::alt_bn128_Fq( decryption_share2[3].asCString() ); - REQUIRE( share == key * decryption_value ); + REQUIRE( share2 == key * decryption_value2 ); } TEST_CASE_METHOD(TestFixtureZMQSign, "ZMQ-ecdsa", "[zmq-ecdsa]") { diff --git a/zmq_src/ReqMessage.cpp b/zmq_src/ReqMessage.cpp index 413d01eb..c174dd19 100644 --- a/zmq_src/ReqMessage.cpp +++ b/zmq_src/ReqMessage.cpp @@ -265,11 +265,11 @@ Json::Value deleteBLSKeyReqMessage::process() { Json::Value GetDecryptionShareReqMessage::process() { auto blsKeyName = getStringRapid("blsKeyName"); - auto publicDecryptionValue = getStringRapid("publicDecryptionValue"); + auto publicDecryptionValues = getJsonValueRapid("publicDecryptionValues"); if (checkKeyOwnership && !isKeyByOwner(blsKeyName, getStringRapid("cert"))) { throw std::invalid_argument("Only owner of the key can access it"); } - auto result = SGXWalletServer::getDecryptionShareImpl(blsKeyName, publicDecryptionValue); + auto result = SGXWalletServer::getDecryptionSharesImpl(blsKeyName, publicDecryptionValues); result["type"] = ZMQMessage::GET_DECRYPTION_SHARE_RSP; return result; } diff --git a/zmq_src/RspMessage.h b/zmq_src/RspMessage.h index cb7e045e..2d2c6c84 100644 --- a/zmq_src/RspMessage.h +++ b/zmq_src/RspMessage.h @@ -255,7 +255,7 @@ class GetDecryptionShareRspMessage : public ZMQMessage { virtual Json::Value process(); Json::Value getShare() { - return getJsonValueRapid("decryptionShare"); + return getJsonValueRapid("decryptionShares"); } }; diff --git a/zmq_src/ZMQClient.cpp b/zmq_src/ZMQClient.cpp index ae5353ff..40ab3c3b 100644 --- a/zmq_src/ZMQClient.cpp +++ b/zmq_src/ZMQClient.cpp @@ -496,11 +496,11 @@ bool ZMQClient::deleteBLSKey(const string& blsKeyName) { return result->isSuccessful(); } -Json::Value ZMQClient::getDecryptionShare(const string& blsKeyName, const string& publicDecryptionValue) { +Json::Value ZMQClient::getDecryptionShares(const string& blsKeyName, const Json::Value& publicDecryptionValues) { Json::Value p; p["type"] = ZMQMessage::GET_DECRYPTION_SHARE_REQ; p["blsKeyName"] = blsKeyName; - p["publicDecryptionValue"] = publicDecryptionValue; + p["publicDecryptionValues"] = publicDecryptionValues["publicDecryptionValues"]; auto result = dynamic_pointer_cast(doRequestReply(p)); CHECK_STATE(result); CHECK_STATE(result->getStatus() == 0); diff --git a/zmq_src/ZMQClient.h b/zmq_src/ZMQClient.h index 0e10f2dd..e06223d2 100644 --- a/zmq_src/ZMQClient.h +++ b/zmq_src/ZMQClient.h @@ -122,7 +122,7 @@ class ZMQClient { bool deleteBLSKey(const string& blsKeyName); - Json::Value getDecryptionShare(const string& blsKeyName, const string& publicDecryptionValue); + Json::Value getDecryptionShares(const string& blsKeyName, const Json::Value& publicDecryptionValues); };