Skip to content

Commit

Permalink
Merge pull request #401 from skalenetwork/feaute/bls-aggregated-signa…
Browse files Browse the repository at this point in the history
…tures-support

Feaute/bls aggregated signatures support
  • Loading branch information
kladkogex authored Jan 20, 2023
2 parents 7c73227 + 83217e1 commit 22d6c98
Show file tree
Hide file tree
Showing 39 changed files with 984 additions and 232 deletions.
125 changes: 106 additions & 19 deletions BLSCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,15 @@
#include "third_party/intel/create_enclave.h"


#include "bls.h"
#include <bls/BLSutils.h>

#include "BLSPrivateKeyShareSGX.h"
#include <tools/utils.h>


#include "sgxwallet_common.h"
#include "sgxwallet.h"
#include "SGXException.h"
#include "third_party/spdlog/spdlog.h"
#include "common.h"
#include "SGXWalletServer.h"
#include "SGXWalletServer.hpp"

#include "SEKManager.h"
#include "LevelDB.h"
Expand All @@ -49,7 +46,7 @@
#include "CryptoTools.h"


string *FqToString(libff::alt_bn128_Fq *_fq) {
shared_ptr<string> FqToString(libff::alt_bn128_Fq *_fq) {

CHECK_STATE(_fq);

Expand All @@ -63,7 +60,7 @@ string *FqToString(libff::alt_bn128_Fq *_fq) {
mpz_get_str(arr, 10, t);
mpz_clear(t);

return new string(arr);
return make_shared<string>(string(arr));
}

bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, char *_sig) {
Expand All @@ -80,21 +77,18 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
throw SGXException(SIGN_AES_INVALID_HASH, string(__FUNCTION__) + ":Invalid hash");
}

shared_ptr <signatures::Bls> obj;
obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n));
shared_ptr <libBLS::Bls> obj;
obj = make_shared<libBLS::Bls>(libBLS::Bls(_t, _n));

pair <libff::alt_bn128_G1, string> hash_with_hint = obj->HashtoG1withHint(hash);

string *xStr = FqToString(&(hash_with_hint.first.X));
shared_ptr<string> xStr = FqToString(&(hash_with_hint.first.X));

CHECK_STATE(xStr);

string *yStr = FqToString(&(hash_with_hint.first.Y));
shared_ptr<string> yStr = FqToString(&(hash_with_hint.first.Y));

if (yStr == nullptr) {
delete xStr;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
CHECK_STATE(yStr);

vector<char> errMsg(BUF_LEN, 0);

Expand All @@ -103,9 +97,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);

delete xStr;
delete yStr;

size_t sz = 0;

SAFE_UINT8_BUF(encryptedKey, BUF_LEN);
Expand All @@ -125,7 +116,7 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" + hash_with_hint.second;
string hint = libBLS::ThresholdUtils::fieldElementToString(hash_with_hint.first.Y) + ":" + hash_with_hint.second;

string sig = signature;

Expand All @@ -143,6 +134,102 @@ bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _sig);
}

bool popProveSGX( const char* encryptedKeyHex, char* prove ) {
CHECK_STATE(encryptedKeyHex);

SAFE_UINT8_BUF(encryptedKey, BUF_LEN);

size_t sz = 0;

if (!hex2carray(encryptedKeyHex, &sz, encryptedKey, BUF_LEN)) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}

sgx_status_t status = SGX_SUCCESS;

vector<char> errMsg(BUF_LEN, 0);

int errStatus = 0;

SAFE_CHAR_BUF(pubKey, 320)

status = trustedGetBlsPubKey(eid, &errStatus, errMsg.data(), encryptedKey, sz, pubKey);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

vector <string> pubKeyVect = splitString(pubKey, ':');

spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("{}", pubKeyVect.at(i));

libff::alt_bn128_G2 publicKey;
publicKey.Z = libff::alt_bn128_Fq2::one();
publicKey.X.c0 = libff::alt_bn128_Fq(pubKeyVect[0].c_str());
publicKey.X.c1 = libff::alt_bn128_Fq(pubKeyVect[1].c_str());
publicKey.Y.c0 = libff::alt_bn128_Fq(pubKeyVect[2].c_str());
publicKey.Y.c1 = libff::alt_bn128_Fq(pubKeyVect[3].c_str());

pair <libff::alt_bn128_G1, string> hashPublicKeyWithHint = libBLS::Bls::HashPublicKeyToG1WithHint( publicKey );

hashPublicKeyWithHint.first.to_affine_coordinates();

shared_ptr<string> xStr = FqToString(&(hashPublicKeyWithHint.first.X));

CHECK_STATE(xStr);

shared_ptr<string> yStr = FqToString(&(hashPublicKeyWithHint.first.Y));

CHECK_STATE(yStr);

SAFE_CHAR_BUF(xStrArg, BUF_LEN);SAFE_CHAR_BUF(yStrArg, BUF_LEN);

strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);

errStatus = 0;

status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey, sz, xStrArg, yStrArg, prove);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

string hint = libBLS::ThresholdUtils::fieldElementToString(hashPublicKeyWithHint.first.Y) + ":" + hashPublicKeyWithHint.second;

string _prove = prove;

_prove.append(":");
_prove.append(hint);

strncpy(prove, _prove.c_str(), BUF_LEN);

return true;
}

bool generateBLSPrivateKeyAggegated(const char* blsKeyName) {
CHECK_STATE(blsKeyName);

vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;

int exportable = 0;

uint64_t encBlsLen = 0;

sgx_status_t status = SGX_SUCCESS;

SAFE_UINT8_BUF(encrBlsKey, BUF_LEN)

status = trustedGenerateBLSKey(eid, &errStatus, errMsg.data(), &exportable, encrBlsKey, &encBlsLen);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

vector<char> hexBLSKey = carray2Hex(encrBlsKey, encBlsLen);

SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());

return true;
}

string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
CHECK_STATE(errStatus);
CHECK_STATE(err_string);
Expand Down
9 changes: 9 additions & 0 deletions BLSCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@

#include "stddef.h"
#include "stdint.h"
#include <memory>
#include <string>
#include <vector>

#include "bls.h"

EXTERNC bool bls_sign(const char* encryptedKeyHex, const char* hashHex, size_t t, size_t n, char* _sig);

EXTERNC bool popProveSGX( const char* encryptedKeyHex, char* _prove );

EXTERNC bool generateBLSPrivateKeyAggegated(const char* blsKeyName);

std::shared_ptr<std::string> FqToString(libff::alt_bn128_Fq *_fq);

std::string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key);

#endif //SGXWALLET_BLSCRYPTO_H
53 changes: 12 additions & 41 deletions BLSPrivateKeyShareSGX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "BLSSigShare.h"
#include "BLSSignature.h"
#include "BLSutils.h"
#include <tools/utils.h>

#include "third_party/spdlog/spdlog.h"
#include "common.h"
Expand All @@ -37,37 +37,15 @@
#include "SEKManager.h"
#include "BLSPrivateKeyShareSGX.h"

string *stringFromFq(libff::alt_bn128_Fq *_fq) {

CHECK_STATE(_fq);

mpz_t t;
mpz_init(t);

_fq->as_bigint().to_mpz(t);

SAFE_CHAR_BUF(arr, mpz_sizeinbase(t, 10) + 2);

char *tmp = mpz_get_str(arr, 10, t);

mpz_clear(t);

return new string(tmp);
}

string *stringFromG1(libff::alt_bn128_G1 *_g1) {
shared_ptr<string> stringFromG1(libff::alt_bn128_G1 *_g1) {

CHECK_STATE(_g1);

auto sX = stringFromFq(&_g1->X);
auto sY = stringFromFq(&_g1->Y);
auto sZ = stringFromFq(&_g1->Z);
auto sX = FqToString(&_g1->X);
auto sY = FqToString(&_g1->Y);
auto sZ = FqToString(&_g1->Z);

auto sG1 = new string(*sX + ":" + *sY + ":" + *sZ);

delete (sX);
delete (sY);
delete (sZ);
auto sG1 = make_shared<string>(*sX + ":" + *sY + ":" + *sZ);

return sG1;
}
Expand Down Expand Up @@ -100,28 +78,24 @@ BLSPrivateKeyShareSGX::BLSPrivateKeyShareSGX(
string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
shared_ptr <array<uint8_t, 32>> hash_byte_arr,
size_t _signerIndex) {
shared_ptr <signatures::Bls> obj;
shared_ptr <libBLS::Bls> obj;

CHECK_STATE(hash_byte_arr)

obj = make_shared<signatures::Bls>(
signatures::Bls(requiredSigners, totalSigners));
obj = make_shared<libBLS::Bls>( libBLS::Bls(requiredSigners, totalSigners));

pair <libff::alt_bn128_G1, string> hash_with_hint =
obj->HashtoG1withHint(hash_byte_arr);

int errStatus = 0;

string *xStr = stringFromFq(&(hash_with_hint.first.X));
shared_ptr<string> xStr = FqToString(&(hash_with_hint.first.X));

CHECK_STATE(xStr);

string *yStr = stringFromFq(&(hash_with_hint.first.Y));
shared_ptr<string> yStr = FqToString(&(hash_with_hint.first.Y));

if (yStr == nullptr) {
delete xStr;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
CHECK_STATE(yStr);

vector<char> errMsg(BUF_LEN, 0);

Expand All @@ -130,9 +104,6 @@ string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);

delete xStr;
delete yStr;

size_t sz = 0;

SAFE_UINT8_BUF(encryptedKey, BUF_LEN);
Expand All @@ -159,7 +130,7 @@ string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
BOOST_THROW_EXCEPTION(runtime_error("Signature is too short:" + to_string(sigLen)));
}

string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
string hint = libBLS::ThresholdUtils::fieldElementToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;

string sig = signature;
Expand Down
23 changes: 22 additions & 1 deletion CryptoTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,25 @@ bool hex2carray(const char *_hex, uint64_t *_bin_len,
}

return true;
}
}

vector <std::string> splitString(const char *coeffs, const char symbol) {
CHECK_STATE(coeffs);
std::string str(coeffs);
std::string delim;
delim.push_back(symbol);
vector <std::string> G2Strings;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos - prev);
if (!token.empty()) {
std::string coeff(token.c_str());
G2Strings.push_back(coeff);
}
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());

return G2Strings;
}
2 changes: 2 additions & 0 deletions CryptoTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ EXTERNC std::vector<char> carray2Hex(const unsigned char *d, uint64_t _len);
EXTERNC bool hex2carray(const char * _hex, uint64_t *_bin_len,
uint8_t* _bin, uint64_t _max_length );

std::vector<std::string> splitString(const char* coeffs, const char symbol);

#endif // SGXWALLET_CRYPTOTOOLS_H
21 changes: 0 additions & 21 deletions DKGCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,6 @@
#include "SEKManager.h"
#include "DKGCrypto.h"

vector <string> splitString(const char *coeffs, const char symbol) {
CHECK_STATE(coeffs);
string str(coeffs);
string delim;
delim.push_back(symbol);
vector <string> G2_strings;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos - prev);
if (!token.empty()) {
string coeff(token.c_str());
G2_strings.push_back(coeff);
}
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());

return G2_strings;
}

template<class T>
string ConvertToString(T field_elem, int base = 10) {
mpz_t t;
Expand Down
2 changes: 0 additions & 2 deletions DKGCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ vector <vector<string>> get_verif_vect(const string& encryptedPolyHex, int t);

vector <vector<string>> getVerificationVectorMult(const std::string& encryptedPolyHex, int t, int n, size_t ind);

vector<string> splitString(const char* coeffs, const char symbol);

string getSecretShares(const string& _polyName, const char* _encryptedPolyHex, const vector<string>& _publicKeys, int _t, int _n);

string getSecretSharesV2(const string& _polyName, const char* _encryptedPolyHex, const vector<string>& _publicKeys, int _t, int _n);
Expand Down
Loading

0 comments on commit 22d6c98

Please sign in to comment.