SKALE-2892 calculate bls public keys on sgx

parent 069d19c0
......@@ -318,6 +318,73 @@ vector<string> GetBLSPubKey(const char *encryptedKeyHex) {
return pubKeyVect;
}
string convertHexToDec(const string& hex_str) {
mpz_t dec;
mpz_init(dec);
string ret = "";
try {
if (mpz_set_str(dec, hex_str.c_str(), 16) == -1) {
mpz_clear(dec);
return ret;
}
char arr[mpz_sizeinbase(dec, 10) + 2];
char *result = mpz_get_str(arr, 10, dec);
ret = result;
} catch (exception &e) {
throw SGXException(INCORRECT_STRING_CONVERSION, e.what());
mpz_clear(dec);
return ret;
} catch (...) {
throw SGXException(UNKNOWN_ERROR, "");
mpz_clear(dec);
return ret;
}
return ret;
}
vector<string> calculateAllBlsPublicKeys(const vector<string>& public_shares) {
size_t n = public_shares.size();
size_t t = public_shares[0].length() / 256;
uint64_t share_length = 256;
uint8_t coord_length = 64;
vector<string> result(n);
vector<libff::alt_bn128_G2> public_keys(n, libff::alt_bn128_G2::zero());
vector<libff::alt_bn128_G2> public_values(n, libff::alt_bn128_G2::zero());
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < t; ++j) {
libff::alt_bn128_G2 public_share;
uint64_t pos0 = share_length * i;
string x_c0_str = convertHexToDec(public_shares[i].substr(pos0, coord_length));
string x_c1_str = convertHexToDec(public_shares[i].substr(pos0 + coord_length, coord_length));
string y_c0_str = convertHexToDec(public_shares[i].substr(pos0 + 2 * coord_length, coord_length));
string y_c1_str = convertHexToDec(public_shares[i].substr(pos0 + 3 * coord_length, coord_length));
public_share.X.c0 = libff::alt_bn128_Fq(x_c0_str.c_str());
public_share.X.c1 = libff::alt_bn128_Fq(x_c1_str.c_str());
public_share.Y.c0 = libff::alt_bn128_Fq(y_c0_str.c_str());
public_share.Y.c1 = libff::alt_bn128_Fq(y_c1_str.c_str());
public_share.Z = libff::alt_bn128_Fq2::one();
public_values[i] = public_values[i] + public_share;
}
}
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
public_keys[i] = public_keys[i] + libff::power(libff::alt_bn128_Fr(j + 1), i) * public_values[j];
}
}
return result;
}
string decryptDHKey(const string &polyName, int ind) {
vector<char> errMsg1(1024, 0);
int errStatus = 0;
......
......@@ -47,6 +47,10 @@ vector<string> GetBLSPubKey(const char * encryptedKeyHex);
vector<string> mult_G2(const string& x);
string convertHexToDec(const string& hex_str);
vector<string> calculateAllBlsPublicKeys(const vector<string>& public_shares);
bool TestCreateBLSShare( const char * s_shares);
#endif //SGXD_DKGCRYPTO_H
......@@ -520,6 +520,45 @@ Json::Value SGXWalletServer::getBLSPublicKeyShareImpl(const string &_blsKeyName)
RETURN_SUCCESS(result);
}
Json::Value calculateAllBLSPublicKeysImpl(const Json::Value& publicShares, int t, int n) {
spdlog::info("Entering {}", __FUNCTION__);
INIT_RESULT(result)
try {
if (!check_n_t(t, n)) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid DKG parameters: n or t ");
}
if (!publicShares.isArray()) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid public shares format");
}
if (publicShares.size() != (uint64_t) n) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid length of public shares");
}
for (int i = 0; i < n; ++i) {
if (!publicShares[i].isString()) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid public shares parts format");
}
if (publicShares[i].asString().length() != (uint64_t) 256 * t) {
throw SGXException(INVALID_DKG_PARAMS, "Invalid length of public shares parts");
}
}
vector<string> public_shares(n);
for (int i = 0; i < n; ++i) {
public_shares[i] = publicShares[i].asString();
}
vector<string> public_keys = calculateAllBlsPublicKeys(public_shares);
for (int i = 0; i < n; ++i) {
result["publicKeys"][i] = public_keys[i];
}
} HANDLE_SGX_EXCEPTION(result)
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int _ind) {
spdlog::info("Entering {}", __FUNCTION__);
INIT_RESULT(result)
......@@ -634,6 +673,10 @@ Json::Value SGXWalletServer::getBLSPublicKeyShare(const string &blsKeyName) {
return getBLSPublicKeyShareImpl(blsKeyName);
}
Json::Value calculateAllBLSPublicKeys(const Json::Value& publicShares, int t, int n) {
return calculateAllBLSPublicKeysImpl(publicShares, t, n);
}
Json::Value SGXWalletServer::generateECDSAKey() {
return generateECDSAKeyImpl();
}
......
......@@ -74,6 +74,8 @@ public:
virtual Json::Value getBLSPublicKeyShare(const string &blsKeyName);
virtual Json::Value calculateAllBLSPublicKeys(const Json::Value& publicShares, int t, int n);
virtual Json::Value complaintResponse(const string &polyName, int ind);
virtual Json::Value multG2(const string &x);
......@@ -120,6 +122,8 @@ public:
static Json::Value getBLSPublicKeyShareImpl(const string &_blsKeyName);
static Json::Value calculateAllBLSPublicKeysImpl(const Json::Value& publicShares, int t, int n);
static Json::Value complaintResponseImpl(const string &_polyName, int _ind);
static Json::Value multG2Impl(const string &_x);
......
......@@ -44,11 +44,12 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
this->bindAndAddMethod(jsonrpc::Procedure("ecdsaSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "base",jsonrpc::JSON_INTEGER,"keyName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::ecdsaSignMessageHashI);
this->bindAndAddMethod(jsonrpc::Procedure("generateDKGPoly", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::generateDKGPolyI);
this->bindAndAddMethod(jsonrpc::Procedure("getVerificationVector", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"polyName",jsonrpc::JSON_STRING, "t",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getVerificationVectorI);
this->bindAndAddMethod(jsonrpc::Procedure("getVerificationVector", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"polyName",jsonrpc::JSON_STRING, "t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getVerificationVectorI);
this->bindAndAddMethod(jsonrpc::Procedure("getSecretShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"publicKeys",jsonrpc::JSON_ARRAY, "n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getSecretShareI);
this->bindAndAddMethod(jsonrpc::Procedure("dkgVerification", 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::dkgVerificationI);
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("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);
......@@ -105,6 +106,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
{
response = this->getBLSPublicKeyShare(request["blsKeyName"].asString());
}
inline virtual void calculateAllBLSPublicKeysI(const Json::Value& request, Json::Value& response) {
response = this->calculateAllBLSPublicKeys(request["publicShares"], request["t"].asInt(), request["n"].asInt());
}
inline virtual void complaintResponseI(const Json::Value &request, Json::Value &response)
{
response = this->complaintResponse( request["polyName"].asString(), request["ind"].asInt());
......@@ -147,6 +151,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value dkgVerification( const std::string& publicShares, const std::string& ethKeyName, const std::string& SecretShare, int t, int n, int index) = 0;
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 multG2(const std::string & x) = 0;
virtual Json::Value isPolyExists(const std::string& polyName) = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment