Unverified Commit feb75973 authored by svetaro's avatar svetaro

SKALE-2003 Add aes encryption to DKG

parent c0f9ff17
......@@ -27,6 +27,7 @@
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "bls.h"
#include <bls/BLSutils.h>
#include "leveldb/db.h"
......@@ -53,6 +54,22 @@
#include "spdlog/spdlog.h"
#include "common.h"
std::string *FqToString(libff::alt_bn128_Fq*_fq) {
mpz_t t;
mpz_init(t);
_fq->as_bigint().to_mpz(t);
char arr[mpz_sizeinbase(t, 10) + 2];
char *tmp = mpz_get_str(arr, 10, t);
mpz_clear(t);
return new std::string(tmp);
}
int char2int(char _input) {
if (_input >= '0' && _input <= '9')
return _input - '0';
......@@ -132,7 +149,6 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
}
bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
char* _sig) {
......@@ -172,9 +188,131 @@ bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t
//cerr<< "sig " << _sig <<endl;
return true;
}
bool sign_aes(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
char* _sig) {
//cerr << "ENTER SIGN" << endl;
auto keyStr = make_shared<string>(_encryptedKeyHex);
auto hash = make_shared<array<uint8_t, 32>>();
uint64_t binLen;
if (!hex2carray(_hashHex, &binLen, hash->data())){
throw RPCException(INVALID_HEX, "Invalid hash");
}
// assert(binLen == hash->size());
// auto keyShare = make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
//
// //cerr << "keyShare created" << endl;
// // {
// auto sigShare = keyShare->signWithHelperSGX(hash, _signerIndex);
// // }
//
// auto sigShareStr = sigShare->toString();
//
// strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
shared_ptr<signatures::Bls> obj;
obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n));
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint =
obj->HashtoG1withHint(hash);
int errStatus = 0;
string* xStr = FqToString(&(hash_with_hint.first.X));
if (xStr == nullptr) {
std::cerr << "Null xStr" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Null xStr"));
}
string* yStr = FqToString(&(hash_with_hint.first.Y));
if (yStr == nullptr) {
std::cerr << "Null yStr" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN);
char xStrArg[BUF_LEN];
char yStrArg[BUF_LEN];
char signature [BUF_LEN];
memset(xStrArg, 0, BUF_LEN);
memset(yStrArg, 0, BUF_LEN);
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);
size_t sz = 0;
uint8_t encryptedKey[BUF_LEN];
bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey);
if (!result) {
cerr << "Invalid hex encrypted key" << endl;
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key"));
}
sgx_status_t status =
bls_sign_message_aes(eid, &errStatus, errMsg, encryptedKey,
sz, xStrArg, yStrArg, signature);
if (status != SGX_SUCCESS) {
cerr <<"SGX enclave call to bls_sign_message failed:" << status << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to bls_sign_message failed"));
}
std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;
std::string sig = signature;
sig.append(":");
sig.append(hint);
strncpy(_sig, sig.c_str(), BUF_LEN);
printf("_sig is: %s\n", sig.c_str());
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
//cerr<< "sig " << _sig <<endl;
return true;
}
bool bls_sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
char* _sig) {
if (!is_aes){
return sign(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
}
else{
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
}
}
char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
char *keyArray = (char *) calloc(BUF_LEN, 1);
......
......@@ -36,7 +36,7 @@
//
//EXTERNC void init_enclave();
EXTERNC bool sign(const char* encryptedKeyHex, const char* hashHex, size_t t, size_t n,
EXTERNC bool bls_sign(const char* encryptedKeyHex, const char* hashHex, size_t t, size_t n,
size_t signerIndex, char* _sig);
EXTERNC int char2int(char _input);
......
......@@ -103,8 +103,8 @@ string gen_dkg_poly( int _t){
//vector<char> hexEncrPoly(DKG_MAX_SEALED_LEN * 2 + 1, 0);//(4*BUF_LEN, 1);
vector<char> hexEncrPoly(2 * length, 0);
vector<char> hexEncrPoly(2 * length + 1, 0);
assert( encrypted_dkg_secret.size() >= length);
//carray2Hex(encrypted_dkg_secret.data(), DKG_MAX_SEALED_LEN, hexEncrPoly.data());
carray2Hex(encrypted_dkg_secret.data(), length, hexEncrPoly.data());
string result(hexEncrPoly.data());
......@@ -124,11 +124,13 @@ vector <vector<string>> get_verif_vect(const char* encryptedPolyHex, int t, int
}
char* public_shares = (char*)calloc(10000, 1);
memset(public_shares, 0, 10000);
// char public_shares[10000];
uint64_t enc_len = 0;
uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN * 2, 1);
memset(encr_dkg_poly, 0, DKG_MAX_SEALED_LEN * 2);
//uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
if (!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)){
......@@ -136,7 +138,7 @@ vector <vector<string>> get_verif_vect(const char* encryptedPolyHex, int t, int
}
if (DEBUG_PRINT) {
cerr << "hex_encr_poly is " << encryptedPolyHex << std::endl;
//cerr << "hex_encr_poly is " << encryptedPolyHex << std::endl;
spdlog::info("hex_encr_poly length is {}", strlen(encryptedPolyHex));
spdlog::info("enc len {}", enc_len);
// cerr << "encr raw poly: " << endl;
......@@ -184,15 +186,19 @@ string get_secret_shares(const string& polyName, const char* encryptedPolyHex, c
//char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
char hexEncrKey[BUF_LEN];
memset(hexEncrKey, 0, BUF_LEN);
uint64_t enc_len = 0;
// uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
memset(encr_dkg_poly, 0, DKG_MAX_SEALED_LEN);
if(!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
std::cerr << "enc_len is " << enc_len << std::endl;
if (!is_aes)
status = set_encrypted_dkg_poly(eid, &err_status, errMsg1, encr_dkg_poly);
else
......@@ -204,18 +210,18 @@ string get_secret_shares(const string& polyName, const char* encryptedPolyHex, c
string result;
//char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
char hexEncrKey[2 * BUF_LEN];
for ( int i = 0; i < n; i++){
uint8_t encrypted_skey[BUF_LEN];
memset(encrypted_skey, 0, BUF_LEN);
uint32_t dec_len;
char cur_share[193];
char s_shareG2[320];
string pub_keyB = publicKeys.at(i);//publicKeys.substr(128*i, 128*i + 128);
if (DEBUG_PRINT) {
spdlog::info("pub_keyB is {}", pub_keyB);
}
// if (DEBUG_PRINT) {
// spdlog::info("pub_keyB is {}", pub_keyB);
// }
char pubKeyB[129];
strncpy(pubKeyB, pub_keyB.c_str(), 128);
pubKeyB[128] = 0;
......@@ -242,11 +248,14 @@ string get_secret_shares(const string& polyName, const char* encryptedPolyHex, c
if (DEBUG_PRINT) {
spdlog::info("dec len is {}", dec_len);
}
carray2Hex(encrypted_skey, dec_len, hexEncrKey);
string DHKey_name = "DKG_DH_KEY_" + polyName + "_" + to_string(i) + ":";
// cerr << "hexEncrKey: " << hexEncrKey << endl;
cerr << "hexEncr DH Key: " << hexEncrKey << endl;
writeDataToDB(DHKey_name, hexEncrKey);
string shareG2_name = "shareG2_" + polyName + "_" + to_string(i) + ":";
......@@ -280,24 +289,27 @@ bool VerifyShares(const char* publicShares, const char* encr_sshare, const char
uint64_t dec_key_len ;
uint8_t encr_key[BUF_LEN];
memset(encr_key, 0, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
int result;
if (DEBUG_PRINT) {
// cerr << "encryptedKeyHex " << encryptedKeyHex << endl;
// cerr << "dec_key_len " << dec_key_len << endl;
// cerr << "encr_sshare length is " << strlen(encr_sshare) << endl; cerr << "public shares " << publicShares << endl;
cerr << "encryptedKeyHex " << encryptedKeyHex << endl;
cerr << "dec_key_len " << dec_key_len << endl;
cerr << "encr_sshare length is " << strlen(encr_sshare) << endl;
//cerr << "public shares " << publicShares << endl;
spdlog::info("publicShares length is {}", char_traits<char>::length(publicShares));
}
char pshares[8193];
strncpy(pshares, publicShares, strlen(publicShares) + 1);
//cerr << "pshares " << pshares << endl;
memset(pshares, 0, 8193);
strncpy(pshares, publicShares, strlen(publicShares) );
dkg_verification(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
if (!is_aes)
dkg_verification(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
else
dkg_verification_aes(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
if (result == 2){
throw RPCException(INVALID_HEX, "Invalid public shares");
......@@ -323,7 +335,9 @@ bool CreateBLSShare( const string& blsKeyName, const char * s_shares, const char
uint64_t dec_key_len ;
uint8_t encr_bls_key[BUF_LEN];
memset(encr_bls_key, 0, BUF_LEN);
uint8_t encr_key[BUF_LEN];
memset(encr_key, 0, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
......@@ -331,10 +345,15 @@ bool CreateBLSShare( const string& blsKeyName, const char * s_shares, const char
uint32_t enc_bls_len = 0;
//cerr << "BEFORE create_bls_key IN ENCLAVE " << endl;
create_bls_key(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
if (!is_aes)
create_bls_key(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
else
create_bls_key_aes(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
//cerr << "AFTER create_bls_key IN ENCLAVE er msg is " << errMsg1 << endl;
if ( err_status != 0){
spdlog::info("ERROR IN ENCLAVE");
//spdlog::info("ERROR IN ENCLAVE with status {}", err_status);
spdlog::error(errMsg1);
spdlog::error("status {}", err_status);
throw RPCException(ERROR_IN_ENCLAVE, "Create BLS private key failed in enclave");
}
else {
......@@ -373,8 +392,13 @@ vector<string> GetBLSPubKey(const char * encryptedKeyHex){
if (DEBUG_PRINT) {
spdlog::info("dec_key_len is {}", dec_key_len);
}
get_bls_pub_key(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
if (!is_aes)
get_bls_pub_key(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
else
get_bls_pub_key_aes(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
if ( err_status != 0){
std::cerr << errMsg1 << " status is " << err_status << std::endl;
throw RPCException(ERROR_IN_ENCLAVE, "Failed to get BLS public key in enclave");
}
vector<string> pub_key_vect = SplitString(pub_key, ':');
......@@ -406,12 +430,19 @@ string decrypt_DHKey(const string& polyName, int ind){
if (!hex2carray(hexEncrKey_ptr->c_str(), &DH_enc_len, encrypted_DHkey)){
throw RPCException(INVALID_HEX, "Invalid hexEncrKey");
}
if (DEBUG_PRINT) {
spdlog::info("encr DH key length is {}", DH_enc_len);
spdlog::info("hex encr DH key length is {}", hexEncrKey_ptr->length());
}
char DHKey[ECDSA_SKEY_LEN];
decrypt_key(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
if ( !is_aes)
decrypt_key(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
else
decrypt_key_aes(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
if (err_status != 0){
throw RPCException(ERROR_IN_ENCLAVE, "decrypt key failed in enclave");
throw RPCException(/*ERROR_IN_ENCLAVE*/ err_status, "decrypt key failed in enclave");
}
return DHKey;
......
......@@ -63,7 +63,7 @@ std::vector<std::string> gen_ecdsa_key(){
if (DEBUG_PRINT) {
std::cerr << "account key is " << errMsg << std::endl;
std::cerr << "enc_len is " << enc_len << std::endl;
std::cerr << "enc_key is " << std::endl;
// std::cerr << "enc_key is " << std::endl;
// for(int i = 0 ; i < 1024; i++)
// std::cerr << (int)encr_pr_key[i] << " " ;
}
......
......@@ -282,7 +282,6 @@ void LevelDB::initDataFolderAndDBs() {
exit(-1);
}
sgx_data_folder = string(cwd) + "/" + SGXDATA_FOLDER;
struct stat info;
......@@ -298,7 +297,6 @@ void LevelDB::initDataFolderAndDBs() {
}
}
auto dbName = sgx_data_folder + WALLETDB_NAME;
levelDb = make_shared<LevelDB>(dbName);
......
......@@ -44,8 +44,8 @@ secure_enclave.edl: secure_enclave/secure_enclave.edl
##
#AM_CPPFLAGS += -g -Og
AM_CFLAGS = -g -Og -rdynamic -Wl,--no-as-needed -lSegFault
AM_CXXFLAGS = ${AM_CPPFLAGS} -rdynamic -Wl,--no-as-needed -lSegFault
AM_CFLAGS = -g -Og -rdynamic -Wl,--no-as-needed -lSegFault -fsanitize=address
AM_CXXFLAGS = ${AM_CPPFLAGS} -rdynamic -Wl,--no-as-needed -lSegFault -fsanitize=address
AM_CPPFLAGS += -Wall -DSKALE_SGX=1 -DBINARY_OUTPUT=1 -Ileveldb/include -IlibBLS/bls -IlibBLS/libff -IlibBLS -fno-builtin-memset $(GMP_CPPFLAGS) -I. -I./libBLS/deps/deps_inst/x86_or_x64/include
......
......@@ -54,3 +54,24 @@ void generate_SEK(){
LevelDB::getLevelDb()->writeDataUnique("SEK", hexEncrKey.data());
}
void setSEK(std::shared_ptr<std::string> hex_encr_SEK){
vector<char> errMsg(1024,0);
int err_status = 0;
//vector<uint8_t> encr_SEK(1024, 0);
uint8_t encr_SEK [BUF_LEN];
uint64_t len;
if (!hex2carray(hex_encr_SEK->c_str(), &len, encr_SEK)){
throw RPCException(INVALID_HEX, "Invalid encrypted SEK Hex");
}
status = set_SEK(eid, &err_status, errMsg.data(), encr_SEK );
if ( err_status != 0 ){
cerr << "RPCException thrown" << endl;
throw RPCException(-666, errMsg.data()) ;
}
}
......@@ -24,6 +24,11 @@
#ifndef SGXD_SEKMANAGER_H
#define SGXD_SEKMANAGER_H
#include <string>
#include <memory>
void generate_SEK();
void setSEK(std::shared_ptr<std::string> hex_encr_SEK);
#endif //SGXD_SEKMANAGER_H
......@@ -218,7 +218,7 @@ Json::Value blsSignMessageHashImpl(const string &keyShareName, const string &mes
}
try {
if (!sign(value->c_str(), messageHash.c_str(), t, n, signerIndex, signature)) {
if (!bls_sign(value->c_str(), messageHash.c_str(), t, n, signerIndex, signature)) {
result["status"] = -1;
result["errorMessage"] = "Could not sign";
return result;
......@@ -501,6 +501,7 @@ Json::Value getSecretShareImpl(const string& polyName, const Json::Value& public
vector<string> pubKeys_vect;
for ( int i = 0; i < n ; i++) {
std::cerr << "publicKeys " << i << " is " << publicKeys[i].asString() <<std::endl;
if ( !checkHex(publicKeys[i].asString(), 64)){
throw RPCException(INVALID_HEX, "Invalid public key");
}
......
......@@ -71,15 +71,17 @@ void init_daemon() {
libff::init_alt_bn128_params();
LevelDB::initDataFolderAndDBs();
std::shared_ptr<std::string> encr_SEK_ptr = LevelDB::getLevelDb()->readString("SEK");
if (encr_SEK_ptr == nullptr){
spdlog::info("SEK was not created yet");
generate_SEK();
}
else{
spdlog::info("SEK was created");
setSEK(encr_SEK_ptr);
}
}
......
......@@ -25,7 +25,8 @@
#include <string.h>
#include <cstdint>
#include "../sgxwallet_common.h"
//#include "../sgxwallet_common.h"
#include "enclave_common.h"
#include "BLSEnclave.h"
......
......@@ -30,7 +30,8 @@
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp>
#include "../sgxwallet_common.h"
//#include "../sgxwallet_common.h"
#include "enclave_common.h"
#include <cstdio>
#include <stdio.h>
......@@ -313,27 +314,31 @@ int Verification ( char * public_shares, mpz_t decr_secret_share, int _t, int in
char arr[mpz_sizeinbase (decr_secret_share, 10) + 2];
char * tmp = mpz_get_str(arr, 10, decr_secret_share);
libff::alt_bn128_Fr sshare(tmp);
// strncpy(public_shares, tmp, strlen(tmp));
// std::string res = ConvertHexToDec("fe43567238abcdef98760");
// strncpy(public_shares, res.c_str(), res.length());
libff::alt_bn128_G2 val2 = sshare * libff::alt_bn128_G2::one();
memset(public_shares, 0, strlen(public_shares));
strncpy(public_shares, ConvertToString(val2.X.c0).c_str(), ConvertToString(val2.X.c0).length());
strncpy(public_shares + ConvertToString(val2.X.c0).length(), ":", 1);
strncpy(public_shares + ConvertToString(val2.X.c0).length() + 1, ConvertToString(val2.X.c1).c_str(), 77);
strncpy(public_shares, tmp, strlen(tmp));
// strncpy(public_shares, ConvertToString(val2.X.c0).c_str(), ConvertToString(val2.X.c0).length());
// strncpy(public_shares + ConvertToString(val2.X.c0).length(), ":", 1);
// strncpy(public_shares + ConvertToString(val2.X.c0).length() + 1, ConvertToString(val2.X.c1).c_str(), 77);
val.to_affine_coordinates();
val2.to_affine_coordinates();
// strncpy(public_shares + strlen(tmp), ":", 1);
// strncpy(public_shares + 77 + 1, ConvertToString(val.X.c0).c_str(), 77);
// strncpy(public_shares + 77 + 78, ":", 1);
// strncpy(public_shares + 77 + 79, ConvertToString(val2.X.c0).c_str(), 77);
strncpy(public_shares, ConvertToString(val.X.c0).c_str(), ConvertToString(val.X.c0).length());
strncpy(public_shares + ConvertToString(val.X.c0).length(), ":", 1);
strncpy(public_shares + ConvertToString(val.X.c0).length() + 1, ConvertToString(val2.X.c0).c_str(), ConvertToString(val2.X.c0).length());
/*strncpy(public_shares + 77 + 77 + 79, "\n", 1);
strncpy(public_shares + 144 + 79, ConvertToString(val2.X.c0).c_str(), 77);
strncpy(public_shares + 144 + 78, ":", 1);
......
This diff is collapsed.
This diff is collapsed.
......@@ -89,6 +89,7 @@ int main(int argc, char *argv[]) {
break;
case 'a':
is_aes = 1;
break;
case '?': // fprintf(stderr, "unknown flag\n");
exit(1);
default:
......
......@@ -93,7 +93,7 @@ extern int is_aes;
#define BASE_PORT 1026
#define WALLETDB_NAME "sgxwallet.db"//"test_sgxwallet.db"//
#define WALLETDB_NAME "sgxwallet.db"//"test_sgxwallet.db"
#define ENCLAVE_NAME "secure_enclave.signed.so"
#define SGXDATA_FOLDER "sgx_data/"
......
......@@ -801,8 +801,7 @@ TEST_CASE("BLS_DKG test", "[bls_dkg]") {
BLSPublicKey common_public(make_shared<map<size_t, shared_ptr<BLSPublicKeyShare>>>(koefs_pkeys_map), t, n);
REQUIRE( common_public.VerifySigWithHelper(hash_arr, commonSig, t, n) );
cout << "try to get bls public key" << endl;
cout << c.getBLSPublicKeyShare("BLS_KEY:SCHAIN_ID:1:NODE_ID:1:DKG_ID:0");
}
TEST_CASE("API test", "[api_test]") {
......@@ -1033,6 +1032,7 @@ void SendRPCRequest(){
TEST_CASE("ManySimultaneousThreads", "[many_threads_test]") {
is_sgx_https = 0;
DEBUG_PRINT = 1;
is_aes = 1;
init_all( false, false );
......@@ -1216,15 +1216,15 @@ TEST_CASE("AES_DKG test", "[aes_dkg]") {
reset_db();
int n = 4, t = 4;
int n = 16, t = 16;
Json::Value EthKeys[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);
vector<string> pubShares(n);
vector<string> poly_names(n);
int schain_id = rand_gen();
int dkg_id = rand_gen();
......@@ -1236,17 +1236,134 @@ TEST_CASE("AES_DKG test", "[aes_dkg]") {
poly_names[i] = polyName;
VerifVects[i] = c.getVerificationVector(polyName, t, n);
cout << "VV " << i << " " << VerifVects[i] << std::endl;
pubEthKeys.append(EthKeys[i]["PublicKey"]);
pubEthKeys.append(EthKeys[i]["publicKey"]);
}
for ( uint8_t i = 0; i < n; i++){
secretShares[i] = c.getSecretShare(poly_names[i], pubEthKeys, t, n);
cout << secretShares[i] << std::endl;
REQUIRE(secretShares[i]["status"] == 0);
for ( uint8_t k = 0; k < t; k++ )
for (uint8_t j = 0; j < 4; j++){
string pubShare = VerifVects[i]["verificationVector"][k][j].asString();
pubShares[i] += ConvertDecToHex(pubShare);
}
// pSharesBad[i][0] = 'q';
// Json::Value wrongVerif = c.dkgVerification(pSharesBad[i], EthKeys[j]["keyName"].asString(), secretShare, t, n, j);
// res = wrongVerif["result"].asBool();
// REQUIRE(!res);
// cerr << "wrong verification " << wrongVerif << endl;
// }
}
int k = 0;
vector <string> secShares_vect(n);
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++){
// if ( i != j ){
cerr << "SecretShare length is " << secretShares[i]["secretShare"].asString().length() << endl;
string secretShare = secretShares[i]["secretShare"].asString().substr(192 * j, 192 );
secShares_vect[i] += secretShares[j]["secretShare"].asString().substr(192 * i, 192 );
Json::Value verif = c.dkgVerification(pubShares[i], EthKeys[j]["keyName"].asString(), secretShare, t, n, j);
cout << verif;
bool res = verif["result"].asBool();
k++;
cerr << "NOW K IS " << k << " i is " << i << " j is " << j << endl;
REQUIRE( res );
// }
}
std::cerr << "before exit " << std::endl;
exit(0);
std::cerr << "after exit " << std::endl;
Json::Value complaintResponse = c.complaintResponse(poly_names[1], 0);
cout << complaintResponse << endl;
REQUIRE(complaintResponse["status"] == 0);
cerr << "share * G2 is " << complaintResponse["share*G2"].asString();
cerr << "DHKey is " << complaintResponse["dhKey"].asString();
BLSSigShareSet sigShareSet(t, n);
string hash = "09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db";
auto hash_arr = make_shared<array<uint8_t, 32>>();
uint64_t binLen;
if (!hex2carray(hash.c_str(), &binLen, hash_arr->data())){
throw RPCException(INVALID_HEX, "Invalid hash");
}
map<size_t, shared_ptr<BLSPublicKeyShare>> koefs_pkeys_map;
for ( int i = 0; i < t; i++){
string endName = poly_names[i].substr(4);
string blsName = "BLS_KEY" + poly_names[i].substr(4);
cout << c.createBLSPrivateKey(blsName, EthKeys[i]["keyName"].asString(), poly_names[i], secShares_vect[i], t, n);
pubBLSKeys[i] = c.getBLSPublicKeyShare(blsName);
cout << pubBLSKeys[i] << endl;
REQUIRE(pubBLSKeys[i]["status"] == 0);
cerr << "BLS KEY SHARE NAME IS" << blsName << endl;
string hash = "09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db";
BLSSigShares[i] = c.blsSignMessageHash(blsName, hash, t, n, i + 1);
cout << BLSSigShares[i] << std::endl;
REQUIRE( BLSSigShares[i]["status"] == 0);
cerr << i << " sig share is created " << endl;
shared_ptr<string> sig_share_ptr = make_shared<string>(BLSSigShares[i]["signatureShare"].asString());
BLSSigShare sig(sig_share_ptr, i + 1, t, n);
sigShareSet.addSigShare(make_shared<BLSSigShare>(sig));
vector<string> pubKey_vect;
for ( uint8_t j = 0; j < 4; j++){
pubKey_vect.push_back(pubBLSKeys[i]["blsPublicKeyShare"][j].asString());
}
BLSPublicKeyShare pubKey(make_shared<vector<string>>(pubKey_vect), t, n);
REQUIRE( pubKey.VerifySigWithHelper(hash_arr, make_shared<BLSSigShare>(sig) , t, n));
koefs_pkeys_map[i+1] = make_shared<BLSPublicKeyShare>(pubKey);
}
// for ( uint8_t i = 0; i < n; i++){
// secretShares[i] = c.getSecretShare(poly_names[i], pubEthKeys, t, n);
// cout << secretShares[i] << std::endl;
// REQUIRE(secretShares[i]["status"] == 0);
// for ( uint8_t k = 0; k < t; k++ ) {
// for (uint8_t j = 0; j < 4; j++) {
// string pubShare = VerifVects[i]["verificationVector"][k][j].asString();
// pubShares[i] += ConvertDecToHex(pubShare);
// }
// }
shared_ptr<BLSSignature> commonSig = sigShareSet.merge();
BLSPublicKey common_public(make_shared<map<size_t, shared_ptr<BLSPublicKeyShare>>>(koefs_pkeys_map), t, n);
REQUIRE( common_public.VerifySigWithHelper(hash_arr, commonSig, t, n) );
sgx_destroy_enclave(eid);
}
TEST_CASE("bls_sign_api test", "[bls_sign]") {
is_sgx_https = 0;
DEBUG_PRINT = 1;
is_aes = 1;
std::cerr << "test started" << std::endl;
init_all(false, false);
cerr << "Server inited" << endl;
HttpClient client("http://localhost:1029");
StubClient c(client, JSONRPC_CLIENT_V2);
cerr << "Client inited" << endl;
string hash = "09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db";
string blsName = "BLS_KEY:SCHAIN_ID:1031067889:NODE_ID:0:DKG_ID:1112462780";
int n = 4, t = 4;
Json::Value pubBLSKey = c.getBLSPublicKeyShare(blsName);
REQUIRE(pubBLSKey["status"] == 0);
cout << pubBLSKey << endl;
Json::Value sign = c.blsSignMessageHash(blsName, hash, t, n, 1);
cout << sign << endl;
REQUIRE(sign["status"] == 0);
// vector<string> pubKey_vect;
// for ( uint8_t j = 0; j < 4; j++){
// pubKey_vect.push_back(pubBLSKeys[i]["blsPublicKeyShare"][j].asString());
// }
// BLSPublicKeyShare pubKey(make_shared<vector<string>>(pubKey_vect), t, n);
// REQUIRE( pubKey.VerifySigWithHelper(hash_arr, make_shared<BLSSigShare>(sig) , t, n));
}
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