Skip to content

Commit

Permalink
Merge pull request #173 from skalenetwork/add-more-te-api-methods
Browse files Browse the repository at this point in the history
Add more te api methods
  • Loading branch information
olehnikolaiev authored Feb 10, 2022
2 parents 3720559 + ef5e04e commit 26f2561
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 2,381 deletions.
1,119 changes: 0 additions & 1,119 deletions Makefile

This file was deleted.

Binary file removed dkg_keygen
Binary file not shown.
Binary file removed dkg_unit_test
Binary file not shown.
Binary file removed generate_key_system
Binary file not shown.
Binary file removed hash_g1
Binary file not shown.
1,242 changes: 0 additions & 1,242 deletions libBLS.cbp

This file was deleted.

Binary file removed libblsd.a
Binary file not shown.
Binary file removed sign_bls
Binary file not shown.
4 changes: 2 additions & 2 deletions test/te_sample_sgx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void importBLSKeys(
std::vector< libff::alt_bn128_Fr > generateSecretKeys(
size_t t, size_t n, const std::string& sgx_url );

libff::alt_bn128_G2 getDecryptionShare( const libBLS::Ciphertext& ciphertext,
const std::string& key_name, const std::string& sgx_url );
libff::alt_bn128_G2 getDecryptionShare(
const libBLS::Ciphertext& ciphertext, const std::string& key_name, const std::string& sgx_url );

int main() {
size_t t;
Expand Down
23 changes: 20 additions & 3 deletions threshold_encryption/TEDecryptSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_
std::string TEDecryptSet::merge( const libBLS::Ciphertext& cyphertext ) {
libBLS::TE::checkCypher( cyphertext );

was_merged = true;

if ( decrypts.size() < requiredSigners ) {
throw libBLS::ThresholdUtils::IsNotWellFormed( "Not enough elements to decrypt message" );
}
Expand All @@ -71,5 +69,24 @@ std::string TEDecryptSet::merge( const libBLS::Ciphertext& cyphertext ) {
decrypted.push_back( encr );
}

return te.CombineShares( cyphertext, decrypted );
auto res = te.CombineShares( cyphertext, decrypted );

was_merged = true;

return res;
}

std::vector< uint8_t > TEDecryptSet::mergeIntoAESKey() {
libBLS::TE te( requiredSigners, totalSigners );
std::vector< std::pair< libff::alt_bn128_G2, size_t > > decrypted;
for ( auto&& item : decrypts ) {
std::pair< libff::alt_bn128_G2, size_t > encr = std::make_pair( *item.second, item.first );
decrypted.push_back( encr );
}

auto res = te.CombineSharesIntoAESKey( decrypted );

was_merged = true;

return res;
}
2 changes: 2 additions & 0 deletions threshold_encryption/TEDecryptSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class TEDecryptSet {
void addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el );

std::string merge( const libBLS::Ciphertext& ciphertext );

std::vector< uint8_t > mergeIntoAESKey();
};


Expand Down
10 changes: 10 additions & 0 deletions threshold_encryption/decryptMessage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <threshold_encryption.h>
#include <fstream>

int main() {
std::ifstream encrypted_data;
encrypted_data.open( "encrypted_data.txt" );


return 0;
}
72 changes: 57 additions & 15 deletions threshold_encryption/threshold_encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,29 @@ std::string TE::CombineShares( const Ciphertext& ciphertext,
throw ThresholdUtils::IncorrectInput( "error during share combining" );
}

auto aesKey = CombineSharesIntoAESKey( decryptionShares );
std::valarray< uint8_t > lhs_to_hash( aesKey.size() );
for ( size_t i = 0; i < aesKey.size(); ++i ) {
lhs_to_hash[i] = aesKey[i];
}

std::valarray< uint8_t > rhs_to_hash( V.size() );
for ( size_t i = 0; i < V.size(); ++i ) {
rhs_to_hash[i] = static_cast< uint8_t >( V[i] );
}

std::valarray< uint8_t > xor_res = lhs_to_hash ^ rhs_to_hash;

std::string message = "";
for ( size_t i = 0; i < xor_res.size(); ++i ) {
message += static_cast< char >( xor_res[i] );
}

return message;
}

std::vector< uint8_t > TE::CombineSharesIntoAESKey(
const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShares ) {
std::vector< size_t > idx( this->t_ );
for ( size_t i = 0; i < this->t_; ++i ) {
idx[i] = decryptionShares[i].second;
Expand All @@ -254,24 +277,12 @@ std::string TE::CombineShares( const Ciphertext& ciphertext,

std::string hash = this->Hash( sum );

std::valarray< uint8_t > lhs_to_hash( hash.size() );
std::vector< uint8_t > ret( hash.size() );
for ( size_t i = 0; i < hash.size(); ++i ) {
lhs_to_hash[i] = static_cast< uint8_t >( hash[i] );
}

std::valarray< uint8_t > rhs_to_hash( V.size() );
for ( size_t i = 0; i < V.size(); ++i ) {
rhs_to_hash[i] = static_cast< uint8_t >( V[i] );
}

std::valarray< uint8_t > xor_res = lhs_to_hash ^ rhs_to_hash;

std::string message = "";
for ( size_t i = 0; i < xor_res.size(); ++i ) {
message += static_cast< char >( xor_res[i] );
ret[i] = static_cast< uint8_t >( hash[i] );
}

return message;
return ret;
}

std::string TE::aesCiphertextToString(
Expand Down Expand Up @@ -351,4 +362,35 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::aesCiphertextFromString(
return {{U, V, W}, aes_cipher};
}

Ciphertext TE::ciphertextFromString( const std::string& ciphertext ) {
ThresholdUtils::initCurve();
ThresholdUtils::initAES();

if ( !ThresholdUtils::checkHex( ciphertext ) ) {
throw ThresholdUtils::IncorrectInput( "Provided string contains non-hex symbols" );
}

if ( ciphertext.size() < 256 + 128 + 128 + 1 ) {
throw ThresholdUtils::IncorrectInput(
"Incoming string is too short to convert to aes ciphertext" );
}

std::string u_str = ciphertext.substr( 0, 256 );
std::string v_str = ciphertext.substr( 256, 128 );
std::string w_str = ciphertext.substr( 256 + 128, 128 );

libff::alt_bn128_G2 U = ThresholdUtils::stringToG2( u_str );

libff::alt_bn128_G1 W = ThresholdUtils::stringToG1( w_str );

std::string V;
V.resize( v_str.size() / 2 );
uint64_t bin_len;
if ( !ThresholdUtils::hex2carray( v_str.data(), &bin_len, ( unsigned char* ) &V[0] ) ) {
throw ThresholdUtils::IncorrectInput( "Bad encrypted aes key provided" );
}

return {U, V, W};
}

} // namespace libBLS
5 changes: 5 additions & 0 deletions threshold_encryption/threshold_encryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class TE {
std::string CombineShares( const Ciphertext& ciphertext,
const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShare );

std::vector< uint8_t > CombineSharesIntoAESKey(
const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShare );

static void checkCypher( const Ciphertext& cypher );

static std::string aesCiphertextToString(
Expand All @@ -74,6 +77,8 @@ class TE {
static std::pair< Ciphertext, std::vector< uint8_t > > aesCiphertextFromString(
const std::string& str );

static Ciphertext ciphertextFromString( const std::string& str );

private:
const size_t t_ = 0;

Expand Down
Binary file removed utils_unit_test
Binary file not shown.
Binary file removed verify_bls
Binary file not shown.

0 comments on commit 26f2561

Please sign in to comment.