Skip to content

Commit ebf576e

Browse files
committed
SKALE-4954 change getDecryptionShare to getDecryptionShares
1 parent 56e798c commit ebf576e

File tree

9 files changed

+98
-46
lines changed

9 files changed

+98
-46
lines changed

SGXWalletServer.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ SGXWalletServer::createBLSPrivateKeyV2Impl(const string &_blsKeyName, const stri
985985
RETURN_SUCCESS(result);
986986
}
987987

988-
Json::Value SGXWalletServer::getDecryptionShareImpl(const std::string& blsKeyName, const std::string& publicDecryptionValue) {
988+
Json::Value SGXWalletServer::getDecryptionSharesImpl(const std::string& blsKeyName, const Json::Value& publicDecryptionValues) {
989989
spdlog::info("Entering {}", __FUNCTION__);
990990
INIT_RESULT(result)
991991

@@ -994,15 +994,23 @@ Json::Value SGXWalletServer::getDecryptionShareImpl(const std::string& blsKeyNam
994994
throw SGXException(BLS_SIGN_INVALID_KS_NAME, string(__FUNCTION__) + ":Invalid BLSKey name");
995995
}
996996

997-
if ( publicDecryptionValue.length() < 7 || publicDecryptionValue.length() > 78 * 4 ) {
998-
throw SGXException(INVALID_DECRYPTION_VALUE_FORMAT, string(__FUNCTION__) + ":Invalid publicDecryptionValue format");
997+
if (!publicDecryptionValues.isArray()) {
998+
throw SGXException(INVALID_DECRYPTION_VALUE_FORMAT,
999+
string(__FUNCTION__) + ":Public decryption values should be an array");
9991000
}
10001001

1001-
shared_ptr<string> encryptedKeyHex_ptr = readFromDb(blsKeyName);
1002+
for (int i = 0; i < publicDecryptionValues.size(); ++i) {
1003+
std::string publicDecryptionValue = publicDecryptionValues[i].asString();
1004+
if ( publicDecryptionValue.length() < 7 || publicDecryptionValue.length() > 78 * 4 ) {
1005+
throw SGXException(INVALID_DECRYPTION_VALUE_FORMAT, string(__FUNCTION__) + ":Invalid publicDecryptionValue format");
1006+
}
1007+
1008+
shared_ptr<string> encryptedKeyHex_ptr = readFromDb(blsKeyName);
10021009

1003-
vector<string> decryptionValueVector = calculateDecryptionShare(encryptedKeyHex_ptr->c_str(), publicDecryptionValue);
1004-
for (uint8_t i = 0; i < 4; ++i) {
1005-
result["decryptionShare"][i] = decryptionValueVector.at(i);
1010+
vector<string> decryptionValueVector = calculateDecryptionShare(encryptedKeyHex_ptr->c_str(), publicDecryptionValue);
1011+
for (uint8_t j = 0; j < 4; ++j) {
1012+
result["decryptionShares"][i][j] = decryptionValueVector.at(j);
1013+
}
10061014
}
10071015
} HANDLE_SGX_EXCEPTION(result)
10081016

@@ -1109,8 +1117,8 @@ SGXWalletServer::createBLSPrivateKeyV2(const string &blsKeyName, const string &e
11091117
return createBLSPrivateKeyV2Impl(blsKeyName, ethKeyName, polyName, SecretShare, t, n);
11101118
}
11111119

1112-
Json::Value SGXWalletServer::getDecryptionShare(const std::string& blsKeyName, const std::string& publicDecryptionValue) {
1113-
return getDecryptionShareImpl(blsKeyName, publicDecryptionValue);
1120+
Json::Value SGXWalletServer::getDecryptionShares(const std::string& blsKeyName, const Json::Value& publicDecryptionValues) {
1121+
return getDecryptionSharesImpl(blsKeyName, publicDecryptionValues);
11141122
}
11151123

11161124
shared_ptr <string> SGXWalletServer::readFromDb(const string &name, const string &prefix) {

SGXWalletServer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class SGXWalletServer : public AbstractStubServer {
113113

114114
virtual Json::Value createBLSPrivateKeyV2(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
115115

116-
virtual Json::Value getDecryptionShare(const std::string& blsKeyName, const std::string& publicDecryptionValue);
116+
virtual Json::Value getDecryptionShares(const std::string& blsKeyName, const Json::Value& publicDecryptionValues);
117117

118118
static shared_ptr<string> readFromDb(const string &name, const string &prefix = "");
119119

@@ -173,7 +173,7 @@ class SGXWalletServer : public AbstractStubServer {
173173

174174
static Json::Value createBLSPrivateKeyV2Impl(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
175175

176-
static Json::Value getDecryptionShareImpl(const std::string& KeyName, const std::string& publicDecryptionValue);
176+
static Json::Value getDecryptionSharesImpl(const std::string& KeyName, const Json::Value& publicDecryptionValues);
177177

178178
static void printDB();
179179

abstractstubserver.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
6363
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);
6464
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);
6565

66-
this->bindAndAddMethod(jsonrpc::Procedure("getDecryptionShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING,"publicDecryptionValue",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getDecryptionShareI);
66+
this->bindAndAddMethod(jsonrpc::Procedure("getDecryptionShares", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING,"publicDecryptionValues",jsonrpc::JSON_ARRAY, NULL), &AbstractStubServer::getDecryptionSharesI);
6767
}
6868

6969
inline virtual void importBLSKeyShareI(const Json::Value &request, Json::Value &response)
@@ -163,9 +163,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
163163
response = this->createBLSPrivateKeyV2(request["blsKeyName"].asString(), request["ethKeyName"].asString(), request["polyName"].asString(),request["secretShare"].asString(),request["t"].asInt(), request["n"].asInt());
164164
}
165165

166-
inline virtual void getDecryptionShareI(const Json::Value &request, Json::Value &response)
166+
inline virtual void getDecryptionSharesI(const Json::Value &request, Json::Value &response)
167167
{
168-
response = this->getDecryptionShare(request["blsKeyName"].asString(), request["publicDecryptionValue"].asString());
168+
response = this->getDecryptionShares(request["blsKeyName"].asString(), request["publicDecryptionValues"]);
169169
}
170170

171171
virtual Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName) = 0;
@@ -194,7 +194,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
194194
virtual Json::Value dkgVerificationV2( const std::string& publicShares, const std::string& ethKeyName, const std::string& SecretShare, int t, int n, int index) = 0;
195195
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;
196196

197-
virtual Json::Value getDecryptionShare(const std::string& KeyName, const std::string& publicDecryptionValue) = 0;
197+
virtual Json::Value getDecryptionShares(const std::string& KeyName, const Json::Value& publicDecryptionValues) = 0;
198198
};
199199

200200
#endif //JSONRPC_CPP_STUB_ABSTRACTSTUBSERVER_H_

stubclient.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ class StubClient : public jsonrpc::Client
214214
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
215215
}
216216

217-
Json::Value getDecryptionShare(const std::string& blsKeyName, const std::string& publicDecryptionValue)
217+
Json::Value getDecryptionShares(const std::string& blsKeyName, const Json::Value& publicDecryptionValues)
218218
{
219219
Json::Value p;
220220
p["blsKeyName"] = blsKeyName;
221-
p["publicDecryptionValue"] = publicDecryptionValue;
221+
p["publicDecryptionValues"] = publicDecryptionValues["publicDecryptionValues"];
222222

223-
Json::Value result = this->CallMethod("getDecryptionShare",p);
223+
Json::Value result = this->CallMethod("getDecryptionShares",p);
224224
if (result.isObject())
225225
return result;
226226
else

testw.cpp

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,21 +1248,43 @@ TEST_CASE_METHOD(TestFixture, "Test decryption share for threshold encryption",
12481248
libff::alt_bn128_Fr key = libff::alt_bn128_Fr(
12491249
"6507625568967977077291849236396320012317305261598035438182864059942098934847");
12501250

1251-
libff::alt_bn128_G2 decryption_value = libff::alt_bn128_G2::random_element();
1252-
decryption_value.to_affine_coordinates();
1251+
libff::alt_bn128_G2 decryption_value1 = libff::alt_bn128_G2::random_element();
1252+
libff::alt_bn128_G2 decryption_value2 = libff::alt_bn128_G2::random_element();
12531253

1254-
auto decrytion_value_str = convertG2ToString( decryption_value, ':' );
1255-
auto decryption_share = c.getDecryptionShare( name, decrytion_value_str )["decryptionShare"];
1254+
decryption_value1.to_affine_coordinates();
1255+
decryption_value2.to_affine_coordinates();
12561256

1257-
libff::alt_bn128_G2 share;
1258-
share.Z = libff::alt_bn128_Fq2::one();
1257+
auto decrytion_value_str1 = convertG2ToString( decryption_value1, ':' );
1258+
auto decrytion_value_str2 = convertG2ToString( decryption_value2, ':' );
12591259

1260-
share.X.c0 = libff::alt_bn128_Fq( decryption_share[0].asCString() );
1261-
share.X.c1 = libff::alt_bn128_Fq( decryption_share[1].asCString() );
1262-
share.Y.c0 = libff::alt_bn128_Fq( decryption_share[2].asCString() );
1263-
share.Y.c1 = libff::alt_bn128_Fq( decryption_share[3].asCString() );
1260+
Json::Value publicDecryptionValues;
1261+
publicDecryptionValues["publicDecryptionValues"][0] = decrytion_value_str1;
1262+
publicDecryptionValues["publicDecryptionValues"][1] = decrytion_value_str2;
12641263

1265-
REQUIRE( share == key * decryption_value );
1264+
auto decryptionShares = c.getDecryptionShares( name, publicDecryptionValues );
1265+
1266+
auto decryption_share1 = decryptionShares["decryptionShares"][0];
1267+
auto decryption_share2 = decryptionShares["decryptionShares"][1];
1268+
1269+
libff::alt_bn128_G2 share1;
1270+
share1.Z = libff::alt_bn128_Fq2::one();
1271+
1272+
share1.X.c0 = libff::alt_bn128_Fq( decryption_share1[0].asCString() );
1273+
share1.X.c1 = libff::alt_bn128_Fq( decryption_share1[1].asCString() );
1274+
share1.Y.c0 = libff::alt_bn128_Fq( decryption_share1[2].asCString() );
1275+
share1.Y.c1 = libff::alt_bn128_Fq( decryption_share1[3].asCString() );
1276+
1277+
REQUIRE( share1 == key * decryption_value1 );
1278+
1279+
libff::alt_bn128_G2 share2;
1280+
share2.Z = libff::alt_bn128_Fq2::one();
1281+
1282+
share2.X.c0 = libff::alt_bn128_Fq( decryption_share2[0].asCString() );
1283+
share2.X.c1 = libff::alt_bn128_Fq( decryption_share2[1].asCString() );
1284+
share2.Y.c0 = libff::alt_bn128_Fq( decryption_share2[2].asCString() );
1285+
share2.Y.c1 = libff::alt_bn128_Fq( decryption_share2[3].asCString() );
1286+
1287+
REQUIRE( share2 == key * decryption_value2 );
12661288
}
12671289

12681290
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
12771299
libff::alt_bn128_Fr key = libff::alt_bn128_Fr(
12781300
"6507625568967977077291849236396320012317305261598035438182864059942098934847");
12791301

1280-
libff::alt_bn128_G2 decryption_value = libff::alt_bn128_G2::random_element();
1281-
decryption_value.to_affine_coordinates();
1302+
libff::alt_bn128_G2 decryption_value1 = libff::alt_bn128_G2::random_element();
1303+
libff::alt_bn128_G2 decryption_value2 = libff::alt_bn128_G2::random_element();
1304+
1305+
decryption_value1.to_affine_coordinates();
1306+
decryption_value2.to_affine_coordinates();
1307+
1308+
auto decrytion_value_str1 = convertG2ToString( decryption_value1, ':' );
1309+
auto decrytion_value_str2 = convertG2ToString( decryption_value2, ':' );
1310+
1311+
Json::Value publicDecryptionValues;
1312+
publicDecryptionValues["publicDecryptionValues"][0] = decrytion_value_str1;
1313+
publicDecryptionValues["publicDecryptionValues"][1] = decrytion_value_str2;
1314+
1315+
auto decryptionShares = client->getDecryptionShares( name, publicDecryptionValues );
1316+
1317+
auto decryption_share1 = decryptionShares[0];
1318+
auto decryption_share2 = decryptionShares[1];
1319+
1320+
libff::alt_bn128_G2 share1;
1321+
share1.Z = libff::alt_bn128_Fq2::one();
1322+
1323+
share1.X.c0 = libff::alt_bn128_Fq( decryption_share1[0].asCString() );
1324+
share1.X.c1 = libff::alt_bn128_Fq( decryption_share1[1].asCString() );
1325+
share1.Y.c0 = libff::alt_bn128_Fq( decryption_share1[2].asCString() );
1326+
share1.Y.c1 = libff::alt_bn128_Fq( decryption_share1[3].asCString() );
12821327

1283-
auto decrytion_value_str = convertG2ToString( decryption_value, ':' );
1284-
auto decryption_share = client->getDecryptionShare( name, decrytion_value_str );
1328+
REQUIRE( share1 == key * decryption_value1 );
12851329

1286-
libff::alt_bn128_G2 share;
1287-
share.Z = libff::alt_bn128_Fq2::one();
1330+
libff::alt_bn128_G2 share2;
1331+
share2.Z = libff::alt_bn128_Fq2::one();
12881332

1289-
share.X.c0 = libff::alt_bn128_Fq( decryption_share[0].asCString() );
1290-
share.X.c1 = libff::alt_bn128_Fq( decryption_share[1].asCString() );
1291-
share.Y.c0 = libff::alt_bn128_Fq( decryption_share[2].asCString() );
1292-
share.Y.c1 = libff::alt_bn128_Fq( decryption_share[3].asCString() );
1333+
share2.X.c0 = libff::alt_bn128_Fq( decryption_share2[0].asCString() );
1334+
share2.X.c1 = libff::alt_bn128_Fq( decryption_share2[1].asCString() );
1335+
share2.Y.c0 = libff::alt_bn128_Fq( decryption_share2[2].asCString() );
1336+
share2.Y.c1 = libff::alt_bn128_Fq( decryption_share2[3].asCString() );
12931337

1294-
REQUIRE( share == key * decryption_value );
1338+
REQUIRE( share2 == key * decryption_value2 );
12951339
}
12961340

12971341
TEST_CASE_METHOD(TestFixtureZMQSign, "ZMQ-ecdsa", "[zmq-ecdsa]") {

zmq_src/ReqMessage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ Json::Value deleteBLSKeyReqMessage::process() {
265265

266266
Json::Value GetDecryptionShareReqMessage::process() {
267267
auto blsKeyName = getStringRapid("blsKeyName");
268-
auto publicDecryptionValue = getStringRapid("publicDecryptionValue");
268+
auto publicDecryptionValues = getJsonValueRapid("publicDecryptionValues");
269269
if (checkKeyOwnership && !isKeyByOwner(blsKeyName, getStringRapid("cert"))) {
270270
throw std::invalid_argument("Only owner of the key can access it");
271271
}
272-
auto result = SGXWalletServer::getDecryptionShareImpl(blsKeyName, publicDecryptionValue);
272+
auto result = SGXWalletServer::getDecryptionSharesImpl(blsKeyName, publicDecryptionValues);
273273
result["type"] = ZMQMessage::GET_DECRYPTION_SHARE_RSP;
274274
return result;
275275
}

zmq_src/RspMessage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class GetDecryptionShareRspMessage : public ZMQMessage {
255255
virtual Json::Value process();
256256

257257
Json::Value getShare() {
258-
return getJsonValueRapid("decryptionShare");
258+
return getJsonValueRapid("decryptionShares");
259259
}
260260
};
261261

zmq_src/ZMQClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,11 @@ bool ZMQClient::deleteBLSKey(const string& blsKeyName) {
496496
return result->isSuccessful();
497497
}
498498

499-
Json::Value ZMQClient::getDecryptionShare(const string& blsKeyName, const string& publicDecryptionValue) {
499+
Json::Value ZMQClient::getDecryptionShares(const string& blsKeyName, const Json::Value& publicDecryptionValues) {
500500
Json::Value p;
501501
p["type"] = ZMQMessage::GET_DECRYPTION_SHARE_REQ;
502502
p["blsKeyName"] = blsKeyName;
503-
p["publicDecryptionValue"] = publicDecryptionValue;
503+
p["publicDecryptionValues"] = publicDecryptionValues["publicDecryptionValues"];
504504
auto result = dynamic_pointer_cast<GetDecryptionShareRspMessage>(doRequestReply(p));
505505
CHECK_STATE(result);
506506
CHECK_STATE(result->getStatus() == 0);

zmq_src/ZMQClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class ZMQClient {
122122

123123
bool deleteBLSKey(const string& blsKeyName);
124124

125-
Json::Value getDecryptionShare(const string& blsKeyName, const string& publicDecryptionValue);
125+
Json::Value getDecryptionShares(const string& blsKeyName, const Json::Value& publicDecryptionValues);
126126
};
127127

128128

0 commit comments

Comments
 (0)