Skip to content

Commit

Permalink
SKALE-4472 change api to receive only string as input
Browse files Browse the repository at this point in the history
  • Loading branch information
olehnikolaiev committed Aug 18, 2021
1 parent 2d5635c commit 00c75c6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
11 changes: 10 additions & 1 deletion test/unit_tests_te.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,16 @@ BOOST_AUTO_TEST_CASE( EncryptionCipherToString ) {

libff::alt_bn128_G2 public_key = secret_key * libff::alt_bn128_G2::one();

auto ciphertext_string = te_instance.encryptMessage( message, public_key );
auto str = crypto::ThresholdUtils::G2ToString( public_key, 16 );
std::string common_public_str = "";
for ( auto& elem : str ) {
while ( elem.size() < 64 ) {
elem = "0" + elem;
}
common_public_str += elem;
}

auto ciphertext_string = te_instance.encryptMessage( message, common_public_str );

auto ciphertext_with_aes = te_instance.aesCiphertextFromString( ciphertext_string );

Expand Down
27 changes: 5 additions & 22 deletions threshold_encryption/threshold_encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::encryptWithAES(
}

std::string TE::encryptMessage(
const std::string& message, const libff::alt_bn128_G2& common_public ) {
const std::string& message, const std::string& common_public_str ) {
libff::alt_bn128_G2 common_public = ThresholdUtils::stringToG2( common_public_str );
auto ciphertext_with_aes = encryptWithAES( message, common_public );
return aesCiphertextToString( ciphertext_with_aes.first, ciphertext_with_aes.second );
}
Expand Down Expand Up @@ -338,27 +339,9 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::aesCiphertextFromString(
throw ThresholdUtils::IncorrectInput( "Bad aes_cipher provided" );
}

std::vector< std::string > coords_u( 4 );
coords_u[0] = u_str.substr( 0, 64 );
coords_u[1] = u_str.substr( 64, 64 );
coords_u[2] = u_str.substr( 128, 64 );
coords_u[3] = u_str.substr( 192, std::string::npos );

libff::alt_bn128_G2 U;
U.Z = libff::alt_bn128_Fq2::one();
U.X.c0 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( coords_u[0] ).c_str() );
U.X.c1 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( coords_u[1] ).c_str() );
U.Y.c0 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( coords_u[2] ).c_str() );
U.Y.c1 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( coords_u[3] ).c_str() );

std::vector< std::string > coords_w( 2 );
coords_w[0] = w_str.substr( 0, 64 );
coords_w[1] = w_str.substr( 64, std::string::npos );

libff::alt_bn128_G1 W;
W.Z = libff::alt_bn128_Fq::one();
W.X = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( coords_w[0] ).c_str() );
W.Y = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( coords_w[1] ).c_str() );
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() - 1 ) / 2 );
Expand Down
2 changes: 1 addition & 1 deletion threshold_encryption/threshold_encryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TE {
const std::string& message, const libff::alt_bn128_G2& common_public );

static std::string encryptMessage(
const std::string& message, const libff::alt_bn128_G2& common_public );
const std::string& message, const std::string& common_public );

static libff::alt_bn128_G2 getDecryptionShare(
const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key );
Expand Down
31 changes: 31 additions & 0 deletions tools/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ std::string ThresholdUtils::convertHexToDec( const std::string& hex_str ) {
return output;
}

libff::alt_bn128_G2 ThresholdUtils::stringToG2( const std::string& str ) {
if ( str.size() != 256 ) {
throw IncorrectInput("Wrong string size to convert to G2");
}

libff::alt_bn128_G2 ret;

ret.Z = libff::alt_bn128_Fq2::one();

ret.X.c0 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( str.substr( 0, 64 ) ).c_str() );
ret.X.c1 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( str.substr( 64, 64 ) ).c_str() );
ret.Y.c0 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( str.substr( 128, 64 ) ).c_str() );
ret.Y.c1 = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( str.substr( 192, std::string::npos ) ).c_str() );

return ret;
}

libff::alt_bn128_G1 ThresholdUtils::stringToG1( const std::string& str ) {
if ( str.size() != 128 ) {
throw IncorrectInput("Wrong string size to convert to G1");
}

libff::alt_bn128_G1 ret;

ret.Z = libff::alt_bn128_Fq::one();
ret.X = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( str.substr( 0, 64 ) ).c_str() );
ret.Y = libff::alt_bn128_Fq( ThresholdUtils::convertHexToDec( str.substr( 64, 64 ) ).c_str() );

return ret;
}

std::vector< libff::alt_bn128_Fr > ThresholdUtils::LagrangeCoeffs(
const std::vector< size_t >& idx, size_t t ) {
if ( idx.size() < t ) {
Expand Down
4 changes: 4 additions & 0 deletions tools/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class ThresholdUtils {

static std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem, int base = 10 );

static libff::alt_bn128_G2 stringToG2( const std::string& str );

static libff::alt_bn128_G1 stringToG1( const std::string& str );

static std::string convertHexToDec( const std::string& hex_str );

static bool checkHex( const std::string& hex );
Expand Down

0 comments on commit 00c75c6

Please sign in to comment.