Unverified Commit 19bd17a3 authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #11 from skalenetwork/enhancement/SKALE-1795-Add-some-methods-to-DKG

SKALE-1795-Add-some-methods-to-DKG Make same order of parameters t,n …
parents 780aebbe 0aeee502
......@@ -122,7 +122,9 @@ bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t
uint64_t binLen;
hex2carray(_hashHex, &binLen, hash->data());
if (!hex2carray(_hashHex, &binLen, hash->data())){
throw RPCException(INVALID_HEX, "Invalid hash");
}
// assert(binLen == hash->size());
auto keyShare = std::make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
......
......@@ -182,7 +182,7 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
int sigLen;
if ((sigLen = strnlen(signature, 10)) < 10) {
BOOST_THROW_EXCEPTION(runtime_error("Signature too short:" + to_string(sigLen)));
BOOST_THROW_EXCEPTION(runtime_error("Signature is too short:" + to_string(sigLen)));
}
......
......@@ -69,7 +69,7 @@ std::string gen_dkg_poly( int _t){
return result;
}
std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyHex, int n, int t){
std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyHex, int t, int n){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
......@@ -81,7 +81,9 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH
uint64_t enc_len = 0;
uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100);
if (!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
std::cerr << "enc len " << enc_len << std::endl;
/*std::cerr << "encr raw poly: " << std::endl;
for ( int i = 0 ; i < 3050; i++)
......@@ -113,14 +115,16 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH
return pub_shares_vect;
}
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::vector<std::string>& publicKeys, int n, int t){
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::vector<std::string>& publicKeys, int t, int n){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
uint64_t enc_len = 0;
uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100);
if(!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
status = set_encrypted_dkg_poly(eid, &err_status, errMsg1, encr_dkg_poly);
......@@ -147,16 +151,15 @@ std::string get_secret_shares(const std::string& polyName, const char* encrypted
std::cerr << "dec len is " << dec_len << std::endl;
carray2Hex(encrypted_skey, dec_len, hexEncrKey);
std::string DHKey_name = "DKG_DH_KEY_" + polyName + "_" + std::to_string(i) + ":";
std::cerr << "name to write to db is " << DHKey_name << std::endl;
// std::cerr << "hexEncrKey: " << hexEncrKey << std::endl;
writeDataToDB(DHKey_name, hexEncrKey);
// std::string DHKey_name = "DKG_DH_KEY_" + polyName + "_" + std::to_string(i) + ":";
// std::cerr << "name to write to db is " << DHKey_name << std::endl;
// std::cerr << "hexEncrKey: " << hexEncrKey << std::endl;
// writeDataToDB(DHKey_name, hexEncrKey);
//
// std::string shareG2_name = "shareG2_" + polyName + "_" + std::to_string(i) + ":";
// std::cerr << "name to write to db is " << shareG2_name << std::endl;
// std::cerr << "s_shareG2: " << s_shareG2 << std::endl;
// writeDataToDB(shareG2_name, s_shareG2);
std::string shareG2_name = "shareG2_" + polyName + "_" + std::to_string(i) + ":";
std::cerr << "name to write to db is " << shareG2_name << std::endl;
std::cerr << "s_shareG2: " << s_shareG2 << std::endl;
writeDataToDB(shareG2_name, s_shareG2);
std::cerr << errMsg1 << std::endl << std::endl;
//std::cerr << "iteration " << i <<" result length is " << result.length() << std::endl ;
......@@ -178,7 +181,9 @@ bool VerifyShares(const char* publicShares, const char* encr_sshare, const char
uint64_t dec_key_len ;
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
//std::cerr << "encryptedKeyHex " << encryptedKeyHex << std::endl;
//std::cerr << "dec_key_len " << dec_key_len << std::endl;
......@@ -209,7 +214,9 @@ bool CreateBLSShare( const std::string& BLSKeyName, const char * s_shares, const
uint64_t dec_key_len ;
uint8_t encr_bls_key[BUF_LEN];
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
uint32_t enc_bls_len = 0;
......@@ -240,7 +247,9 @@ std::vector<std::string> GetBLSPubKey(const char * encryptedKeyHex){
uint64_t dec_key_len ;
uint8_t encr_bls_key[BUF_LEN];
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
// for ( int i = 0; i < BUF_LEN; i++ )
// std::cerr << encr_key[i] << " ";
......@@ -268,7 +277,9 @@ std::string decrypt_DHKey(const std::string& polyName, int ind){
uint64_t DH_enc_len = 0;
uint8_t encrypted_DHkey[BUF_LEN];
hex2carray(hexEncrKey_ptr->c_str(), &DH_enc_len, encrypted_DHkey);
if (!hex2carray(hexEncrKey_ptr->c_str(), &DH_enc_len, encrypted_DHkey)){
throw RPCException(INVALID_HEX, "Invalid hexEncrKey");
}
char DHKey[ECDSA_SKEY_LEN];
......
......@@ -10,11 +10,11 @@
std::string gen_dkg_poly( int _t);
std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyHex, int n, int t);
std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyHex, int t, int n);
std::vector<std::string> SplitString(const char* koefs, const char symbol);
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::vector<std::string>& publicKeys, int n, int t);
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::vector<std::string>& publicKeys, int t, int n);
bool VerifyShares(const char* publicShares, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind);
......
......@@ -14,6 +14,13 @@
static std::default_random_engine rand_gen((unsigned int) time(0));
std::string concatPubKeyWith0x(char* pub_key_x, char* pub_key_y){
std::string px = pub_key_x;
std::string py = pub_key_y;
std::string result = "0x" + px + py;// + std::to_string(pub_key_x) + std::to_string(pub_key_y);
return result;
}
std::vector<std::string> gen_ecdsa_key(){
char *errMsg = (char *)calloc(1024, 1);
int err_status = 0;
......@@ -32,7 +39,7 @@ std::vector<std::string> gen_ecdsa_key(){
char *hexEncrKey = (char *) calloc(2*BUF_LEN, 1);
carray2Hex(encr_pr_key, enc_len, hexEncrKey);
keys.at(0) = hexEncrKey;
keys.at(1) = std::string(pub_key_x) + std::string(pub_key_y);
keys.at(1) = std::string(pub_key_x) + std::string(pub_key_y);//concatPubKeyWith0x(pub_key_x, pub_key_y);//
//std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl;
//std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl;
......@@ -75,13 +82,19 @@ std::string get_ecdsa_pubkey(const char* encryptedKeyHex){
uint64_t enc_len = 0;
uint8_t encr_pr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &enc_len, encr_pr_key);
if (!hex2carray(encryptedKeyHex, &enc_len, encr_pr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
status = get_public_ecdsa_key(eid, &err_status, errMsg, encr_pr_key, enc_len, pub_key_x, pub_key_y );
if ( err_status != 0){
throw RPCException(-666, errMsg) ;
}
std::string pubKey = std::string(pub_key_x) + std::string(pub_key_y);
std::string pubKey = std::string(pub_key_x) + std::string(pub_key_y);//concatPubKeyWith0x(pub_key_x, pub_key_y);//
std:: cerr << "pubkey is " << pubKey << std::endl;
std:: cerr << "pubkey length is " << pubKey.length() << std::endl;
std::cerr << "err str " << errMsg << std::endl;
free(errMsg);
......@@ -102,7 +115,9 @@ std::vector<std::string> ecdsa_sign_hash(const char* encryptedKeyHex, const char
uint64_t dec_len = 0;
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_len, encr_key);
if (!hex2carray(encryptedKeyHex, &dec_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
std::cerr << "encryptedKeyHex: "<< encryptedKeyHex << std::endl;
......
......@@ -73,7 +73,7 @@ void LevelDB::writeString(const std::string &_key, const std::string &_value) {
throwExceptionOnError(status);
std::cerr << "written key " << _key << " value " << _value <<std::endl;
std::cerr << "written key " << _key << std::endl;//<< " value " << _value << std::endl;
}
......
......@@ -57,38 +57,37 @@ SGXWalletServer::SGXWalletServer(AbstractServerConnector &connector,
HttpServer *hs = nullptr;
int init_server() {
std::string certPath ="";//"cert/SGXServerCertificate.crt";
std::string keyPath ="";//"cert/SGXServerCertificate.key";
std::string certPath = "cert/SGXServerCertificate.crt";
std::string keyPath = "cert/SGXServerCertificate.key";
if (!boost::filesystem::exists(certPath) ){
std::cerr << "NO!!! " << std::endl;
std::cerr << "CERTIFICATE IS GOING TO BE CREATED" << std::endl;
std::string genCert = "cd cert && ./self-signed-tls -c=US -s=California -l=San-Francisco -o=\"Skale Labs\" -u=\"Department of Software Engineering\" -n=\"SGXServerCertificate\" -e=info@skalelabs.com";
if (system(genCert.c_str()) == 0){
std::cerr << "CERTIFICATE IS SUCCESSFULLY GENERATED" << std::endl;
}
else{
std::cerr << "CERTIFICATE GENERATION FAILED" << std::endl;
exit(-1);
}
}
// if (!boost::filesystem::exists(certPath) ){
// std::cerr << "NO!!! " << std::endl;
// std::cerr << "CERTIFICATE IS GOING TO BE CREATED" << std::endl;
//
// std::string genCert = "cd cert && ./self-signed-tls -c=US -s=California -l=San-Francisco -o=\"Skale Labs\" -u=\"Department of Software Engineering\" -n=\"SGXServerCertificate\" -e=info@skalelabs.com";
//
// if (system(genCert.c_str()) == 0){
// std::cerr << "CERTIFICATE IS SUCCESSFULLY GENERATED" << std::endl;
// }
// else{
// std::cerr << "CERTIFICATE GENERATION FAILED" << std::endl;
// exit(-1);
// }
// }
hs = new HttpServer(1026, certPath, keyPath);
hs = new HttpServer(1027, certPath, keyPath);
s = new SGXWalletServer(*hs,
JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
if (!s->StartListening()) {
cerr << "Server could not start listening" << endl;
exit(-1);
if (!s->StartListening()) {
cerr << "Server could not start listening" << endl;
exit(-1);
}
return 0;
}
Json::Value
importBLSKeyShareImpl(const std::string &_keyShare, const std::string &_keyShareName, int n, int t, int index) {
importBLSKeyShareImpl(const std::string &_keyShare, const std::string &_keyShareName, int t, int n, int index) {
Json::Value result;
int errStatus = UNKNOWN_ERROR;
......@@ -125,22 +124,18 @@ importBLSKeyShareImpl(const std::string &_keyShare, const std::string &_keyShare
return result;
}
Json::Value blsSignMessageHashImpl(const std::string &keyShareName, const std::string &messageHash,int n, int t, int signerIndex) {
Json::Value blsSignMessageHashImpl(const std::string &keyShareName, const std::string &messageHash,int t, int n, int signerIndex) {
Json::Value result;
result["status"] = -1;
result["errorMessage"] = "Unknown server error";
result["signatureShare"] = "";
//int errStatus = UNKNOWN_ERROR;
//char *errMsg = (char *) calloc(BUF_LEN, 1);
char *signature = (char *) calloc(BUF_LEN, 1);
shared_ptr <std::string> value = nullptr;
try {
if ( !checkName(keyShareName, "BLS_KEY")){
throw RPCException(INVALID_POLY_NAME, "Invalid BLSKey name");
......@@ -330,7 +325,8 @@ Json::Value getPublicECDSAKeyImpl(const std::string& keyName){
}
std::shared_ptr<std::string> key_ptr = readFromDb(keyName);
Pkey = get_ecdsa_pubkey( key_ptr->c_str());
std::cerr << "PublicKey" << Pkey << std::endl;
std::cerr << "PublicKey " << Pkey << std::endl;
std::cerr << "PublicKey length" << Pkey.length() << std::endl;
result["PublicKey"] = Pkey;
} catch (RPCException &_e) {
......@@ -353,7 +349,7 @@ Json::Value generateDKGPolyImpl(const std::string& polyName, int t) {
std::string encrPolyHex;
try {
if ( !checkPolyName(polyName)){
if ( !checkName(polyName, "POLY")){
throw RPCException(INVALID_POLY_NAME, "Invalid polynomial name");
}
if ( t <= 0){
......@@ -374,7 +370,7 @@ Json::Value generateDKGPolyImpl(const std::string& polyName, int t) {
return result;
}
Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t) {
Json::Value getVerificationVectorImpl(const std::string& polyName, int t, int n) {
Json::Value result;
result["status"] = 0;
......@@ -382,16 +378,16 @@ Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t)
std::vector <std::vector<std::string>> verifVector;
try {
if ( !checkPolyName(polyName)){
if ( !checkName(polyName, "POLY")){
throw RPCException(INVALID_POLY_NAME, "Invalid polynomial name");
}
if( !check_n_t(n, t)){
if( !check_n_t(t, n)){
throw RPCException(INVALID_DKG_PARAMS, "Invalid parameters: n or t ");
}
std::shared_ptr<std::string> encr_poly_ptr = readFromDb(polyName);
verifVector = get_verif_vect(encr_poly_ptr->c_str(), n, t);
verifVector = get_verif_vect(encr_poly_ptr->c_str(), t, n);
//std::cerr << "verif vect size " << verifVector.size() << std::endl;
for ( int i = 0; i < t; i++){
......@@ -411,7 +407,7 @@ Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t)
return result;
}
Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& publicKeys, int n, int t){
Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& publicKeys, int t, int n){
std::cerr << " enter getSecretShareImpl" << std::endl;
Json::Value result;
result["status"] = 0;
......@@ -422,10 +418,10 @@ Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& p
result["errorMessage"] = "wrong number of public keys";
return result;
}
if ( !checkPolyName(polyName)){
if ( !checkName(polyName, "POLY")){
throw RPCException(INVALID_POLY_NAME, "Invalid polynomial name");
}
if( !check_n_t(n, t)){
if( !check_n_t(t, n)){
throw RPCException(INVALID_DKG_PARAMS, "Invalid DKG parameters: n or t ");
}
......@@ -439,7 +435,7 @@ Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& p
pubKeys_vect.push_back(publicKeys[i].asString());
}
std::string s = get_secret_shares(polyName, encr_poly_ptr->c_str(), pubKeys_vect, n, t);
std::string s = get_secret_shares(polyName, encr_poly_ptr->c_str(), pubKeys_vect, t, n);
//std::cerr << "result is " << s << std::endl;
result["SecretShare"] = s;
......@@ -468,7 +464,7 @@ Json::Value DKGVerificationImpl(const std::string& publicShares, const std::stri
if ( !checkECDSAKeyName(EthKeyName)){
throw RPCException(INVALID_ECDSA_KEY_NAME, "Invalid ECDSA key name");
}
if( !check_n_t(n, t) || ind > n || ind < 0){
if( !check_n_t(t, n) || ind > n || ind < 0){
throw RPCException(INVALID_DKG_PARAMS, "Invalid DKG parameters: n or t ");
}
// if ( !checkHex(SecretShare, SECRET_SHARE_NUM_BYTES)){
......@@ -506,12 +502,13 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s
try {
if (SecretShare.length() != n * 192){
std::cerr << "wrong length of secret shares - " << SecretShare.length() << std::endl;
result["errorMessage"] = "wrong length of secret shares";
return result;
}
// if ( !checkECDSAKeyName(EthKeyName)){
// throw RPCException(INVALID_ECDSA_KEY_NAME, "Invalid ECDSA key name");
// }
if ( !checkECDSAKeyName(EthKeyName)){
throw RPCException(INVALID_ECDSA_KEY_NAME, "Invalid ECDSA key name");
}
if ( !checkName(polyName, "POLY")){
throw RPCException(INVALID_POLY_NAME, "Invalid polynomial name");
}
......@@ -621,14 +618,14 @@ Json::Value SGXWalletServer::generateDKGPoly(const std::string& polyName, int t)
return generateDKGPolyImpl(polyName, t);
}
Json::Value SGXWalletServer::getVerificationVector(const std::string& polyName, int n, int t){
Json::Value SGXWalletServer::getVerificationVector(const std::string& polyName, int t, int n){
lock_guard<recursive_mutex> lock(m);
return getVerificationVectorImpl(polyName, n, t);
return getVerificationVectorImpl(polyName, t, n);
}
Json::Value SGXWalletServer::getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t){
Json::Value SGXWalletServer::getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int t, int n){
lock_guard<recursive_mutex> lock(m);
return getSecretShareImpl(polyName, publicKeys, n, t);
return getSecretShareImpl(polyName, publicKeys, t, n);
}
Json::Value SGXWalletServer::DKGVerification( const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index){
......@@ -673,16 +670,16 @@ Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_
Json::Value
SGXWalletServer::importBLSKeyShare(const std::string &_keyShare, const std::string &_keyShareName, int n,
int t, int index) {
SGXWalletServer::importBLSKeyShare(const std::string &_keyShare, const std::string &_keyShareName, int t, int n,
int index) {
lock_guard<recursive_mutex> lock(m);
return importBLSKeyShareImpl(_keyShare, _keyShareName, n, t, index );
return importBLSKeyShareImpl(_keyShare, _keyShareName, t, n, index );
}
Json::Value SGXWalletServer::blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash,int n,
int t, int signerIndex) {
Json::Value SGXWalletServer::blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n,
int signerIndex) {
lock_guard<recursive_mutex> lock(m);
return blsSignMessageHashImpl(keyShareName, messageHash, n,t, signerIndex);
return blsSignMessageHashImpl(keyShareName, messageHash, t, n, signerIndex);
}
Json::Value SGXWalletServer::importECDSAKey(const std::string &key, const std::string &keyName) {
......
......@@ -20,8 +20,8 @@ class SGXWalletServer : public AbstractStubServer {
public:
SGXWalletServer(AbstractServerConnector &connector, serverVersion_t type);
virtual Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName, int n, int t, int index);
virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int t, int signerIndex);
virtual Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName, int t, int n, int index);
virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int t, int n, int signerIndex);
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName);
virtual Json::Value generateECDSAKey();
......@@ -30,8 +30,8 @@ public:
virtual Json::Value getPublicECDSAKey(const std::string& keyName);
virtual Json::Value generateDKGPoly(const std::string& polyName, int t);
virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t);
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t);
virtual Json::Value getVerificationVector(const std::string& polyName, int t, int n);
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int t, int n);
virtual Json::Value DKGVerification(const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
virtual Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
virtual Json::Value GetBLSPublicKeyShare(const std::string & BLSKeyName);
......@@ -45,8 +45,8 @@ void writeDataToDB(const string & Name, const string &value);
void writeKeyShare(const string &_keyShareName, const string &value, int index, int n, int t);
shared_ptr<std::string> readKeyShare(const string& _keyShare);
Json::Value importBLSKeyShareImpl(const std::string& keyShare, const std::string& keyShareName, int n, int t, int index);
Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::string& messageHash, int n, int t, int signerIndex);
Json::Value importBLSKeyShareImpl(const std::string& keyShare, const std::string& keyShareName, int t, int n, int index);
Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::string& messageHash, int t, int n, int signerIndex);
Json::Value importECDSAKeyImpl(const std::string& key, const std::string& keyName);
Json::Value generateECDSAKeyImpl();
......@@ -55,8 +55,8 @@ Json::Value ecdsaSignMessageHashImpl(int base, const std::string& keyName, const
Json::Value getPublicECDSAKeyImpl(const std::string& keyName);
Json::Value generateDKGPolyImpl(const std::string& polyName, int t);
Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t);
Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& publicKeys, int n, int t);
Json::Value getVerificationVectorImpl(const std::string& polyName, int t, int n);
Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& publicKeys, int t, int n);
Json::Value DKGVerificationImpl(const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
Json::Value GetBLSPublicKeyShareImpl(const std::string & BLSKeyName);
......
......@@ -65,85 +65,39 @@ bool checkHex(const std::string& hex, const uint32_t sizeInBytes){
return true;
}
bool checkPolyName (const std::string& polyName){
std::vector<std::string> parts = SplitString(polyName);
if ( parts.size() != 7) {
return false;
}
if ( parts.at(0) != "POLY") {
return false;
}
if ( parts.at(1) != "SCHAIN_ID"){
return false;
}
if ( parts.at(3) != "NODE_ID"){
return false;
}
if ( parts.at(5) != "DKG_ID"){
return false;
}
if ( parts.at(2).length() > 64 || parts.at(2).length() < 1){
return false;
}
if (parts.at(4).length() > 5 || parts.at(4).length() < 1){
return false;
}
if ( parts.at(6).length() > 64 || parts.at(6).length() < 1){
return false;
}
mpz_t num;
mpz_init(num);
if ( mpz_set_str(num, parts.at(2).c_str(), 16) == -1){
mpz_clear(num);
return false;
}
mpz_clear(num);
mpz_init(num);
if ( mpz_set_str(num, parts.at(4).c_str(), 10) == -1){
mpz_clear(num);
return false;
}
mpz_clear(num);
mpz_init(num);
if ( mpz_set_str(num, parts.at(6).c_str(),16) == -1){
mpz_clear(num);
return false;
}
mpz_clear(num);
return true;
}
bool checkName (const std::string& Name, const std::string& prefix){
std::vector<std::string> parts = SplitString(Name);
if ( parts.size() != 7) {
std::cerr << "parts.size() != 7" << std::endl;
return false;
}
if ( parts.at(0) != prefix ) {
std::cerr << "parts.at(0) != prefix" << std::endl;
return false;
}
if ( parts.at(1) != "SCHAIN_ID"){
std::cerr << "parts.at(1) != SCHAIN_ID" << std::endl;
return false;
}
if ( parts.at(3) != "NODE_ID"){
std::cerr << "parts.at(3) != Node_ID" << std::endl;
return false;
}
if ( parts.at(5) != "DKG_ID"){
std::cerr << "parts.at(1) != DKG_ID" << std::endl;
return false;
}
if ( parts.at(2).length() > 78 || parts.at(2).length() < 1){
std::cerr << "parts.at(2).length() > 78" << std::endl;
return false;
}
if (parts.at(4).length() > 5 || parts.at(4).length() < 1){
std::cerr << "parts.at(4).length() > 5" << std::endl;
return false;
}
if ( parts.at(6).length() > 78 || parts.at(6).length() < 1){
std::cerr << "parts.at(6).length() > 78" << std::endl;
return false;
}
......@@ -152,6 +106,7 @@ bool checkName (const std::string& Name, const std::string& prefix){
if ( mpz_set_str(num, parts.at(2).c_str(), 10) == -1){
mpz_clear(num);
std::cerr << "parts.at(2) not num" << std::endl;
return false;
}
mpz_clear(num);
......@@ -159,6 +114,7 @@ bool checkName (const std::string& Name, const std::string& prefix){
if ( mpz_set_str(num, parts.at(4).c_str(), 10) == -1){
mpz_clear(num);
std::cerr << "parts.at(4) not num" << std::endl;
return false;
}
mpz_clear(num);
......@@ -166,6 +122,7 @@ bool checkName (const std::string& Name, const std::string& prefix){
if ( mpz_set_str(num, parts.at(6).c_str(),10) == -1){
mpz_clear(num);
std::cerr << "parts.at(6) not num" << std::endl;
return false;
}
mpz_clear(num);
......@@ -173,7 +130,7 @@ bool checkName (const std::string& Name, const std::string& prefix){
return true;
}
bool check_n_t ( const int n, const int t){
bool check_n_t ( const int t, const int n){
if (t > n){
return false;
}
......
......@@ -15,6 +15,6 @@ bool checkPolyName (const std::string& polyName);
bool checkName (const std::string& Name, const std::string& prefix);
bool check_n_t ( const int n, const int t);
bool check_n_t ( const int t, const int n);
#endif // SGXD_SERVERDATACHECKER_H
......@@ -104,5 +104,6 @@ void init_all() {
init_server();
init_enclave();
std::cerr << "enclave inited" << std::endl;
init_daemon();
}
......@@ -12,8 +12,8 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
public:
AbstractStubServer(jsonrpc::AbstractServerConnector &conn, jsonrpc::serverVersion_t type = jsonrpc::JSONRPC_SERVER_V2) : jsonrpc::AbstractServer<AbstractStubServer>(conn, type)
{
this->bindAndAddMethod(jsonrpc::Procedure("importBLSKeyShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"keyShare",jsonrpc::JSON_STRING,"keyShareName",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, "index",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::importBLSKeyShareI);
this->bindAndAddMethod(jsonrpc::Procedure("blsSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyShareName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"signerIndex",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::blsSignMessageHashI);
this->bindAndAddMethod(jsonrpc::Procedure("importBLSKeyShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"keyShare",jsonrpc::JSON_STRING,"keyShareName",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER,"n",jsonrpc::JSON_INTEGER, "index",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::importBLSKeyShareI);
this->bindAndAddMethod(jsonrpc::Procedure("blsSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyShareName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, "n",jsonrpc::JSON_INTEGER, "signerIndex",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::blsSignMessageHashI);
this->bindAndAddMethod(jsonrpc::Procedure("importECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "key",jsonrpc::JSON_STRING,"keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::importECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("generateECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, NULL), &AbstractStubServer::generateECDSAKeyI);
......@@ -22,7 +22,7 @@ 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, "n",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,"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);
......@@ -33,11 +33,11 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
inline virtual void importBLSKeyShareI(const Json::Value &request, Json::Value &response)
{
response = this->importBLSKeyShare( request["keyShare"].asString(), request["keyShareName"].asString(), request["n"].asInt(), request["t"].asInt(), request["index"].asInt());
response = this->importBLSKeyShare( request["keyShare"].asString(), request["keyShareName"].asString(), request["t"].asInt(), request["n"].asInt(), request["index"].asInt());
}
inline virtual void blsSignMessageHashI(const Json::Value &request, Json::Value &response)
{
response = this->blsSignMessageHash(request["keyShareName"].asString(), request["messageHash"].asString(), request["n"].asInt(), request["t"].asInt(), request["signerIndex"].asInt());
response = this->blsSignMessageHash(request["keyShareName"].asString(), request["messageHash"].asString(), request["t"].asInt(), request["n"].asInt(), request["signerIndex"].asInt());
}
inline virtual void importECDSAKeyI(const Json::Value &request, Json::Value &response)
{
......@@ -66,11 +66,11 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
}
inline virtual void getVerificationVectorI(const Json::Value &request, Json::Value &response)
{
response = this->getVerificationVector(request["polyName"].asString(), request["n"].asInt(), request["t"].asInt());
response = this->getVerificationVector(request["polyName"].asString(), request["t"].asInt(), request["n"].asInt());
}
inline virtual void getSecretShareI(const Json::Value &request, Json::Value &response)
{
response = this->getSecretShare(request["polyName"].asString(), request["publicKeys"], request["n"].asInt(),request["t"].asInt());
response = this->getSecretShare(request["polyName"].asString(), request["publicKeys"], request["t"].asInt(),request["n"].asInt());
}
inline virtual void DKGVerificationI(const Json::Value &request, Json::Value &response)
{
......@@ -89,8 +89,8 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
response = this->ComplaintResponse( request["polyName"].asString(), request["ind"].asInt());
}
virtual Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName, int n, int t, int index) = 0;
virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int signerIndex, int t) = 0;
virtual Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName, int t, int n, int index) = 0;
virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int t, int n, int signerIndex ) = 0;
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName) = 0;
virtual Json::Value generateECDSAKey() = 0;
virtual Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName) = 0;
......@@ -98,8 +98,8 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0;
virtual Json::Value generateDKGPoly(const std::string& polyName, int t) = 0;
virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t) = 0;
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t) = 0;
virtual Json::Value getVerificationVector(const std::string& polyName, int t, int n) = 0;
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int t, int n) = 0;
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;
......
......@@ -102,11 +102,17 @@ void xor_encrypt(char* key, char* message, char* cypher){
//uint8_t key_bin[33];
uint8_t* key_bin = (uint8_t*)malloc(33);
uint64_t key_length;
hex2carray(key, &key_length, key_bin);
if (!hex2carray(key, &key_length, key_bin)){
cypher = NULL;
return;
}
uint64_t msg_length;
uint8_t msg_bin[33];//[ECDSA_BIN_LEN];
hex2carray(message, &msg_length, msg_bin);
if (!hex2carray(message, &msg_length, msg_bin)){
cypher = NULL;
return;
}
for (int i = 0; i < 32; i++){
......@@ -125,11 +131,17 @@ void xor_decrypt(char* key, char* cypher, char* message){
//uint8_t key_bin[33];
uint8_t* key_bin = (uint8_t*)malloc(33);
uint64_t key_length;
hex2carray(key, &key_length, key_bin);
if (!hex2carray(key, &key_length, key_bin)){
message = NULL;
return;
}
uint64_t cypher_length;
uint8_t cypher_bin[33];//[ECDSA_BIN_LEN];
hex2carray(cypher, &cypher_length, cypher_bin);
if (!hex2carray(cypher, &cypher_length, cypher_bin)){
message = NULL;
return;
}
for (int i = 0; i < 32; i++){
......
......@@ -222,6 +222,8 @@ void get_public_ecdsa_key(int *err_status, char *err_string,
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
snprintf(err_string, BUF_LEN,"wrong string to init private key");
*err_status = -10;
mpz_clear(skey_mpz);
return;
}
//Public key
......@@ -507,7 +509,12 @@ void ecdsa_sign1(int *err_status, char *err_string, uint8_t *encrypted_key, uint
snprintf(err_string, BUF_LEN,"pr key is %s length %d ", skey, strlen(skey));
mpz_t skey_mpz;
mpz_init(skey_mpz);
mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE);
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid secret key");
mpz_clear(skey_mpz);
return;
}
/*mpz_t test_skey;
mpz_init(test_skey);
......@@ -519,7 +526,12 @@ void ecdsa_sign1(int *err_status, char *err_string, uint8_t *encrypted_key, uint
mpz_t msg_mpz;
mpz_init(msg_mpz);
mpz_set_str(msg_mpz, hash, 16);
if (mpz_set_str(msg_mpz, hash, 16) == -1){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid message hash");
mpz_clear(msg_mpz);
return;
}
//mpz_set_str(msg_mpz,"4b688df40bcedbe641ddb16ff0a1842d9c67ea1c3bf63f3e0471baa664531d1a", 16);
signature sign = signature_init();
......@@ -612,6 +624,11 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
char* cypher = (char *)malloc(65);
xor_encrypt(common_key, s_share, cypher);
if (cypher == NULL){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
return;
}
//snprintf(err_string, BUF_LEN ,"cypher is %s length is %d", cypher, strlen(cypher));
strncpy(result_str, cypher, strlen(cypher));
......@@ -681,6 +698,11 @@ void dkg_verification(int *err_status, char* err_string, const char * public_sha
common_key[64] = 0;
xor_decrypt(common_key, encr_sshare, decr_sshare);
if (decr_sshare == NULL){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
return;
}
//snprintf(err_string, BUF_LEN,"encr_share is %s length is %d", encr_sshare, strlen(encr_sshare));
......@@ -693,7 +715,12 @@ void dkg_verification(int *err_status, char* err_string, const char * public_sha
mpz_t s;
mpz_init(s);
mpz_set_str(s, decr_sshare, 16);
if (mpz_set_str(s, decr_sshare, 16) == -1){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid decr secret share");
mpz_clear(s);
return;
}
*result = Verification(public_shares, s, _t, _ind);
......@@ -755,6 +782,11 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
char decr_sshare[65];
xor_decrypt(common_key, encr_sshare, decr_sshare);
if (decr_sshare == NULL){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
return;
}
//decr_sshare[64] = 0;
//snprintf(err_string + 89*i, BUF_LEN,"share is %s length is %d ", decr_sshare, strlen(decr_sshare));
......@@ -763,7 +795,11 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
mpz_t decr_secret_share;
mpz_init(decr_secret_share);
mpz_set_str(decr_secret_share, decr_sshare, 16);
if (mpz_set_str(decr_secret_share, decr_sshare, 16) == -1){
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid decrypted secret share");
return;
}
mpz_addmul_ui(sum, decr_secret_share, 1);
mpz_clear(decr_secret_share);
......
......@@ -56,8 +56,7 @@
#define SGX_ENCLAVE_ERROR -666;
#define WALLETDB_NAME "test_sgxwallet.db"//"sgxwallet.db" //
#define WALLETDB_NAME "test_sgxwallet1.db"//"sgxwallet.db"
#define ENCLAVE_NAME "secure_enclave.signed.so"
......
......@@ -12,7 +12,7 @@ class StubClient : public jsonrpc::Client
public:
StubClient(jsonrpc::IClientConnector &conn, jsonrpc::clientVersion_t type = jsonrpc::JSONRPC_CLIENT_V2) : jsonrpc::Client(conn, type) {}
Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName, int n, int t, int index) throw (jsonrpc::JsonRpcException)
Json::Value importBLSKeyShare(const std::string& keyShare, const std::string& keyShareName, int t, int n, int index) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["index"] = index;
......@@ -26,7 +26,7 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int t, int signerIndex) throw (jsonrpc::JsonRpcException)
Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int t, int n, int signerIndex) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["keyShareName"] = keyShareName;
......@@ -105,7 +105,7 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getVerificationVector(const std::string& polyName, int n, int t) throw (jsonrpc::JsonRpcException)
Json::Value getVerificationVector(const std::string& polyName, int t, int n) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["polyName"] = polyName;
......@@ -117,7 +117,7 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t) throw (jsonrpc::JsonRpcException)
Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int t, int n) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["polyName"] = polyName;
......@@ -145,7 +145,7 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string& SecretShare, int n, int t) throw (jsonrpc::JsonRpcException)
Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string& SecretShare, int t, int n) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["BLSKeyName"] = BLSKeyName;
......
......@@ -71,7 +71,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "stubclient.h"
#include "BLSSigShare.h"
#include "BLSSigShareSet.h"
#include "BLSPublicKeyShare.h"
#include "BLSPublicKey.h"
std::string stringFromFr(libff::alt_bn128_Fr& el) {
......@@ -718,31 +721,130 @@ TEST_CASE( "pub_bls_key", "[pub_bls]" ) {
using namespace jsonrpc;
using namespace std;
std::string ConvertDecToHex(std::string dec, int numBytes = 32){
mpz_t num;
mpz_init(num);
mpz_set_str(num, dec.c_str(), 10);
char tmp[mpz_sizeinbase (num, 16) + 2];
char * hex = mpz_get_str(tmp, 16, num);
std::string result = hex;
int n_zeroes = numBytes * 2 - result.length();
result.insert(0, n_zeroes, '0');
return result;
}
TEST_CASE("BLS_DKG test", "[bls_dkg]") {
std::cerr<< "test started" << std::endl;
init_all();
cerr << "Server inited" << endl;
HttpClient client("http://localhost:1026");
HttpClient client("http://localhost:1027");
StubClient c(client, JSONRPC_CLIENT_V2);
cerr << "Client inited" << endl;
int n = 2, t = 2;
reset_db();
int n = 4, t = 2;
Json::Value EthKeys[n];
Json::Value Polys[n];
Json::Value VerifVects[n];
Json::Value pubEthKeys;
Json::Value secretShares[n];
Json::Value pubBLSKeys[n];
Json::Value BLSSigShares[n];
std::vector<std::string> pubShares(n);
std::vector<std::string> poly_names(n);
for ( uint8_t i = 0; i < n; i++){
EthKeys[i] = c.generateECDSAKey();
std::string polyName = "POLY:SCHAIN_ID:1:NODE_ID:" + std::to_string(i) + ":DKG_ID:0";
Polys[i] = c.generateDKGPoly(polyName, t);
VerifVects[i] = c.getVerificationVector(polyName, n, t);
c.generateDKGPoly(polyName, t);
poly_names[i] = polyName;
VerifVects[i] = c.getVerificationVector(polyName, t, n);
cout << "VV " << i << " " << VerifVects[i] << std::endl;
pubEthKeys.append(EthKeys[i]["PublicKey"]);
}
for ( uint8_t i = 0; i < n; i++){
secretShares[i] = c.getSecretShare(poly_names[i], pubEthKeys, t, n);
for ( uint8_t k = 0; k < t; k++ ) {
for (uint8_t j = 0; j < 4; j++) {
std::string pubShare = VerifVects[i]["Verification Vector"][k][j].asString();
pubShares[i] += ConvertDecToHex(pubShare);
}
}
std::cerr << "i is " << i << " pubShares[i] = " << pubShares[i] << std::endl;
std::cerr << "length is" << pubShares[i].length() << std::endl;
}
Json::Value ComplaintResponse = c.ComplaintResponse(poly_names[1], 0);
std::cerr << "share * G2 is " << ComplaintResponse["share*G2"].asString();
std::cerr << "DHKey is " << ComplaintResponse["DHKey"].asString();
int k = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++){
// if ( i != j ){
std::cerr << "SecretShare length is " << secretShares[i]["SecretShare"].asString().length() << std::endl;
std::string secretShare = secretShares[i]["SecretShare"].asString().substr(192*j, 192 * (j+1));
bool res = c.DKGVerification(pubShares[i], EthKeys[j]["KeyName"].asString(), secretShare, t, n, j)["result"].asBool();
k++;
std::cerr << "NOW K IS " << k << " i is " << i << " j is " << j << std::endl;
REQUIRE( res );
// }
}
BLSSigShareSet sigShareSet(t, n);
std::string hash = "09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db";
auto hash_arr = std::make_shared<std::array<uint8_t, 32>>();
uint64_t binLen;
if (!hex2carray(hash.c_str(), &binLen, hash_arr->data())){
throw RPCException(INVALID_HEX, "Invalid hash");
}
std::map<size_t, std::shared_ptr<BLSPublicKeyShare>> koefs_pkeys_map;
for ( int i = 0; i < t; i++){
std::string endName = poly_names[i].substr(4);
std::string blsName = "BLS_KEY" + poly_names[i].substr(4);
std::string secretShare = secretShares[i]["SecretShare"].asString();
cout << c.CreateBLSPrivateKey(blsName, EthKeys[i]["KeyName"].asString(), poly_names[i], secretShare, t, n);
pubBLSKeys[i] = c.GetBLSPublicKeyShare(blsName);
//std::string hash = "09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db";
BLSSigShares[i] = c.blsSignMessageHash(blsName, hash, t, n, i + 1);
std::cerr << i << " sig share is created " << std::endl;
std::shared_ptr<std::string> sig_share_ptr = std::make_shared<std::string>(BLSSigShares[i]["signatureShare"].asString());
BLSSigShare sig(sig_share_ptr, i + 1, t, n);
sigShareSet.addSigShare(std::make_shared<BLSSigShare>(sig));
std::vector<std::string> pubKey_vect;
for ( uint8_t j = 0; j < 4; j++){
pubKey_vect.push_back(pubBLSKeys[i]["BLSPublicKeyShare"][j].asString());
}
BLSPublicKeyShare pubKey(std::make_shared<std::vector<std::string>>(pubKey_vect), t, n);
REQUIRE( pubKey.VerifySigWithHelper(hash_arr, std::make_shared<BLSSigShare>(sig) , t, n));
koefs_pkeys_map[i+1] = std::make_shared<BLSPublicKeyShare>(pubKey);
}
std::shared_ptr<BLSSignature> commonSig = sigShareSet.merge();
BLSPublicKey common_public(std::make_shared<std::map<size_t, std::shared_ptr<BLSPublicKeyShare>>>(koefs_pkeys_map), t, n);
REQUIRE( common_public.VerifySigWithHelper(hash_arr, commonSig, t, n) );
}
TEST_CASE("API test", "[api_test]") {
std::cerr << __GNUC__ << std::endl;
......@@ -753,7 +855,7 @@ TEST_CASE("API test", "[api_test]") {
// JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
// s.StartListening();
cerr << "Server inited" << endl;
HttpClient client("http://localhost:1026");
HttpClient client("http://localhost:1027");
StubClient c(client, JSONRPC_CLIENT_V2);
cerr << "Client inited" << endl;
......@@ -763,24 +865,25 @@ TEST_CASE("API test", "[api_test]") {
//levelDb->deleteOlegKey("1");
// levelDb->deleteDHDKGKey("p2_0:");
//levelDb->deleteDHDKGKey("p2_1:");
for ( uint8_t i = 0; i < 2; i++) {
levelDb->deleteKey("POLY:SCHAIN_ID:0:NODE_ID:" + std::to_string(i) +
":DKG_ID:0");
levelDb->deleteKey(" DKG_DH_KEY_POLY:SCHAIN_ID:0:NODE_ID:" + std::to_string(i)+ ":DKG_ID:0_0");
levelDb->deleteKey(" DKG_DH_KEY_POLY:SCHAIN_ID:0:NODE_ID:" + std::to_string(i)+ ":DKG_ID:0_1");
}
// for ( uint8_t i = 0; i < 2; i++) {
// levelDb->deleteKey("POLY:SCHAIN_ID:1:NODE_ID:" + std::to_string(i) +
// ":DKG_ID:0");
//
// levelDb->deleteKey(" DKG_DH_KEY_POLY:SCHAIN_ID:0:NODE_ID:" + std::to_string(i)+ ":DKG_ID:0_0");
// levelDb->deleteKey(" DKG_DH_KEY_POLY:SCHAIN_ID:0:NODE_ID:" + std::to_string(i)+ ":DKG_ID:0_1");
// }
cout << c.importBLSKeyShare("4160780231445160889237664391382223604184857153814275770598791864649971919844","BLS_KEY:SCHAIN_ID:2660016693368503500803087136248943520694587309641817:NODE_ID:33909:DKG_ID:3522960548719023733985054069487289468077787284706573", 4, 3,1);
//cout << c.importBLSKeyShare("4160780231445160889237664391382223604184857153814275770598791864649971919844","BLS_KEY:SCHAIN_ID:2660016693368503500803087136248943520694587309641817:NODE_ID:33909:DKG_ID:3522960548719023733985054069487289468077787284706573", 4, 3,1);
// cout << c.generateECDSAKey() << endl;
// cout << c.generateECDSAKey() << endl;
// cout << c.renameESDSAKey("NODE_1CHAIN_1","tmp_NEK:bcacde0d26c0ea2c7e649992e7f791e1fba2492f5b7ae63dadb799075167c7fc");
// cout<<c.getPublicECDSAKey("NEK:697fadfc597bdbfae9ffb7412b80939e848c9c2fec2657bb2122b6d0d4a0dca8");
// cout<<c.getPublicECDSAKey("NEK:7ca98cf32fd1edba26ea685820719fd2201b068a10c1264d382abbde13802a0e");
//cout << c.ecdsaSignMessageHash(16, "NEK:697fadfc597bdbfae9ffb7412b80939e848c9c2fec2657bb2122b6d0d4a0dca8","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" );
//cout << c.ecdsaSignMessageHash(16, "known_key1","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" );
// cout << c.blsSignMessageHash(TEST_BLS_KEY_NAME, "0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db", 2,2,1 );
// cout << c.generateDKGPoly("pp2", 2);
// cout << c.generateDKGPoly("POLY:SCHAIN_ID:1:NODE_ID:1:DKG_ID:1", 2);
cout << c.generateDKGPoly("POLY:SCHAIN_ID:14225439306783892379384764908040542049263455631509697460847850632966314337557:NODE_ID:1:DKG_ID:71951190446274221430521459675625214118086594348715", 1);
//cout << c.getVerificationVector("POLY:SCHAIN_ID:1:NODE_ID:1:DKG_ID:2", 2, 2);
// cout << c.getVerificationVector("polyy", 5, 5);
......
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