SKALE-2892 fix tests

parent a711eb3a
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
#include "SGXWalletServer.hpp" #include "SGXWalletServer.hpp"
#include "SGXException.h" #include "SGXException.h"
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include "third_party/spdlog/spdlog.h" #include "third_party/spdlog/spdlog.h"
#include "common.h" #include "common.h"
...@@ -73,6 +71,57 @@ template<class T> string ConvertToString(T field_elem, int base = 10) { ...@@ -73,6 +71,57 @@ template<class T> string ConvertToString(T field_elem, int base = 10) {
return output; return output;
} }
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) {
mpz_clear(dec);
throw SGXException(INCORRECT_STRING_CONVERSION, e.what());
} catch (...) {
mpz_clear(dec);
throw SGXException(UNKNOWN_ERROR, "");
}
return ret;
}
string convertG2ToString(const libff::alt_bn128_G2& elem, int base, const string& delim) {
string result = "";
try {
result += ConvertToString(elem.X.c0);
result += delim;
result += ConvertToString(elem.X.c1);
result += delim;
result += ConvertToString(elem.Y.c0);
result += delim;
result += ConvertToString(elem.Y.c1);
return result;
} catch (exception &e) {
throw SGXException(INCORRECT_STRING_CONVERSION, e.what());
return result;
} catch (...) {
throw SGXException(UNKNOWN_ERROR, "");
return result;
}
return result;
}
string gen_dkg_poly(int _t) { string gen_dkg_poly(int _t) {
vector<char> errMsg(1024, 0); vector<char> errMsg(1024, 0);
int errStatus = 0; int errStatus = 0;
...@@ -318,66 +367,50 @@ vector<string> GetBLSPubKey(const char *encryptedKeyHex) { ...@@ -318,66 +367,50 @@ vector<string> GetBLSPubKey(const char *encryptedKeyHex) {
return pubKeyVect; 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) {
mpz_clear(dec);
throw SGXException(INCORRECT_STRING_CONVERSION, e.what());
} catch (...) {
mpz_clear(dec);
throw SGXException(UNKNOWN_ERROR, "");
}
return ret;
}
vector<string> calculateAllBlsPublicKeys(const vector<string>& public_shares) { vector<string> calculateAllBlsPublicKeys(const vector<string>& public_shares) {
size_t n = public_shares.size(); size_t n = public_shares.size();
size_t t = public_shares[0].length() / 256; size_t t = public_shares[0].length() / 256;
uint64_t share_length = 256; uint64_t share_length = 256;
uint8_t coord_length = 64; 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_keys(n, libff::alt_bn128_G2::zero());
vector<libff::alt_bn128_G2> public_values(n, libff::alt_bn128_G2::zero()); vector<libff::alt_bn128_G2> public_values(t, libff::alt_bn128_G2::zero());
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < t; ++j) { for (size_t j = 0; j < t; ++j) {
libff::alt_bn128_G2 public_share; libff::alt_bn128_G2 public_share;
uint64_t pos0 = share_length * i; uint64_t pos0 = share_length * j;
string x_c0_str = convertHexToDec(public_shares[i].substr(pos0, coord_length)); 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 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_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)); string y_c1_str = convertHexToDec(public_shares[i].substr(pos0 + 3 * coord_length, coord_length));
if (x_c0_str == "" || x_c1_str == "" || y_c0_str == "" || y_c1_str == "") {
return {};
}
public_share.X.c0 = libff::alt_bn128_Fq(x_c0_str.c_str()); 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.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.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.Y.c1 = libff::alt_bn128_Fq(y_c1_str.c_str());
public_share.Z = libff::alt_bn128_Fq2::one(); public_share.Z = libff::alt_bn128_Fq2::one();
public_values[i] = public_values[i] + public_share; public_values[j] = public_values[j] + public_share;
} }
} }
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) { for (size_t j = 0; j < t; ++j) {
public_keys[i] = public_keys[i] + libff::power(libff::alt_bn128_Fr(j + 1), i) * public_values[j]; public_keys[i] = public_keys[i] + libff::power(libff::alt_bn128_Fr(i + 1), j) * public_values[j];
} }
public_keys[i].to_affine_coordinates();
}
vector<string> result(n);
for (size_t i = 0; i < n; ++i) {
result[i] = convertG2ToString(public_keys[i]);
} }
return result; return result;
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
using namespace std; using namespace std;
string gen_dkg_poly( int _t); string gen_dkg_poly( int _t);
...@@ -49,6 +51,8 @@ vector<string> mult_G2(const string& x); ...@@ -49,6 +51,8 @@ vector<string> mult_G2(const string& x);
string convertHexToDec(const string& hex_str); string convertHexToDec(const string& hex_str);
string convertG2ToString(const libff::alt_bn128_G2& elem, int base = 10, const string& delim = ":");
vector<string> calculateAllBlsPublicKeys(const vector<string>& public_shares); vector<string> calculateAllBlsPublicKeys(const vector<string>& public_shares);
bool TestCreateBLSShare( const char * s_shares); bool TestCreateBLSShare( const char * s_shares);
......
...@@ -546,11 +546,18 @@ Json::Value SGXWalletServer::calculateAllBLSPublicKeysImpl(const Json::Value& pu ...@@ -546,11 +546,18 @@ Json::Value SGXWalletServer::calculateAllBLSPublicKeysImpl(const Json::Value& pu
throw SGXException(INVALID_DKG_PARAMS, "Invalid length of public shares parts"); throw SGXException(INVALID_DKG_PARAMS, "Invalid length of public shares parts");
} }
} }
vector<string> public_shares(n); vector<string> public_shares(n);
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
public_shares[i] = publicShares[i].asString(); public_shares[i] = publicShares[i].asString();
} }
vector<string> public_keys = calculateAllBlsPublicKeys(public_shares); vector<string> public_keys = calculateAllBlsPublicKeys(public_shares);
if (public_keys.size() != n) {
throw SGXException(UNKNOWN_ERROR, "");
}
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
result["publicKeys"][i] = public_keys[i]; result["publicKeys"][i] = public_keys[i];
} }
......
...@@ -196,14 +196,12 @@ void TestUtils::sendRPCRequest() { ...@@ -196,14 +196,12 @@ void TestUtils::sendRPCRequest() {
secretShares[i] = c.getSecretShare(polyNames[i], pubEthKeys, t, n); secretShares[i] = c.getSecretShare(polyNames[i], pubEthKeys, t, n);
for (uint8_t k = 0; k < t; k++) { for (uint8_t k = 0; k < t; k++) {
for (uint8_t j = 0; j < 4; j++) { for (uint8_t j = 0; j < 4; j++) {
string pubShare = verifVects[i]["Verification Vector"][k][j].asString(); string pubShare = verifVects[i]["verificationVector"][k][j].asString();
pubShares[i] += convertDecToHex(pubShare); pubShares[i] += convertDecToHex(pubShare);
} }
} }
} }
int k = 0;
vector <string> secShares(n); vector <string> secShares(n);
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
...@@ -212,8 +210,6 @@ void TestUtils::sendRPCRequest() { ...@@ -212,8 +210,6 @@ void TestUtils::sendRPCRequest() {
secShares[i] += secretShares[j]["secretShare"].asString().substr(192 * i, 192); secShares[i] += secretShares[j]["secretShare"].asString().substr(192 * i, 192);
Json::Value verif = c.dkgVerification(pubShares[i], ethKeys[j]["keyName"].asString(), secretShare, t, n, j); Json::Value verif = c.dkgVerification(pubShares[i], ethKeys[j]["keyName"].asString(), secretShare, t, n, j);
CHECK_STATE(verif["status"] == 0); CHECK_STATE(verif["status"] == 0);
k++;
} }
BLSSigShareSet sigShareSet(t, n); BLSSigShareSet sigShareSet(t, n);
...@@ -246,9 +242,15 @@ void TestUtils::sendRPCRequest() { ...@@ -246,9 +242,15 @@ void TestUtils::sendRPCRequest() {
pubBLSKeys[i] = c.getBLSPublicKeyShare(blsName); pubBLSKeys[i] = c.getBLSPublicKeyShare(blsName);
CHECK_STATE(pubBLSKeys[i]["status"] == 0); CHECK_STATE(pubBLSKeys[i]["status"] == 0);
std::cout << "HERE" << std::endl; libff::alt_bn128_G2 publicKey(libff::alt_bn128_Fq2(libff::alt_bn128_Fq(pubBLSKeys[i]["blsPublicKeyShare"][0].asCString()),
libff::alt_bn128_Fq(pubBLSKeys[i]["blsPublicKeyShare"][1].asCString())),
libff::alt_bn128_Fq2(libff::alt_bn128_Fq(pubBLSKeys[i]["blsPublicKeyShare"][2].asCString()),
libff::alt_bn128_Fq(pubBLSKeys[i]["blsPublicKeyShare"][3].asCString())),
libff::alt_bn128_Fq2::one());
string public_key_str = convertG2ToString(publicKey);
CHECK_STATE(pubBLSKeys[i]["result"]["blsPublicKeyShare"].asString() == blsPublicKeys["result"]["publicKeys"][i].asString()); CHECK_STATE(public_key_str == blsPublicKeys["publicKeys"][i].asString());
string hash = SAMPLE_HASH; string hash = SAMPLE_HASH;
blsSigShares[i] = c.blsSignMessageHash(blsName, hash, t, n); blsSigShares[i] = c.blsSignMessageHash(blsName, hash, t, n);
......
...@@ -962,9 +962,6 @@ void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_share ...@@ -962,9 +962,6 @@ void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_share
CHECK_STATUS("session_key_recover failed"); CHECK_STATUS("session_key_recover failed");
common_key[64] = 0; common_key[64] = 0;
SAFE_CHAR_BUF(decr_sshare, 65); SAFE_CHAR_BUF(decr_sshare, 65);
......
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