Skip to content

Commit

Permalink
Merge branch 'develop' into bug/fix-0x-bls-sign
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev authored Feb 21, 2022
2 parents efbef35 + 505be9b commit b751034
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 50 deletions.
26 changes: 17 additions & 9 deletions SGXWalletServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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<string> 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<string> encryptedKeyHex_ptr = readFromDb(blsKeyName);

vector<string> decryptionValueVector = calculateDecryptionShare(encryptedKeyHex_ptr->c_str(), publicDecryptionValue);
for (uint8_t i = 0; i < 4; ++i) {
result["decryptionShare"][i] = decryptionValueVector.at(i);
vector<string> 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)

Expand Down Expand Up @@ -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 <string> SGXWalletServer::readFromDb(const string &name, const string &prefix) {
Expand Down
4 changes: 2 additions & 2 deletions SGXWalletServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> readFromDb(const string &name, const string &prefix = "");

Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions abstractstubserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
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)
Expand Down Expand Up @@ -163,9 +163,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
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;
Expand Down Expand Up @@ -194,7 +194,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
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_
2 changes: 1 addition & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ string __ERR_STRING__ = string("SGX enclave call to ") + \
__FUNCTION__ + " failed with status:" \
+ to_string(__STATUS__) + \
" Err message:" + __ERR_MSG__; \
BOOST_THROW_EXCEPTION(SGXException(-102, string(__ERR_MSG__))); \
BOOST_THROW_EXCEPTION(SGXException(-102, string(__ERR_STRING__))); \
}\
\
if (__ERR_STATUS__ != 0) {\
Expand Down
6 changes: 3 additions & 3 deletions secure_enclave/secure_enclave.config.xml.sim
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<ISVSVN>3</ISVSVN>
<StackMaxSize>0x200000</StackMaxSize>
<HeapMaxSize>0x200000</HeapMaxSize>
<TCSNum>20</TCSNum>
<TCSMaxNum>20</TCSMaxNum>
<TCSMinPool>20</TCSMinPool>
<TCSNum>25</TCSNum>
<TCSMaxNum>25</TCSMaxNum>
<TCSMinPool>25</TCSMinPool>
<TCSPolicy>0</TCSPolicy>
<!-- Recommend changing 'DisableDebug' to 1 to make the enclave undebuggable for enclave release -->
<DisableDebug>0</DisableDebug>
Expand Down
6 changes: 3 additions & 3 deletions stubclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 66 additions & 22 deletions testw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]") {
Expand All @@ -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]") {
Expand Down
4 changes: 2 additions & 2 deletions zmq_src/ReqMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion zmq_src/RspMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class GetDecryptionShareRspMessage : public ZMQMessage {
virtual Json::Value process();

Json::Value getShare() {
return getJsonValueRapid("decryptionShare");
return getJsonValueRapid("decryptionShares");
}
};

Expand Down
4 changes: 2 additions & 2 deletions zmq_src/ZMQClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetDecryptionShareRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
Expand Down
2 changes: 1 addition & 1 deletion zmq_src/ZMQClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
2 changes: 2 additions & 0 deletions zmq_src/ZMQServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pair <string, shared_ptr<zmq::message_t>> ZMQServer::receiveMessage() {
}

auto result = string((char *) reqMsg->data(), reqMsg->size());
spdlog::debug("Received request via ZMQ server: {}", result);

return {result, identity};
}
Expand All @@ -255,6 +256,7 @@ void ZMQServer::sendToClient(Json::Value &_result, shared_ptr <zmq::message_t> &
if (!s_send(*socket, replyStr)) {
exit(-16);
}
spdlog::debug("Send response to client: {}", replyStr);
} catch (ExitRequestedException) {
throw;
} catch (exception &e) {
Expand Down

0 comments on commit b751034

Please sign in to comment.