Skip to content

Commit

Permalink
use smart pointers instead of new/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Jan 20, 2023
1 parent 569e579 commit 83217e1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 65 deletions.
31 changes: 8 additions & 23 deletions BLSCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
#include "third_party/intel/create_enclave.h"


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

#include "BLSPrivateKeyShareSGX.h"


#include "sgxwallet_common.h"
#include "sgxwallet.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 @@ -85,16 +82,13 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz

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 Down Expand Up @@ -183,25 +174,19 @@ bool popProveSGX( const char* encryptedKeyHex, char* prove ) {

hashPublicKeyWithHint.first.to_affine_coordinates();

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

CHECK_STATE(xStr);

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

if (yStr == nullptr) {
delete xStr;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
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);

delete xStr;
delete yStr;

errStatus = 0;

status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey, sz, xStrArg, yStrArg, prove);
Expand Down
5 changes: 5 additions & 0 deletions BLSCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +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
44 changes: 8 additions & 36 deletions BLSPrivateKeyShareSGX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -111,16 +89,13 @@ string BLSPrivateKeyShareSGX::signWithHelperSGXstr(

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 @@ -129,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 Down
8 changes: 2 additions & 6 deletions LevelDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ uint64_t LevelDB::visitKeys(LevelDB::KeyVisitor *_visitor, uint64_t _maxKeysToVi

uint64_t readCounter = 0;

leveldb::Iterator *it = db->NewIterator(readOptions);
shared_ptr<leveldb::Iterator> it( db->NewIterator(readOptions) );
for (it->SeekToFirst(); it->Valid(); it->Next()) {
_visitor->visitDBKey(it->key().data());
readCounter++;
Expand All @@ -136,16 +136,14 @@ uint64_t LevelDB::visitKeys(LevelDB::KeyVisitor *_visitor, uint64_t _maxKeysToVi
}
}

delete it;

return readCounter;
}

std::vector<string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit){
uint64_t readCounter = 0;
std::vector<string> keys;

leveldb::Iterator *it = db->NewIterator(readOptions);
shared_ptr<leveldb::Iterator> it( db->NewIterator(readOptions) );
for (it->SeekToFirst(); it->Valid(); it->Next()) {
string cur_key(it->key().data(), it->key().size());
keys.push_back(cur_key);
Expand All @@ -155,8 +153,6 @@ std::vector<string> LevelDB::writeKeysToVector1(uint64_t _maxKeysToVisit){
}
}

delete it;

return keys;
}

Expand Down

0 comments on commit 83217e1

Please sign in to comment.