From 2634001fa145a4b8df44994649ba32c385595efe Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 27 Jul 2021 19:47:42 +0300 Subject: [PATCH 01/40] SKALE-4409 move te to the same curve as bls --- bls/BLSutils.cpp | 2 +- bls/BLSutils.h | 3 +- bls/bls.h | 2 +- test/unit_tests_dkg_te.cpp | 2 +- test/unit_tests_te.cpp | 268 +++--------- threshold_encryption/CMakeLists.txt | 20 +- threshold_encryption/TEDecryptSet.cpp | 12 +- threshold_encryption/TEDecryptSet.h | 4 +- threshold_encryption/TEPrivateKey.cpp | 19 +- threshold_encryption/TEPrivateKey.h | 9 +- threshold_encryption/TEPrivateKeyShare.cpp | 59 ++- threshold_encryption/TEPrivateKeyShare.h | 10 +- threshold_encryption/TEPublicKey.cpp | 64 ++- threshold_encryption/TEPublicKey.h | 10 +- threshold_encryption/TEPublicKeyShare.cpp | 41 +- threshold_encryption/TEPublicKeyShare.h | 8 +- threshold_encryption/threshold_encryption.cpp | 389 ++++-------------- threshold_encryption/threshold_encryption.h | 71 +--- threshold_encryption/utils.cpp | 43 +- threshold_encryption/utils.h | 6 - tools/utils.cpp | 151 +++++++ tools/utils.h | 45 ++ 22 files changed, 460 insertions(+), 778 deletions(-) create mode 100644 tools/utils.cpp create mode 100644 tools/utils.h diff --git a/bls/BLSutils.cpp b/bls/BLSutils.cpp index 7c708907..8b19a484 100644 --- a/bls/BLSutils.cpp +++ b/bls/BLSutils.cpp @@ -14,7 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file BLSUtils.cpp @author Sveta Rogova diff --git a/bls/BLSutils.h b/bls/BLSutils.h index 695f895c..abc506f7 100644 --- a/bls/BLSutils.h +++ b/bls/BLSutils.h @@ -14,7 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file BLSUtils.h @author Sveta Rogova @@ -36,7 +36,6 @@ class BLSutils { const std::shared_ptr< std::string >, const std::string& delim ); static void initBLS(); - static std::atomic< bool > is_initialized; }; diff --git a/bls/bls.h b/bls/bls.h index 19ad5fa9..849e11e6 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file bls.h @author Oleh Nikolaiev diff --git a/test/unit_tests_dkg_te.cpp b/test/unit_tests_dkg_te.cpp index 71c83592..b1c97442 100644 --- a/test/unit_tests_dkg_te.cpp +++ b/test/unit_tests_dkg_te.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file unit_tests_dkg_te.cpp @author Oleh Nikolaiev diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index eae99a0d..f2c74696 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file unit_tests_te.cpp @author Oleh Nikolaiev @@ -28,65 +28,8 @@ #include -static char aparam[] = - "type a\n" - "q " - "8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422" - "662221423155858769582317459277713367317481324925129998224791\n" - "h " - "1201601226489114607938882136674053420480295440125131182291961513104720728935970453110284480218" - "3906537786776\n" - "r 730750818665451621361119245571504901405976559617\n" - "exp2 159\n" - "exp1 107\n" - "sign1 1\n" - "sign0 1\n"; - BOOST_AUTO_TEST_SUITE( ThresholdEncryption ) -BOOST_AUTO_TEST_CASE( PairingBillinearity ) { - pairing_t pairing; - - pairing_init_set_str( pairing, aparam ); - - element_t g, h; - element_t public_key, secret_key; - element_t sig; - element_t temp1, temp2; - - element_init_Zr( secret_key, pairing ); - element_init_G1( h, pairing ); - element_init_G1( sig, pairing ); - element_init_G1( g, pairing ); - element_init_G1( public_key, pairing ); - element_init_GT( temp1, pairing ); - element_init_GT( temp2, pairing ); - - element_random( g ); - element_random( secret_key ); - element_pow_zn( public_key, g, secret_key ); - - const char message[] = "abcdef"; - element_from_hash( h, ( char* ) message, 6 ); - - element_pow_zn( sig, h, secret_key ); - - pairing_apply( temp1, sig, g, pairing ); - pairing_apply( temp2, h, public_key, pairing ); - - BOOST_REQUIRE( !element_cmp( temp1, temp2 ) ); - - element_clear( g ); - element_clear( h ); - element_clear( public_key ); - element_clear( secret_key ); - element_clear( sig ); - element_clear( temp1 ); - element_clear( temp2 ); - - pairing_clear( pairing ); -} - BOOST_AUTO_TEST_CASE( SimpleEncryption ) { encryption::TE te_instance = encryption::TE( 1, 1 ); @@ -94,105 +37,56 @@ BOOST_AUTO_TEST_CASE( SimpleEncryption ) { "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 // length - element_t secret_key; - element_init_Zr( secret_key, TEDataSingleton::getData().pairing_ ); - element_random( secret_key ); - - element_t public_key; - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - element_pow_zn( public_key, TEDataSingleton::getData().generator_, secret_key ); + libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); + libff::alt_bn128_G2 public_key = secret_key * libff::alt_bn128_G2::one(); + auto ciphertext = te_instance.Encrypt( message, public_key ); - element_t decrypted; - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); - - te_instance.Decrypt( decrypted, ciphertext, secret_key ); + libff::alt_bn128_G2 decryption_share = te_instance.getDecryptionShare( ciphertext, secret_key ); - BOOST_REQUIRE( te_instance.Verify( ciphertext, decrypted, public_key ) ); + BOOST_REQUIRE( te_instance.Verify( ciphertext, decryption_share, public_key ) ); - std::vector< std::pair< encryption::element_wrapper, size_t > > shares; - encryption::element_wrapper ev( decrypted ); - shares.push_back( std::make_pair( ev, size_t( 1 ) ) ); + std::vector< std::pair< libff::alt_bn128_G2, size_t > > shares; + shares.push_back( std::make_pair( decryption_share, size_t( 1 ) ) ); std::string res = te_instance.CombineShares( ciphertext, shares ); - element_clear( secret_key ); - element_clear( public_key ); - element_clear( decrypted ); - BOOST_REQUIRE( res == message ); } BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { encryption::TE obj = encryption::TE( 11, 16 ); - std::vector< encryption::element_wrapper > coeffs( 11 ); + std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - - element_random( tmp ); - - while ( element_is0( tmp ) ) { - element_random( tmp ); + elem = libff::alt_bn128_Fr::random_element(); + while ( elem.is_zero() ) { + elem = libff::alt_bn128_Fr::random_element(); } - - elem = encryption::element_wrapper( tmp ); - - element_clear( tmp ); } - std::vector< encryption::element_wrapper > secret_keys( 16 ); + std::vector< libff::alt_bn128_Fr > secret_keys( 16 ); for ( size_t i = 0; i < 16; ++i ) { - element_t sk; - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set0( sk ); + libff::alt_bn128_Fr sk = libff::alt_bn128_Fr::zero(); for ( size_t j = 0; j < 11; ++j ) { - element_t tmp1; - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp1, i + 1 ); - - element_t tmp2; - element_init_Zr( tmp2, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp2, j ); - - element_t tmp3; - element_init_Zr( tmp3, TEDataSingleton::getData().pairing_ ); - element_pow_zn( tmp3, tmp1, tmp2 ); - - element_t tmp4; - element_init_Zr( tmp4, TEDataSingleton::getData().pairing_ ); - element_mul_zn( tmp4, coeffs[j].el_, tmp3 ); + libff::alt_bn128_Fr tmp1(i + 1); - element_clear( tmp1 ); - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_add( tmp1, sk, tmp4 ); + libff::alt_bn128_Fr tmp3 = libff::power( tmp1, j ); - element_clear( sk ); - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set( sk, tmp1 ); + libff::alt_bn128_Fr tmp4 = coeffs[j] * tmp3; - element_clear( tmp1 ); - element_clear( tmp2 ); - element_clear( tmp3 ); - element_clear( tmp4 ); + sk += tmp4; } - secret_keys[i] = encryption::element_wrapper( sk ); - - element_clear( sk ); + secret_keys[i] = sk; } - element_t common_secret; - element_init_Zr( common_secret, TEDataSingleton::getData().pairing_ ); - element_set( common_secret, coeffs[0].el_ ); + libff::alt_bn128_Fr common_secret = coeffs[0]; - element_t common_public; - element_init_G1( common_public, TEDataSingleton::getData().pairing_ ); - element_pow_zn( common_public, TEDataSingleton::getData().generator_, common_secret ); + libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 @@ -200,109 +94,60 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { auto ciphertext = obj.Encrypt( message, common_public ); - std::vector< std::pair< encryption::element_wrapper, size_t > > shares( 11 ); + std::vector< std::pair< libff::alt_bn128_G2, size_t > > shares( 11 ); for ( size_t i = 0; i < 11; ++i ) { - element_t decrypted; - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); - - obj.Decrypt( decrypted, ciphertext, secret_keys[i].el_ ); + libff::alt_bn128_G2 decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); - element_t public_key; - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - element_pow_zn( public_key, TEDataSingleton::getData().generator_, secret_keys[i].el_ ); + libff::alt_bn128_G2 public_key = secret_keys[i] * libff::alt_bn128_G2::one(); BOOST_REQUIRE( obj.Verify( ciphertext, decrypted, public_key ) ); - shares[i].first = encryption::element_wrapper( decrypted ); - - element_clear( decrypted ); - element_clear( public_key ); + shares[i].first = decrypted; shares[i].second = i + 1; } std::string res = obj.CombineShares( ciphertext, shares ); - element_clear( common_secret ); - element_clear( common_public ); - BOOST_REQUIRE( res == message ); } BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { encryption::TE obj = encryption::TE( 11, 16 ); - std::vector< encryption::element_wrapper > coeffs( 11 ); + std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - - element_random( tmp ); - - while ( element_is0( tmp ) ) { - element_random( tmp ); + elem = libff::alt_bn128_Fr::random_element(); + while ( elem.is_zero() ) { + elem = libff::alt_bn128_Fr::random_element(); } - - elem = encryption::element_wrapper( tmp ); - - element_clear( tmp ); } - std::vector< encryption::element_wrapper > secret_keys( 16 ); + std::vector< libff::alt_bn128_Fr > secret_keys( 16 ); for ( size_t i = 0; i < 16; ++i ) { - element_t sk; - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set0( sk ); + libff::alt_bn128_Fr sk = libff::alt_bn128_Fr::zero(); for ( size_t j = 0; j < 11; ++j ) { - element_t tmp1; - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp1, i + 1 ); + libff::alt_bn128_Fr tmp1(i + 1); - element_t tmp2; - element_init_Zr( tmp2, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp2, j ); - - element_t tmp3; - element_init_Zr( tmp3, TEDataSingleton::getData().pairing_ ); - element_pow_zn( tmp3, tmp1, tmp2 ); - - element_t tmp4; - element_init_Zr( tmp4, TEDataSingleton::getData().pairing_ ); - element_mul_zn( tmp4, coeffs[j].el_, tmp3 ); - - element_clear( tmp1 ); - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_add( tmp1, sk, tmp4 ); + libff::alt_bn128_Fr tmp3 = libff::power( tmp1, j ); - element_clear( sk ); - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set( sk, tmp1 ); + libff::alt_bn128_Fr tmp4 = coeffs[j] * tmp3; - element_clear( tmp1 ); - element_clear( tmp2 ); - element_clear( tmp3 ); - element_clear( tmp4 ); + sk += tmp4; } - secret_keys[i] = encryption::element_wrapper( sk ); - - element_clear( sk ); + secret_keys[i] = sk; } - element_t common_secret; - element_init_Zr( common_secret, TEDataSingleton::getData().pairing_ ); - element_set( common_secret, coeffs[0].el_ ); - - element_t common_public; - element_init_G1( common_public, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_Fr common_secret = coeffs[0]; // element_pow_zn(common_public, obj.generator_, common_secret); // let common_public be a random element of G1 instead of correct one in the previous line - element_random( common_public ); + libff::alt_bn128_G2 common_public = libff::alt_bn128_G2::random_element(); std::string message = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 @@ -310,31 +155,20 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { auto ciphertext = obj.Encrypt( message, common_public ); - std::vector< std::pair< encryption::element_wrapper, size_t > > shares( 11 ); + std::vector< std::pair< libff::alt_bn128_G2, size_t > > shares( 11 ); for ( size_t i = 0; i < 11; ++i ) { - element_t decrypted; - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); - - obj.Decrypt( decrypted, ciphertext, secret_keys[i].el_ ); + libff::alt_bn128_G2 decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); - element_t public_key; - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - element_pow_zn( public_key, TEDataSingleton::getData().generator_, secret_keys[i].el_ ); + libff::alt_bn128_G2 public_key = secret_keys[i] * libff::alt_bn128_G2::one(); BOOST_REQUIRE( obj.Verify( ciphertext, decrypted, public_key ) ); - shares[i].first = encryption::element_wrapper( decrypted ); - - element_clear( decrypted ); - element_clear( public_key ); + shares[i].first = decrypted; shares[i].second = i + 1; } - element_clear( common_secret ); - element_clear( common_public ); - std::string res = obj.CombineShares( ciphertext, shares ); BOOST_REQUIRE( res != message ); @@ -343,23 +177,15 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { encryption::TE obj = encryption::TE( 11, 16 ); - std::vector< encryption::element_wrapper > coeffs( 11 ); + std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - - element_random( tmp ); - - while ( element_is0( tmp ) ) { - element_random( tmp ); + elem = libff::alt_bn128_Fr::random_element(); + while ( elem.is_zero() ) { + elem = libff::alt_bn128_Fr::random_element(); } - - elem = encryption::element_wrapper( tmp ); - - element_clear( tmp ); } - std::vector< encryption::element_wrapper > secret_keys( 16 ); + std::vector< libff::alt_bn128_Fr > secret_keys( 16 ); for ( size_t i = 0; i < 16; ++i ) { element_t sk; diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 25652139..84e91a7c 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -16,10 +16,11 @@ set(sources TEPublicKey.cpp TEPublicKeyShare.cpp TEDataSingleton.h - ../dkg/dkg_te.cpp - ../dkg/DKGTEWrapper.cpp - ../dkg/DKGTESecret.cpp + ../dkg/dkg.cpp + ../dkg/DKGBLSWrapper.cpp + ../dkg/DKGBLSSecret.cpp utils.cpp + ../tools/utils.cpp ) set(headers @@ -30,10 +31,11 @@ set(headers TEPublicKey.h TEPublicKeyShare.h TEDataSingleton.cpp - ../dkg/dkg_te.h - ../dkg/DKGTEWrapper.h - ../dkg/DKGTESecret.h + ../dkg/dkg.h + ../dkg/DKGBLSWrapper.h + ../dkg/DKGBLSSecret.h utils.h + ../tools/utils.h ) set(PROJECT_VERSION 0.1.0) @@ -56,9 +58,9 @@ target_include_directories(te PRIVATE ${PBC_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_ target_link_libraries(te PRIVATE pbc) if (BUILD_TESTS) - add_executable(dkg_te_unit_test ../test/unit_tests_dkg_te.cpp) - target_include_directories(dkg_te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${TEST_DIR}) - target_link_libraries(dkg_te_unit_test PRIVATE te pbc ${GMP_LIBRARY} ${GMPXX_LIBRARY}) + # add_executable(dkg_te_unit_test ../test/unit_tests_dkg_te.cpp) + # target_include_directories(dkg_te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${TEST_DIR}) + # target_link_libraries(dkg_te_unit_test PRIVATE te pbc ${GMP_LIBRARY} ${GMPXX_LIBRARY}) add_test(NAME dkg_te_tests COMMAND dkg_te_unit_test) diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index f66a2a14..d5c987bd 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -29,11 +29,10 @@ along with libBLS. If not, see . TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + // TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); } -void TEDecryptSet::addDecrypt( - size_t _signerIndex, std::shared_ptr< encryption::element_wrapper > _el ) { +void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el ) { if ( decrypts.count( _signerIndex ) > 0 ) { throw std::runtime_error( "Already have this index:" + std::to_string( _signerIndex ) ); } @@ -46,7 +45,7 @@ void TEDecryptSet::addDecrypt( throw std::runtime_error( "try to add Null _element to decrypt set" ); } - if ( isG1Element0( _el->el_ ) ) { + if ( _el->is_zero() ) { throw std::runtime_error( "try to add zero _element to decrypt set" ); } @@ -63,10 +62,9 @@ std::string TEDecryptSet::merge( const encryption::Ciphertext& cyphertext ) { } encryption::TE te( requiredSigners, totalSigners ); - std::vector< std::pair< encryption::element_wrapper, size_t > > decrypted; + std::vector< std::pair< libff::alt_bn128_G2, size_t > > decrypted; for ( auto&& item : decrypts ) { - std::pair< encryption::element_wrapper, size_t > encr = - std::make_pair( *item.second, item.first ); + std::pair< libff::alt_bn128_G2, size_t > encr = std::make_pair( *item.second, item.first ); decrypted.push_back( encr ); } diff --git a/threshold_encryption/TEDecryptSet.h b/threshold_encryption/TEDecryptSet.h index 59dba125..fd08eab5 100644 --- a/threshold_encryption/TEDecryptSet.h +++ b/threshold_encryption/TEDecryptSet.h @@ -34,12 +34,12 @@ class TEDecryptSet { bool was_merged; - std::map< size_t, std::shared_ptr< encryption::element_wrapper > > decrypts; + std::map< size_t, std::shared_ptr< libff::alt_bn128_G2 > > decrypts; public: TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ); - void addDecrypt( size_t _signerIndex, std::shared_ptr< encryption::element_wrapper > _el ); + void addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el ); std::string merge( const encryption::Ciphertext& ciphertext ); }; diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 4ab3a828..15f24921 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -14,13 +14,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @date 2019 */ +#include "../tools/utils.h" #include #include @@ -33,30 +34,26 @@ TEPrivateKey::TEPrivateKey( throw std::runtime_error( "private key is null" ); } - element_t pkey; - element_init_Zr( pkey, TEDataSingleton::getData().pairing_ ); - element_set_str( pkey, _key_str->c_str(), 10 ); - privateKey = encryption::element_wrapper( pkey ); - element_clear( pkey ); + privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); - if ( element_is0( privateKey.el_ ) ) { + if ( privateKey.is_zero() ) { throw std::runtime_error( " private key is zero" ); } } TEPrivateKey::TEPrivateKey( - encryption::element_wrapper _skey, size_t _requiredSigners, size_t _totalSigners ) + libff::alt_bn128_Fr _skey, size_t _requiredSigners, size_t _totalSigners ) : privateKey( _skey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); - if ( element_is0( _skey.el_ ) ) + if ( _skey.is_zero() ) throw std::runtime_error( " private key is zero" ); } std::string TEPrivateKey::toString() { - return ElementZrToString( privateKey.el_ ); + return fieldElementToString( privateKey ); } -encryption::element_wrapper TEPrivateKey::getPrivateKey() const { +libff::alt_bn128_Fr TEPrivateKey::getPrivateKey() const { return privateKey; } diff --git a/threshold_encryption/TEPrivateKey.h b/threshold_encryption/TEPrivateKey.h index 599c8310..cb2d875e 100644 --- a/threshold_encryption/TEPrivateKey.h +++ b/threshold_encryption/TEPrivateKey.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @@ -29,7 +29,7 @@ class TEPrivateKey { private: - encryption::element_wrapper privateKey; + libff::alt_bn128_Fr privateKey; size_t requiredSigners; size_t totalSigners; @@ -38,12 +38,11 @@ class TEPrivateKey { TEPrivateKey( std::shared_ptr< std::string > _key_str_ptr, size_t _requiredSigners, size_t _totalSigners ); - TEPrivateKey( - encryption::element_wrapper _skey, size_t _requiredSigners, size_t _totalSigners ); + TEPrivateKey( libff::alt_bn128_Fr _skey, size_t _requiredSigners, size_t _totalSigners ); std::string toString(); - encryption::element_wrapper getPrivateKey() const; + libff::alt_bn128_Fr getPrivateKey() const; }; diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index d56f0027..97707bec 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -14,14 +14,15 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @date 2019 */ -#include +#include "../tools/utils.h" +#include #include #include @@ -36,18 +37,14 @@ TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, s throw std::runtime_error( "private key share is null" ); } - element_t pkey; - element_init_Zr( pkey, TEDataSingleton::getData().pairing_ ); - element_set_str( pkey, _key_str->c_str(), 10 ); - privateKey = encryption::element_wrapper( pkey ); - element_clear( pkey ); + privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); - if ( element_is0( privateKey.el_ ) ) { + if ( privateKey.is_zero() ) { throw std::runtime_error( "Zero private key share" ); } } -TEPrivateKeyShare::TEPrivateKeyShare( encryption::element_wrapper _skey_share, size_t _signerIndex, +TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ) : privateKey( _skey_share ), signerIndex( _signerIndex ), @@ -55,71 +52,65 @@ TEPrivateKeyShare::TEPrivateKeyShare( encryption::element_wrapper _skey_share, s totalSigners( _totalSigners ) { TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); - /* if (_signerIndex > _totalSigners) { - throw std::runtime_error ("Wrong _signerIndex"); - }*/ - if ( element_is0( _skey_share.el_ ) ) { + if ( _signerIndex > _totalSigners ) { + throw std::runtime_error( "Wrong _signerIndex" ); + } + + if ( _skey_share.is_zero() ) { throw std::runtime_error( "Zero private key share" ); } } -encryption::element_wrapper TEPrivateKeyShare::decrypt( encryption::Ciphertext& cypher ) { - checkCypher( cypher ); +libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( encryption::Ciphertext& cipher ) { + checkCypher( cipher ); encryption::TE te( requiredSigners, totalSigners ); element_t decrypt; element_init_G1( decrypt, TEDataSingleton::getData().pairing_ ); - te.Decrypt( decrypt, cypher, privateKey.el_ ); - encryption::element_wrapper decrypted( decrypt ); + libff::alt_bn128_G2 decryption_share = te.getDecryptionShare( cipher, privateKey ); - if ( isG1Element0( decrypt ) ) { + if ( decryption_share.is_zero() ) { std::runtime_error( "zero decrypt" ); } - element_clear( decrypt ); - return decrypted; + + return decryption_share; } std::string TEPrivateKeyShare::toString() { - return ElementZrToString( privateKey.el_ ); + return fieldElementToString( privateKey ); } size_t TEPrivateKeyShare::getSignerIndex() const { return signerIndex; } -encryption::element_wrapper TEPrivateKeyShare::getPrivateKey() const { +libff::alt_bn128_Fr TEPrivateKeyShare::getPrivateKey() const { return privateKey; } std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > TEPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) { - encryption::DkgTe dkg_te( _requiredSigners, _totalSigners ); - - std::vector< encryption::element_wrapper > poly = dkg_te.GeneratePolynomial(); - element_t zero; - element_init_Zr( zero, TEDataSingleton::getData().pairing_ ); - element_set0( zero ); - encryption::element_wrapper zero_el( zero ); + signatures::Dkg dkg_te( _requiredSigners, _totalSigners ); - element_clear( zero ); + std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - encryption::element_wrapper common_skey = dkg_te.ComputePolynomialValue( poly, zero_el ); + libff::alt_bn128_Fr common_skey = dkg_te.PolynomialValue( poly, libff::alt_bn128_Fr::zero() ); TEPrivateKey common_private( common_skey, _requiredSigners, _totalSigners ); TEPublicKey common_public( common_private, _requiredSigners, _totalSigners ); - std::vector< encryption::element_wrapper > skeys = dkg_te.CreateSecretKeyContribution( poly ); + std::vector< libff::alt_bn128_Fr > skeys = dkg_te.SecretKeyContribution( poly ); std::vector< std::shared_ptr< TEPrivateKeyShare > > skey_shares; for ( size_t i = 0; i < _totalSigners; i++ ) { - TEPrivateKeyShare skey( skeys[i].el_, i + 1, _requiredSigners, _totalSigners ); + TEPrivateKeyShare skey( skeys[i], i + 1, _requiredSigners, _totalSigners ); skey_shares.emplace_back( std::make_shared< TEPrivateKeyShare >( skey ) ); } - std::pair keys = std::make_pair( + auto keys = std::make_pair( std::make_shared< std::vector< std::shared_ptr< TEPrivateKeyShare > > >( skey_shares ), std::make_shared< TEPublicKey >( common_public ) ); return keys; diff --git a/threshold_encryption/TEPrivateKeyShare.h b/threshold_encryption/TEPrivateKeyShare.h index 9bf2d6da..2358214f 100644 --- a/threshold_encryption/TEPrivateKeyShare.h +++ b/threshold_encryption/TEPrivateKeyShare.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @@ -30,7 +30,7 @@ class TEPrivateKeyShare { private: - encryption::element_wrapper privateKey; + libff::alt_bn128_Fr privateKey; size_t signerIndex; size_t requiredSigners; @@ -40,10 +40,10 @@ class TEPrivateKeyShare { TEPrivateKeyShare( std::shared_ptr< std::string > _key_str_ptr, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ); - TEPrivateKeyShare( encryption::element_wrapper _skey_share, size_t _signerIndex, + TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ); - encryption::element_wrapper decrypt( encryption::Ciphertext& cipher ); + libff::alt_bn128_G2 getDecryptionShare( encryption::Ciphertext& cipher ); static std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > @@ -53,7 +53,7 @@ class TEPrivateKeyShare { size_t getSignerIndex() const; - encryption::element_wrapper getPrivateKey() const; + libff::alt_bn128_Fr getPrivateKey() const; }; diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 4ef3ba29..bfb7a214 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -14,13 +14,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @date 2019 */ +#include "../tools/utils.h" #include #include #include @@ -38,48 +39,48 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str throw std::runtime_error( "public key is null" ); } - std::string key_str = "[" + _key_str_ptr->at( 0 ) + "," + _key_str_ptr->at( 1 ) + "]"; + if ( _key_str_ptr->size() != 4 ) { + throw std::runtime_error( "wrong number of components in public key share" ); + } + + if ( !isStringNumber( _key_str_ptr->at( 0 ) ) || !isStringNumber( _key_str_ptr->at( 1 ) ) || + !isStringNumber( _key_str_ptr->at( 2 ) ) || !isStringNumber( _key_str_ptr->at( 3 ) ) ) { + throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); + } - element_t pkey; - element_init_G1( pkey, TEDataSingleton::getData().pairing_ ); - element_set_str( pkey, key_str.c_str(), 10 ); - PublicKey = encryption::element_wrapper( pkey ); - element_clear( pkey ); + PublicKey.Z = libff::alt_bn128_Fq2::one(); + PublicKey.X.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 0 ).c_str() ); + PublicKey.X.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 1 ).c_str() ); + PublicKey.Y.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 2 ).c_str() ); + PublicKey.Y.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 3 ).c_str() ); - if ( isG1Element0( PublicKey.el_ ) ) { + if ( PublicKey.is_zero() ) { throw std::runtime_error( "corrupted string or zero public key" ); } } TEPublicKey::TEPublicKey( - TEPrivateKey _comon_private, size_t _requiredSigners, size_t _totalSigners ) + TEPrivateKey _common_private, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); - if ( element_is0( _comon_private.getPrivateKey().el_ ) ) { + if ( _common_private.getPrivateKey().is_zero() ) { throw std::runtime_error( "zero key" ); } - element_t pkey; - element_init_G1( pkey, TEDataSingleton::getData().pairing_ ); - element_mul_zn( - pkey, TEDataSingleton::getData().generator_, _comon_private.getPrivateKey().el_ ); - - PublicKey = pkey; - element_clear( pkey ); + PublicKey = _common_private.getPrivateKey() * libff::alt_bn128_G2::one(); } -TEPublicKey::TEPublicKey( - encryption::element_wrapper _pkey, size_t _requiredSigners, size_t _totalSigners ) +TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, size_t _totalSigners ) : PublicKey( _pkey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); - if ( isG1Element0( _pkey.el_ ) ) { + if ( _pkey.is_zero() ) { throw std::runtime_error( "zero public key" ); } } -encryption::Ciphertext TEPublicKey::encrypt( const std::shared_ptr< std::string >& mes_ptr ) { +encryption::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr ) { encryption::TE te( requiredSigners, totalSigners ); if ( mes_ptr == nullptr ) { @@ -90,35 +91,26 @@ encryption::Ciphertext TEPublicKey::encrypt( const std::shared_ptr< std::string throw std::runtime_error( "Message length is not equal to 64" ); } - encryption::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey.el_ ); + encryption::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); checkCypher( cypher ); - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_set( U, std::get< 0 >( cypher ).el_ ); - encryption::element_wrapper U_wrap( U ); + libff::alt_bn128_G2 U = std::get< 0 >( cypher ); /*if (element_item_count(U) == 0 ) { throw std::runtime_error("U is zero"); }*/ - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_set( W, std::get< 2 >( cypher ).el_ ); + libff::alt_bn128_G1 W = std::get< 2 >( cypher ); /*if (element_item_count(W) == 0) { throw std::runtime_error("W is zero"); }*/ - encryption::element_wrapper W_wrap( W ); - element_clear( W ); - - element_clear( U ); - return std::make_tuple( U_wrap, std::get< 1 >( cypher ), W_wrap ); + return std::make_tuple( U, std::get< 1 >( cypher ), W ); } std::shared_ptr< std::vector< std::string > > TEPublicKey::toString() { - return ElementG1ToString( PublicKey.el_ ); + return std::make_shared< std::vector< std::string > >( G2ToString( PublicKey ) ); } -encryption::element_wrapper TEPublicKey::getPublicKey() const { +libff::alt_bn128_G2 TEPublicKey::getPublicKey() const { return PublicKey; } diff --git a/threshold_encryption/TEPublicKey.h b/threshold_encryption/TEPublicKey.h index 4008d8fd..50dac6a2 100644 --- a/threshold_encryption/TEPublicKey.h +++ b/threshold_encryption/TEPublicKey.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @@ -29,7 +29,7 @@ class TEPublicKey { private: - encryption::element_wrapper PublicKey; + libff::alt_bn128_G2 PublicKey; size_t requiredSigners; size_t totalSigners; @@ -39,15 +39,15 @@ class TEPublicKey { TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str_ptr, size_t _requiredSigners, size_t _totalSigners ); - TEPublicKey( encryption::element_wrapper _pkey, size_t _requiredSigners, size_t _totalSigners ); + TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, size_t _totalSigners ); TEPublicKey( TEPrivateKey _comon_private, size_t _requiredSigners, size_t _totalSigners ); std::shared_ptr< std::vector< std::string > > toString(); - encryption::Ciphertext encrypt( const std::shared_ptr< std::string >& message ); + encryption::Ciphertext encrypt( std::shared_ptr< std::string > message ); - encryption::element_wrapper getPublicKey() const; + libff::alt_bn128_G2 getPublicKey() const; }; diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index b04deb00..bf53cf77 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -14,13 +14,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @date 2019 */ +#include "../tools/utils.h" #include #include #include @@ -36,21 +37,22 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > throw std::runtime_error( "public key share is null" ); } - if ( _key_str_ptr->size() != 2 ) + if ( _key_str_ptr->size() != 4 ) { throw std::runtime_error( "wrong number of components in public key share" ); + } - if ( !isStringNumber( _key_str_ptr->at( 0 ) ) || !isStringNumber( _key_str_ptr->at( 1 ) ) ) + if ( !isStringNumber( _key_str_ptr->at( 0 ) ) || !isStringNumber( _key_str_ptr->at( 1 ) ) || + !isStringNumber( _key_str_ptr->at( 2 ) ) || !isStringNumber( _key_str_ptr->at( 3 ) ) ) { throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); + } - std::string key_str = "[" + _key_str_ptr->at( 0 ) + "," + _key_str_ptr->at( 1 ) + "]"; - - element_t pkey; - element_init_G1( pkey, TEDataSingleton::getData().pairing_ ); - element_set_str( pkey, key_str.c_str(), 10 ); - PublicKey = encryption::element_wrapper( pkey ); - element_clear( pkey ); + PublicKey.Z = libff::alt_bn128_Fq2::one(); + PublicKey.X.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 0 ).c_str() ); + PublicKey.X.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 1 ).c_str() ); + PublicKey.Y.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 2 ).c_str() ); + PublicKey.Y.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 3 ).c_str() ); - if ( isG1Element0( PublicKey.el_ ) ) { + if ( PublicKey.is_zero() ) { throw std::runtime_error( "corrupted string or zero public key share" ); } } @@ -60,31 +62,26 @@ TEPublicKeyShare::TEPublicKeyShare( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); - element_t pkey; - element_init_G1( pkey, TEDataSingleton::getData().pairing_ ); - element_mul_zn( pkey, TEDataSingleton::getData().generator_, _p_key.getPrivateKey().el_ ); - - PublicKey = pkey; + PublicKey = _p_key.getPrivateKey() * libff::alt_bn128_G2::one(); signerIndex = _p_key.getSignerIndex(); - element_clear( pkey ); } bool TEPublicKeyShare::Verify( - const encryption::Ciphertext& cyphertext, const element_t& decrypted ) { + const encryption::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { checkCypher( cyphertext ); - if ( isG1Element0( const_cast< element_t& >( decrypted ) ) ) { + if ( decryptionShare.is_zero() ) { throw std::runtime_error( "zero decrypt" ); } encryption::TE te( requiredSigners, totalSigners ); - return te.Verify( cyphertext, decrypted, PublicKey.el_ ); + return te.Verify( cyphertext, decryptionShare, PublicKey ); } std::shared_ptr< std::vector< std::string > > TEPublicKeyShare::toString() { - return ElementG1ToString( PublicKey.el_ ); + return std::make_shared< std::vector< std::string > >( G2ToString( PublicKey ) ); } -encryption::element_wrapper TEPublicKeyShare::getPublicKey() const { +libff::alt_bn128_G2 TEPublicKeyShare::getPublicKey() const { return PublicKey; } diff --git a/threshold_encryption/TEPublicKeyShare.h b/threshold_encryption/TEPublicKeyShare.h index 5846e0a5..b91e346a 100644 --- a/threshold_encryption/TEPublicKeyShare.h +++ b/threshold_encryption/TEPublicKeyShare.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @@ -29,7 +29,7 @@ class TEPublicKeyShare { private: - encryption::element_wrapper PublicKey; + libff::alt_bn128_G2 PublicKey; size_t signerIndex; size_t requiredSigners; @@ -42,11 +42,11 @@ class TEPublicKeyShare { TEPublicKeyShare( TEPrivateKeyShare _p_key, size_t _requiredSigners, size_t _totalSigners ); - bool Verify( const encryption::Ciphertext& ciphertext, const element_t& decrypted ); + bool Verify( const encryption::Ciphertext& ciphertext, const libff::alt_bn128_G2& decrypted ); std::shared_ptr< std::vector< std::string > > toString(); - encryption::element_wrapper getPublicKey() const; + libff::alt_bn128_G2 getPublicKey() const; }; diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 75799fbe..b1f69ed7 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file threshold_encryption.cpp @author Oleh Nikolaiev @@ -25,6 +25,7 @@ #include #include +#include "../tools/utils.h" #include #include @@ -34,142 +35,50 @@ TE::TE( const size_t t, const size_t n ) : t_( t ), n_( n ) {} TE::~TE() {} -std::string TE::Hash( const element_t& Y, std::string ( *hash_func )( const std::string& str ) ) { - // assumed that Y lies in from G1 +std::string TE::Hash( + const libff::alt_bn128_G2& Y, std::string ( *hash_func )( const std::string& str ) ) { + auto vectorCoordinates = G2ToString( Y ); - mpz_t z; - mpz_init( z ); - element_to_mpz( z, element_item( const_cast< element_t& >( Y ), 0 ) ); - - char arr[mpz_sizeinbase( z, 10 ) + 2]; - char* tmp_c = mpz_get_str( arr, 10, z ); - std::string tmp1 = tmp_c; - mpz_clear( z ); - - mpz_init( z ); - element_to_mpz( z, element_item( const_cast< element_t& >( Y ), 1 ) ); - - char arr1[mpz_sizeinbase( z, 10 ) + 2]; - char* other_tmp = mpz_get_str( arr1, 10, z ); - std::string tmp2 = other_tmp; - mpz_clear( z ); - - std::string tmp = tmp1 + tmp2; + std::string tmp = ""; + for ( const auto& coord : vectorCoordinates ) { + tmp += coord; + } const std::string sha256hex = hash_func( tmp ); return sha256hex; } -void TE::HashToGroup( element_t ret_val, const element_t& U, const std::string& V, +libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::string& V, std::string ( *hash_func )( const std::string& str ) ) { // assumed that U lies in G1 - std::shared_ptr< std::vector< std::string > > U_str_ptr = - ElementG1ToString( const_cast< element_t& >( U ) ); - - const std::string sha256hex = hash_func( U_str_ptr->at( 0 ) + U_str_ptr->at( 1 ) + V ); - - mpz_t hex; - mpz_init( hex ); - mpz_set_str( hex, sha256hex.c_str(), 16 ); - - mpz_t modulus_q; - mpz_init( modulus_q ); - mpz_set_str( modulus_q, - "878071079966331252243778198475404981580688319941420821102865339926647563088022295707862517" - "9422662221423155858769582317459277713367317481324925129998224791", - 10 ); - - mpz_t x_coord; - mpz_init( x_coord ); - mpz_mod( x_coord, hex, modulus_q ); - - mpz_clear( hex ); - - mpz_t y_coord; - mpz_init( y_coord ); - - mpz_t one; - mpz_init( one ); - mpz_set_ui( one, 1 ); - - while ( true ) { - mpz_t x_cubed; - mpz_init( x_cubed ); - - mpz_powm_ui( x_cubed, x_coord, 3, modulus_q ); - - mpz_t sum; - mpz_init( sum ); - mpz_add( sum, x_cubed, x_coord ); - mpz_clear( x_cubed ); - - mpz_t y_squared; - mpz_init( y_squared ); - mpz_mod( y_squared, sum, modulus_q ); - mpz_clear( sum ); + auto U_str = G2ToString( U ); - int is_square = mpz_legendre( y_squared, modulus_q ); + const std::string sha256hex = hash_func( U_str[0] + U_str[1] + U_str[2] + U_str[3] + V ); - if ( is_square == -1 || is_square == 0 ) { - mpz_addmul_ui( x_coord, one, 1 ); - mpz_clear( y_squared ); - - } else { - MpzSquareRoot( y_coord, y_squared ); - - mpz_clear( y_squared ); - mpz_clear( one ); - - break; - } - } - - char arr1[mpz_sizeinbase( x_coord, 10 ) + 2]; - char* coord_x = mpz_get_str( arr1, 10, x_coord ); - - char arr2[mpz_sizeinbase( y_coord, 10 ) + 2]; - char* coord_y = mpz_get_str( arr2, 10, y_coord ); - - std::string coords_str = "[" + std::string( coord_x ) + "," + std::string( coord_y ) + "]"; - - - int num = element_set_str( ret_val, coords_str.c_str(), 10 ); - if ( num == 0 ) { - std::runtime_error( "Incorrectly formed string for G1 point" ); + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); + std::string hash_str = cryptlite::sha256::hash_hex( sha256hex ); + for ( size_t i = 0; i < 32; ++i ) { + hash_bytes_arr->at( i ) = static_cast< uint8_t >( hash_str[i] ); } - mpz_clear( modulus_q ); - mpz_clear( y_coord ); - mpz_clear( x_coord ); + return HashtoG1( hash_bytes_arr ); } -Ciphertext TE::Encrypt( const std::string& message, const element_t& common_public ) { - element_t r; - element_init_Zr( r, TEDataSingleton::getData().pairing_ ); - element_random( r ); +Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ) { + libff::alt_bn128_Fr r = libff::alt_bn128_Fr::random_element(); - while ( element_is0( r ) ) { - element_random( r ); + while ( r.is_zero() ) { + r = libff::alt_bn128_Fr::random_element(); } - element_t g; - element_init_G1( g, TEDataSingleton::getData().pairing_ ); - element_set( g, TEDataSingleton::getData().generator_ ); - - element_t U, Y; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_init_G1( Y, TEDataSingleton::getData().pairing_ ); - element_mul_zn( U, g, r ); - element_mul_zn( Y, const_cast< element_t& >( common_public ), r ); - - element_clear( g ); + libff::alt_bn128_G2 U, Y; + U = r * libff::alt_bn128_G2::one(); + Y = r * common_public; std::string hash = this->Hash( Y ); - element_clear( Y ); - // assuming message and hash are the same size strings // the behaviour is undefined when the two arguments are valarrays with different sizes @@ -191,203 +100,118 @@ Ciphertext TE::Encrypt( const std::string& message, const element_t& common_publ V += static_cast< char >( res[i] ); } - element_t W, H; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_init_G1( H, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_G1 W, H; - this->HashToGroup( H, U, V ); - element_mul_zn( W, H, r ); - - element_clear( H ); - element_clear( r ); + H = this->HashToGroup( U, V ); + W = r * H; Ciphertext result; - std::get< 0 >( result ) = element_wrapper( U ); + std::get< 0 >( result ) = U; std::get< 1 >( result ) = V; - std::get< 2 >( result ) = element_wrapper( W ); - - element_clear( U ); - element_clear( W ); + std::get< 2 >( result ) = W; return result; } -void TE::Decrypt( element_t ret_val, const Ciphertext& ciphertext, const element_t& secret_key ) { +libff::alt_bn128_G2 TE::getDecryptionShare( + const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key ) { checkCypher( ciphertext ); - if ( element_is0( const_cast< element_t& >( secret_key ) ) ) + if ( secret_key.is_zero() ) throw std::runtime_error( "zero secret key" ); - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_set( U, const_cast< element_t& >( std::get< 0 >( ciphertext ).el_ ) ); + libff::alt_bn128_G2 U = std::get< 0 >( ciphertext ); std::string V = std::get< 1 >( ciphertext ); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_set( W, const_cast< element_t& >( std::get< 2 >( ciphertext ).el_ ) ); + libff::alt_bn128_G1 W = std::get< 2 >( ciphertext ); - element_t H; - element_init_G1( H, TEDataSingleton::getData().pairing_ ); - this->HashToGroup( H, U, V ); + libff::alt_bn128_G1 H = this->HashToGroup( U, V ); - element_t fst, snd; - element_init_GT( fst, TEDataSingleton::getData().pairing_ ); - element_init_GT( snd, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_GT fst, snd; + fst = libff::alt_bn128_ate_reduced_pairing( W, libff::alt_bn128_G2::one() ); + snd = libff::alt_bn128_ate_reduced_pairing( H, U ); - pairing_apply( - fst, TEDataSingleton::getData().generator_, W, TEDataSingleton::getData().pairing_ ); - pairing_apply( snd, U, H, TEDataSingleton::getData().pairing_ ); + bool res = fst == snd; - bool res = element_cmp( fst, snd ); - - element_clear( fst ); - element_clear( snd ); - - if ( res ) { - element_clear( U ); - element_clear( W ); - element_clear( H ); + if ( !res ) { throw std::runtime_error( "cannot decrypt data" ); } - element_mul_zn( ret_val, U, const_cast< element_t& >( secret_key ) ); + libff::alt_bn128_G2 ret_val = secret_key * U; - element_clear( U ); - element_clear( W ); - element_clear( H ); + return ret_val; } -bool TE::Verify( - const Ciphertext& ciphertext, const element_t& decrypted, const element_t& public_key ) { - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_set( U, const_cast< element_t& >( std::get< 0 >( ciphertext ).el_ ) ); +bool TE::Verify( const Ciphertext& ciphertext, const libff::alt_bn128_G2& decryptionShare, + const libff::alt_bn128_G2& public_key ) { + libff::alt_bn128_G2 U = std::get< 0 >( ciphertext ); std::string V = std::get< 1 >( ciphertext ); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_set( W, const_cast< element_t& >( std::get< 2 >( ciphertext ).el_ ) ); + libff::alt_bn128_G1 W = std::get< 2 >( ciphertext ); - element_t H; - element_init_G1( H, TEDataSingleton::getData().pairing_ ); - this->HashToGroup( H, U, V ); + libff::alt_bn128_G1 H = this->HashToGroup( U, V ); - element_t fst, snd; - element_init_GT( fst, TEDataSingleton::getData().pairing_ ); - element_init_GT( snd, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_GT fst, snd; + fst = libff::alt_bn128_ate_reduced_pairing( W, libff::alt_bn128_G2::one() ); + snd = libff::alt_bn128_ate_reduced_pairing( H, U ); - pairing_apply( - fst, TEDataSingleton::getData().generator_, W, TEDataSingleton::getData().pairing_ ); - pairing_apply( snd, U, H, TEDataSingleton::getData().pairing_ ); - - bool res = !element_cmp( fst, snd ); + bool res = fst == snd; bool ret_val = true; if ( res ) { - if ( isG1Element0( const_cast< element_t& >( decrypted ) ) ) { + if ( decryptionShare.is_zero() ) { ret_val = false; } else { - element_t pp1, pp2; - element_init_GT( pp1, TEDataSingleton::getData().pairing_ ); - element_init_GT( pp2, TEDataSingleton::getData().pairing_ ); - - pairing_apply( pp1, TEDataSingleton::getData().generator_, - const_cast< element_t& >( decrypted ), TEDataSingleton::getData().pairing_ ); - pairing_apply( pp2, U, const_cast< element_t& >( public_key ), - TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_GT pp1, pp2; + pp1 = libff::alt_bn128_ate_reduced_pairing( W, public_key ); + pp2 = libff::alt_bn128_ate_reduced_pairing( H, decryptionShare ); - bool check = element_cmp( pp1, pp2 ); + bool check = pp1 == pp2; if ( check ) { ret_val = false; } - - element_clear( pp1 ); - element_clear( pp2 ); } } else { ret_val = false; } - element_clear( fst ); - element_clear( snd ); - - element_clear( U ); - element_clear( W ); - element_clear( H ); - return ret_val; } std::string TE::CombineShares( const Ciphertext& ciphertext, - const std::vector< std::pair< element_wrapper, size_t > >& decrypted ) { - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_set( U, const_cast< element_t& >( std::get< 0 >( ciphertext ).el_ ) ); + const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShares ) { + libff::alt_bn128_G2 U = std::get< 0 >( ciphertext ); std::string V = std::get< 1 >( ciphertext ); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_set( W, const_cast< element_t& >( std::get< 2 >( ciphertext ).el_ ) ); - - element_t H; - element_init_G1( H, TEDataSingleton::getData().pairing_ ); - this->HashToGroup( H, U, V ); - - element_t fst, snd; - element_init_GT( fst, TEDataSingleton::getData().pairing_ ); - element_init_GT( snd, TEDataSingleton::getData().pairing_ ); - - pairing_apply( - fst, TEDataSingleton::getData().generator_, W, TEDataSingleton::getData().pairing_ ); - pairing_apply( snd, U, H, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_G1 W = std::get< 2 >( ciphertext ); - element_clear( U ); - element_clear( W ); - element_clear( H ); + libff::alt_bn128_G1 H = this->HashToGroup( U, V ); - bool res = element_cmp( fst, snd ); + libff::alt_bn128_GT fst, snd; + fst = libff::alt_bn128_ate_reduced_pairing( W, libff::alt_bn128_G2::one() ); + snd = libff::alt_bn128_ate_reduced_pairing( H, U ); - element_clear( fst ); - element_clear( snd ); + bool res = fst == snd; - if ( res ) { + if ( !res ) { throw std::runtime_error( "error during share combining" ); } std::vector< int > idx( this->t_ ); for ( size_t i = 0; i < this->t_; ++i ) { - idx[i] = decrypted[i].second; + idx[i] = decryptionShares[i].second; } + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = this->LagrangeCoeffs( idx ); - std::vector< element_wrapper > lagrange_coeffs = this->LagrangeCoeffs( idx ); - - element_t sum; - element_init_G1( sum, TEDataSingleton::getData().pairing_ ); - element_set0( sum ); + libff::alt_bn128_G2 sum = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < this->t_; ++i ) { - element_t temp; - element_init_G1( temp, TEDataSingleton::getData().pairing_ ); - element_mul_zn( temp, - ( const_cast< std::vector< std::pair< element_wrapper, size_t > >& >( decrypted ) )[i] - .first.el_, - lagrange_coeffs[i].el_ ); - - element_t tmp1; - element_init_G1( tmp1, TEDataSingleton::getData().pairing_ ); - - element_add( tmp1, sum, temp ); + libff::alt_bn128_G2 temp = lagrange_coeffs[i] * decryptionShares[i].first; - element_clear( sum ); - element_init_G1( sum, TEDataSingleton::getData().pairing_ ); - element_set( sum, tmp1 ); - - element_clear( temp ); - element_clear( tmp1 ); + sum = sum + temp; } std::string hash = this->Hash( sum ); @@ -409,88 +233,43 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, message += static_cast< char >( xor_res[i] ); } - element_clear( sum ); - return message; } -std::vector< element_wrapper > TE::LagrangeCoeffs( const std::vector< int >& idx ) { +std::vector< libff::alt_bn128_Fr > TE::LagrangeCoeffs( const std::vector< int >& idx ) { if ( idx.size() < this->t_ ) { - throw std::runtime_error( "Error, not enough participants in the threshold group" ); + // throw IncorrectInput( "not enough participants in the threshold group" ); + throw std::runtime_error( "not enough participants in the threshold group" ); } - std::vector< element_wrapper > res( this->t_ ); + std::vector< libff::alt_bn128_Fr > res( this->t_ ); - element_t w; - element_init_Zr( w, TEDataSingleton::getData().pairing_ ); - element_set1( w ); - - element_t a; - element_init_Zr( a, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_Fr w = libff::alt_bn128_Fr::one(); for ( size_t i = 0; i < this->t_; ++i ) { - element_mul_si( a, w, idx[i] ); - element_clear( w ); - element_init_Zr( w, TEDataSingleton::getData().pairing_ ); - element_set( w, a ); + w *= libff::alt_bn128_Fr( idx[i] ); } - element_clear( a ); - for ( size_t i = 0; i < this->t_; ++i ) { - element_t v; - element_init_Zr( v, TEDataSingleton::getData().pairing_ ); - element_set_si( v, idx[i] ); + libff::alt_bn128_Fr v = libff::alt_bn128_Fr( idx[i] ); for ( size_t j = 0; j < this->t_; ++j ) { if ( j != i ) { - if ( idx[i] == idx[j] ) { - element_clear( w ); - element_clear( v ); + if ( libff::alt_bn128_Fr( idx[i] ) == libff::alt_bn128_Fr( idx[j] ) ) { + // throw IncorrectInput( + // "during the interpolation, have same indexes in list of indexes" ); throw std::runtime_error( - "Error during the interpolation, have same indexes in the list of " - "indexes" ); + "during the interpolation, have same indexes in list of indexes" ); } - element_t u; - element_init_Zr( u, TEDataSingleton::getData().pairing_ ); - - element_set_si( u, idx[j] - idx[i] ); - - element_init_Zr( a, TEDataSingleton::getData().pairing_ ); - element_mul_zn( a, v, u ); - element_clear( v ); - element_init_Zr( v, TEDataSingleton::getData().pairing_ ); - element_set( v, a ); - - element_clear( a ); - - element_clear( u ); + v *= ( libff::alt_bn128_Fr( idx[j] ) - + libff::alt_bn128_Fr( idx[i] ) ); // calculating Lagrange coefficients } } - element_init_Zr( a, TEDataSingleton::getData().pairing_ ); - element_invert( a, v ); - element_clear( v ); - element_init_Zr( v, TEDataSingleton::getData().pairing_ ); - element_set( v, a ); - - element_clear( a ); - - - element_init_Zr( a, TEDataSingleton::getData().pairing_ ); - element_mul_zn( a, w, v ); - - element_init_Zr( res[i].el_, TEDataSingleton::getData().pairing_ ); - element_set( res[i].el_, a ); - - element_clear( a ); - - element_clear( v ); + res[i] = w * v.invert(); } - element_clear( w ); - return res; } diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index cc937e25..bde0e34e 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -14,7 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file threshold_encryption.h @author Oleh Nikolaiev @@ -30,59 +30,11 @@ along with libBLS. If not, see . #include -#include -#include +#include namespace encryption { -class element_wrapper { -public: - element_t el_ = {0, 0}; - - void clear() { - if ( el_[0].data ) { - element_clear( el_ ); - } - } - - void assign( const element_t& e ) { - if ( ( ( void* ) ( &el_ ) ) == ( ( void* ) ( &e ) ) ) { - return; - } - - clear(); - element_init_same_as( el_, const_cast< element_t& >( e ) ); - element_set( el_, const_cast< element_t& >( e ) ); - } - - void assign( const element_wrapper& other ) { - if ( ( ( void* ) this ) == ( ( void* ) ( &other ) ) ) { - return; - } - - assign( other.el_ ); - } - - element_wrapper() {} - - element_wrapper( const element_wrapper& other ) { assign( other ); } - - element_wrapper( const element_t& e ) { assign( e ); } - - ~element_wrapper() { clear(); } - - element_wrapper& operator=( const element_wrapper& other ) { - assign( other ); - return ( *this ); - } - - element_wrapper& operator=( const element_t& e ) { - assign( e ); - return ( *this ); - } -}; - -typedef std::tuple< element_wrapper, std::string, element_wrapper > Ciphertext; +typedef std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 > Ciphertext; class TE { public: @@ -90,23 +42,24 @@ class TE { ~TE(); - Ciphertext Encrypt( const std::string& message, const element_t& common_public ); + Ciphertext Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ); - void Decrypt( element_t ret_val, const Ciphertext& ciphertext, const element_t& secret_key ); + libff::alt_bn128_G2 getDecryptionShare( + const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key ); - void HashToGroup( element_t ret_val, const element_t& U, const std::string& V, + libff::alt_bn128_G1 HashToGroup( const libff::alt_bn128_G2& U, const std::string& V, std::string ( *hash_func )( const std::string& str ) = cryptlite::sha256::hash_hex ); - std::string Hash( const element_t& Y, + std::string Hash( const libff::alt_bn128_G2& Y, std::string ( *hash_func )( const std::string& str ) = cryptlite::sha256::hash_hex ); - bool Verify( - const Ciphertext& ciphertext, const element_t& decrypted, const element_t& public_key ); + bool Verify( const Ciphertext& ciphertext, const libff::alt_bn128_G2& decryptionShare, + const libff::alt_bn128_G2& public_key ); std::string CombineShares( const Ciphertext& ciphertext, - const std::vector< std::pair< element_wrapper, size_t > >& decrypted ); + const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShare ); - std::vector< element_wrapper > LagrangeCoeffs( const std::vector< int >& idx ); + std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx ); private: const size_t t_ = 0; diff --git a/threshold_encryption/utils.cpp b/threshold_encryption/utils.cpp index d707b45c..dba1e8f8 100644 --- a/threshold_encryption/utils.cpp +++ b/threshold_encryption/utils.cpp @@ -46,37 +46,6 @@ void MpzSquareRoot( mpz_t ret_val, mpz_t x ) { mpz_clears( deg, mode, 0 ); } -std::string ElementZrToString( element_t el ) { - std::string str = "1"; - if ( element_item_count( el ) ) { - str = "2"; - } else { - mpz_t a; - mpz_init( a ); - - element_to_mpz( a, el ); - - char arr[mpz_sizeinbase( a, 10 ) + 2]; - - char* tmp = mpz_get_str( arr, 10, a ); - mpz_clear( a ); - - str = tmp; - } - - return str; -} - -std::shared_ptr< std::vector< std::string > > ElementG1ToString( element_t& el ) { - std::vector< std::string > res_str; - - for ( int i = 0; i < element_item_count( el ); ++i ) { - res_str.push_back( ElementZrToString( element_item( el, i ) ) ); - } - - return std::make_shared< std::vector< std::string > >( res_str ); -} - bool isStringNumber( std::string& str ) { if ( str.at( 0 ) == '0' && str.length() > 1 ) return false; @@ -88,18 +57,8 @@ bool isStringNumber( std::string& str ) { return true; } -bool isG1Element0( element_t& el ) { - if ( element_item_count( el ) != 2 ) - throw std::runtime_error( "not 2 component element " ); - if ( element_is0( element_item( el, 0 ) ) && element_is0( element_item( el, 1 ) ) ) - return true; - else - return false; -} - void checkCypher( const encryption::Ciphertext& cyphertext ) { - if ( isG1Element0( const_cast< element_t& >( std::get< 0 >( cyphertext ).el_ ) ) || - isG1Element0( const_cast< element_t& >( std::get< 2 >( cyphertext ).el_ ) ) ) + if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) throw std::runtime_error( "zero element in cyphertext" ); if ( std::get< 1 >( cyphertext ).length() != 64 ) diff --git a/threshold_encryption/utils.h b/threshold_encryption/utils.h index f6ecc448..431c551b 100644 --- a/threshold_encryption/utils.h +++ b/threshold_encryption/utils.h @@ -35,12 +35,6 @@ using type_a_Fq = libff::Fp_model< num_limbs, modulus >; void MpzSquareRoot( mpz_t ret_val, mpz_t x ); -std::string ElementZrToString( element_t el ); - -std::shared_ptr< std::vector< std::string > > ElementG1ToString( element_t& el ); - bool isStringNumber( std::string& str ); -bool isG1Element0( element_t& el ); - void checkCypher( const encryption::Ciphertext& cypher ); diff --git a/tools/utils.cpp b/tools/utils.cpp new file mode 100644 index 00000000..ac64a0cb --- /dev/null +++ b/tools/utils.cpp @@ -0,0 +1,151 @@ +/* + Copyright (C) 2021- SKALE Labs + + This file is part of libBLS. + + libBLS is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libBLS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with libBLS. If not, see . + + @file utils.cpp + @author Oleh Nikolaiev + @date 2021 +*/ + +#include "utils.h" + +template < class T > +std::string fieldElementToString( const T& field_elem ) { + mpz_t t; + mpz_init( t ); + + field_elem.as_bigint().to_mpz( t ); + + char arr[mpz_sizeinbase( t, 10 ) + 2]; + + char* tmp = mpz_get_str( arr, 10, t ); + mpz_clear( t ); + + std::string output = tmp; + + return output; +} + +std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ) { + std::vector< std::string > pkey_str_vect; + + elem.to_affine_coordinates(); + + pkey_str_vect.push_back( fieldElementToString( elem.X.c0 ) ); + pkey_str_vect.push_back( fieldElementToString( elem.X.c1 ) ); + pkey_str_vect.push_back( fieldElementToString( elem.Y.c0 ) ); + pkey_str_vect.push_back( fieldElementToString( elem.Y.c1 ) ); + + return pkey_str_vect; +} + +std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx, size_t t ) { + if ( idx.size() < t ) { + // throw IncorrectInput( "not enough participants in the threshold group" ); + throw std::runtime_error( "not enough participants in the threshold group" ); + } + + std::vector< libff::alt_bn128_Fr > res( t ); + + libff::alt_bn128_Fr w = libff::alt_bn128_Fr::one(); + + for ( size_t i = 0; i < t; ++i ) { + w *= libff::alt_bn128_Fr( idx[i] ); + } + + for ( size_t i = 0; i < t; ++i ) { + libff::alt_bn128_Fr v = libff::alt_bn128_Fr( idx[i] ); + + for ( size_t j = 0; j < t; ++j ) { + if ( j != i ) { + if ( libff::alt_bn128_Fr( idx[i] ) == libff::alt_bn128_Fr( idx[j] ) ) { + // throw IncorrectInput( + // "during the interpolation, have same indexes in list of indexes" ); + throw std::runtime_error( + "during the interpolation, have same indexes in list of indexes" ); + } + + v *= ( libff::alt_bn128_Fr( idx[j] ) - + libff::alt_bn128_Fr( idx[i] ) ); // calculating Lagrange coefficients + } + } + + res[i] = w * v.invert(); + } + + return res; +} + +libff::alt_bn128_Fq HashToFq( + std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { + libff::bigint< libff::alt_bn128_q_limbs > from_hex; + + std::vector< uint8_t > hex( 64 ); + for ( size_t i = 0; i < 32; ++i ) { + hex[2 * i] = static_cast< int >( hash_byte_arr->at( i ) ) / 16; + hex[2 * i + 1] = static_cast< int >( hash_byte_arr->at( i ) ) % 16; + } + mpn_set_str( from_hex.data, hex.data(), 64, 16 ); + + libff::alt_bn128_Fq ret_val( from_hex ); + + return ret_val; +} + +libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { + libff::alt_bn128_Fq x1( HashToFq( hash_byte_arr ) ); + + libff::alt_bn128_G1 result; + + while ( true ) { + libff::alt_bn128_Fq y1_sqr = x1 ^ 3; + y1_sqr = y1_sqr + libff::alt_bn128_coeff_b; + + libff::alt_bn128_Fq euler = y1_sqr ^ libff::alt_bn128_Fq::euler; + + if ( euler == libff::alt_bn128_Fq::one() || + euler == libff::alt_bn128_Fq::zero() ) { // if y1_sqr is a square + result.X = x1; + libff::alt_bn128_Fq temp_y = y1_sqr.sqrt(); + + mpz_t pos_y; + mpz_init( pos_y ); + + temp_y.as_bigint().to_mpz( pos_y ); + + mpz_t neg_y; + mpz_init( neg_y ); + + ( -temp_y ).as_bigint().to_mpz( neg_y ); + + if ( mpz_cmp( pos_y, neg_y ) < 0 ) { + temp_y = -temp_y; + } + + mpz_clear( pos_y ); + mpz_clear( neg_y ); + + result.Y = temp_y; + break; + } else { + x1 = x1 + 1; + } + } + result.Z = libff::alt_bn128_Fq::one(); + + return result; +} diff --git a/tools/utils.h b/tools/utils.h new file mode 100644 index 00000000..27b7088e --- /dev/null +++ b/tools/utils.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2021- SKALE Labs + + This file is part of libBLS. + + libBLS is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libBLS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with libBLS. If not, see . + + @file utils.h + @author Oleh Nikolaiev + @date 2021 +*/ + +#ifndef LIBBLS_UTILS_H +#define LIBBLS_UTILS_H + +#include +#include +#include +#include + +#include + +template < class T > +std::string fieldElementToString( const T& field_elem ); + +std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); + +std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx, size_t t ); + +libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr); + +libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr); + +#endif // LIBBLS_UTILS_H From dd49eec1934140155a2d9f5c47c0bfc7fa87e679 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 10:42:28 +0300 Subject: [PATCH 02/40] SKALE-4409 fix te unit tests --- test/unit_tests_te.cpp | 183 ++++-------------- threshold_encryption/CMakeLists.txt | 6 +- threshold_encryption/threshold_encryption.cpp | 8 +- tools/utils.cpp | 3 +- tools/utils.h | 6 +- 5 files changed, 56 insertions(+), 150 deletions(-) diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index f2c74696..2b4335fa 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -40,7 +40,7 @@ BOOST_AUTO_TEST_CASE( SimpleEncryption ) { libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); libff::alt_bn128_G2 public_key = secret_key * libff::alt_bn128_G2::one(); - + auto ciphertext = te_instance.Encrypt( message, public_key ); libff::alt_bn128_G2 decryption_share = te_instance.getDecryptionShare( ciphertext, secret_key ); @@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { libff::alt_bn128_Fr sk = libff::alt_bn128_Fr::zero(); for ( size_t j = 0; j < 11; ++j ) { - libff::alt_bn128_Fr tmp1(i + 1); + libff::alt_bn128_Fr tmp1( i + 1 ); libff::alt_bn128_Fr tmp3 = libff::power( tmp1, j ); @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { libff::alt_bn128_Fr sk = libff::alt_bn128_Fr::zero(); for ( size_t j = 0; j < 11; ++j ) { - libff::alt_bn128_Fr tmp1(i + 1); + libff::alt_bn128_Fr tmp1( i + 1 ); libff::alt_bn128_Fr tmp3 = libff::power( tmp1, j ); @@ -142,7 +142,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { secret_keys[i] = sk; } - libff::alt_bn128_Fr common_secret = coeffs[0]; + // libff::alt_bn128_Fr common_secret = coeffs[0]; // element_pow_zn(common_public, obj.generator_, common_secret); // let common_public be a random element of G1 instead of correct one in the previous line @@ -188,60 +188,29 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { std::vector< libff::alt_bn128_Fr > secret_keys( 16 ); for ( size_t i = 0; i < 16; ++i ) { - element_t sk; - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set0( sk ); + libff::alt_bn128_Fr sk = libff::alt_bn128_Fr::zero(); for ( size_t j = 0; j < 11; ++j ) { - element_t tmp1; - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp1, i + 1 ); - - element_t tmp2; - element_init_Zr( tmp2, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp2, j ); - - element_t tmp3; - element_init_Zr( tmp3, TEDataSingleton::getData().pairing_ ); - element_pow_zn( tmp3, tmp1, tmp2 ); - - element_t tmp4; - element_init_Zr( tmp4, TEDataSingleton::getData().pairing_ ); - element_mul_zn( tmp4, coeffs[j].el_, tmp3 ); - - element_clear( tmp1 ); - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_add( tmp1, sk, tmp4 ); - - element_clear( sk ); - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set( sk, tmp1 ); - - element_clear( tmp1 ); - element_clear( tmp2 ); - element_clear( tmp3 ); - element_clear( tmp4 ); + libff::alt_bn128_Fr tmp1( i + 1 ); + + libff::alt_bn128_Fr tmp3 = libff::power( tmp1, j ); + + libff::alt_bn128_Fr tmp4 = coeffs[j] * tmp3; + + sk += tmp4; } // let secret_key[7] be a random generated value instead of correctly generated if ( i == 7 ) { - element_clear( sk ); - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_random( sk ); + sk = libff::alt_bn128_Fr::random_element(); } - secret_keys[i] = encryption::element_wrapper( sk ); - - element_clear( sk ); + secret_keys[i] = sk; } - element_t common_secret; - element_init_Zr( common_secret, TEDataSingleton::getData().pairing_ ); - element_set( common_secret, coeffs[0].el_ ); + libff::alt_bn128_Fr common_secret = coeffs[0]; - element_t common_public; - element_init_G1( common_public, TEDataSingleton::getData().pairing_ ); - element_pow_zn( common_public, TEDataSingleton::getData().generator_, common_secret ); + libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 @@ -249,107 +218,57 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { auto ciphertext = obj.Encrypt( message, common_public ); - std::vector< std::pair< encryption::element_wrapper, size_t > > shares( 11 ); + std::vector< std::pair< libff::alt_bn128_G2, size_t > > shares( 11 ); for ( size_t i = 0; i < 11; ++i ) { - element_t decrypted; - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); - - obj.Decrypt( decrypted, ciphertext, secret_keys[i].el_ ); + libff::alt_bn128_G2 decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); - element_t public_key; - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - element_pow_zn( public_key, TEDataSingleton::getData().generator_, secret_keys[i].el_ ); + libff::alt_bn128_G2 public_key = secret_keys[i] * libff::alt_bn128_G2::one(); BOOST_REQUIRE( obj.Verify( ciphertext, decrypted, public_key ) ); - shares[i].first = encryption::element_wrapper( decrypted ); - - element_clear( decrypted ); - element_clear( public_key ); + shares[i].first = decrypted; shares[i].second = i + 1; } std::string res = obj.CombineShares( ciphertext, shares ); - element_clear( common_secret ); - element_clear( common_public ); - BOOST_REQUIRE( res != message ); } BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { encryption::TE obj = encryption::TE( 11, 16 ); - std::vector< encryption::element_wrapper > coeffs( 11 ); + std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - - element_random( tmp ); - - while ( element_is0( tmp ) ) { - element_random( tmp ); + elem = libff::alt_bn128_Fr::random_element(); + while ( elem.is_zero() ) { + elem = libff::alt_bn128_Fr::random_element(); } - - elem = encryption::element_wrapper( tmp ); - - element_clear( tmp ); } - std::vector< encryption::element_wrapper > secret_keys( 16 ); + std::vector< libff::alt_bn128_Fr > secret_keys( 16 ); for ( size_t i = 0; i < 16; ++i ) { - element_t sk; - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set0( sk ); + libff::alt_bn128_Fr sk = libff::alt_bn128_Fr::zero(); for ( size_t j = 0; j < 11; ++j ) { - element_t tmp1; - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp1, i + 1 ); - - element_t tmp2; - element_init_Zr( tmp2, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp2, j ); - - element_t tmp3; - element_init_Zr( tmp3, TEDataSingleton::getData().pairing_ ); - element_pow_zn( tmp3, tmp1, tmp2 ); - - element_t tmp4; - element_init_Zr( tmp4, TEDataSingleton::getData().pairing_ ); - element_mul_zn( tmp4, coeffs[j].el_, tmp3 ); - - element_clear( tmp1 ); - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_add( tmp1, sk, tmp4 ); - - element_clear( sk ); - element_init_Zr( sk, TEDataSingleton::getData().pairing_ ); - element_set( sk, tmp1 ); - - element_clear( tmp1 ); - element_clear( tmp2 ); - element_clear( tmp3 ); - element_clear( tmp4 ); - } + libff::alt_bn128_Fr tmp1( i + 1 ); - secret_keys[i] = encryption::element_wrapper( sk ); + libff::alt_bn128_Fr tmp3 = libff::power( tmp1, j ); - element_clear( sk ); - } + libff::alt_bn128_Fr tmp4 = coeffs[j] * tmp3; - element_t common_secret; - element_init_Zr( common_secret, TEDataSingleton::getData().pairing_ ); - element_set( common_secret, coeffs[0].el_ ); + sk += tmp4; + } + + secret_keys[i] = sk; + } - element_t common_public; - element_init_G1( common_public, TEDataSingleton::getData().pairing_ ); - element_pow_zn( common_public, TEDataSingleton::getData().generator_, common_secret ); + libff::alt_bn128_Fr common_secret = coeffs[0]; - element_clear( common_secret ); + libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 @@ -357,46 +276,30 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { auto ciphertext = obj.Encrypt( message, common_public ); - element_clear( common_public ); + libff::alt_bn128_G1 rand = libff::alt_bn128_G1::random_element(); - element_t rand; - element_init_G1( rand, TEDataSingleton::getData().pairing_ ); - element_random( rand ); - - std::tuple< encryption::element_wrapper, std::string, encryption::element_wrapper > - corrupted_ciphertext; + std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 > corrupted_ciphertext; std::get< 0 >( corrupted_ciphertext ) = std::get< 0 >( ciphertext ); std::get< 1 >( corrupted_ciphertext ) = std::get< 1 >( ciphertext ); - std::get< 2 >( corrupted_ciphertext ) = encryption::element_wrapper( rand ); - - element_clear( rand ); + std::get< 2 >( corrupted_ciphertext ) = rand; for ( size_t i = 0; i < 11; ++i ) { - element_t decrypted; - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_G2 decrypted; bool is_exception_caught = false; try { - obj.Decrypt( decrypted, corrupted_ciphertext, secret_keys[i].el_ ); + decrypted = obj.getDecryptionShare( corrupted_ciphertext, secret_keys[i] ); } catch ( std::runtime_error& ) { is_exception_caught = true; } - element_clear( decrypted ); BOOST_REQUIRE( is_exception_caught ); - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); - - obj.Decrypt( decrypted, ciphertext, secret_keys[i].el_ ); + decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); - element_t public_key; - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - element_pow_zn( public_key, TEDataSingleton::getData().generator_, secret_keys[i].el_ ); + libff::alt_bn128_G2 public_key = secret_keys[i] * libff::alt_bn128_G2::one(); BOOST_REQUIRE( !obj.Verify( corrupted_ciphertext, decrypted, public_key ) ); - - element_clear( decrypted ); - element_clear( public_key ); } } diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 84e91a7c..1f57f85d 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -48,14 +48,14 @@ add_library(te ${sources} ${headers}) include_directories(${CMAKE_BINARY_DIR}) link_directories(${CMAKE_BINARY_DIR}) -target_include_directories(te PRIVATE ${GMP_INCLUDE_DIR}) +target_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(te PRIVATE ${GMPXX_LIBRARY} ${GMP_LIBRARY}) include_directories(${CMAKE_BINARY_DIR}/deps/include ) link_directories(${CMAKE_BINARY_DIR}/deps/lib) target_include_directories(te PRIVATE ${PBC_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) -target_link_libraries(te PRIVATE pbc) +target_link_libraries(te PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) if (BUILD_TESTS) # add_executable(dkg_te_unit_test ../test/unit_tests_dkg_te.cpp) @@ -66,7 +66,7 @@ if (BUILD_TESTS) add_executable(te_unit_test ../test/unit_tests_te.cpp) target_include_directories(te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) - target_link_libraries(te_unit_test PRIVATE te pbc ${GMP_LIBRARY} ${GMPXX_LIBRARY}) + target_link_libraries(te_unit_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_test(NAME te_tests COMMAND te_unit_test) diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index b1f69ed7..2e174ed0 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -28,10 +28,14 @@ #include "../tools/utils.h" #include #include +#include namespace encryption { -TE::TE( const size_t t, const size_t n ) : t_( t ), n_( n ) {} +TE::TE( const size_t t, const size_t n ) : t_( t ), n_( n ) { + libff::init_alt_bn128_params(); + libff::inhibit_profiling_info = true; +} TE::~TE() {} @@ -169,7 +173,7 @@ bool TE::Verify( const Ciphertext& ciphertext, const libff::alt_bn128_G2& decryp pp2 = libff::alt_bn128_ate_reduced_pairing( H, decryptionShare ); bool check = pp1 == pp2; - if ( check ) { + if ( !check ) { ret_val = false; } } diff --git a/tools/utils.cpp b/tools/utils.cpp index ac64a0cb..50c7df90 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -90,8 +90,7 @@ std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx return res; } -libff::alt_bn128_Fq HashToFq( - std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { +libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { libff::bigint< libff::alt_bn128_q_limbs > from_hex; std::vector< uint8_t > hex( 64 ); diff --git a/tools/utils.h b/tools/utils.h index 27b7088e..929adf29 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -38,8 +38,8 @@ std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx, size_t t ); -libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr); +libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); -libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr); +libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); -#endif // LIBBLS_UTILS_H +#endif // LIBBLS_UTILS_H From 4771a3fe624ffb74e567fd47bbf42b7ffba1b9da Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 14:41:00 +0300 Subject: [PATCH 03/40] SKALE-4409 fix tests --- CMakeLists.txt | 2 +- dkg/DKGBLSWrapper.h | 2 +- dkg/DKGTESecret.cpp | 14 +- dkg/DKGTESecret.h | 10 +- dkg/DKGTEWrapper.cpp | 58 +-- dkg/DKGTEWrapper.h | 16 +- dkg/dkg_te.cpp | 188 +++------ dkg/dkg_te.h | 27 +- test/test_TE_wrappers.cpp | 428 ++++++--------------- threshold_encryption/CMakeLists.txt | 23 +- threshold_encryption/TEDataSingleton.cpp | 68 ---- threshold_encryption/TEDataSingleton.h | 49 --- threshold_encryption/TEDecryptSet.cpp | 10 +- threshold_encryption/TEPrivateKey.cpp | 14 +- threshold_encryption/TEPrivateKey.h | 3 +- threshold_encryption/TEPrivateKeyShare.cpp | 21 +- threshold_encryption/TEPrivateKeyShare.h | 3 +- threshold_encryption/TEPublicKey.cpp | 11 +- threshold_encryption/TEPublicKeyShare.cpp | 9 +- threshold_encryption/utils.h | 2 +- tools/utils.cpp | 34 +- tools/utils.h | 19 + 22 files changed, 315 insertions(+), 696 deletions(-) delete mode 100644 threshold_encryption/TEDataSingleton.cpp delete mode 100644 threshold_encryption/TEDataSingleton.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cc93afb3..7943f60d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ set(headers_bls third_party/cryptlite/base64.h ) -set(PROJECT_VERSION 0.1.1) +set(PROJECT_VERSION 0.2.0) add_definitions(-DBLS_VERSION=${PROJECT_VERSION}) if ( APPLE ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wparentheses -Wunused -Wunreachable-code -Wextra -fPIC -std=c++17") diff --git a/dkg/DKGBLSWrapper.h b/dkg/DKGBLSWrapper.h index 42b59d63..c42f0295 100644 --- a/dkg/DKGBLSWrapper.h +++ b/dkg/DKGBLSWrapper.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp index f77986ba..90776b46 100644 --- a/dkg/DKGTESecret.cpp +++ b/dkg/DKGTESecret.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @@ -23,19 +23,21 @@ #include "DKGTESecret.h" +#include "../tools/utils.h" #include -#include DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); encryption::DkgTe dkg_te( requiredSigners, totalSigners ); poly = dkg_te.GeneratePolynomial(); } -void DKGTESecret::setPoly( std::vector< encryption::element_wrapper >& _poly ) { +void DKGTESecret::setPoly( std::vector< libff::alt_bn128_Fr >& _poly ) { if ( _poly.size() != requiredSigners ) { throw std::runtime_error( "Wrong size of vector" ); } @@ -43,12 +45,12 @@ void DKGTESecret::setPoly( std::vector< encryption::element_wrapper >& _poly ) { poly = _poly; } -std::vector< encryption::element_wrapper > DKGTESecret::getDKGTESecretShares() { +std::vector< libff::alt_bn128_Fr > DKGTESecret::getDKGTESecretShares() { encryption::DkgTe dkg_te( requiredSigners, totalSigners ); return dkg_te.CreateSecretKeyContribution( poly ); } -std::vector< encryption::element_wrapper > DKGTESecret::getDKGTEPublicShares() { +std::vector< libff::alt_bn128_G2 > DKGTESecret::getDKGTEPublicShares() { encryption::DkgTe dkg_te( requiredSigners, totalSigners ); return dkg_te.CreateVerificationVector( poly ); } diff --git a/dkg/DKGTESecret.h b/dkg/DKGTESecret.h index bdfc128f..fffdf9fa 100644 --- a/dkg/DKGTESecret.h +++ b/dkg/DKGTESecret.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @@ -31,13 +31,13 @@ class DKGTESecret { private: size_t requiredSigners; size_t totalSigners; - std::vector< encryption::element_wrapper > poly; + std::vector< libff::alt_bn128_Fr > poly; public: DKGTESecret( size_t _requiredSigners, size_t _totalSigners ); - void setPoly( std::vector< encryption::element_wrapper >& _poly ); - std::vector< encryption::element_wrapper > getDKGTESecretShares(); - std::vector< encryption::element_wrapper > getDKGTEPublicShares(); + void setPoly( std::vector< libff::alt_bn128_Fr >& _poly ); + std::vector< libff::alt_bn128_Fr > getDKGTESecretShares(); + std::vector< libff::alt_bn128_G2 > getDKGTEPublicShares(); }; diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index 89ff8fe6..f6950872 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @@ -23,20 +23,21 @@ #include "DKGTEWrapper.h" - -#include +#include "../tools/utils.h" DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); DKGTESecret temp( _requiredSigners, _totalSigners ); dkg_secret_ptr = std::make_shared< DKGTESecret >( temp ); } -bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const encryption::element_wrapper& _share, - const std::shared_ptr< std::vector< encryption::element_wrapper > >& _verification_vector ) { - if ( element_is0( const_cast< element_t& >( _share.el_ ) ) ) +bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share, + const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector ) { + if ( _share.is_zero() ) throw std::runtime_error( "Zero secret share" ); if ( _verification_vector == nullptr ) throw std::runtime_error( "Null verification vector" ); @@ -47,26 +48,26 @@ bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const encryption::elemen } void DKGTEWrapper::setDKGSecret( - std::shared_ptr< std::vector< encryption::element_wrapper > >& _poly_ptr ) { + std::shared_ptr< std::vector< libff::alt_bn128_Fr > >& _poly_ptr ) { if ( _poly_ptr == nullptr ) throw std::runtime_error( "Null polynomial ptr" ); dkg_secret_ptr->setPoly( *_poly_ptr ); } -std::shared_ptr< std::vector< encryption::element_wrapper > > +std::shared_ptr< std::vector< libff::alt_bn128_Fr > > DKGTEWrapper::createDKGSecretShares() { - return std::make_shared< std::vector< encryption::element_wrapper > >( + return std::make_shared< std::vector< libff::alt_bn128_Fr > >( dkg_secret_ptr->getDKGTESecretShares() ); } -std::shared_ptr< std::vector< encryption::element_wrapper > > +std::shared_ptr< std::vector< libff::alt_bn128_G2 > > DKGTEWrapper::createDKGPublicShares() { - return std::make_shared< std::vector< encryption::element_wrapper > >( + return std::make_shared< std::vector< libff::alt_bn128_G2 > >( dkg_secret_ptr->getDKGTEPublicShares() ); } TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( size_t signerIndex_, - std::shared_ptr< std::vector< encryption::element_wrapper > > secret_shares_ptr ) { + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) { if ( secret_shares_ptr == nullptr ) throw std::runtime_error( "Null secret_shares_ptr " ); if ( secret_shares_ptr->size() != totalSigners ) @@ -74,43 +75,26 @@ TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( size_t signerIndex_, encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - encryption::element_wrapper skey_share = dkg_te.CreateSecretKeyShare( *secret_shares_ptr ); + libff::alt_bn128_Fr skey_share = dkg_te.CreateSecretKeyShare( *secret_shares_ptr ); return TEPrivateKeyShare( skey_share, signerIndex_, requiredSigners, totalSigners ); } TEPublicKey DKGTEWrapper::CreateTEPublicKey( - std::shared_ptr< std::vector< std::vector< encryption::element_wrapper > > > public_shares_all, + std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all, size_t _requiredSigners, size_t _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( public_shares_all == nullptr ) throw std::runtime_error( "Null public shares all" ); - element_t public_key; - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - element_set0( public_key ); + libff::alt_bn128_G2 public_key = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < _totalSigners; i++ ) { - element_t temp; - element_init_G1( temp, TEDataSingleton::getData().pairing_ ); - element_set( temp, public_shares_all->at( i ).at( 0 ).el_ ); - - element_t value; - element_init_G1( value, TEDataSingleton::getData().pairing_ ); - element_add( value, public_key, temp ); - - element_clear( temp ); - element_clear( public_key ); - element_init_G1( public_key, TEDataSingleton::getData().pairing_ ); - - element_set( public_key, value ); - - element_clear( value ); + public_key = public_key + public_shares_all->at( i ).at( 0 ); } - TEPublicKey common_public( - encryption::element_wrapper( public_key ), _requiredSigners, _totalSigners ); - element_clear( public_key ); + TEPublicKey common_public( public_key, _requiredSigners, _totalSigners ); + return common_public; } diff --git a/dkg/DKGTEWrapper.h b/dkg/DKGTEWrapper.h index b05c7fc1..cade40b4 100644 --- a/dkg/DKGTEWrapper.h +++ b/dkg/DKGTEWrapper.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @@ -39,20 +39,20 @@ class DKGTEWrapper { public: DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ); - bool VerifyDKGShare( size_t signerIndex, const encryption::element_wrapper& share, - const std::shared_ptr< std::vector< encryption::element_wrapper > >& verification_vector ); + bool VerifyDKGShare( size_t signerIndex, const libff::alt_bn128_Fr& share, + const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& verification_vector ); - void setDKGSecret( std::shared_ptr< std::vector< encryption::element_wrapper > >& _poly_ptr ); + void setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > >& _poly_ptr ); - std::shared_ptr< std::vector< encryption::element_wrapper > > createDKGSecretShares(); + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > createDKGSecretShares(); - std::shared_ptr< std::vector< encryption::element_wrapper > > createDKGPublicShares(); + std::shared_ptr< std::vector< libff::alt_bn128_G2 > > createDKGPublicShares(); TEPrivateKeyShare CreateTEPrivateKeyShare( size_t signerIndex_, - std::shared_ptr< std::vector< encryption::element_wrapper > > secret_shares_ptr ); + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ); static TEPublicKey CreateTEPublicKey( - std::shared_ptr< std::vector< std::vector< encryption::element_wrapper > > > + std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all, size_t _requiredSigners, size_t _totalSigners ); }; diff --git a/dkg/dkg_te.cpp b/dkg/dkg_te.cpp index 7ebadafc..e00e4cdc 100644 --- a/dkg/dkg_te.cpp +++ b/dkg/dkg_te.cpp @@ -14,7 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file dkg_te.cpp @author Oleh Nikolaiev @@ -27,198 +27,98 @@ along with libBLS. If not, see . namespace encryption { -typedef std::vector< element_wrapper > Polynomial; +typedef std::vector< libff::alt_bn128_Fr > Polynomial; -DkgTe::DkgTe( const size_t t, const size_t n ) : t_( t ), n_( n ) {} +DkgTe::DkgTe( const size_t t, const size_t n ) : t_( t ), n_( n ) { + libff::init_alt_bn128_params(); +} Polynomial DkgTe::GeneratePolynomial() { Polynomial pol( this->t_ ); for ( size_t i = 0; i < this->t_; ++i ) { - element_t g; - element_init_Zr( g, TEDataSingleton::getData().pairing_ ); - element_random( g ); + libff::alt_bn128_Fr g = libff::alt_bn128_Fr::random_element(); - while ( i == this->t_ - 1 && element_is0( g ) ) { - element_random( g ); + while ( i == this->t_ - 1 && g.is_zero() ) { + g = libff::alt_bn128_Fr::random_element(); } - pol[i] = element_wrapper( g ); - - element_clear( g ); + pol[i] = g; } return pol; } -std::vector< element_wrapper > DkgTe::CreateVerificationVector( - const std::vector< element_wrapper >& polynomial ) { - std::vector< element_wrapper > verification_vector( this->t_ ); +std::vector< libff::alt_bn128_G2 > DkgTe::CreateVerificationVector( + const std::vector< libff::alt_bn128_Fr >& polynomial ) { + std::vector< libff::alt_bn128_G2 > verification_vector( this->t_ ); for ( size_t i = 0; i < this->t_; ++i ) { - element_t tmp; - element_init_G1( tmp, TEDataSingleton::getData().pairing_ ); - element_mul_zn( tmp, TEDataSingleton::getData().generator_, - const_cast< element_t& >( polynomial[i].el_ ) ); - - verification_vector[i] = element_wrapper( tmp ); - - element_clear( tmp ); + verification_vector[i] = polynomial[i] * libff::alt_bn128_G2::one(); } return verification_vector; } -element_wrapper DkgTe::ComputePolynomialValue( - const std::vector< element_wrapper >& polynomial, const element_wrapper& point ) { - element_t value; - element_init_Zr( value, TEDataSingleton::getData().pairing_ ); - element_set0( value ); - - element_t pow; - element_init_Zr( pow, TEDataSingleton::getData().pairing_ ); - element_set1( pow ); +libff::alt_bn128_Fr DkgTe::ComputePolynomialValue( + const std::vector< libff::alt_bn128_Fr >& polynomial, const libff::alt_bn128_Fr& point ) { + libff::alt_bn128_Fr value = libff::alt_bn128_Fr::zero(); + libff::alt_bn128_Fr pow = libff::alt_bn128_Fr::one(); for ( size_t i = 0; i < this->t_; ++i ) { - if ( i == this->t_ - 1 && element_is0( const_cast< element_t& >( polynomial[i].el_ ) ) ) { - element_clear( value ); - element_clear( pow ); - throw std::runtime_error( "Error, incorrect degree of a polynomial" ); + if ( i == this->t_ - 1 && polynomial[i] == libff::alt_bn128_Fr::zero() ) { + throw std::logic_error( "Error, incorrect degree of a polynomial" ); } - - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - element_mul( tmp, const_cast< element_t& >( polynomial[i].el_ ), pow ); - - element_t tmp1; - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_set( tmp1, value ); - - element_clear( value ); - - element_init_Zr( value, TEDataSingleton::getData().pairing_ ); - - element_add( value, tmp1, tmp ); - - element_clear( tmp1 ); - - element_clear( tmp ); - - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - element_set( tmp, pow ); - - element_clear( pow ); - element_init_Zr( pow, TEDataSingleton::getData().pairing_ ); - - element_mul( pow, tmp, const_cast< element_t& >( point.el_ ) ); - - element_clear( tmp ); + value += polynomial[i] * pow; + pow *= point; } - element_clear( pow ); - - element_wrapper ret_val = element_wrapper( value ); - element_clear( value ); - - return ret_val; + return value; } -std::vector< element_wrapper > DkgTe::CreateSecretKeyContribution( - const std::vector< element_wrapper >& polynomial ) { - std::vector< element_wrapper > secret_key_contribution( this->n_ ); +std::vector< libff::alt_bn128_Fr > DkgTe::CreateSecretKeyContribution( + const std::vector< libff::alt_bn128_Fr >& polynomial ) { + std::vector< libff::alt_bn128_Fr > secret_key_contribution( this->n_ ); for ( size_t i = 0; i < this->n_; ++i ) { - element_t point; - element_init_Zr( point, TEDataSingleton::getData().pairing_ ); - element_set_si( point, i + 1 ); + libff::alt_bn128_Fr point = i + 1; secret_key_contribution[i] = ComputePolynomialValue( polynomial, point ); - - element_clear( point ); } return secret_key_contribution; } -element_wrapper DkgTe::CreateSecretKeyShare( - const std::vector< element_wrapper >& secret_key_contribution ) { - element_t secret_key_share; - element_init_Zr( secret_key_share, TEDataSingleton::getData().pairing_ ); - element_set0( secret_key_share ); +libff::alt_bn128_Fr DkgTe::CreateSecretKeyShare( + const std::vector< libff::alt_bn128_Fr >& secret_key_contribution ) { + libff::alt_bn128_Fr secret_key_share = libff::alt_bn128_Fr::zero(); for ( size_t i = 0; i < this->n_; ++i ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - - element_set( tmp, secret_key_share ); - element_add( - secret_key_share, tmp, const_cast< element_t& >( secret_key_contribution[i].el_ ) ); - - element_clear( tmp ); + secret_key_share += secret_key_contribution[i]; } - if ( element_is0( secret_key_share ) ) { + if ( secret_key_share.is_zero() ) { throw std::runtime_error( "Error, at least one secret key share is equal to zero" ); } - element_wrapper skey_share( secret_key_share ); - element_clear( secret_key_share ); - - return skey_share; + return secret_key_share; } -bool DkgTe::Verify( size_t idx, const element_wrapper& share, - const std::vector< element_wrapper >& verification_vector ) { - element_t value; - element_init_G1( value, TEDataSingleton::getData().pairing_ ); - +bool DkgTe::Verify( size_t idx, const libff::alt_bn128_Fr& share, + const std::vector< libff::alt_bn128_G2 >& verification_vector ) { + libff::alt_bn128_G2 value = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < this->t_; ++i ) { - element_t tmp1; - element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp1, idx + 1 ); - - element_t tmp2; - element_init_Zr( tmp2, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp2, i ); - - element_t tmp3; - element_init_Zr( tmp3, TEDataSingleton::getData().pairing_ ); - element_pow_zn( tmp3, tmp1, tmp2 ); - - element_t tmp4; - element_init_G1( tmp4, TEDataSingleton::getData().pairing_ ); - element_mul_zn( tmp4, const_cast< element_t& >( verification_vector[i].el_ ), tmp3 ); - - if ( i == 0 ) { - element_set( value, tmp4 ); - } else { - element_t tmp; - element_init_G1( tmp, TEDataSingleton::getData().pairing_ ); - element_set( tmp, value ); - - element_clear( value ); - element_init_G1( value, TEDataSingleton::getData().pairing_ ); - - element_add( value, tmp, tmp4 ); - - element_clear( tmp ); + if ( !this->isG2( verification_vector[i] ) ) { + return false; } - - element_clear( tmp1 ); - element_clear( tmp2 ); - element_clear( tmp3 ); - element_clear( tmp4 ); + value = value + power( libff::alt_bn128_Fr( idx + 1 ), i ) * verification_vector[i]; } - element_t mul; - element_init_G1( mul, TEDataSingleton::getData().pairing_ ); - element_mul_zn( - mul, TEDataSingleton::getData().generator_, const_cast< element_t& >( share.el_ ) ); - - bool ret_val = ( element_cmp( value, mul ) == 0 ); - - element_clear( value ); - element_clear( mul ); + return ( value == share * libff::alt_bn128_G2::one() ); +} - return ret_val; +bool DkgTe::isG2( const libff::alt_bn128_G2& point ) { + return point.is_well_formed() && + libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); } + } // namespace encryption diff --git a/dkg/dkg_te.h b/dkg/dkg_te.h index 6af1dd60..a8477639 100644 --- a/dkg/dkg_te.h +++ b/dkg/dkg_te.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file dkg_te.h @author Oleh Nikolaiev @@ -23,7 +23,6 @@ #pragma once -#include #include namespace encryption { @@ -32,22 +31,24 @@ class DkgTe { public: DkgTe( const size_t t, const size_t n ); - std::vector< element_wrapper > GeneratePolynomial(); + std::vector< libff::alt_bn128_Fr > GeneratePolynomial(); - std::vector< element_wrapper > CreateVerificationVector( - const std::vector< element_wrapper >& polynomial ); + std::vector< libff::alt_bn128_G2 > CreateVerificationVector( + const std::vector< libff::alt_bn128_Fr >& polynomial ); - element_wrapper ComputePolynomialValue( - const std::vector< element_wrapper >& polynomial, const element_wrapper& point ); + libff::alt_bn128_Fr ComputePolynomialValue( + const std::vector< libff::alt_bn128_Fr >& polynomial, const libff::alt_bn128_Fr& point ); - std::vector< element_wrapper > CreateSecretKeyContribution( - const std::vector< element_wrapper >& polynomial ); + std::vector< libff::alt_bn128_Fr > CreateSecretKeyContribution( + const std::vector< libff::alt_bn128_Fr >& polynomial ); - element_wrapper CreateSecretKeyShare( - const std::vector< element_wrapper >& secret_key_contribution ); + libff::alt_bn128_Fr CreateSecretKeyShare( + const std::vector< libff::alt_bn128_Fr >& secret_key_contribution ); - bool Verify( size_t idx, const element_wrapper& share, - const std::vector< element_wrapper >& verification_vector ); + bool Verify( size_t idx, const libff::alt_bn128_Fr& share, + const std::vector< libff::alt_bn128_G2 >& verification_vector ); + + static bool isG2( const libff::alt_bn128_G2& point ); private: const size_t t_ = 0; diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 87b9806e..58bf9968 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -24,6 +24,7 @@ along with libBLS. If not, see . #define BOOST_TEST_MODULE +#include "../tools/utils.h" #include #include #include @@ -54,56 +55,6 @@ std::string spoilMessage( std::string& message ) { BOOST_AUTO_TEST_SUITE( ThresholdEncryptionWrappers ) -BOOST_AUTO_TEST_CASE( testSqrt ) { - for ( size_t i = 0; i < 100; i++ ) { - gmp_randstate_t state; - gmp_randinit_default( state ); - - mpz_t rand; - mpz_init( rand ); - - mpz_t num_limbs_mpz; - mpz_init( num_limbs_mpz ); - mpz_set_si( num_limbs_mpz, num_limbs ); - - mpz_urandomm( rand, state, num_limbs_mpz ); - - mpz_clear( num_limbs_mpz ); - - mpz_t modulus_q; - mpz_init( modulus_q ); - mpz_set_str( modulus_q, - "87807107996633125224377819847540498158068831994142082110286533992664756308802229570786" - "25179422662221423155858769582317459277713367317481324925129998224791", - 10 ); - - mpz_t sqr_mod; - mpz_init( sqr_mod ); - mpz_powm_ui( sqr_mod, rand, 2, modulus_q ); - - mpz_t mpz_sqrt0; - mpz_init( mpz_sqrt0 ); - mpz_mod( mpz_sqrt0, rand, modulus_q ); - - mpz_clear( rand ); - - mpz_t mpz_sqrt; - mpz_init( mpz_sqrt ); - - MpzSquareRoot( mpz_sqrt, sqr_mod ); - - mpz_t sum; - mpz_init( sum ); - - mpz_add( sum, mpz_sqrt0, mpz_sqrt ); - - BOOST_REQUIRE( mpz_cmp( mpz_sqrt0, mpz_sqrt ) == 0 || mpz_cmp( sum, modulus_q ) == 0 ); - - mpz_clears( mpz_sqrt0, mpz_sqrt, sqr_mod, sum, modulus_q, 0 ); - gmp_randclear( state ); - } -} - BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { for ( size_t i = 0; i < 10; i++ ) { size_t num_all = rand_gen() % 16 + 1; @@ -111,16 +62,12 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { encryption::DkgTe dkg_te( num_signed, num_all ); - std::vector< encryption::element_wrapper > poly = dkg_te.GeneratePolynomial(); - element_t zero; - element_init_Zr( zero, TEDataSingleton::getData().pairing_ ); - element_set0( zero ); - encryption::element_wrapper zero_el( zero ); - - element_clear( zero ); + std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); + + libff::alt_bn128_Fr zero_el = libff::alt_bn128_Fr::zero(); - encryption::element_wrapper common_skey = dkg_te.ComputePolynomialValue( poly, zero_el ); - BOOST_REQUIRE( element_cmp( common_skey.el_, poly.at( 0 ).el_ ) == 0 ); + libff::alt_bn128_Fr common_skey = dkg_te.ComputePolynomialValue( poly, zero_el ); + BOOST_REQUIRE( common_skey == poly.at( 0 ) ); TEPrivateKey common_private( common_skey, num_signed, num_all ); @@ -131,16 +78,15 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { } TEPublicKey common_public( common_private, num_signed, num_all ); - std::shared_ptr msg_ptr = std::make_shared< std::string >( message ); + auto msg_ptr = std::make_shared< std::string >( message ); encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); - std::vector< encryption::element_wrapper > skeys = - dkg_te.CreateSecretKeyContribution( poly ); + std::vector< libff::alt_bn128_Fr > skeys = dkg_te.CreateSecretKeyContribution( poly ); std::vector< TEPrivateKeyShare > skey_shares; std::vector< TEPublicKeyShare > public_key_shares; for ( size_t i = 0; i < num_all; i++ ) { skey_shares.emplace_back( - TEPrivateKeyShare( skeys[i].el_, i + 1, num_signed, num_all ) ); + TEPrivateKeyShare( skeys[i], i + 1, num_signed, num_all ) ); public_key_shares.emplace_back( TEPublicKeyShare( skey_shares[i], num_signed, num_all ) ); } @@ -157,9 +103,9 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { TEDecryptSet decr_set( num_signed, num_all ); for ( size_t i = 0; i < num_signed; i++ ) { - encryption::element_wrapper decrypt = skey_shares[i].decrypt( cypher ); - BOOST_REQUIRE( public_key_shares[i].Verify( cypher, decrypt.el_ ) ); - std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt ); + libff::alt_bn128_G2 decrypt = skey_shares[i].getDecryptionShare( cypher ); + BOOST_REQUIRE( public_key_shares[i].Verify( cypher, decrypt ) ); + auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt ); decr_set.addDecrypt( skey_shares[i].getSignerIndex(), decr_ptr ); } std::string message_decrypted = decr_set.merge( cypher ); @@ -185,8 +131,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { BOOST_REQUIRE( is_exception_caught ); bad_cypher = cypher; // corrupt U in cypher - element_t rand_el; - element_init_G1( rand_el, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_G2 rand_el = libff::alt_bn128_G2::random_element(); std::get< 0 >( bad_cypher ) = rand_el; is_exception_caught = false; @@ -198,8 +143,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { BOOST_REQUIRE( is_exception_caught ); bad_cypher = cypher; // corrupt W in cypher - element_t rand_el2; - element_init_G1( rand_el2, TEDataSingleton::getData().pairing_ ); + libff::alt_bn128_G1 rand_el2 = libff::alt_bn128_G1::random_element(); std::get< 2 >( bad_cypher ) = rand_el2; is_exception_caught = false; try { @@ -211,39 +155,31 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { size_t ind = rand_gen() % num_signed; // corrupt random private key share - element_t bad_pkey; - element_init_Zr( bad_pkey, TEDataSingleton::getData().pairing_ ); - element_random( bad_pkey ); - TEPrivateKeyShare bad_key( encryption::element_wrapper( bad_pkey ), - skey_shares[ind].getSignerIndex(), num_signed, num_all ); + libff::alt_bn128_Fr bad_pkey = libff::alt_bn128_Fr::random_element(); + TEPrivateKeyShare bad_key( bad_pkey, skey_shares[ind].getSignerIndex(), num_signed, num_all ); skey_shares[ind] = bad_key; - element_clear( bad_pkey ); TEDecryptSet bad_decr_set( num_signed, num_all ); for ( size_t i = 0; i < num_signed; i++ ) { - encryption::element_wrapper decrypt = skey_shares[i].decrypt( cypher ); + libff::alt_bn128_G2 decrypt = skey_shares[i].getDecryptionShare( cypher ); if ( i == ind ) - BOOST_REQUIRE( !public_key_shares[i].Verify( cypher, decrypt.el_ ) ); - std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt ); + BOOST_REQUIRE( !public_key_shares[i].Verify( cypher, decrypt ) ); + auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt ); bad_decr_set.addDecrypt( skey_shares[i].getSignerIndex(), decr_ptr ); } std::string bad_message_decrypted = bad_decr_set.merge( cypher ); BOOST_REQUIRE( message != bad_message_decrypted ); - - element_clear( rand_el ); - element_clear( rand_el2 ); } } BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { - for ( size_t i = 0; i < 10; i++ ) { + for ( size_t i = 0; i < 10; ++i ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; encryption::DkgTe dkg_te( num_signed, num_all ); - std::string message; size_t msg_length = 64; for ( size_t length = 0; length < msg_length; ++length ) { @@ -255,7 +191,7 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { keys = TEPrivateKeyShare::generateSampleKeys( num_signed, num_all ); - std::shared_ptr msg_ptr = std::make_shared< std::string >( message ); + auto msg_ptr = std::make_shared< std::string >( message ); encryption::Ciphertext cypher = keys.second->encrypt( msg_ptr ); std::vector< TEPublicKeyShare > public_key_shares; @@ -276,9 +212,9 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { TEDecryptSet decr_set( num_signed, num_all ); for ( size_t i = 0; i < num_signed; i++ ) { - encryption::element_wrapper decrypt = ( *keys.first->at( i ) ).decrypt( cypher ); - BOOST_REQUIRE( public_key_shares.at( i ).Verify( cypher, decrypt.el_ ) ); - std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt ); + libff::alt_bn128_G2 decrypt = ( *keys.first->at( i ) ).getDecryptionShare( cypher ); + BOOST_REQUIRE( public_key_shares.at( i ).Verify( cypher, decrypt ) ); + auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt ); decr_set.addDecrypt( ( *keys.first->at( i ) ).getSignerIndex(), decr_ptr ); } std::string message_decrypted = decr_set.merge( cypher ); @@ -291,49 +227,32 @@ BOOST_AUTO_TEST_CASE( WrappersFromString ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - element_t test0; - element_init_G1( test0, TEDataSingleton::getData().pairing_ ); - element_random( test0 ); - TEPublicKey common_pkey( encryption::element_wrapper( test0 ), num_signed, num_all ); - - element_clear( test0 ); + libff::alt_bn128_G2 test0 = libff::alt_bn128_G2::random_element(); + TEPublicKey common_pkey( test0, num_signed, num_all ); TEPublicKey common_pkey_from_str( common_pkey.toString(), num_signed, num_all ); - BOOST_REQUIRE( element_cmp( common_pkey.getPublicKey().el_, - common_pkey_from_str.getPublicKey().el_ ) == 0 ); - - element_t test; - element_init_Zr( test, TEDataSingleton::getData().pairing_ ); - element_random( test ); - TEPrivateKey private_key( encryption::element_wrapper( test ), num_signed, num_all ); + BOOST_REQUIRE( common_pkey.getPublicKey() == common_pkey_from_str.getPublicKey() ); - element_clear( test ); + libff::alt_bn128_Fr test = libff::alt_bn128_Fr::random_element(); + TEPrivateKey private_key( test, num_signed, num_all ); TEPrivateKey private_key_from_str( std::make_shared< std::string >( private_key.toString() ), num_signed, num_all ); - BOOST_REQUIRE( element_cmp( private_key.getPrivateKey().el_, - private_key_from_str.getPrivateKey().el_ ) == 0 ); + BOOST_REQUIRE( private_key.getPrivateKey() == private_key_from_str.getPrivateKey() ); - element_t test2; - element_init_Zr( test2, TEDataSingleton::getData().pairing_ ); - element_random( test2 ); + libff::alt_bn128_Fr test2 = libff::alt_bn128_Fr::random_element(); size_t signer = rand_gen() % num_all; - TEPrivateKeyShare pr_key_share( - encryption::element_wrapper( test2 ), signer, num_signed, num_all ); - - element_clear( test2 ); + TEPrivateKeyShare pr_key_share( test2, signer, num_signed, num_all ); TEPrivateKeyShare pr_key_share_from_str( std::make_shared< std::string >( pr_key_share.toString() ), signer, num_signed, num_all ); - BOOST_REQUIRE( element_cmp( pr_key_share.getPrivateKey().el_, - pr_key_share_from_str.getPrivateKey().el_ ) == 0 ); + BOOST_REQUIRE( pr_key_share.getPrivateKey() == pr_key_share_from_str.getPrivateKey() ); TEPublicKeyShare pkey( pr_key_share, num_signed, num_all ); TEPublicKeyShare pkey_from_str( pkey.toString(), pr_key_share.getSignerIndex(), num_signed, num_all ); - BOOST_REQUIRE( - element_cmp( pkey.getPublicKey().el_, pkey_from_str.getPublicKey().el_ ) == 0 ); + BOOST_REQUIRE( pkey.getPublicKey() == pkey_from_str.getPublicKey() ); } std::cerr << "TE wrappers tests finished" << std::endl; } @@ -342,8 +261,8 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { for ( size_t i = 0; i < 10; i++ ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % num_all + 1; - std::vector< std::vector< encryption::element_wrapper > > secret_shares_all; - std::vector< std::vector< encryption::element_wrapper > > public_shares_all; + std::vector< std::vector< libff::alt_bn128_Fr > > secret_shares_all; + std::vector< std::vector< libff::alt_bn128_G2 > > public_shares_all; std::vector< DKGTEWrapper > dkgs; std::vector< TEPrivateKeyShare > skeys; std::vector< TEPublicKeyShare > pkeys; @@ -352,15 +271,15 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { DKGTEWrapper dkg_wrap( num_signed, num_all ); encryption::DkgTe dkg_te( num_signed, num_all ); - std::vector< encryption::element_wrapper > poly = dkg_te.GeneratePolynomial(); + std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); auto shared_poly = - std::make_shared< std::vector< encryption::element_wrapper > >( poly ); + std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ); dkg_wrap.setDKGSecret( shared_poly ); dkgs.push_back( dkg_wrap ); - std::shared_ptr< std::vector< encryption::element_wrapper > > secret_shares_ptr = + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr = dkg_wrap.createDKGSecretShares(); - std::shared_ptr< std::vector< encryption::element_wrapper > > public_shares_ptr = + std::shared_ptr< std::vector< libff::alt_bn128_G2 > > public_shares_ptr = dkg_wrap.createDKGPublicShares(); secret_shares_all.push_back( *secret_shares_ptr ); public_shares_all.push_back( *public_shares_ptr ); @@ -370,14 +289,14 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { for ( size_t i = 0; i < num_all; i++ ) for ( size_t j = 0; j < num_all; j++ ) { BOOST_REQUIRE( dkgs.at( i ).VerifyDKGShare( j, secret_shares_all.at( i ).at( j ), - std::make_shared< std::vector< encryption::element_wrapper > >( + std::make_shared< std::vector< libff::alt_bn128_G2 > >( public_shares_all.at( i ) ) ) ); } - std::vector< std::vector< encryption::element_wrapper > > secret_key_shares; + std::vector< std::vector< libff::alt_bn128_Fr > > secret_key_shares; for ( size_t i = 0; i < num_all; i++ ) { - std::vector< encryption::element_wrapper > secret_key_contribution; + std::vector< libff::alt_bn128_Fr > secret_key_contribution; for ( size_t j = 0; j < num_all; j++ ) { secret_key_contribution.push_back( secret_shares_all.at( j ).at( i ) ); } @@ -386,7 +305,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { for ( size_t i = 0; i < num_all; i++ ) { TEPrivateKeyShare pkey_share = dkgs.at( i ).CreateTEPrivateKeyShare( - i + 1, std::make_shared< std::vector< encryption::element_wrapper > >( + i + 1, std::make_shared< std::vector< libff::alt_bn128_Fr > >( secret_key_shares.at( i ) ) ); skeys.push_back( pkey_share ); pkeys.push_back( TEPublicKeyShare( pkey_share, num_signed, num_all ) ); @@ -419,7 +338,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { element_clear(public_key);*/ TEPublicKey common_public = DKGTEWrapper::CreateTEPublicKey( - std::make_shared< std::vector< std::vector< encryption::element_wrapper > > >( + std::make_shared< std::vector< std::vector< libff::alt_bn128_G2 > > >( public_shares_all ), num_signed, num_all ); @@ -429,7 +348,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { message += char( rand_gen() % 128 ); } - std::shared_ptr msg_ptr = std::make_shared< std::string >( message ); + auto msg_ptr = std::make_shared< std::string >( message ); encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); for ( size_t i = 0; i < num_all - num_signed; ++i ) { @@ -444,9 +363,9 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { TEDecryptSet decr_set( num_signed, num_all ); for ( size_t i = 0; i < num_signed; i++ ) { - encryption::element_wrapper decrypt = skeys[i].decrypt( cypher ); - BOOST_REQUIRE( pkeys[i].Verify( cypher, decrypt.el_ ) ); - std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt ); + libff::alt_bn128_G2 decrypt = skeys[i].getDecryptionShare( cypher ); + BOOST_REQUIRE( pkeys[i].Verify( cypher, decrypt ) ); + auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt ); decr_set.addDecrypt( skeys[i].getSignerIndex(), decr_ptr ); } @@ -462,7 +381,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { bool is_exception_caught = false; try { - TEDataSingleton::checkSigners( 0, num_all ); + checkSigners( 0, num_all ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -470,7 +389,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; try { - TEDataSingleton::checkSigners( 0, 0 ); + checkSigners( 0, 0 ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -516,33 +435,20 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // one zero component in cypher try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); - encryption::element_wrapper el_wrap( el ); - element_clear( el ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); TEPublicKeyShare pkey( - TEPrivateKeyShare( el_wrap, 1, num_signed, num_all ), num_signed, num_all ); - - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_set_str( U, "[0, 0]", 10 ); - encryption::element_wrapper U_wrap( U ); - element_clear( U ); + TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); + libff::alt_bn128_G2 U = libff::alt_bn128_G2::zero(); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_random( W ); - encryption::element_wrapper W_wrap( W ); - element_clear( W ); + libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); encryption::Ciphertext cypher; - std::get< 0 >( cypher ) = U_wrap; + std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "tra-la-la"; - std::get< 2 >( cypher ) = W_wrap; + std::get< 2 >( cypher ) = W; - pkey.Verify( cypher, el ); + pkey.Verify( cypher, U ); } catch ( std::runtime_error& ) { is_exception_caught = true; @@ -552,31 +458,20 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // wrong string length in cypher try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); - encryption::element_wrapper el_wrap( el ); - element_clear( el ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); TEPublicKeyShare pkey( - TEPrivateKeyShare( el_wrap, 1, num_signed, num_all ), num_signed, num_all ); - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_random( U ); + TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); + libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_random( W ); + libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); encryption::Ciphertext cypher; - std::get< 0 >( cypher ) = encryption::element_wrapper( U ); + std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "tra-la-la"; - std::get< 2 >( cypher ) = encryption::element_wrapper( W ); + std::get< 2 >( cypher ) = W; - element_clear( U ); - element_clear( W ); - - pkey.Verify( cypher, el ); + pkey.Verify( cypher, U ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -584,40 +479,21 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero decrypted try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); - TEPublicKeyShare pkey( - TEPrivateKeyShare( encryption::element_wrapper( el ), 1, num_signed, num_all ), - num_signed, num_all ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); + TEPublicKeyShare pkey( TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_random( U ); + libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_random( W ); + libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); encryption::Ciphertext cypher; - std::get< 0 >( cypher ) = encryption::element_wrapper( U ); - std::get< 1 >( cypher ) = - "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; - std::get< 2 >( cypher ) = encryption::element_wrapper( W ); - - element_t decr; - element_init_G1( decr, TEDataSingleton::getData().pairing_ ); - element_set_str( decr, "[0, 0]", 10 ); - encryption::element_wrapper decrypt( decr ); - element_clear( decr ); - - element_clear( el ); - element_clear( U ); - element_clear( W ); - - pkey.Verify( cypher, decrypt.el_ ); + std::get< 0 >( cypher ) = U; + std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; + std::get< 2 >( cypher ) = W; + libff::alt_bn128_G2 decrypt = libff::alt_bn128_G2::zero(); + pkey.Verify( cypher, decrypt ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -643,12 +519,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero private key share try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_set0( el ); - encryption::element_wrapper el_wrap( el ); - element_clear( el ); - TEPrivateKeyShare( el_wrap, 1, num_signed, num_all ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); + TEPrivateKeyShare( el, 1, num_signed, num_all ); } catch ( std::runtime_error& ) { is_exception_caught = true; @@ -675,12 +547,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero public key try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_set_si( el, 0 ); - encryption::element_wrapper el_wrap( el ); - element_clear( el ); - TEPublicKey pkey( TEPrivateKey( el_wrap, num_signed, num_all ), num_signed, num_all ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); + TEPublicKey pkey( TEPrivateKey( el, num_signed, num_all ), num_signed, num_all ); } catch ( std::runtime_error& ) { is_exception_caught = true; @@ -689,12 +557,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero public key try { - element_t el; - element_init_G1( el, TEDataSingleton::getData().pairing_ ); - element_set_str( el, "[0, 0]", 10 ); - encryption::element_wrapper el_wrap( el ); - element_clear( el ); - TEPublicKey pkey( el_wrap, num_signed, num_all ); + libff::alt_bn128_G2 el = libff::alt_bn128_G2::zero(); + TEPublicKey pkey( el, num_signed, num_all ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -702,12 +566,9 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // null message try { - element_t el; - element_init_G1( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); + libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element(); TEPublicKey pkey( el, num_signed, num_all ); - element_clear( el ); pkey.encrypt( nullptr ); } catch ( std::runtime_error& ) { @@ -717,12 +578,9 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // message length is not 64 try { - element_t el; - element_init_G1( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); + libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element(); TEPublicKey pkey( el, num_signed, num_all ); - element_clear( el ); pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ); } catch ( std::runtime_error& ) { @@ -749,12 +607,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero private key try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_set0( el ); - encryption::element_wrapper el_wrap( el ); - element_clear( el ); - TEPrivateKey( el_wrap, num_signed, num_all ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); + TEPrivateKey( el, num_signed, num_all ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -772,17 +626,11 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { try { TEDecryptSet decr_set( num_signed, num_all ); // same indices in decrypt set - element_t el1; - element_init_G1( el1, TEDataSingleton::getData().pairing_ ); - element_random( el1 ); - std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el1 ); - element_clear( el1 ); + libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element(); + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); - element_t el2; - element_init_G1( el2, TEDataSingleton::getData().pairing_ ); - element_random( el2 ); - std::shared_ptr el_ptr2 = std::make_shared< encryption::element_wrapper >( el2 ); - element_clear( el2 ); + libff::alt_bn128_G2 el2 = libff::alt_bn128_G2::random_element(); + auto el_ptr2 = std::make_shared< libff::alt_bn128_G2 >( el2 ); decr_set.addDecrypt( 1, el_ptr1 ); decr_set.addDecrypt( 1, el_ptr2 ); @@ -796,11 +644,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { try { TEDecryptSet decr_set( num_signed, num_all ); // zero element in decrypt set - element_t el1; - element_init_G1( el1, TEDataSingleton::getData().pairing_ ); - element_set_str( el1, "[0, 0]", 10 ); - std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el1 ); - element_clear( el1 ); + libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero(); + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); decr_set.addDecrypt( 1, el_ptr1 ); } catch ( std::runtime_error& ) { @@ -811,13 +656,10 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; try { TEDecryptSet decr_set( num_signed, num_all ); // null element in decrypt set - element_t el1; - element_init_G1( el1, TEDataSingleton::getData().pairing_ ); - element_set_str( el1, "[0, 0]", 10 ); - encryption::element_wrapper el_wrap( el1 ); - std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el_wrap ); + libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero(); + + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); el_ptr1 = nullptr; - element_clear( el1 ); decr_set.addDecrypt( 1, el_ptr1 ); } catch ( std::runtime_error& ) { @@ -828,31 +670,20 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; try { TEDecryptSet decr_set( num_signed, num_all ); // not enough elements in decrypt set - element_t el1; - element_init_G1( el1, TEDataSingleton::getData().pairing_ ); - element_random( el1 ); - encryption::element_wrapper el_wrap( el1 ); - element_clear( el1 ); - std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el_wrap ); + libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element(); + + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); decr_set.addDecrypt( 1, el_ptr1 ); - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_random( U ); - encryption::element_wrapper U_wrap( U ); - element_clear( U ); + libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_random( W ); - encryption::element_wrapper W_wrap( W ); - element_clear( W ); + libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); encryption::Ciphertext cypher; - std::get< 0 >( cypher ) = U_wrap; + std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; - std::get< 2 >( cypher ) = W_wrap; + std::get< 2 >( cypher ) = W; decr_set.merge( cypher ); } catch ( std::runtime_error& ) { @@ -863,30 +694,19 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; try { TEDecryptSet decr_set( 1, 1 ); // cannot combine shares - element_t el1; - element_init_G1( el1, TEDataSingleton::getData().pairing_ ); - element_random( el1 ); - std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el1 ); - element_clear( el1 ); + libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element(); + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); decr_set.addDecrypt( 1, el_ptr1 ); - element_t U; - element_init_G1( U, TEDataSingleton::getData().pairing_ ); - element_random( U ); - encryption::element_wrapper U_wrap( U ); - element_clear( U ); + libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); - element_t W; - element_init_G1( W, TEDataSingleton::getData().pairing_ ); - element_random( W ); - encryption::element_wrapper W_wrap( W ); - element_clear( W ); + libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); encryption::Ciphertext cypher; - std::get< 0 >( cypher ) = U_wrap; + std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; - std::get< 2 >( cypher ) = W_wrap; + std::get< 2 >( cypher ) = W; decr_set.merge( cypher ); } catch ( std::runtime_error& ) { @@ -905,13 +725,9 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { // zero share DKGTEWrapper dkg_te( num_signed, num_all ); - element_t el1; - element_init_Zr( el1, TEDataSingleton::getData().pairing_ ); - element_set0( el1 ); - encryption::element_wrapper el_wrap( el1 ); - element_clear( el1 ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - dkg_te.VerifyDKGShare( 1, el_wrap, dkg_te.createDKGPublicShares() ); + dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -922,12 +738,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { // null verification vector DKGTEWrapper dkg_te( num_signed, num_all ); - element_t el1; - element_init_Zr( el1, TEDataSingleton::getData().pairing_ ); - element_random( el1 ); - encryption::element_wrapper el_wrap( el1 ); - element_clear( el1 ); - dkg_te.VerifyDKGShare( 1, el_wrap, nullptr ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); + dkg_te.VerifyDKGShare( 1, el, nullptr ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -937,17 +749,13 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { try { DKGTEWrapper dkg_te( num_signed, num_all ); - element_t el1; - element_init_Zr( el1, TEDataSingleton::getData().pairing_ ); - element_random( el1 ); - encryption::element_wrapper el_wrap( el1 ); - element_clear( el1 ); + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); - std::vector< encryption::element_wrapper > pub_shares = *dkg_te.createDKGPublicShares(); + std::vector< libff::alt_bn128_G2 > pub_shares = *dkg_te.createDKGPublicShares(); pub_shares.erase( pub_shares.begin() ); - dkg_te.VerifyDKGShare( 1, el_wrap, - std::make_shared< std::vector< encryption::element_wrapper > >( pub_shares ) ); + dkg_te.VerifyDKGShare( 1, el, + std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ); } catch ( std::runtime_error& ) { is_exception_caught = true; @@ -957,7 +765,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { is_exception_caught = false; try { DKGTEWrapper dkg_te( num_signed, num_all ); - std::shared_ptr< std::vector< encryption::element_wrapper > > shares = + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares = dkg_te.createDKGSecretShares(); shares = nullptr; dkg_te.setDKGSecret( shares ); @@ -969,7 +777,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { is_exception_caught = false; try { DKGTEWrapper dkg_te( num_signed, num_all ); - std::shared_ptr< std::vector< encryption::element_wrapper > > shares = + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares = dkg_te.createDKGSecretShares(); shares->erase( shares->begin() + shares->size() - 2 ); shares->shrink_to_fit(); @@ -991,7 +799,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { is_exception_caught = false; try { DKGTEWrapper dkg_te( num_signed, num_all ); - auto wrong_size_vector = std::make_shared< std::vector< encryption::element_wrapper > >(); + auto wrong_size_vector = std::make_shared< std::vector< libff::alt_bn128_Fr > >(); wrong_size_vector->resize( num_signed - 1 ); dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ); } catch ( std::runtime_error& ) { @@ -1002,7 +810,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { is_exception_caught = false; try { DKGTEWrapper dkg_te( num_signed, num_all ); - std::shared_ptr< std::vector< encryption::element_wrapper > > shares; + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares; dkg_te.setDKGSecret( shares ); } catch ( std::runtime_error& ) { is_exception_caught = true; diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 1f57f85d..34d021d9 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -15,10 +15,9 @@ set(sources TEPrivateKey.cpp TEPublicKey.cpp TEPublicKeyShare.cpp - TEDataSingleton.h - ../dkg/dkg.cpp - ../dkg/DKGBLSWrapper.cpp - ../dkg/DKGBLSSecret.cpp + ../dkg/dkg_te.cpp + ../dkg/DKGTEWrapper.cpp + ../dkg/DKGTESecret.cpp utils.cpp ../tools/utils.cpp ) @@ -30,15 +29,14 @@ set(headers TEPrivateKey.h TEPublicKey.h TEPublicKeyShare.h - TEDataSingleton.cpp - ../dkg/dkg.h - ../dkg/DKGBLSWrapper.h - ../dkg/DKGBLSSecret.h + ../dkg/dkg_te.h + ../dkg/DKGTEWrapper.h + ../dkg/DKGTESecret.h utils.h ../tools/utils.h ) -set(PROJECT_VERSION 0.1.0) +set(PROJECT_VERSION 0.2.0) set (THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../) set (TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../) @@ -48,13 +46,10 @@ add_library(te ${sources} ${headers}) include_directories(${CMAKE_BINARY_DIR}) link_directories(${CMAKE_BINARY_DIR}) -target_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(te PRIVATE ${GMPXX_LIBRARY} ${GMP_LIBRARY}) - include_directories(${CMAKE_BINARY_DIR}/deps/include ) link_directories(${CMAKE_BINARY_DIR}/deps/lib) -target_include_directories(te PRIVATE ${PBC_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) +target_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) target_link_libraries(te PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) if (BUILD_TESTS) @@ -72,7 +67,7 @@ if (BUILD_TESTS) add_executable(te_test ../test/test_TE_wrappers.cpp) target_include_directories(te_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) - target_link_libraries(te_test PRIVATE te pbc ${GMP_LIBRARY} ${GMPXX_LIBRARY}) + target_link_libraries(te_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_test(NAME te_wrap_tests COMMAND te_unit_test) diff --git a/threshold_encryption/TEDataSingleton.cpp b/threshold_encryption/TEDataSingleton.cpp deleted file mode 100644 index 1fc40c71..00000000 --- a/threshold_encryption/TEDataSingleton.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* -Copyright (C) 2018-2019 SKALE Labs - -This file is part of libBLS. - -libBLS is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -libBLS is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . - -@file TEPublicKey.h -@author Sveta Rogova -@date 2019 -*/ - -#include -#include - -TEDataSingleton::TEDataSingleton() { - char aparam[] = - "type a\n" - "q " - "878071079966331252243778198475404981580688319941420821102865339926647563088022295707862517" - "9422662221423155858769582317459277713367317481324925129998224791\n" - "h " - "120160122648911460793888213667405342048029544012513118229196151310472072893597045311028448" - "02183906537786776\n" - "r 730750818665451621361119245571504901405976559617\n" - "exp2 159\n" - "exp1 107\n" - "sign1 1\n" - "sign0 1\n"; - - pairing_init_set_str( pairing_, aparam ); - - element_init_G1( generator_, pairing_ ); - element_random( generator_ ); - while ( element_is0( generator_ ) ) { - element_random( generator_ ); - } -} - -void TEDataSingleton::checkSigners( size_t _requiredSigners, size_t _totalSigners ) { - if ( _requiredSigners > _totalSigners ) { - throw std::runtime_error( "_requiredSigners > _totalSigners" ); - } - - if ( _totalSigners == 0 ) { - throw std::runtime_error( "_totalSigners == 0" ); - } - - if ( _requiredSigners == 0 ) { - throw std::runtime_error( "_requiredSigners == 0" ); - } -} - -TEDataSingleton::~TEDataSingleton() { - element_clear( generator_ ); - pairing_clear( pairing_ ); -} diff --git a/threshold_encryption/TEDataSingleton.h b/threshold_encryption/TEDataSingleton.h deleted file mode 100644 index e64cf329..00000000 --- a/threshold_encryption/TEDataSingleton.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright (C) 2018-2019 SKALE Labs - - This file is part of libBLS. - - libBLS is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - libBLS is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . - - @file TEPublicKey.h - @author Sveta Rogova - @date 2019 -*/ - -#include - -#ifndef LIBBLS_TEBASEWRAPPER_H -#define LIBBLS_TEBASEWRAPPER_H - - -class TEDataSingleton { -private: - TEDataSingleton(); - -public: - pairing_t pairing_; - element_t generator_; - - static TEDataSingleton& getData() { - static TEDataSingleton data; - return data; - } - - static void checkSigners( size_t _requiredSigners, size_t _totalSigners ); - - ~TEDataSingleton(); -}; - - -#endif // LIBBLS_TEBASEWRAPPER_H diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index d5c987bd..a26ab222 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -14,22 +14,26 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file TEPublicKey.h @author Sveta Rogova @date 2019 */ -#include #include #include #include +#include "../tools/utils.h" + TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - // TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); + } void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el ) { diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 15f24921..0063f04e 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -28,29 +28,33 @@ along with libBLS. If not, see . TEPrivateKey::TEPrivateKey( std::shared_ptr< std::string > _key_str, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { throw std::runtime_error( "private key is null" ); } + libff::init_alt_bn128_params(); + privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); if ( privateKey.is_zero() ) { - throw std::runtime_error( " private key is zero" ); + throw std::runtime_error( "private key is zero" ); } } TEPrivateKey::TEPrivateKey( libff::alt_bn128_Fr _skey, size_t _requiredSigners, size_t _totalSigners ) : privateKey( _skey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); if ( _skey.is_zero() ) - throw std::runtime_error( " private key is zero" ); + throw std::runtime_error( "private key is zero" ); } -std::string TEPrivateKey::toString() { +std::string TEPrivateKey::toString() const { return fieldElementToString( privateKey ); } diff --git a/threshold_encryption/TEPrivateKey.h b/threshold_encryption/TEPrivateKey.h index cb2d875e..af96ca7e 100644 --- a/threshold_encryption/TEPrivateKey.h +++ b/threshold_encryption/TEPrivateKey.h @@ -24,7 +24,6 @@ #ifndef LIBBLS_TEPRIVATEKEY_H #define LIBBLS_TEPRIVATEKEY_H -#include #include class TEPrivateKey { @@ -40,7 +39,7 @@ class TEPrivateKey { TEPrivateKey( libff::alt_bn128_Fr _skey, size_t _requiredSigners, size_t _totalSigners ); - std::string toString(); + std::string toString() const; libff::alt_bn128_Fr getPrivateKey() const; }; diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index 97707bec..bbebd498 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -22,7 +22,7 @@ along with libBLS. If not, see . */ #include "../tools/utils.h" -#include +#include #include #include @@ -31,12 +31,14 @@ TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, s : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { throw std::runtime_error( "private key share is null" ); } + libff::init_alt_bn128_params(); + privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); if ( privateKey.is_zero() ) { @@ -50,12 +52,14 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( _signerIndex > _totalSigners ) { throw std::runtime_error( "Wrong _signerIndex" ); } + libff::init_alt_bn128_params(); + if ( _skey_share.is_zero() ) { throw std::runtime_error( "Zero private key share" ); } @@ -66,9 +70,6 @@ libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( encryption::Ciphertex encryption::TE te( requiredSigners, totalSigners ); - element_t decrypt; - element_init_G1( decrypt, TEDataSingleton::getData().pairing_ ); - libff::alt_bn128_G2 decryption_share = te.getDecryptionShare( cipher, privateKey ); if ( decryption_share.is_zero() ) { @@ -78,7 +79,7 @@ libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( encryption::Ciphertex return decryption_share; } -std::string TEPrivateKeyShare::toString() { +std::string TEPrivateKeyShare::toString() const { return fieldElementToString( privateKey ); } @@ -93,15 +94,15 @@ libff::alt_bn128_Fr TEPrivateKeyShare::getPrivateKey() const { std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > TEPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) { - signatures::Dkg dkg_te( _requiredSigners, _totalSigners ); + encryption::DkgTe dkg_te( _requiredSigners, _totalSigners ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - libff::alt_bn128_Fr common_skey = dkg_te.PolynomialValue( poly, libff::alt_bn128_Fr::zero() ); + libff::alt_bn128_Fr common_skey = dkg_te.ComputePolynomialValue( poly, libff::alt_bn128_Fr::zero() ); TEPrivateKey common_private( common_skey, _requiredSigners, _totalSigners ); TEPublicKey common_public( common_private, _requiredSigners, _totalSigners ); - std::vector< libff::alt_bn128_Fr > skeys = dkg_te.SecretKeyContribution( poly ); + std::vector< libff::alt_bn128_Fr > skeys = dkg_te.CreateSecretKeyContribution( poly ); std::vector< std::shared_ptr< TEPrivateKeyShare > > skey_shares; diff --git a/threshold_encryption/TEPrivateKeyShare.h b/threshold_encryption/TEPrivateKeyShare.h index 2358214f..4b472b61 100644 --- a/threshold_encryption/TEPrivateKeyShare.h +++ b/threshold_encryption/TEPrivateKeyShare.h @@ -24,7 +24,6 @@ #ifndef LIBBLS_TEPRIVATEKEYSHARE_H #define LIBBLS_TEPRIVATEKEYSHARE_H -#include #include #include @@ -49,7 +48,7 @@ class TEPrivateKeyShare { std::shared_ptr< TEPublicKey > > generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ); - std::string toString(); + std::string toString() const; size_t getSignerIndex() const; diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index bfb7a214..b1e03c92 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -22,7 +22,6 @@ along with libBLS. If not, see . */ #include "../tools/utils.h" -#include #include #include @@ -33,7 +32,7 @@ along with libBLS. If not, see . TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str_ptr, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { throw std::runtime_error( "public key is null" ); @@ -48,6 +47,8 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); } + libff::init_alt_bn128_params(); + PublicKey.Z = libff::alt_bn128_Fq2::one(); PublicKey.X.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 0 ).c_str() ); PublicKey.X.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 1 ).c_str() ); @@ -62,7 +63,9 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str TEPublicKey::TEPublicKey( TEPrivateKey _common_private, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); if ( _common_private.getPrivateKey().is_zero() ) { throw std::runtime_error( "zero key" ); @@ -73,7 +76,7 @@ TEPublicKey::TEPublicKey( TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, size_t _totalSigners ) : PublicKey( _pkey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( _pkey.is_zero() ) { throw std::runtime_error( "zero public key" ); diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index bf53cf77..ff031850 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -22,7 +22,6 @@ along with libBLS. If not, see . */ #include "../tools/utils.h" -#include #include #include @@ -31,7 +30,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { throw std::runtime_error( "public key share is null" ); @@ -46,6 +45,8 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); } + libff::init_alt_bn128_params(); + PublicKey.Z = libff::alt_bn128_Fq2::one(); PublicKey.X.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 0 ).c_str() ); PublicKey.X.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 1 ).c_str() ); @@ -60,7 +61,9 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > TEPublicKeyShare::TEPublicKeyShare( TEPrivateKeyShare _p_key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); PublicKey = _p_key.getPrivateKey() * libff::alt_bn128_G2::one(); signerIndex = _p_key.getSignerIndex(); diff --git a/threshold_encryption/utils.h b/threshold_encryption/utils.h index 431c551b..b941980c 100644 --- a/threshold_encryption/utils.h +++ b/threshold_encryption/utils.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file utils.h @author Oleh Nikolaiev diff --git a/tools/utils.cpp b/tools/utils.cpp index 50c7df90..221cb2ff 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -23,21 +23,35 @@ #include "utils.h" -template < class T > -std::string fieldElementToString( const T& field_elem ) { - mpz_t t; - mpz_init( t ); +// template < class T > +// std::string fieldElementToString( const T& field_elem ) { +// mpz_t t; +// mpz_init( t ); - field_elem.as_bigint().to_mpz( t ); +// field_elem.as_bigint().to_mpz( t ); - char arr[mpz_sizeinbase( t, 10 ) + 2]; +// char arr[mpz_sizeinbase( t, 10 ) + 2]; - char* tmp = mpz_get_str( arr, 10, t ); - mpz_clear( t ); +// char* tmp = mpz_get_str( arr, 10, t ); +// mpz_clear( t ); - std::string output = tmp; +// std::string output = tmp; - return output; +// return output; +// } + +void checkSigners( size_t _requiredSigners, size_t _totalSigners ) { + if ( _requiredSigners > _totalSigners ) { + throw std::runtime_error( "_requiredSigners > _totalSigners" ); + } + + if ( _totalSigners == 0 ) { + throw std::runtime_error( "_totalSigners == 0" ); + } + + if ( _requiredSigners == 0 ) { + throw std::runtime_error( "_requiredSigners == 0" ); + } } std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ) { diff --git a/tools/utils.h b/tools/utils.h index 929adf29..00b487fa 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -31,6 +31,8 @@ #include +void checkSigners( size_t _requiredSigners, size_t _totalSigners ); + template < class T > std::string fieldElementToString( const T& field_elem ); @@ -42,4 +44,21 @@ libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_ libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); +template < class T > +std::string fieldElementToString( const T& field_elem ) { + mpz_t t; + mpz_init( t ); + + field_elem.as_bigint().to_mpz( t ); + + char arr[mpz_sizeinbase( t, 10 ) + 2]; + + char* tmp = mpz_get_str( arr, 10, t ); + mpz_clear( t ); + + std::string output = tmp; + + return output; +} + #endif // LIBBLS_UTILS_H From 33b86167b850edf0172d32f304e93c8c478a7625 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 15:01:03 +0300 Subject: [PATCH 04/40] SKALE-4409 format code --- dkg/DKGTEWrapper.cpp | 12 +++++------- dkg/DKGTEWrapper.h | 3 +-- test/test_TE_wrappers.cpp | 21 +++++++++++---------- threshold_encryption/TEDecryptSet.cpp | 1 - threshold_encryption/TEPrivateKeyShare.cpp | 3 ++- tools/utils.cpp | 19 +------------------ tools/utils.h | 3 --- 7 files changed, 20 insertions(+), 42 deletions(-) diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index f6950872..366c5cf3 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -37,7 +37,7 @@ DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share, const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector ) { - if ( _share.is_zero() ) + if ( _share.is_zero() ) throw std::runtime_error( "Zero secret share" ); if ( _verification_vector == nullptr ) throw std::runtime_error( "Null verification vector" ); @@ -54,20 +54,18 @@ void DKGTEWrapper::setDKGSecret( dkg_secret_ptr->setPoly( *_poly_ptr ); } -std::shared_ptr< std::vector< libff::alt_bn128_Fr > > -DKGTEWrapper::createDKGSecretShares() { +std::shared_ptr< std::vector< libff::alt_bn128_Fr > > DKGTEWrapper::createDKGSecretShares() { return std::make_shared< std::vector< libff::alt_bn128_Fr > >( dkg_secret_ptr->getDKGTESecretShares() ); } -std::shared_ptr< std::vector< libff::alt_bn128_G2 > > -DKGTEWrapper::createDKGPublicShares() { +std::shared_ptr< std::vector< libff::alt_bn128_G2 > > DKGTEWrapper::createDKGPublicShares() { return std::make_shared< std::vector< libff::alt_bn128_G2 > >( dkg_secret_ptr->getDKGTEPublicShares() ); } -TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( size_t signerIndex_, - std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) { +TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( + size_t signerIndex_, std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) { if ( secret_shares_ptr == nullptr ) throw std::runtime_error( "Null secret_shares_ptr " ); if ( secret_shares_ptr->size() != totalSigners ) diff --git a/dkg/DKGTEWrapper.h b/dkg/DKGTEWrapper.h index cade40b4..f5f0d0c8 100644 --- a/dkg/DKGTEWrapper.h +++ b/dkg/DKGTEWrapper.h @@ -52,8 +52,7 @@ class DKGTEWrapper { std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ); static TEPublicKey CreateTEPublicKey( - std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > - public_shares_all, + std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all, size_t _requiredSigners, size_t _totalSigners ); }; diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 58bf9968..7092d2e7 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { encryption::DkgTe dkg_te( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - + libff::alt_bn128_Fr zero_el = libff::alt_bn128_Fr::zero(); libff::alt_bn128_Fr common_skey = dkg_te.ComputePolynomialValue( poly, zero_el ); @@ -85,8 +85,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { std::vector< TEPrivateKeyShare > skey_shares; std::vector< TEPublicKeyShare > public_key_shares; for ( size_t i = 0; i < num_all; i++ ) { - skey_shares.emplace_back( - TEPrivateKeyShare( skeys[i], i + 1, num_signed, num_all ) ); + skey_shares.emplace_back( TEPrivateKeyShare( skeys[i], i + 1, num_signed, num_all ) ); public_key_shares.emplace_back( TEPublicKeyShare( skey_shares[i], num_signed, num_all ) ); } @@ -156,7 +155,8 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { size_t ind = rand_gen() % num_signed; // corrupt random private key share libff::alt_bn128_Fr bad_pkey = libff::alt_bn128_Fr::random_element(); - TEPrivateKeyShare bad_key( bad_pkey, skey_shares[ind].getSignerIndex(), num_signed, num_all ); + TEPrivateKeyShare bad_key( + bad_pkey, skey_shares[ind].getSignerIndex(), num_signed, num_all ); skey_shares[ind] = bad_key; TEDecryptSet bad_decr_set( num_signed, num_all ); @@ -272,8 +272,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { encryption::DkgTe dkg_te( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - auto shared_poly = - std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ); + auto shared_poly = std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ); dkg_wrap.setDKGSecret( shared_poly ); dkgs.push_back( dkg_wrap ); @@ -480,7 +479,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero decrypted try { libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); - TEPublicKeyShare pkey( TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); + TEPublicKeyShare pkey( + TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); @@ -488,7 +488,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { encryption::Ciphertext cypher; std::get< 0 >( cypher ) = U; - std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; + std::get< 1 >( cypher ) = + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; std::get< 2 >( cypher ) = W; libff::alt_bn128_G2 decrypt = libff::alt_bn128_G2::zero(); @@ -754,8 +755,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { std::vector< libff::alt_bn128_G2 > pub_shares = *dkg_te.createDKGPublicShares(); pub_shares.erase( pub_shares.begin() ); - dkg_te.VerifyDKGShare( 1, el, - std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ); + dkg_te.VerifyDKGShare( + 1, el, std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ); } catch ( std::runtime_error& ) { is_exception_caught = true; diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index a26ab222..8c9027b0 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -33,7 +33,6 @@ TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); - } void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el ) { diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index bbebd498..fcfbd9fa 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -98,7 +98,8 @@ TEPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSig std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - libff::alt_bn128_Fr common_skey = dkg_te.ComputePolynomialValue( poly, libff::alt_bn128_Fr::zero() ); + libff::alt_bn128_Fr common_skey = + dkg_te.ComputePolynomialValue( poly, libff::alt_bn128_Fr::zero() ); TEPrivateKey common_private( common_skey, _requiredSigners, _totalSigners ); TEPublicKey common_public( common_private, _requiredSigners, _totalSigners ); diff --git a/tools/utils.cpp b/tools/utils.cpp index 221cb2ff..aa9b9b1a 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -23,23 +23,6 @@ #include "utils.h" -// template < class T > -// std::string fieldElementToString( const T& field_elem ) { -// mpz_t t; -// mpz_init( t ); - -// field_elem.as_bigint().to_mpz( t ); - -// char arr[mpz_sizeinbase( t, 10 ) + 2]; - -// char* tmp = mpz_get_str( arr, 10, t ); -// mpz_clear( t ); - -// std::string output = tmp; - -// return output; -// } - void checkSigners( size_t _requiredSigners, size_t _totalSigners ) { if ( _requiredSigners > _totalSigners ) { throw std::runtime_error( "_requiredSigners > _totalSigners" ); @@ -161,4 +144,4 @@ libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_ result.Z = libff::alt_bn128_Fq::one(); return result; -} +} \ No newline at end of file diff --git a/tools/utils.h b/tools/utils.h index 00b487fa..78d18e00 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -33,9 +33,6 @@ void checkSigners( size_t _requiredSigners, size_t _totalSigners ); -template < class T > -std::string fieldElementToString( const T& field_elem ); - std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx, size_t t ); From b54f71386810005f9a5bd06fa1a9148c567722ed Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 15:18:34 +0300 Subject: [PATCH 05/40] SKALE-4409 add ThresholdUtils --- .github/workflows/nightly.yml | 1 - .github/workflows/test.yml | 1 - dkg/DKGTESecret.cpp | 2 +- dkg/DKGTEWrapper.cpp | 4 ++-- test/test_TE_wrappers.cpp | 4 ++-- threshold_encryption/TEDecryptSet.cpp | 2 +- threshold_encryption/TEPrivateKey.cpp | 6 +++--- threshold_encryption/TEPrivateKeyShare.cpp | 6 +++--- threshold_encryption/TEPublicKey.cpp | 9 ++++---- threshold_encryption/TEPublicKeyShare.cpp | 7 ++++--- threshold_encryption/threshold_encryption.cpp | 8 +++---- tools/utils.cpp | 13 +++++++----- tools/utils.h | 21 +++++++++++++------ 13 files changed, 48 insertions(+), 36 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 291339c4..6a6f163d 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -68,7 +68,6 @@ jobs: $NIGHTLY_BUILD_FLAGS ./bls_test $NIGHTLY_BUILD_FLAGS ./threshold_encryption/te_unit_test $NIGHTLY_BUILD_FLAGS ./threshold_encryption/te_test - $NIGHTLY_BUILD_FLAGS ./threshold_encryption/dkg_te_unit_test $NIGHTLY_BUILD_FLAGS ./dkg_attack # - name: Run python test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 835304a5..1ab6aca2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -76,7 +76,6 @@ jobs: ./bls_unit_test ./dkg_unit_test ./bls_test - ./threshold_encryption/dkg_te_unit_test ./threshold_encryption/te_unit_test ./threshold_encryption/te_test ./dkg_attack diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp index 90776b46..018ed1fd 100644 --- a/dkg/DKGTESecret.cpp +++ b/dkg/DKGTESecret.cpp @@ -29,7 +29,7 @@ DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index 366c5cf3..3ff3e22a 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -27,7 +27,7 @@ DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -81,7 +81,7 @@ TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( TEPublicKey DKGTEWrapper::CreateTEPublicKey( std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all, size_t _requiredSigners, size_t _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( public_shares_all == nullptr ) throw std::runtime_error( "Null public shares all" ); diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 7092d2e7..e06f2d71 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { bool is_exception_caught = false; try { - checkSigners( 0, num_all ); + ThresholdUtils::checkSigners( 0, num_all ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -388,7 +388,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; try { - checkSigners( 0, 0 ); + ThresholdUtils::checkSigners( 0, 0 ); } catch ( std::runtime_error& ) { is_exception_caught = true; } diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index 8c9027b0..26ada71e 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -30,7 +30,7 @@ along with libBLS. If not, see . TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); } diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 0063f04e..3ac20c95 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -28,7 +28,7 @@ along with libBLS. If not, see . TEPrivateKey::TEPrivateKey( std::shared_ptr< std::string > _key_str, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { throw std::runtime_error( "private key is null" ); @@ -46,7 +46,7 @@ TEPrivateKey::TEPrivateKey( TEPrivateKey::TEPrivateKey( libff::alt_bn128_Fr _skey, size_t _requiredSigners, size_t _totalSigners ) : privateKey( _skey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -55,7 +55,7 @@ TEPrivateKey::TEPrivateKey( } std::string TEPrivateKey::toString() const { - return fieldElementToString( privateKey ); + return ThresholdUtils::fieldElementToString( privateKey ); } libff::alt_bn128_Fr TEPrivateKey::getPrivateKey() const { diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index fcfbd9fa..a1ed43e6 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -31,7 +31,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, s : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { throw std::runtime_error( "private key share is null" ); @@ -52,7 +52,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _signerIndex > _totalSigners ) { throw std::runtime_error( "Wrong _signerIndex" ); @@ -80,7 +80,7 @@ libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( encryption::Ciphertex } std::string TEPrivateKeyShare::toString() const { - return fieldElementToString( privateKey ); + return ThresholdUtils::fieldElementToString( privateKey ); } size_t TEPrivateKeyShare::getSignerIndex() const { diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index b1e03c92..4aff78ad 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -32,7 +32,7 @@ along with libBLS. If not, see . TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str_ptr, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { throw std::runtime_error( "public key is null" ); @@ -63,7 +63,7 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str TEPublicKey::TEPublicKey( TEPrivateKey _common_private, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -76,7 +76,7 @@ TEPublicKey::TEPublicKey( TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, size_t _totalSigners ) : PublicKey( _pkey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _pkey.is_zero() ) { throw std::runtime_error( "zero public key" ); @@ -111,7 +111,8 @@ encryption::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ } std::shared_ptr< std::vector< std::string > > TEPublicKey::toString() { - return std::make_shared< std::vector< std::string > >( G2ToString( PublicKey ) ); + return std::make_shared< std::vector< std::string > >( + ThresholdUtils::G2ToString( PublicKey ) ); } libff::alt_bn128_G2 TEPublicKey::getPublicKey() const { diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index ff031850..0ac139f3 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -30,7 +30,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { throw std::runtime_error( "public key share is null" ); @@ -61,7 +61,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > TEPublicKeyShare::TEPublicKeyShare( TEPrivateKeyShare _p_key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -82,7 +82,8 @@ bool TEPublicKeyShare::Verify( } std::shared_ptr< std::vector< std::string > > TEPublicKeyShare::toString() { - return std::make_shared< std::vector< std::string > >( G2ToString( PublicKey ) ); + return std::make_shared< std::vector< std::string > >( + ThresholdUtils::G2ToString( PublicKey ) ); } libff::alt_bn128_G2 TEPublicKeyShare::getPublicKey() const { diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 2e174ed0..edf42b45 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -41,7 +41,7 @@ TE::~TE() {} std::string TE::Hash( const libff::alt_bn128_G2& Y, std::string ( *hash_func )( const std::string& str ) ) { - auto vectorCoordinates = G2ToString( Y ); + auto vectorCoordinates = ThresholdUtils::G2ToString( Y ); std::string tmp = ""; for ( const auto& coord : vectorCoordinates ) { @@ -55,9 +55,9 @@ std::string TE::Hash( libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::string& V, std::string ( *hash_func )( const std::string& str ) ) { - // assumed that U lies in G1 + // assumed that U lies in G2 - auto U_str = G2ToString( U ); + auto U_str = ThresholdUtils::G2ToString( U ); const std::string sha256hex = hash_func( U_str[0] + U_str[1] + U_str[2] + U_str[3] + V ); @@ -67,7 +67,7 @@ libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::st hash_bytes_arr->at( i ) = static_cast< uint8_t >( hash_str[i] ); } - return HashtoG1( hash_bytes_arr ); + return ThresholdUtils::HashtoG1( hash_bytes_arr ); } Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ) { diff --git a/tools/utils.cpp b/tools/utils.cpp index aa9b9b1a..acca85b7 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -23,7 +23,7 @@ #include "utils.h" -void checkSigners( size_t _requiredSigners, size_t _totalSigners ) { +void ThresholdUtils::checkSigners( size_t _requiredSigners, size_t _totalSigners ) { if ( _requiredSigners > _totalSigners ) { throw std::runtime_error( "_requiredSigners > _totalSigners" ); } @@ -37,7 +37,7 @@ void checkSigners( size_t _requiredSigners, size_t _totalSigners ) { } } -std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ) { +std::vector< std::string > ThresholdUtils::G2ToString( libff::alt_bn128_G2 elem ) { std::vector< std::string > pkey_str_vect; elem.to_affine_coordinates(); @@ -50,7 +50,8 @@ std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ) { return pkey_str_vect; } -std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx, size_t t ) { +std::vector< libff::alt_bn128_Fr > ThresholdUtils::LagrangeCoeffs( + const std::vector< int >& idx, size_t t ) { if ( idx.size() < t ) { // throw IncorrectInput( "not enough participants in the threshold group" ); throw std::runtime_error( "not enough participants in the threshold group" ); @@ -87,7 +88,8 @@ std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx return res; } -libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { +libff::alt_bn128_Fq ThresholdUtils::HashToFq( + std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { libff::bigint< libff::alt_bn128_q_limbs > from_hex; std::vector< uint8_t > hex( 64 ); @@ -102,7 +104,8 @@ libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_ return ret_val; } -libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { +libff::alt_bn128_G1 ThresholdUtils::HashtoG1( + std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { libff::alt_bn128_Fq x1( HashToFq( hash_byte_arr ) ); libff::alt_bn128_G1 result; diff --git a/tools/utils.h b/tools/utils.h index 78d18e00..14a9c47c 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -31,18 +31,27 @@ #include -void checkSigners( size_t _requiredSigners, size_t _totalSigners ); +class ThresholdUtils { +public: + static void checkSigners( size_t _requiredSigners, size_t _totalSigners ); -std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); + static std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); -std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx, size_t t ); + static std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( + const std::vector< int >& idx, size_t t ); -libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); + static libff::alt_bn128_Fq HashToFq( + std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); -libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); + static libff::alt_bn128_G1 HashtoG1( + std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); + + template < class T > + static std::string fieldElementToString( const T& field_elem ); +}; template < class T > -std::string fieldElementToString( const T& field_elem ) { +std::string ThresholdUtils::fieldElementToString( const T& field_elem ) { mpz_t t; mpz_init( t ); From 777c1eaf55746d76e8600888b91fb6779b898708 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 16:08:25 +0300 Subject: [PATCH 06/40] SKALE-4409 refactor code --- CMakeLists.txt | 6 ++- bls/BLSPrivateKey.cpp | 12 ++--- bls/BLSPublicKey.cpp | 7 +-- bls/BLSSigShare.cpp | 5 ++- bls/BLSSigShareSet.cpp | 7 +-- bls/bls.cpp | 34 -------------- bls/bls.h | 2 - test/test_TE_wrappers.cpp | 2 +- test/test_bls.cpp | 14 +++--- test/unit_tests_bls.cpp | 24 ++++++---- test/unit_tests_te.cpp | 9 ++-- threshold_encryption/CMakeLists.txt | 24 ++++------ threshold_encryption/deps/ProjectPBC.cmake | 16 ------- threshold_encryption/threshold_encryption.cpp | 44 ++----------------- threshold_encryption/threshold_encryption.h | 2 - threshold_encryption/utils.cpp | 25 +---------- threshold_encryption/utils.h | 10 ----- tools/bls_glue.cpp | 3 +- tools/generate_key_system.cpp | 5 ++- tools/sign_bls.cpp | 4 +- tools/utils.cpp | 2 +- tools/utils.h | 2 +- 22 files changed, 76 insertions(+), 183 deletions(-) delete mode 100644 threshold_encryption/deps/ProjectPBC.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7943f60d..1d55b3e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,9 @@ set( CLANG_FORMAT_EXCLUDE_PATTERNS ) include( BlsFindClangFormat ) +set( TOOLS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tools/") +set( DKG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dkg/") + set(sourses_bls bls/bls.cpp bls/BLSPrivateKeyShare.cpp @@ -75,6 +78,7 @@ set(sourses_bls dkg/DKGBLSWrapper.cpp dkg/DKGBLSSecret.cpp third_party/cryptlite/base64.cpp + tools/utils.cpp ) set(headers_bls @@ -96,6 +100,7 @@ set(headers_bls third_party/cryptlite/sha1.h third_party/cryptlite/hmac.h third_party/cryptlite/base64.h + tools/utils.h ) set(PROJECT_VERSION 0.2.0) @@ -113,7 +118,6 @@ add_library(bls ${sourses_bls} ${headers_bls}) include_directories(${Boost_INCLUDE_DIRS}) - if( SKALE_HAVE_BOOST_FROM_HUNTER ) set( BOOST_LIBS_4_BLS Boost::program_options ) else() diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index d94e231c..5e56fec7 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -21,9 +21,10 @@ @date 2019 */ -#include "bls.h" #include #include +#include +#include BLSPrivateKey::BLSPrivateKey( @@ -55,11 +56,12 @@ BLSPrivateKey::BLSPrivateKey( if ( koefs == nullptr ) { throw signatures::Bls::IncorrectInput( "Signers indices ptr is null" ); } - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - signatures::Bls obj = signatures::Bls( _requiredSigners, _totalSigners ); - std::vector lagrange_koefs = obj.LagrangeCoeffs( *koefs ); + + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + + auto lagrange_koefs = ThresholdUtils::LagrangeCoeffs( *koefs, this->requiredSigners ); libff::alt_bn128_Fr privateKeyObj( libff::alt_bn128_Fr::zero() ); - for ( size_t i = 0; i < requiredSigners; i++ ) { + for ( size_t i = 0; i < requiredSigners; ++i ) { libff::alt_bn128_Fr skey = *skeys->at( koefs->at( i ) - 1 )->getPrivateKey(); privateKeyObj = privateKeyObj + lagrange_koefs.at( i ) * skey; } diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index 3eeb8e8e..e22d838e 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -25,6 +25,7 @@ #include #include #include +#include BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > pkey_str_vect, @@ -144,9 +145,8 @@ BLSPublicKey::BLSPublicKey( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { BLSutils::initBLS(); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - signatures::Bls obj = signatures::Bls( requiredSigners, totalSigners ); if ( !koefs_pkeys_map ) { throw signatures::Bls::IncorrectInput( "map is null" ); } @@ -158,7 +158,8 @@ BLSPublicKey::BLSPublicKey( participatingNodes.push_back( static_cast< uint64_t >( item.first ) ); } - std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = obj.LagrangeCoeffs( participatingNodes ); + std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = + ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); libff::alt_bn128_G2 key = libff::alt_bn128_G2::zero(); size_t i = 0; diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp index b4499e7a..fa0cfe84 100644 --- a/bls/BLSSigShare.cpp +++ b/bls/BLSSigShare.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -52,7 +53,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( requiredSigners, totalSigners ); + ThresholdUtils::checkSigners( requiredSigners, totalSigners ); BLSutils::initBLS(); if ( _signerIndex == 0 ) { throw signatures::Bls::IncorrectInput( "Zero signer index" ); @@ -104,7 +105,7 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { BLSutils::initBLS(); - BLSSignature::checkSigners( requiredSigners, totalSigners ); + ThresholdUtils::checkSigners( requiredSigners, totalSigners ); if ( !_sigShare ) { throw signatures::Bls::IncorrectInput( "Null _s" ); } diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp index e792e8cd..0c4c98c0 100644 --- a/bls/BLSSigShareSet.cpp +++ b/bls/BLSSigShareSet.cpp @@ -28,6 +28,7 @@ #include #include #include +#include bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) { @@ -37,7 +38,6 @@ bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) { throw signatures::Bls::IncorrectInput( "Invalid state:was already merged" ); } - if ( sigShares.count( _sigShare->getSignerIndex() ) > 0 ) { throw signatures::Bls::IncorrectInput( "Already have this index:" + std::to_string( _sigShare->getSignerIndex() ) ); @@ -65,7 +65,7 @@ std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index } BLSSigShareSet::BLSSigShareSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); BLSutils::initBLS(); } @@ -90,7 +90,8 @@ std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { shares.push_back( *item.second->getSigShare() ); } - std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = obj.LagrangeCoeffs( participatingNodes ); + std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = + ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); libff::alt_bn128_G1 signature = obj.SignatureRecover( shares, lagrangeCoeffs ); diff --git a/bls/bls.cpp b/bls/bls.cpp index 8a230ed3..c85c1efa 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -303,38 +303,4 @@ libff::alt_bn128_G1 Bls::SignatureRecover( const std::vector< libff::alt_bn128_G return sign; // first element is hash of a receiving message } -std::vector< libff::alt_bn128_Fr > Bls::LagrangeCoeffs( const std::vector< size_t >& idx ) { - if ( idx.size() < this->t_ ) { - throw IncorrectInput( "not enough participants in the threshold group" ); - } - - std::vector< libff::alt_bn128_Fr > res( this->t_ ); - - libff::alt_bn128_Fr w = libff::alt_bn128_Fr::one(); - - for ( size_t i = 0; i < this->t_; ++i ) { - w *= libff::alt_bn128_Fr( idx[i] ); - } - - for ( size_t i = 0; i < this->t_; ++i ) { - libff::alt_bn128_Fr v = libff::alt_bn128_Fr( idx[i] ); - - for ( size_t j = 0; j < this->t_; ++j ) { - if ( j != i ) { - if ( libff::alt_bn128_Fr( idx[i] ) == libff::alt_bn128_Fr( idx[j] ) ) { - throw IncorrectInput( - "during the interpolation, have same indexes in list of indexes" ); - } - - v *= ( libff::alt_bn128_Fr( idx[j] ) - - libff::alt_bn128_Fr( idx[i] ) ); // calculating Lagrange coefficients - } - } - - res[i] = w * v.invert(); - } - - return res; -} - } // namespace signatures diff --git a/bls/bls.h b/bls/bls.h index 849e11e6..79ea119d 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -107,8 +107,6 @@ class Bls { libff::alt_bn128_G1 SignatureRecover( const std::vector< libff::alt_bn128_G1 >& shares, const std::vector< libff::alt_bn128_Fr >& coeffs ); - std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< size_t >& idx ); - private: const size_t t_ = 0; diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index e06f2d71..183513dd 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -24,7 +24,6 @@ along with libBLS. If not, see . #define BOOST_TEST_MODULE -#include "../tools/utils.h" #include #include #include @@ -33,6 +32,7 @@ along with libBLS. If not, see . #include #include #include +#include #include #include diff --git a/test/test_bls.cpp b/test/test_bls.cpp index e3695d4d..42ef7b42 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -32,7 +32,8 @@ #include #include #include -#include +#include +#include #include @@ -143,7 +144,8 @@ BOOST_AUTO_TEST_CASE( libBls ) { signatures::Bls::IsNotWellFormed ); } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( participants ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( participants, num_signed ); libff::alt_bn128_G1 signature = obj.SignatureRecover( signatures, lagrange_coeffs ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, skeys ); @@ -425,8 +427,8 @@ BOOST_AUTO_TEST_CASE( private_keys_equality ) { std::shared_ptr< std::vector< size_t > > participants = choose_rand_signers( num_signed, num_all ); - signatures::Bls bls_obj = signatures::Bls( num_signed, num_all ); - std::vector< libff::alt_bn128_Fr > lagrange_koefs = bls_obj.LagrangeCoeffs( *participants ); + std::vector< libff::alt_bn128_Fr > lagrange_koefs = + ThresholdUtils::LagrangeCoeffs( *participants, num_signed ); libff::alt_bn128_Fr common_skey = libff::alt_bn128_Fr::zero(); for ( size_t i = 0; i < num_signed; ++i ) { common_skey = @@ -450,8 +452,8 @@ BOOST_AUTO_TEST_CASE( public_keys_equality ) { std::shared_ptr< std::vector< size_t > > participants = choose_rand_signers( num_signed, num_all ); - signatures::Bls bls_obj = signatures::Bls( num_signed, num_all ); - std::vector< libff::alt_bn128_Fr > lagrange_koefs = bls_obj.LagrangeCoeffs( *participants ); + std::vector< libff::alt_bn128_Fr > lagrange_koefs = + ThresholdUtils::LagrangeCoeffs( *participants, num_signed ); libff::alt_bn128_G2 common_pkey1 = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < num_signed; ++i ) { common_pkey1 = common_pkey1 + lagrange_koefs.at( i ) * diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 0d52f439..0714e2e8 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file unit_tests_bls.cpp @author Oleh Nikolaiev @@ -23,6 +23,7 @@ #include +#include #include #include @@ -129,7 +130,8 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { std::vector< size_t > testing_nodes = {1, 2}; - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( testing_nodes ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, secret_keys ); @@ -188,7 +190,8 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { std::vector< size_t > testing_nodes = {1, 2}; - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( testing_nodes ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); libff::alt_bn128_G1 fst_signature = obj.Signing( hash, fst_secret ); libff::alt_bn128_G1 snd_signature = obj.Signing( hash, snd_secret ); @@ -242,7 +245,8 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesReal ) { testing_nodes[i] = i + 1; } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( testing_nodes ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( testing_nodes, 11 ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, secret_keys ); @@ -308,7 +312,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs_fst = - obj.LagrangeCoeffs( testing_nodes_fst ); + ThresholdUtils::LagrangeCoeffs( testing_nodes_fst, 11 ); auto recovered_keys_fst = obj.KeysRecover( lagrange_coeffs_fst, secret_keys ); @@ -342,7 +346,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs_snd = - obj.LagrangeCoeffs( testing_nodes_snd ); + ThresholdUtils::LagrangeCoeffs( testing_nodes_snd, 11 ); std::vector< libff::alt_bn128_Fr > secret_keys_for_random_subgroup( 11 ); for ( size_t i = 0; i < 11; ++i ) { @@ -489,7 +493,7 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { } signatures::Bls obj = signatures::Bls( deg + 1, deg + 1 ); - auto coeffs = obj.LagrangeCoeffs( indexes ); + auto coeffs = ThresholdUtils::LagrangeCoeffs( indexes, deg + 1 ); std::vector< libff::alt_bn128_Fr > values( deg + 1 ); for ( size_t i = 0; i < deg + 1; ++i ) { @@ -573,12 +577,14 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { std::vector< size_t > idx = {1}; - BOOST_REQUIRE_THROW( obj.LagrangeCoeffs( idx ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + ThresholdUtils::LagrangeCoeffs( idx, num_signed ), std::runtime_error ); // signatures::Bls::IncorrectInput idx.clear(); idx = {1, 1}; - BOOST_REQUIRE_THROW( obj_2_2.LagrangeCoeffs( idx ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + ThresholdUtils::LagrangeCoeffs( idx, 2 ), std::runtime_error ); // signatures::Bls::IncorrectInput std::cerr << "Exceptions test passed" << std::endl; } diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 2b4335fa..6ab91a29 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -22,6 +22,7 @@ */ #include +#include #include #define BOOST_TEST_MODULE @@ -312,10 +313,10 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { bool is_exception_caught = false; try { encryption::TE obj( num_signed, num_all ); - std::vector< int > vect; + std::vector< size_t > vect; for ( size_t i = 0; i < num_signed - 1; i++ ) vect.push_back( i + 1 ); - obj.LagrangeCoeffs( vect ); + ThresholdUtils::LagrangeCoeffs( vect, num_signed ); } catch ( std::runtime_error& ) { is_exception_caught = true; } @@ -324,12 +325,12 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { is_exception_caught = false; try { encryption::TE obj( num_signed, num_all ); - std::vector< int > vect; + std::vector< size_t > vect; for ( size_t i = 0; i < num_signed; i++ ) { vect.push_back( i + 1 ); } vect.at( 1 ) = vect.at( 0 ); - obj.LagrangeCoeffs( vect ); + ThresholdUtils::LagrangeCoeffs( vect, num_signed ); } catch ( std::runtime_error& ) { is_exception_caught = true; } diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 34d021d9..0ebcfff8 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -15,11 +15,11 @@ set(sources TEPrivateKey.cpp TEPublicKey.cpp TEPublicKeyShare.cpp - ../dkg/dkg_te.cpp - ../dkg/DKGTEWrapper.cpp - ../dkg/DKGTESecret.cpp + ${DKG_DIR}/dkg_te.cpp + ${DKG_DIR}/DKGTEWrapper.cpp + ${DKG_DIR}/DKGTESecret.cpp utils.cpp - ../tools/utils.cpp + ${TOOLS_DIR}/utils.cpp ) set(headers @@ -29,11 +29,11 @@ set(headers TEPrivateKey.h TEPublicKey.h TEPublicKeyShare.h - ../dkg/dkg_te.h - ../dkg/DKGTEWrapper.h - ../dkg/DKGTESecret.h + ${DKG_DIR}/dkg_te.h + ${DKG_DIR}/DKGTEWrapper.h + ${DKG_DIR}/DKGTESecret.h utils.h - ../tools/utils.h + ${TOOLS_DIR}/utils.h ) set(PROJECT_VERSION 0.2.0) @@ -53,12 +53,6 @@ target_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_ target_link_libraries(te PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) if (BUILD_TESTS) - # add_executable(dkg_te_unit_test ../test/unit_tests_dkg_te.cpp) - # target_include_directories(dkg_te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${TEST_DIR}) - # target_link_libraries(dkg_te_unit_test PRIVATE te pbc ${GMP_LIBRARY} ${GMPXX_LIBRARY}) - - add_test(NAME dkg_te_tests COMMAND dkg_te_unit_test) - add_executable(te_unit_test ../test/unit_tests_te.cpp) target_include_directories(te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) target_link_libraries(te_unit_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) @@ -77,5 +71,5 @@ if (BUILD_TESTS) DEPENDS te_unit_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Run all TE tests" - ) + ) endif() diff --git a/threshold_encryption/deps/ProjectPBC.cmake b/threshold_encryption/deps/ProjectPBC.cmake deleted file mode 100644 index aa0f5fb6..00000000 --- a/threshold_encryption/deps/ProjectPBC.cmake +++ /dev/null @@ -1,16 +0,0 @@ -include(ExternalProject) - -get_filename_component(PBC_PREFIX "${CMAKE_BINARY_DIR}/deps" - REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") - -set(PBC_LIBRARY "${PBC_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}pbc${CMAKE_STATIC_LIBRARY_SUFFIX}") -set(PBC_INCLUDE_DIR "${PBC_PREFIX}/include/pbc") - -ExternalProject_Add( pbc - SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../pbc - BUILD_IN_SOURCE 1 - CONFIGURE_COMMAND libtoolize --force && aclocal && autoheader && automake --force-missing - --add-missing && autoconf && ./configure --with-pic --enable-static - --disable-shared --prefix=${PBC_PREFIX} - BUILD_COMMAND make install -) diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index edf42b45..78861357 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -25,9 +25,9 @@ #include #include -#include "../tools/utils.h" #include #include +#include #include namespace encryption { @@ -204,12 +204,13 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, throw std::runtime_error( "error during share combining" ); } - std::vector< int > idx( this->t_ ); + std::vector< size_t > idx( this->t_ ); for ( size_t i = 0; i < this->t_; ++i ) { idx[i] = decryptionShares[i].second; } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = this->LagrangeCoeffs( idx ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( idx, this->t_ ); libff::alt_bn128_G2 sum = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < this->t_; ++i ) { @@ -240,41 +241,4 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, return message; } -std::vector< libff::alt_bn128_Fr > TE::LagrangeCoeffs( const std::vector< int >& idx ) { - if ( idx.size() < this->t_ ) { - // throw IncorrectInput( "not enough participants in the threshold group" ); - throw std::runtime_error( "not enough participants in the threshold group" ); - } - - std::vector< libff::alt_bn128_Fr > res( this->t_ ); - - libff::alt_bn128_Fr w = libff::alt_bn128_Fr::one(); - - for ( size_t i = 0; i < this->t_; ++i ) { - w *= libff::alt_bn128_Fr( idx[i] ); - } - - for ( size_t i = 0; i < this->t_; ++i ) { - libff::alt_bn128_Fr v = libff::alt_bn128_Fr( idx[i] ); - - for ( size_t j = 0; j < this->t_; ++j ) { - if ( j != i ) { - if ( libff::alt_bn128_Fr( idx[i] ) == libff::alt_bn128_Fr( idx[j] ) ) { - // throw IncorrectInput( - // "during the interpolation, have same indexes in list of indexes" ); - throw std::runtime_error( - "during the interpolation, have same indexes in list of indexes" ); - } - - v *= ( libff::alt_bn128_Fr( idx[j] ) - - libff::alt_bn128_Fr( idx[i] ) ); // calculating Lagrange coefficients - } - } - - res[i] = w * v.invert(); - } - - return res; -} - } // namespace encryption diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index bde0e34e..6e698656 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -59,8 +59,6 @@ class TE { std::string CombineShares( const Ciphertext& ciphertext, const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShare ); - std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< int >& idx ); - private: const size_t t_ = 0; diff --git a/threshold_encryption/utils.cpp b/threshold_encryption/utils.cpp index dba1e8f8..7089e8cd 100644 --- a/threshold_encryption/utils.cpp +++ b/threshold_encryption/utils.cpp @@ -14,7 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . +along with libBLS. If not, see . @file utils.cpp @author Oleh Nikolaiev @@ -23,29 +23,6 @@ along with libBLS. If not, see . #include -libff::bigint< num_limbs > modulus = libff::bigint< num_limbs >( - "8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422" - "662221423155858769582317459277713367317481324925129998224791" ); - -void MpzSquareRoot( mpz_t ret_val, mpz_t x ) { - libff::bigint< num_limbs > to_find_square_root = libff::bigint< num_limbs >( - "219517769991582813060944549618851245395172079985355205275716334981661890772005" - "573926965629485566555535578896469239557936481942834182937033123128249955619" - "8" ); // type_a_Fq(libff::bigint(x)); - - mpz_t deg; - mpz_init( deg ); - to_find_square_root.to_mpz( deg ); - - mpz_t mode; - mpz_init( mode ); - modulus.to_mpz( mode ); - - mpz_powm( ret_val, x, deg, mode ); - - mpz_clears( deg, mode, 0 ); -} - bool isStringNumber( std::string& str ) { if ( str.at( 0 ) == '0' && str.length() > 1 ) return false; diff --git a/threshold_encryption/utils.h b/threshold_encryption/utils.h index b941980c..5fffd745 100644 --- a/threshold_encryption/utils.h +++ b/threshold_encryption/utils.h @@ -24,16 +24,6 @@ #pragma once #include -#include - -const mp_size_t bitcount = 512; -const mp_size_t num_limbs = ( bitcount + GMP_NUMB_BITS - 1 ) / GMP_NUMB_BITS; - -extern libff::bigint< num_limbs > modulus; - -using type_a_Fq = libff::Fp_model< num_limbs, modulus >; - -void MpzSquareRoot( mpz_t ret_val, mpz_t x ); bool isStringNumber( std::string& str ); diff --git a/tools/bls_glue.cpp b/tools/bls_glue.cpp index 132df512..75bedd37 100644 --- a/tools/bls_glue.cpp +++ b/tools/bls_glue.cpp @@ -26,6 +26,7 @@ #include #include +#include #include @@ -62,7 +63,7 @@ void RecoverSignature( const size_t t, const size_t n, const std::vector< std::s signature_shares[i] = signature_share; } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = bls_instance.LagrangeCoeffs( idx ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = ThresholdUtils::LagrangeCoeffs( idx, t ); libff::alt_bn128_G1 common_signature = bls_instance.SignatureRecover( signature_shares, lagrange_coeffs ); diff --git a/tools/generate_key_system.cpp b/tools/generate_key_system.cpp index baddba8a..66be03f8 100644 --- a/tools/generate_key_system.cpp +++ b/tools/generate_key_system.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file generate_key_system.cpp @author Oleh Nikolaiev @@ -26,6 +26,7 @@ #include #include +#include #include @@ -59,7 +60,7 @@ void GenerateKeys( const size_t t, const size_t n, std::ostream& outfile ) { for ( size_t i = 0; i < n; ++i ) { idx[i] = i + 1; } - auto lagrange_coeffs = bls_instance.LagrangeCoeffs( idx ); + auto lagrange_coeffs = ThresholdUtils::LagrangeCoeffs( idx, t ); auto common_keys = bls_instance.KeysRecover( lagrange_coeffs, secret_keys ); common_keys.second.to_affine_coordinates(); diff --git a/tools/sign_bls.cpp b/tools/sign_bls.cpp index fd78d19a..c0c6a050 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -23,6 +23,7 @@ #include #include +#include #include @@ -118,7 +119,8 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream idx[i] = i + 1; } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = bls_instance.LagrangeCoeffs( idx ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( idx, t ); common_signature = bls_instance.SignatureRecover( signature_shares, lagrange_coeffs ); } else { diff --git a/tools/utils.cpp b/tools/utils.cpp index acca85b7..2cf4e9ca 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -51,7 +51,7 @@ std::vector< std::string > ThresholdUtils::G2ToString( libff::alt_bn128_G2 elem } std::vector< libff::alt_bn128_Fr > ThresholdUtils::LagrangeCoeffs( - const std::vector< int >& idx, size_t t ) { + const std::vector< size_t >& idx, size_t t ) { if ( idx.size() < t ) { // throw IncorrectInput( "not enough participants in the threshold group" ); throw std::runtime_error( "not enough participants in the threshold group" ); diff --git a/tools/utils.h b/tools/utils.h index 14a9c47c..012054b5 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -38,7 +38,7 @@ class ThresholdUtils { static std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); static std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( - const std::vector< int >& idx, size_t t ); + const std::vector< size_t >& idx, size_t t ); static libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); From 7041779326fd4178396fa46ea6695b3ea632e23a Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 16:26:06 +0300 Subject: [PATCH 07/40] SAKLE-4409 move all utility fucntions to one file --- test/test_TE_wrappers.cpp | 1 - test/unit_tests_bls.cpp | 8 ++-- threshold_encryption/CMakeLists.txt | 2 - threshold_encryption/TEDecryptSet.cpp | 5 +-- threshold_encryption/TEPrivateKey.cpp | 3 +- threshold_encryption/TEPrivateKeyShare.cpp | 5 +-- threshold_encryption/TEPublicKey.cpp | 11 ++--- threshold_encryption/TEPublicKeyShare.cpp | 11 ++--- threshold_encryption/threshold_encryption.cpp | 3 +- threshold_encryption/utils.cpp | 43 ------------------- threshold_encryption/utils.h | 30 ------------- tools/utils.cpp | 22 +++++++++- tools/utils.h | 5 +++ 13 files changed, 48 insertions(+), 101 deletions(-) delete mode 100644 threshold_encryption/utils.cpp delete mode 100644 threshold_encryption/utils.h diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 183513dd..d71696d6 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -31,7 +31,6 @@ along with libBLS. If not, see . #include #include #include -#include #include #include diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 0714e2e8..25ff37f6 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -577,14 +577,14 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { std::vector< size_t > idx = {1}; - BOOST_REQUIRE_THROW( - ThresholdUtils::LagrangeCoeffs( idx, num_signed ), std::runtime_error ); // signatures::Bls::IncorrectInput + BOOST_REQUIRE_THROW( ThresholdUtils::LagrangeCoeffs( idx, num_signed ), + std::runtime_error ); // signatures::Bls::IncorrectInput idx.clear(); idx = {1, 1}; - BOOST_REQUIRE_THROW( - ThresholdUtils::LagrangeCoeffs( idx, 2 ), std::runtime_error ); // signatures::Bls::IncorrectInput + BOOST_REQUIRE_THROW( ThresholdUtils::LagrangeCoeffs( idx, 2 ), + std::runtime_error ); // signatures::Bls::IncorrectInput std::cerr << "Exceptions test passed" << std::endl; } diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 0ebcfff8..a9f2ec06 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -18,7 +18,6 @@ set(sources ${DKG_DIR}/dkg_te.cpp ${DKG_DIR}/DKGTEWrapper.cpp ${DKG_DIR}/DKGTESecret.cpp - utils.cpp ${TOOLS_DIR}/utils.cpp ) @@ -32,7 +31,6 @@ set(headers ${DKG_DIR}/dkg_te.h ${DKG_DIR}/DKGTEWrapper.h ${DKG_DIR}/DKGTESecret.h - utils.h ${TOOLS_DIR}/utils.h ) diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index 26ada71e..29927e6a 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -22,10 +22,9 @@ along with libBLS. If not, see . */ #include -#include #include -#include "../tools/utils.h" +#include TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) @@ -56,7 +55,7 @@ void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_ } std::string TEDecryptSet::merge( const encryption::Ciphertext& cyphertext ) { - checkCypher( cyphertext ); + ThresholdUtils::checkCypher( cyphertext ); was_merged = true; diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 3ac20c95..ea1fb6c8 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -21,9 +21,8 @@ along with libBLS. If not, see . @date 2019 */ -#include "../tools/utils.h" #include -#include +#include TEPrivateKey::TEPrivateKey( std::shared_ptr< std::string > _key_str, size_t _requiredSigners, size_t _totalSigners ) diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index a1ed43e6..6398238a 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -21,10 +21,9 @@ along with libBLS. If not, see . @date 2019 */ -#include "../tools/utils.h" #include #include -#include +#include TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ) @@ -66,7 +65,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s } libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( encryption::Ciphertext& cipher ) { - checkCypher( cipher ); + ThresholdUtils::checkCypher( cipher ); encryption::TE te( requiredSigners, totalSigners ); diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 4aff78ad..5dab8883 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -21,9 +21,8 @@ along with libBLS. If not, see . @date 2019 */ -#include "../tools/utils.h" #include -#include +#include #include #include @@ -42,8 +41,10 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str throw std::runtime_error( "wrong number of components in public key share" ); } - if ( !isStringNumber( _key_str_ptr->at( 0 ) ) || !isStringNumber( _key_str_ptr->at( 1 ) ) || - !isStringNumber( _key_str_ptr->at( 2 ) ) || !isStringNumber( _key_str_ptr->at( 3 ) ) ) { + if ( !ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || + !ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || + !ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || + !ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); } @@ -95,7 +96,7 @@ encryption::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ } encryption::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); - checkCypher( cypher ); + ThresholdUtils::checkCypher( cypher ); libff::alt_bn128_G2 U = std::get< 0 >( cypher ); /*if (element_item_count(U) == 0 ) { diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index 0ac139f3..fd34edca 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -21,9 +21,8 @@ along with libBLS. If not, see . @date 2019 */ -#include "../tools/utils.h" #include -#include +#include TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > > _key_str_ptr, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ) @@ -40,8 +39,10 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > throw std::runtime_error( "wrong number of components in public key share" ); } - if ( !isStringNumber( _key_str_ptr->at( 0 ) ) || !isStringNumber( _key_str_ptr->at( 1 ) ) || - !isStringNumber( _key_str_ptr->at( 2 ) ) || !isStringNumber( _key_str_ptr->at( 3 ) ) ) { + if ( !ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || + !ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || + !ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || + !ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); } @@ -71,7 +72,7 @@ TEPublicKeyShare::TEPublicKeyShare( bool TEPublicKeyShare::Verify( const encryption::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { - checkCypher( cyphertext ); + ThresholdUtils::checkCypher( cyphertext ); if ( decryptionShare.is_zero() ) { throw std::runtime_error( "zero decrypt" ); } diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 78861357..fddef94f 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -26,7 +26,6 @@ #include #include -#include #include #include @@ -119,7 +118,7 @@ Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& c libff::alt_bn128_G2 TE::getDecryptionShare( const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key ) { - checkCypher( ciphertext ); + ThresholdUtils::checkCypher( ciphertext ); if ( secret_key.is_zero() ) throw std::runtime_error( "zero secret key" ); diff --git a/threshold_encryption/utils.cpp b/threshold_encryption/utils.cpp deleted file mode 100644 index 7089e8cd..00000000 --- a/threshold_encryption/utils.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright (C) 2018-2019 SKALE Labs - -This file is part of libBLS. - -libBLS is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -libBLS is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . - -@file utils.cpp -@author Oleh Nikolaiev -@date 2019 -*/ - -#include - -bool isStringNumber( std::string& str ) { - if ( str.at( 0 ) == '0' && str.length() > 1 ) - return false; - for ( char& c : str ) { - if ( !( c >= '0' && c <= '9' ) ) { - return false; - } - } - return true; -} - -void checkCypher( const encryption::Ciphertext& cyphertext ) { - if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) - throw std::runtime_error( "zero element in cyphertext" ); - - if ( std::get< 1 >( cyphertext ).length() != 64 ) - throw std::runtime_error( "wrong string length in cyphertext" ); -} diff --git a/threshold_encryption/utils.h b/threshold_encryption/utils.h deleted file mode 100644 index 5fffd745..00000000 --- a/threshold_encryption/utils.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - Copyright (C) 2018-2019 SKALE Labs - - This file is part of libBLS. - - libBLS is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - libBLS is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . - - @file utils.h - @author Oleh Nikolaiev - @date 2019 - */ - -#pragma once - -#include - -bool isStringNumber( std::string& str ); - -void checkCypher( const encryption::Ciphertext& cypher ); diff --git a/tools/utils.cpp b/tools/utils.cpp index 2cf4e9ca..ed94b4fb 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -147,4 +147,24 @@ libff::alt_bn128_G1 ThresholdUtils::HashtoG1( result.Z = libff::alt_bn128_Fq::one(); return result; -} \ No newline at end of file +} + +bool ThresholdUtils::isStringNumber( std::string& str ) { + if ( str.at( 0 ) == '0' && str.length() > 1 ) + return false; + for ( char& c : str ) { + if ( !( c >= '0' && c <= '9' ) ) { + return false; + } + } + return true; +} + +void ThresholdUtils::checkCypher( + const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cyphertext ) { + if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) + throw std::runtime_error( "zero element in cyphertext" ); + + if ( std::get< 1 >( cyphertext ).length() != 64 ) + throw std::runtime_error( "wrong string length in cyphertext" ); +} diff --git a/tools/utils.h b/tools/utils.h index 012054b5..81e4a9db 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -46,6 +46,11 @@ class ThresholdUtils { static libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); + static bool isStringNumber( std::string& str ); + + static void checkCypher( + const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cypher ); + template < class T > static std::string fieldElementToString( const T& field_elem ); }; From 581d94a194e9be1f2b8acafa5069ee0a547b0590 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 16:34:38 +0300 Subject: [PATCH 08/40] SKALE-4409 move all utility functions to one file --- bls/BLSPrivateKeyShare.cpp | 3 ++- bls/bls.cpp | 49 ++------------------------------------ bls/bls.h | 2 -- test/test_bls.cpp | 2 +- tools/sign_bls.cpp | 2 +- 5 files changed, 6 insertions(+), 52 deletions(-) diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index 557565bb..36108bfe 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -69,7 +70,7 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) ); - libff::alt_bn128_G1 hash = obj->HashtoG1( hash_byte_arr ); + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_byte_arr ); auto ss = std::make_shared< libff::alt_bn128_G1 >( obj->Signing( hash, *privateKey ) ); diff --git a/bls/bls.cpp b/bls/bls.cpp index c85c1efa..607ac159 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -23,6 +23,7 @@ along with libBLS. If not, see . #include +#include #include #include @@ -79,52 +80,6 @@ libff::alt_bn128_G1 Bls::Hashing( return hash; } -libff::alt_bn128_G1 Bls::HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { - CHECK( hash_byte_arr ); - - libff::alt_bn128_Fq x1( BLSutils::HashToFq( hash_byte_arr ) ); - - libff::alt_bn128_G1 result; - - while ( true ) { - libff::alt_bn128_Fq y1_sqr = x1 ^ 3; - y1_sqr = y1_sqr + libff::alt_bn128_coeff_b; - - libff::alt_bn128_Fq euler = y1_sqr ^ libff::alt_bn128_Fq::euler; - - if ( euler == libff::alt_bn128_Fq::one() || - euler == libff::alt_bn128_Fq::zero() ) { // if y1_sqr is a square - result.X = x1; - libff::alt_bn128_Fq temp_y = y1_sqr.sqrt(); - - mpz_t pos_y; - mpz_init( pos_y ); - - temp_y.as_bigint().to_mpz( pos_y ); - - mpz_t neg_y; - mpz_init( neg_y ); - - ( -temp_y ).as_bigint().to_mpz( neg_y ); - - if ( mpz_cmp( pos_y, neg_y ) < 0 ) { - temp_y = -temp_y; - } - - mpz_clear( pos_y ); - mpz_clear( neg_y ); - - result.Y = temp_y; - break; - } else { - x1 = x1 + 1; - } - } - result.Z = libff::alt_bn128_Fq::one(); - - return result; -} - std::pair< libff::alt_bn128_G1, std::string > Bls::HashtoG1withHint( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { CHECK( hash_byte_arr ); @@ -255,7 +210,7 @@ bool Bls::Verification( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_a throw IsNotWellFormed( "Error, signature is not member of G1" ); } - libff::alt_bn128_G1 hash = HashtoG1( hash_byte_arr ); + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_byte_arr ); return ( libff::alt_bn128_ate_reduced_pairing( sign, libff::alt_bn128_G2::one() ) == libff::alt_bn128_ate_reduced_pairing( hash, public_key ) ); diff --git a/bls/bls.h b/bls/bls.h index 79ea119d..a1197177 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -86,8 +86,6 @@ class Bls { static libff::alt_bn128_G1 HashBytes( const char* raw_bytes, size_t length, std::string ( *hash_func )( const std::string& str ) = cryptlite::sha256::hash_hex ); - static libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > ); - static std::pair< libff::alt_bn128_G1, std::string > HashtoG1withHint( std::shared_ptr< std::array< uint8_t, 32 > > ); diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 42ef7b42..1686b5f9 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE( libBls ) { for ( size_t i = 0; i < 10; ++i ) { std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr = std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ); - libff::alt_bn128_G1 hash = obj.HashtoG1( hash_ptr ); + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_ptr ); for ( size_t i = 0; i < num_signed; ++i ) signatures.at( i ) = obj.Signing( hash, skeys.at( i ) ); diff --git a/tools/sign_bls.cpp b/tools/sign_bls.cpp index c0c6a050..3e3a2799 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -89,7 +89,7 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream } } - libff::alt_bn128_G1 hash = bls_instance.HashtoG1( hash_bytes_arr ); + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); nlohmann::json hash_json; hash_json["message"] = message; From e88149b16f9eadcb5504162d7cace41d720d1d00 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 18:02:57 +0300 Subject: [PATCH 09/40] SKALE-4409 move all utility functions to one file --- bls/BLSPrivateKey.cpp | 2 +- bls/BLSPrivateKeyShare.cpp | 12 ++++++------ bls/BLSPublicKey.cpp | 8 ++++---- bls/BLSPublicKeyShare.cpp | 9 +++++---- bls/BLSutils.h | 19 ------------------- bls/bls.cpp | 3 +-- test/test_bls.cpp | 2 +- tools/bls_glue.cpp | 6 ++---- tools/dkg_glue.cpp | 23 ++++++++++------------- tools/dkg_key_gen.cpp | 17 +++++++++-------- tools/generate_key_system.cpp | 18 +++++++++--------- tools/hash_g1.cpp | 5 +++-- tools/sign_bls.cpp | 6 ++---- 13 files changed, 53 insertions(+), 77 deletions(-) diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 5e56fec7..6c59a7c3 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -79,7 +79,7 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKey::getPrivateKey() const { std::shared_ptr< std::string > BLSPrivateKey::toString() { std::shared_ptr< std::string > key_str = - std::make_shared< std::string >( BLSutils::ConvertToString( *privateKey ) ); + std::make_shared< std::string >( ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) throw signatures::Bls::ZeroSecretKey( "Secret key share string is empty" ); diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index 36108bfe..8479c00b 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -78,8 +78,8 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( std::pair< libff::alt_bn128_G1, std::string > hash_with_hint = obj->HashtoG1withHint( hash_byte_arr ); - std::string hint = - BLSutils::ConvertToString( hash_with_hint.first.Y ) + ":" + hash_with_hint.second; + std::string hint = ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + ":" + + hash_with_hint.second; auto s = std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners ); @@ -108,8 +108,8 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper( ss->to_affine_coordinates(); - std::string hint = - BLSutils::ConvertToString( hash_with_hint.first.Y ) + ":" + hash_with_hint.second; + std::string hint = ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + ":" + + hash_with_hint.second; auto s = std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners ); @@ -133,7 +133,7 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi std::make_shared< BLSPublicKey >( common_skey, _requiredSigners, _totalSigners ); for ( size_t i = 0; i < _totalSigners; ++i ) { - std::string key_str = BLSutils::ConvertToString( skeys.at( i ) ); + std::string key_str = ThresholdUtils::fieldElementToString( skeys.at( i ) ); std::shared_ptr< BLSPrivateKeyShare > key_ptr = std::make_shared< BLSPrivateKeyShare >( key_str, _requiredSigners, _totalSigners ); @@ -162,7 +162,7 @@ std::shared_ptr< std::string > BLSPrivateKeyShare::toString() { throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); } std::shared_ptr< std::string > key_str = - std::make_shared< std::string >( BLSutils::ConvertToString( *privateKey ) ); + std::make_shared< std::string >( ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) throw signatures::Bls::IncorrectInput( "Secret key share string is empty" ); diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index e22d838e..c51740d2 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -183,10 +183,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKey::toString() { libffPublicKey->to_affine_coordinates(); - pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->X.c0 ) ); - pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->X.c1 ) ); - pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->Y.c0 ) ); - pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->Y.c1 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->X.c0 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->X.c1 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->Y.c0 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->Y.c1 ) ); return std::make_shared< std::vector< std::string > >( pkey_str_vect ); } diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index 45ac2aaf..a7f0088f 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -26,6 +26,7 @@ #include #include #include +#include BLSPublicKeyShare::BLSPublicKeyShare( const std::shared_ptr< std::vector< std::string > > pkey_str_vect, size_t _requiredSigners, @@ -75,10 +76,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKeyShare::toString() { publicKey->to_affine_coordinates(); - pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->X.c0 ) ); - pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->X.c1 ) ); - pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->Y.c0 ) ); - pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->Y.c1 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->X.c0 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->X.c1 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->Y.c0 ) ); + pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->Y.c1 ) ); return std::make_shared< std::vector< std::string > >( pkey_str_vect ); } diff --git a/bls/BLSutils.h b/bls/BLSutils.h index abc506f7..609220e9 100644 --- a/bls/BLSutils.h +++ b/bls/BLSutils.h @@ -28,8 +28,6 @@ along with libBLS. If not, see . class BLSutils { public: - template < class T > - static std::string ConvertToString( const T& field_elem ); static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( std::string& ); static libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > ); static std::shared_ptr< std::vector< std::string > > SplitString( @@ -38,20 +36,3 @@ class BLSutils { static std::atomic< bool > is_initialized; }; - -template < class T > -std::string BLSutils::ConvertToString( const T& field_elem ) { - mpz_t t; - mpz_init( t ); - - field_elem.as_bigint().to_mpz( t ); - - char arr[mpz_sizeinbase( t, 10 ) + 2]; - - char* tmp = mpz_get_str( arr, 10, t ); - mpz_clear( t ); - - std::string output = tmp; - - return output; -} diff --git a/bls/bls.cpp b/bls/bls.cpp index 607ac159..f05609b7 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -126,7 +126,7 @@ std::pair< libff::alt_bn128_G1, std::string > Bls::HashtoG1withHint( } point.Z = libff::alt_bn128_Fq::one(); - return std::make_pair( point, BLSutils::ConvertToString( counter ) ); + return std::make_pair( point, ThresholdUtils::fieldElementToString( counter ) ); } libff::alt_bn128_G1 Bls::HashBytes( @@ -138,7 +138,6 @@ libff::alt_bn128_G1 Bls::HashBytes( std::string from_bytes( raw_bytes, length ); - libff::alt_bn128_G1 hash = Hashing( from_bytes, *hash_func ); return hash; diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 1686b5f9..e73d668a 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -553,7 +553,7 @@ BOOST_AUTO_TEST_CASE( BLSWITHDKG ) { std::shared_ptr< BLSSignature > common_sig_ptr = sigSet.merge(); // verifying signature - std::string common_secret_str = BLSutils::ConvertToString( common_secret ); + std::string common_secret_str = ThresholdUtils::fieldElementToString( common_secret ); BLSPrivateKey common_skey( std::make_shared< std::string >( common_secret_str ), num_signed, num_all ); diff --git a/tools/bls_glue.cpp b/tools/bls_glue.cpp index 75bedd37..a943c471 100644 --- a/tools/bls_glue.cpp +++ b/tools/bls_glue.cpp @@ -71,10 +71,8 @@ void RecoverSignature( const size_t t, const size_t n, const std::vector< std::s nlohmann::json outdata; - outdata["signature"]["X"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_signature.X ); - outdata["signature"]["Y"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_signature.Y ); + outdata["signature"]["X"] = ThresholdUtils::fieldElementToString( common_signature.X ); + outdata["signature"]["Y"] = ThresholdUtils::fieldElementToString( common_signature.Y ); outfile << outdata.dump( 4 ) << '\n'; } diff --git a/tools/dkg_glue.cpp b/tools/dkg_glue.cpp index fe83fbaf..de3f5be9 100644 --- a/tools/dkg_glue.cpp +++ b/tools/dkg_glue.cpp @@ -24,6 +24,7 @@ #include #include +#include #include @@ -126,21 +127,17 @@ void GenerateSecretKeys( const size_t t, const size_t n, const std::vector< std: nlohmann::json BLS_key_file; BLS_key_file["insecureBLSPrivateKey"] = - BLSutils::ConvertToString< libff::alt_bn128_Fr >( secret_key[i] ); + ThresholdUtils::fieldElementToString( secret_key[i] ); std::string str_file_name = "BLS_keys" + std::to_string( i ) + ".json"; std::ofstream out( str_file_name.c_str() ); libff::alt_bn128_G2 publ_key = dkg_instance.GetPublicKeyFromSecretKey( secret_key[i] ); publ_key.to_affine_coordinates(); - BLS_key_file["BLSPublicKey0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.X.c0 ); - BLS_key_file["BLSPublicKey1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.X.c1 ); - BLS_key_file["BLSPublicKey2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.Y.c0 ); - BLS_key_file["BLSPublicKey3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.Y.c1 ); + BLS_key_file["BLSPublicKey0"] = ThresholdUtils::fieldElementToString( publ_key.X.c0 ); + BLS_key_file["BLSPublicKey1"] = ThresholdUtils::fieldElementToString( publ_key.X.c1 ); + BLS_key_file["BLSPublicKey2"] = ThresholdUtils::fieldElementToString( publ_key.Y.c0 ); + BLS_key_file["BLSPublicKey3"] = ThresholdUtils::fieldElementToString( publ_key.Y.c1 ); if ( g_b_verbose_mode ) { std::cout << str_file_name << " file:\n" << BLS_key_file.dump( 4 ) << "\n\n"; @@ -151,13 +148,13 @@ void GenerateSecretKeys( const size_t t, const size_t n, const std::vector< std: common_public_key.to_affine_coordinates(); nlohmann::json public_key_json; public_key_json["commonBLSPublicKey0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.X.c0 ); + ThresholdUtils::fieldElementToString( common_public_key.X.c0 ); public_key_json["commonBLSPublicKey1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.X.c1 ); + ThresholdUtils::fieldElementToString( common_public_key.X.c1 ); public_key_json["commonBLSPublicKey2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.Y.c0 ); + ThresholdUtils::fieldElementToString( common_public_key.Y.c0 ); public_key_json["commonBLSPublicKey3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.Y.c1 ); + ThresholdUtils::fieldElementToString( common_public_key.Y.c1 ); std::ofstream outfile_pk( "common_public_key.json" ); outfile_pk << public_key_json.dump( 4 ) << "\n"; diff --git a/tools/dkg_key_gen.cpp b/tools/dkg_key_gen.cpp index 0a0eaad8..f0353d3b 100644 --- a/tools/dkg_key_gen.cpp +++ b/tools/dkg_key_gen.cpp @@ -10,7 +10,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file dkg_key_gen.cpp @author Oleh Nikolaiev @date 2019 @@ -18,6 +18,7 @@ #include +#include #include @@ -131,23 +132,23 @@ void KeyGeneration( const size_t t, const size_t n, bool generate_all = true, in for ( size_t i = 0; i < n; ++i ) { data["secret_key_contribution"][std::to_string( i )] = - BLSutils::ConvertToString< libff::alt_bn128_Fr >( secret_key_contribution[i] ); + ThresholdUtils::fieldElementToString( secret_key_contribution[i] ); } for ( size_t i = 0; i < t; ++i ) { data["verification_vector"][std::to_string( i )]["X"]["c0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].X.c0 ); + ThresholdUtils::fieldElementToString( verification_vector[i].X.c0 ); data["verification_vector"][std::to_string( i )]["X"]["c1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].X.c1 ); + ThresholdUtils::fieldElementToString( verification_vector[i].X.c1 ); data["verification_vector"][std::to_string( i )]["Y"]["c0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].Y.c0 ); + ThresholdUtils::fieldElementToString( verification_vector[i].Y.c0 ); data["verification_vector"][std::to_string( i )]["Y"]["c1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].Y.c1 ); + ThresholdUtils::fieldElementToString( verification_vector[i].Y.c1 ); data["verification_vector"][std::to_string( i )]["Z"]["c0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].Z.c0 ); + ThresholdUtils::fieldElementToString( verification_vector[i].Z.c0 ); data["verification_vector"][std::to_string( i )]["Z"]["c1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].Z.c1 ); + ThresholdUtils::fieldElementToString( verification_vector[i].Z.c1 ); } std::ofstream outfile( "data_for_" + std::to_string( idx ) + "-th_participant.json" ); diff --git a/tools/generate_key_system.cpp b/tools/generate_key_system.cpp index 66be03f8..74eda569 100644 --- a/tools/generate_key_system.cpp +++ b/tools/generate_key_system.cpp @@ -68,26 +68,26 @@ void GenerateKeys( const size_t t, const size_t n, std::ostream& outfile ) { nlohmann::json outdata; outdata["commonBLSPublicKey"]["0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.X.c0 ); + ThresholdUtils::fieldElementToString( common_keys.second.X.c0 ); outdata["commonBLSPublicKey"]["1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.X.c1 ); + ThresholdUtils::fieldElementToString( common_keys.second.X.c1 ); outdata["commonBLSPublicKey"]["2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.Y.c0 ); + ThresholdUtils::fieldElementToString( common_keys.second.Y.c0 ); outdata["commonBLSPublicKey"]["3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.Y.c1 ); + ThresholdUtils::fieldElementToString( common_keys.second.Y.c1 ); for ( size_t i = 0; i < n; ++i ) { outdata["privateKey"][std::to_string( i )] = - BLSutils::ConvertToString< libff::alt_bn128_Fr >( secret_keys[i] ); + ThresholdUtils::fieldElementToString( secret_keys[i] ); outdata["BLSPublicKey"][std::to_string( i )]["0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( public_keys[i].X.c0 ); + ThresholdUtils::fieldElementToString( public_keys[i].X.c0 ); outdata["BLSPublicKey"][std::to_string( i )]["1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( public_keys[i].X.c1 ); + ThresholdUtils::fieldElementToString( public_keys[i].X.c1 ); outdata["BLSPublicKey"][std::to_string( i )]["2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( public_keys[i].Y.c0 ); + ThresholdUtils::fieldElementToString( public_keys[i].Y.c0 ); outdata["BLSPublicKey"][std::to_string( i )]["3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( public_keys[i].Y.c1 ); + ThresholdUtils::fieldElementToString( public_keys[i].Y.c1 ); } outfile << outdata.dump( 4 ) << '\n'; diff --git a/tools/hash_g1.cpp b/tools/hash_g1.cpp index 4eb7f592..8d99d21a 100644 --- a/tools/hash_g1.cpp +++ b/tools/hash_g1.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -93,8 +94,8 @@ void hash_g1( const size_t t, const size_t n ) { nlohmann::json joG1 = nlohmann::json::object(); joG1["g1"] = nlohmann::json::object(); joG1["g1"]["hashPoint"] = nlohmann::json::object(); - joG1["g1"]["hashPoint"]["X"] = BLSutils::ConvertToString( p2vals.first.X ); - joG1["g1"]["hashPoint"]["Y"] = BLSutils::ConvertToString( p2vals.first.Y ); + joG1["g1"]["hashPoint"]["X"] = ThresholdUtils::fieldElementToString( p2vals.first.X ); + joG1["g1"]["hashPoint"]["Y"] = ThresholdUtils::fieldElementToString( p2vals.first.Y ); joG1["g1"]["hint"] = p2vals.second; std::ofstream g1_file( "g1.json" ); diff --git a/tools/sign_bls.cpp b/tools/sign_bls.cpp index 3e3a2799..2e6b06fe 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -144,10 +144,8 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream signature["index"] = std::to_string( idx ); } - signature["signature"]["X"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_signature.X ); - signature["signature"]["Y"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_signature.Y ); + signature["signature"]["X"] = ThresholdUtils::fieldElementToString( common_signature.X ); + signature["signature"]["Y"] = ThresholdUtils::fieldElementToString( common_signature.Y ); std::ofstream outfile_h( "hash.json" ); outfile_h << hash_json.dump( 4 ) << "\n"; From dc19859233b69114760b5af47186b237ca3e3d7c Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 18:05:38 +0300 Subject: [PATCH 10/40] SKALE-4409 move all utility functions to one file --- bls/BLSPublicKey.cpp | 2 +- bls/BLSPublicKeyShare.cpp | 2 +- bls/BLSutils.cpp | 16 ---------------- bls/BLSutils.h | 1 - bls/bls.cpp | 2 +- 5 files changed, 3 insertions(+), 20 deletions(-) diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index c51740d2..fa33ad63 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -122,7 +122,7 @@ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = BLSutils::ParseHint( hint ); - libff::alt_bn128_Fq x = BLSutils::HashToFq( hash_ptr ); + libff::alt_bn128_Fq x = ThresholdUtils::HashToFq( hash_ptr ); x = x + y_shift_x.second; libff::alt_bn128_Fq y_sqr = y_shift_x.first ^ 2; diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index a7f0088f..b902ae95 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -119,7 +119,7 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_ std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = BLSutils::ParseHint( hint ); - libff::alt_bn128_Fq x = BLSutils::HashToFq( hash_ptr ); + libff::alt_bn128_Fq x = ThresholdUtils::HashToFq( hash_ptr ); x = x + y_shift_x.second; diff --git a/bls/BLSutils.cpp b/bls/BLSutils.cpp index 8b19a484..011ca023 100644 --- a/bls/BLSutils.cpp +++ b/bls/BLSutils.cpp @@ -54,22 +54,6 @@ std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > BLSutils::ParseHint( std:: return std::make_pair( y, shift_x ); } -libff::alt_bn128_Fq BLSutils::HashToFq( - std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) { - libff::bigint< libff::alt_bn128_q_limbs > from_hex; - - std::vector< uint8_t > hex( 64 ); - for ( size_t i = 0; i < 32; ++i ) { - hex[2 * i] = static_cast< int >( hash_byte_arr->at( i ) ) / 16; - hex[2 * i + 1] = static_cast< int >( hash_byte_arr->at( i ) ) % 16; - } - mpn_set_str( from_hex.data, hex.data(), 64, 16 ); - - libff::alt_bn128_Fq ret_val( from_hex ); - - return ret_val; -} - std::shared_ptr< std::vector< std::string > > BLSutils::SplitString( std::shared_ptr< std::string > str, const std::string& delim ) { CHECK( str ); diff --git a/bls/BLSutils.h b/bls/BLSutils.h index 609220e9..e4cbcbaa 100644 --- a/bls/BLSutils.h +++ b/bls/BLSutils.h @@ -29,7 +29,6 @@ along with libBLS. If not, see . class BLSutils { public: static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( std::string& ); - static libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > ); static std::shared_ptr< std::vector< std::string > > SplitString( const std::shared_ptr< std::string >, const std::string& delim ); static void initBLS(); diff --git a/bls/bls.cpp b/bls/bls.cpp index f05609b7..e3a555b3 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -86,7 +86,7 @@ std::pair< libff::alt_bn128_G1, std::string > Bls::HashtoG1withHint( libff::alt_bn128_G1 point; libff::alt_bn128_Fq counter = libff::alt_bn128_Fq::zero(); - libff::alt_bn128_Fq x1( BLSutils::HashToFq( hash_byte_arr ) ); + libff::alt_bn128_Fq x1( ThresholdUtils::HashToFq( hash_byte_arr ) ); while ( true ) { From 72e63c28023fcf2a6a7ad6f4c689ad032578bcd3 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Jul 2021 18:30:09 +0300 Subject: [PATCH 11/40] SKALE-4409 move all utility functions to one file --- CMakeLists.txt | 2 - bls/BLSPrivateKey.cpp | 5 +-- bls/BLSPrivateKeyShare.cpp | 3 +- bls/BLSPublicKey.cpp | 14 +++---- bls/BLSPublicKeyShare.cpp | 8 ++-- bls/BLSSigShare.cpp | 10 ++--- bls/BLSSigShareSet.cpp | 5 +-- bls/BLSSignature.cpp | 10 ++--- bls/BLSutils.cpp | 74 ----------------------------------- bls/BLSutils.h | 37 ------------------ bls/bls.cpp | 4 +- dkg/dkg.cpp | 4 +- test/dkg_attack.cpp | 1 - test/test_bls.cpp | 1 - test/unit_tests_bls.cpp | 3 +- test/unit_tests_dkg.cpp | 3 +- tools/bls_glue.cpp | 1 - tools/dkg_glue.cpp | 1 - tools/dkg_key_gen.cpp | 1 - tools/generate_key_system.cpp | 1 - tools/hash_g1.cpp | 1 - tools/sign_bls.cpp | 1 - tools/utils.cpp | 48 ++++++++++++++++++++++- tools/utils.h | 11 ++++++ 24 files changed, 89 insertions(+), 160 deletions(-) delete mode 100644 bls/BLSutils.cpp delete mode 100644 bls/BLSutils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d55b3e8..ccf2ee33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,6 @@ set(sourses_bls bls/BLSSignature.cpp bls/BLSSigShare.cpp bls/BLSSigShareSet.cpp - bls/BLSutils.cpp dkg/dkg.cpp dkg/DKGBLSWrapper.cpp dkg/DKGBLSSecret.cpp @@ -91,7 +90,6 @@ set(headers_bls bls/BLSSignature.h bls/BLSSigShare.h bls/BLSSigShareSet.h - bls/BLSutils.h dkg/dkg.h dkg/DKGBLSWrapper.h dkg/DKGBLSSecret.h diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 6c59a7c3..a0239806 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file BLSPrivateKey.cpp @author Sveta Rogova @@ -22,7 +22,6 @@ */ #include -#include #include #include @@ -30,7 +29,7 @@ BLSPrivateKey::BLSPrivateKey( const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( _key == nullptr ) { diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index 8479c00b..c6f03d1b 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -26,7 +26,6 @@ #include #include -#include #include @@ -34,7 +33,7 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( const std::string& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); if ( _key.empty() ) { throw signatures::Bls::IncorrectInput( "Secret key share string is empty" ); } diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index fa33ad63..a8145577 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file BLSPublicKey.cpp @author Sveta Rogova @@ -24,14 +24,13 @@ #include #include -#include #include BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > pkey_str_vect, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); CHECK( pkey_str_vect ) @@ -58,7 +57,7 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); @@ -88,7 +87,7 @@ size_t BLSPublicKey::getRequiredSigners() const { bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); std::shared_ptr< signatures::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); @@ -120,7 +119,8 @@ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 std::string hint = sign_ptr->getHint(); - std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = BLSutils::ParseHint( hint ); + std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = + ThresholdUtils::ParseHint( hint ); libff::alt_bn128_Fq x = ThresholdUtils::HashToFq( hash_ptr ); x = x + y_shift_x.second; @@ -143,7 +143,7 @@ BLSPublicKey::BLSPublicKey( std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > koefs_pkeys_map, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index b902ae95..9c8e8667 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -36,7 +35,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); publicKey = std::make_shared< libff::alt_bn128_G2 >(); @@ -59,7 +58,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( BLSPublicKeyShare::BLSPublicKeyShare( const libff::alt_bn128_Fr& _skey, size_t _totalSigners, size_t _requiredSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); if ( _skey.is_zero() ) { throw signatures::Bls::ZeroSecretKey( "Zero BLS Secret Key" ); } @@ -117,7 +116,8 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_ std::string hint = sign_ptr->getHint(); - std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = BLSutils::ParseHint( hint ); + std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = + ThresholdUtils::ParseHint( hint ); libff::alt_bn128_Fq x = ThresholdUtils::HashToFq( hash_ptr ); diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp index fa0cfe84..d5a32982 100644 --- a/bls/BLSSigShare.cpp +++ b/bls/BLSSigShare.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file BLSSigShare.cpp @author Stan Kladko, Sveta Rogova @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -54,7 +53,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { ThresholdUtils::checkSigners( requiredSigners, totalSigners ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); if ( _signerIndex == 0 ) { throw signatures::Bls::IncorrectInput( "Zero signer index" ); } @@ -75,7 +74,8 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign } - std::shared_ptr< std::vector< std::string > > result = BLSutils::SplitString( _sigShare, ":" ); + std::shared_ptr< std::vector< std::string > > result = + ThresholdUtils::SplitString( _sigShare, ":" ); if ( result->size() != 4 ) throw signatures::Bls::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { @@ -104,7 +104,7 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); ThresholdUtils::checkSigners( requiredSigners, totalSigners ); if ( !_sigShare ) { throw signatures::Bls::IncorrectInput( "Null _s" ); diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp index 0c4c98c0..a0c3652f 100644 --- a/bls/BLSSigShareSet.cpp +++ b/bls/BLSSigShareSet.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file BLSSigShareSet.cpp @author Stan Kladko, Sveta Rogova @@ -27,7 +27,6 @@ #include #include #include -#include #include @@ -67,7 +66,7 @@ BLSSigShareSet::BLSSigShareSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); } bool BLSSigShareSet::isEnough() { diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp index 0294ebe8..33254c93 100644 --- a/bls/BLSSignature.cpp +++ b/bls/BLSSignature.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file BLSSignature.cpp @author Stan Kladko, Sveta Rogova @@ -22,7 +22,7 @@ */ #include -#include +#include std::shared_ptr< libff::alt_bn128_G1 > BLSSignature::getSig() const { CHECK( sig ); @@ -38,7 +38,7 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st CHECK( sig ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); if ( sig->is_zero() ) { @@ -56,7 +56,7 @@ BLSSignature::BLSSignature( BLSSignature::checkSigners( requiredSigners, totalSigners ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); if ( _sig->size() < 10 ) { throw signatures::Bls::IsNotWellFormed( @@ -68,7 +68,7 @@ BLSSignature::BLSSignature( "Signature too long:" + std::to_string( _sig->size() ) ); } - std::shared_ptr< std::vector< std::string > > result = BLSutils::SplitString( _sig, ":" ); + std::shared_ptr< std::vector< std::string > > result = ThresholdUtils::SplitString( _sig, ":" ); if ( result->size() != 4 ) throw signatures::Bls::IncorrectInput( "Misformatted signature" ); diff --git a/bls/BLSutils.cpp b/bls/BLSutils.cpp deleted file mode 100644 index 011ca023..00000000 --- a/bls/BLSutils.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* -Copyright (C) 2018-2019 SKALE Labs - -This file is part of libBLS. - -libBLS is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -libBLS is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . - -@file BLSUtils.cpp -@author Sveta Rogova -@date 2019 -*/ - -#include "bls.h" -#include -#include -#include - -#include -#include - -std::atomic< bool > BLSutils::is_initialized = false; - -std::mutex initMutex; - -void BLSutils::initBLS() { - std::lock_guard< std::mutex > lock( initMutex ); - if ( !is_initialized ) { - libff::init_alt_bn128_params(); - is_initialized = true; - } -} - -std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > BLSutils::ParseHint( std::string& _hint ) { - auto position = _hint.find( ":" ); - - if ( position == std::string::npos ) { - throw std::runtime_error( "Misformatted hint" ); - } - - libff::alt_bn128_Fq y( _hint.substr( 0, position ).c_str() ); - libff::alt_bn128_Fq shift_x( _hint.substr( position + 1 ).c_str() ); - - return std::make_pair( y, shift_x ); -} - -std::shared_ptr< std::vector< std::string > > BLSutils::SplitString( - std::shared_ptr< std::string > str, const std::string& delim ) { - CHECK( str ); - - std::vector< std::string > tokens; - 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() ) - tokens.push_back( token ); - prev = pos + delim.length(); - } while ( pos < str->length() && prev < str->length() ); - - return std::make_shared< std::vector< std::string > >( tokens ); -} diff --git a/bls/BLSutils.h b/bls/BLSutils.h deleted file mode 100644 index e4cbcbaa..00000000 --- a/bls/BLSutils.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright (C) 2018-2019 SKALE Labs - -This file is part of libBLS. - -libBLS is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -libBLS is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . - -@file BLSUtils.h -@author Sveta Rogova -@date 2019 -*/ - -#include -#include - -#include - -class BLSutils { -public: - static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( std::string& ); - static std::shared_ptr< std::vector< std::string > > SplitString( - const std::shared_ptr< std::string >, const std::string& delim ); - static void initBLS(); - - static std::atomic< bool > is_initialized; -}; diff --git a/bls/bls.cpp b/bls/bls.cpp index e3a555b3..8da1a9b9 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -36,12 +36,10 @@ along with libBLS. If not, see . #include #include -#include - namespace signatures { Bls::Bls( const size_t t, const size_t n ) : t_( t ), n_( n ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); } std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeyGeneration() { diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index 93701ccd..427fe9d3 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -21,8 +21,8 @@ @date 2018 */ -#include "bls/BLSutils.h" #include +#include #include #include @@ -33,7 +33,7 @@ namespace signatures { typedef std::vector< libff::alt_bn128_Fr > Polynomial; Dkg::Dkg( const size_t t, const size_t n ) : t_( t ), n_( n ) { - BLSutils::initBLS(); + ThresholdUtils::initCurve(); } Polynomial Dkg::GeneratePolynomial() { diff --git a/test/dkg_attack.cpp b/test/dkg_attack.cpp index 248120af..2c62eb41 100644 --- a/test/dkg_attack.cpp +++ b/test/dkg_attack.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/test/test_bls.cpp b/test/test_bls.cpp index e73d668a..ffefc829 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 25ff37f6..0a36c65a 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -36,7 +36,6 @@ #define BOOST_TEST_MODULE -#include #include BOOST_AUTO_TEST_SUITE( libBls ) @@ -454,7 +453,7 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { std::vector< libff::alt_bn128_Fr > pol( deg + 1 ); - BLSutils::initBLS(); + ThresholdUtils::initCurve(); // random polynomial generation for ( size_t i = 0; i < deg + 1; ++i ) { diff --git a/test/unit_tests_dkg.cpp b/test/unit_tests_dkg.cpp index 3e2405f7..1459e241 100644 --- a/test/unit_tests_dkg.cpp +++ b/test/unit_tests_dkg.cpp @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file unit_tests_dkg.cpp @author Oleh Nikolaiev @@ -27,7 +27,6 @@ #include #include #include -#include #include #include diff --git a/tools/bls_glue.cpp b/tools/bls_glue.cpp index a943c471..ce20a253 100644 --- a/tools/bls_glue.cpp +++ b/tools/bls_glue.cpp @@ -24,7 +24,6 @@ #include -#include #include #include diff --git a/tools/dkg_glue.cpp b/tools/dkg_glue.cpp index de3f5be9..dd5b65b1 100644 --- a/tools/dkg_glue.cpp +++ b/tools/dkg_glue.cpp @@ -22,7 +22,6 @@ */ -#include #include #include diff --git a/tools/dkg_key_gen.cpp b/tools/dkg_key_gen.cpp index f0353d3b..45704215 100644 --- a/tools/dkg_key_gen.cpp +++ b/tools/dkg_key_gen.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #define EXPAND_AS_STR( x ) __EXPAND_AS_STR__( x ) diff --git a/tools/generate_key_system.cpp b/tools/generate_key_system.cpp index 74eda569..537328cc 100644 --- a/tools/generate_key_system.cpp +++ b/tools/generate_key_system.cpp @@ -24,7 +24,6 @@ #include -#include #include #include diff --git a/tools/hash_g1.cpp b/tools/hash_g1.cpp index 8d99d21a..bd8158fc 100644 --- a/tools/hash_g1.cpp +++ b/tools/hash_g1.cpp @@ -22,7 +22,6 @@ */ #include -#include #include #include #include diff --git a/tools/sign_bls.cpp b/tools/sign_bls.cpp index 2e6b06fe..51feb1c9 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -21,7 +21,6 @@ @date 2019 */ -#include #include #include diff --git a/tools/utils.cpp b/tools/utils.cpp index ed94b4fb..e373bf24 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -21,7 +21,20 @@ @date 2021 */ -#include "utils.h" +#include +#include + +std::atomic< bool > ThresholdUtils::is_initialized = false; + +std::mutex initMutex; + +void ThresholdUtils::initCurve() { + std::lock_guard< std::mutex > lock( initMutex ); + if ( !is_initialized ) { + libff::init_alt_bn128_params(); + is_initialized = true; + } +} void ThresholdUtils::checkSigners( size_t _requiredSigners, size_t _totalSigners ) { if ( _requiredSigners > _totalSigners ) { @@ -168,3 +181,36 @@ void ThresholdUtils::checkCypher( if ( std::get< 1 >( cyphertext ).length() != 64 ) throw std::runtime_error( "wrong string length in cyphertext" ); } + +std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( + const std::string& _hint ) { + auto position = _hint.find( ":" ); + + if ( position == std::string::npos ) { + throw std::runtime_error( "Misformatted hint" ); + } + + libff::alt_bn128_Fq y( _hint.substr( 0, position ).c_str() ); + libff::alt_bn128_Fq shift_x( _hint.substr( position + 1 ).c_str() ); + + return std::make_pair( y, shift_x ); +} + +std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( + std::shared_ptr< std::string > str, const std::string& delim ) { + // CHECK( str ); + + std::vector< std::string > tokens; + 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() ) + tokens.push_back( token ); + prev = pos + delim.length(); + } while ( pos < str->length() && prev < str->length() ); + + return std::make_shared< std::vector< std::string > >( tokens ); +} diff --git a/tools/utils.h b/tools/utils.h index 81e4a9db..3107049b 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -25,6 +25,7 @@ #define LIBBLS_UTILS_H #include +#include #include #include #include @@ -33,6 +34,10 @@ class ThresholdUtils { public: + static void initCurve(); + + static std::atomic< bool > is_initialized; + static void checkSigners( size_t _requiredSigners, size_t _totalSigners ); static std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); @@ -51,6 +56,12 @@ class ThresholdUtils { static void checkCypher( const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cypher ); + static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( + const std::string& hint ); + + static std::shared_ptr< std::vector< std::string > > SplitString( + const std::shared_ptr< std::string >, const std::string& delim ); + template < class T > static std::string fieldElementToString( const T& field_elem ); }; From 83603bca6f0c60e761f879e4df9eb2cdd8f228d0 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 12:04:00 +0300 Subject: [PATCH 12/40] SKALE-4409 remove extra tests --- test/unit_tests_dkg_te.cpp | 128 ------------------------------------- 1 file changed, 128 deletions(-) delete mode 100644 test/unit_tests_dkg_te.cpp diff --git a/test/unit_tests_dkg_te.cpp b/test/unit_tests_dkg_te.cpp deleted file mode 100644 index b1c97442..00000000 --- a/test/unit_tests_dkg_te.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - Copyright (C) 2018-2019 SKALE Labs - - This file is part of libBLS. - - libBLS is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - libBLS is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . - - @file unit_tests_dkg_te.cpp - @author Oleh Nikolaiev - @date 2019 -*/ - -#include - -#define BOOST_TEST_MODULE - -#include - -BOOST_AUTO_TEST_SUITE( DkgTeAlgorithm ) - -BOOST_AUTO_TEST_CASE( PolynomialValue ) { - encryption::DkgTe obj = encryption::DkgTe( 3, 4 ); - std::vector< encryption::element_wrapper > polynomial; - - for ( size_t i = 0; i < 3; ++i ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp, ( i % 2 == 0 ? 1 : 0 ) ); - - polynomial.push_back( encryption::element_wrapper( tmp ) ); - - element_clear( tmp ); - } - - element_t five; - element_init_Zr( five, TEDataSingleton::getData().pairing_ ); - element_set_si( five, 5 ); - - encryption::element_wrapper value = - obj.ComputePolynomialValue( polynomial, encryption::element_wrapper( five ) ); - - element_t twenty_six; - element_init_Zr( twenty_six, TEDataSingleton::getData().pairing_ ); - element_set_si( twenty_six, 26 ); - - BOOST_REQUIRE( !element_cmp( twenty_six, value.el_ ) ); // element_cmp(a, b) returns false iff - // a == b - - element_clear( twenty_six ); - - polynomial.clear(); - - for ( size_t i = 0; i < 3; ++i ) { - element_t tmp; - element_init_Zr( tmp, TEDataSingleton::getData().pairing_ ); - element_set_si( tmp, ( i % 2 == 0 ? 0 : 1 ) ); - - polynomial.push_back( encryption::element_wrapper( tmp ) ); - - element_clear( tmp ); - } - - bool is_exception_caught = false; - - try { - value = obj.ComputePolynomialValue( polynomial, encryption::element_wrapper( five ) ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - - element_clear( five ); - - BOOST_REQUIRE( is_exception_caught ); -} - -BOOST_AUTO_TEST_CASE( Verification ) { - encryption::DkgTe obj = encryption::DkgTe( 2, 2 ); - - auto polynomial_fst = obj.GeneratePolynomial(); - auto polynomial_snd = obj.GeneratePolynomial(); - - auto verification_vector_fst = obj.CreateVerificationVector( polynomial_fst ); - auto verification_vector_snd = obj.CreateVerificationVector( polynomial_snd ); - - encryption::element_wrapper shared_by_fst_to_snd = - obj.CreateSecretKeyContribution( polynomial_snd )[1]; - encryption::element_wrapper shared_by_snd_to_fst = - obj.CreateSecretKeyContribution( polynomial_fst )[0]; - - BOOST_REQUIRE( obj.Verify( 0, shared_by_snd_to_fst, verification_vector_fst ) ); - BOOST_REQUIRE( obj.Verify( 1, shared_by_fst_to_snd, verification_vector_snd ) ); - - element_t rand; - element_init_Zr( rand, TEDataSingleton::getData().pairing_ ); - element_random( rand ); - - element_t sum; - element_init_Zr( sum, TEDataSingleton::getData().pairing_ ); - element_add( sum, rand, shared_by_snd_to_fst.el_ ); - - BOOST_REQUIRE( obj.Verify( 0, sum, verification_vector_fst ) == false ); - - element_clear( sum ); - element_clear( rand ); - - element_init_Zr( rand, TEDataSingleton::getData().pairing_ ); - element_random( rand ); - - element_init_Zr( sum, TEDataSingleton::getData().pairing_ ); - element_add( sum, rand, shared_by_fst_to_snd.el_ ); - BOOST_REQUIRE( obj.Verify( 1, sum, verification_vector_snd ) == false ); - - element_clear( sum ); - element_clear( rand ); -} - -BOOST_AUTO_TEST_SUITE_END() From d99674ea86fab38c105f2e8f466986d830f25aa2 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 12:29:55 +0300 Subject: [PATCH 13/40] SKALE-4409 remove redundant dkg class --- dkg/DKGBLSSecret.h | 2 +- dkg/DKGBLSWrapper.cpp | 2 +- dkg/DKGBLSWrapper.h | 6 +- dkg/DKGTESecret.cpp | 16 +-- dkg/DKGTEWrapper.cpp | 17 ++- dkg/DKGTEWrapper.h | 6 +- dkg/dkg_te.cpp | 124 --------------------- dkg/dkg_te.h | 59 ---------- libBLS.h | 3 +- test/test_TE_wrappers.cpp | 12 +- threshold_encryption/CMakeLists.txt | 4 +- threshold_encryption/TEPrivateKeyShare.cpp | 9 +- 12 files changed, 39 insertions(+), 221 deletions(-) delete mode 100644 dkg/dkg_te.cpp delete mode 100644 dkg/dkg_te.h diff --git a/dkg/DKGBLSSecret.h b/dkg/DKGBLSSecret.h index 4179782b..ab2c5bcd 100644 --- a/dkg/DKGBLSSecret.h +++ b/dkg/DKGBLSSecret.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova diff --git a/dkg/DKGBLSWrapper.cpp b/dkg/DKGBLSWrapper.cpp index 3b984c78..7cc25955 100644 --- a/dkg/DKGBLSWrapper.cpp +++ b/dkg/DKGBLSWrapper.cpp @@ -35,7 +35,7 @@ DKGBLSWrapper::DKGBLSWrapper( size_t _requiredSigners, size_t _totalSigners ) } bool DKGBLSWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share, - const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector ) { + std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ) { if ( _share.is_zero() ) throw std::runtime_error( " Zero secret share" ); if ( _verification_vector == nullptr ) { diff --git a/dkg/DKGBLSWrapper.h b/dkg/DKGBLSWrapper.h index c42f0295..0ec8b335 100644 --- a/dkg/DKGBLSWrapper.h +++ b/dkg/DKGBLSWrapper.h @@ -39,7 +39,7 @@ class DKGBLSWrapper { DKGBLSWrapper( size_t _requiredSigners, size_t _totalSigners ); bool VerifyDKGShare( size_t signerIndex, const libff::alt_bn128_Fr& share, - const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector ); + std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ); void setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ); @@ -51,6 +51,10 @@ class DKGBLSWrapper { std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ); libff::alt_bn128_Fr getValueAt0(); + + static BLSPublicKey CreateTEPublicKey( + std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all, + size_t _requiredSigners, size_t _totalSigners ); }; diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp index 018ed1fd..c14a012a 100644 --- a/dkg/DKGTESecret.cpp +++ b/dkg/DKGTESecret.cpp @@ -22,10 +22,10 @@ */ -#include "DKGTESecret.h" -#include "../tools/utils.h" +#include +#include -#include +#include DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { @@ -33,7 +33,7 @@ DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) libff::init_alt_bn128_params(); - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); + signatures::Dkg dkg_te( requiredSigners, totalSigners ); poly = dkg_te.GeneratePolynomial(); } @@ -46,11 +46,11 @@ void DKGTESecret::setPoly( std::vector< libff::alt_bn128_Fr >& _poly ) { } std::vector< libff::alt_bn128_Fr > DKGTESecret::getDKGTESecretShares() { - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - return dkg_te.CreateSecretKeyContribution( poly ); + signatures::Dkg dkg_te( requiredSigners, totalSigners ); + return dkg_te.SecretKeyContribution( poly ); } std::vector< libff::alt_bn128_G2 > DKGTESecret::getDKGTEPublicShares() { - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - return dkg_te.CreateVerificationVector( poly ); + signatures::Dkg dkg_te( requiredSigners, totalSigners ); + return dkg_te.VerificationVector( poly ); } diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index 3ff3e22a..960ed083 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -22,8 +22,8 @@ */ -#include "DKGTEWrapper.h" -#include "../tools/utils.h" +#include +#include DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { @@ -36,19 +36,18 @@ DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) } bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share, - const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector ) { + std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ) { if ( _share.is_zero() ) throw std::runtime_error( "Zero secret share" ); if ( _verification_vector == nullptr ) throw std::runtime_error( "Null verification vector" ); if ( _verification_vector->size() != requiredSigners ) throw std::runtime_error( "Wrong size of verification vector" ); - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - return dkg_te.Verify( _signerIndex, _share, *_verification_vector ); + signatures::Dkg dkg_te( requiredSigners, totalSigners ); + return dkg_te.Verification( _signerIndex, _share, *_verification_vector ); } -void DKGTEWrapper::setDKGSecret( - std::shared_ptr< std::vector< libff::alt_bn128_Fr > >& _poly_ptr ) { +void DKGTEWrapper::setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ) { if ( _poly_ptr == nullptr ) throw std::runtime_error( "Null polynomial ptr" ); dkg_secret_ptr->setPoly( *_poly_ptr ); @@ -71,9 +70,9 @@ TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( if ( secret_shares_ptr->size() != totalSigners ) throw std::runtime_error( "Wrong number of secret key parts " ); - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); + signatures::Dkg dkg_te( requiredSigners, totalSigners ); - libff::alt_bn128_Fr skey_share = dkg_te.CreateSecretKeyShare( *secret_shares_ptr ); + libff::alt_bn128_Fr skey_share = dkg_te.SecretKeyShareCreate( *secret_shares_ptr ); return TEPrivateKeyShare( skey_share, signerIndex_, requiredSigners, totalSigners ); } diff --git a/dkg/DKGTEWrapper.h b/dkg/DKGTEWrapper.h index f5f0d0c8..e846edf3 100644 --- a/dkg/DKGTEWrapper.h +++ b/dkg/DKGTEWrapper.h @@ -26,7 +26,7 @@ #define LIBBLS_DKGTEWRAPPER_H #include -#include +#include #include class DKGTEWrapper { @@ -40,9 +40,9 @@ class DKGTEWrapper { DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ); bool VerifyDKGShare( size_t signerIndex, const libff::alt_bn128_Fr& share, - const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& verification_vector ); + std::shared_ptr< std::vector< libff::alt_bn128_G2 > > verification_vector ); - void setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > >& _poly_ptr ); + void setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ); std::shared_ptr< std::vector< libff::alt_bn128_Fr > > createDKGSecretShares(); diff --git a/dkg/dkg_te.cpp b/dkg/dkg_te.cpp deleted file mode 100644 index e00e4cdc..00000000 --- a/dkg/dkg_te.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* -Copyright (C) 2018-2019 SKALE Labs - -This file is part of libBLS. - -libBLS is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published -by the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -libBLS is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with libBLS. If not, see . - -@file dkg_te.cpp -@author Oleh Nikolaiev -@date 2019 -*/ - -#include - -#include - -namespace encryption { - -typedef std::vector< libff::alt_bn128_Fr > Polynomial; - -DkgTe::DkgTe( const size_t t, const size_t n ) : t_( t ), n_( n ) { - libff::init_alt_bn128_params(); -} - -Polynomial DkgTe::GeneratePolynomial() { - Polynomial pol( this->t_ ); - - for ( size_t i = 0; i < this->t_; ++i ) { - libff::alt_bn128_Fr g = libff::alt_bn128_Fr::random_element(); - - while ( i == this->t_ - 1 && g.is_zero() ) { - g = libff::alt_bn128_Fr::random_element(); - } - - pol[i] = g; - } - - return pol; -} - -std::vector< libff::alt_bn128_G2 > DkgTe::CreateVerificationVector( - const std::vector< libff::alt_bn128_Fr >& polynomial ) { - std::vector< libff::alt_bn128_G2 > verification_vector( this->t_ ); - - for ( size_t i = 0; i < this->t_; ++i ) { - verification_vector[i] = polynomial[i] * libff::alt_bn128_G2::one(); - } - - return verification_vector; -} - -libff::alt_bn128_Fr DkgTe::ComputePolynomialValue( - const std::vector< libff::alt_bn128_Fr >& polynomial, const libff::alt_bn128_Fr& point ) { - libff::alt_bn128_Fr value = libff::alt_bn128_Fr::zero(); - - libff::alt_bn128_Fr pow = libff::alt_bn128_Fr::one(); - for ( size_t i = 0; i < this->t_; ++i ) { - if ( i == this->t_ - 1 && polynomial[i] == libff::alt_bn128_Fr::zero() ) { - throw std::logic_error( "Error, incorrect degree of a polynomial" ); - } - value += polynomial[i] * pow; - pow *= point; - } - - return value; -} - -std::vector< libff::alt_bn128_Fr > DkgTe::CreateSecretKeyContribution( - const std::vector< libff::alt_bn128_Fr >& polynomial ) { - std::vector< libff::alt_bn128_Fr > secret_key_contribution( this->n_ ); - for ( size_t i = 0; i < this->n_; ++i ) { - libff::alt_bn128_Fr point = i + 1; - - secret_key_contribution[i] = ComputePolynomialValue( polynomial, point ); - } - - return secret_key_contribution; -} - -libff::alt_bn128_Fr DkgTe::CreateSecretKeyShare( - const std::vector< libff::alt_bn128_Fr >& secret_key_contribution ) { - libff::alt_bn128_Fr secret_key_share = libff::alt_bn128_Fr::zero(); - - for ( size_t i = 0; i < this->n_; ++i ) { - secret_key_share += secret_key_contribution[i]; - } - - if ( secret_key_share.is_zero() ) { - throw std::runtime_error( "Error, at least one secret key share is equal to zero" ); - } - - return secret_key_share; -} - -bool DkgTe::Verify( size_t idx, const libff::alt_bn128_Fr& share, - const std::vector< libff::alt_bn128_G2 >& verification_vector ) { - libff::alt_bn128_G2 value = libff::alt_bn128_G2::zero(); - for ( size_t i = 0; i < this->t_; ++i ) { - if ( !this->isG2( verification_vector[i] ) ) { - return false; - } - value = value + power( libff::alt_bn128_Fr( idx + 1 ), i ) * verification_vector[i]; - } - - return ( value == share * libff::alt_bn128_G2::one() ); -} - -bool DkgTe::isG2( const libff::alt_bn128_G2& point ) { - return point.is_well_formed() && - libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); -} - -} // namespace encryption diff --git a/dkg/dkg_te.h b/dkg/dkg_te.h deleted file mode 100644 index a8477639..00000000 --- a/dkg/dkg_te.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - Copyright (C) 2018-2019 SKALE Labs - - This file is part of libBLS. - - libBLS is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - libBLS is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . - - @file dkg_te.h - @author Oleh Nikolaiev - @date 2019 - */ - -#pragma once - -#include - -namespace encryption { - -class DkgTe { -public: - DkgTe( const size_t t, const size_t n ); - - std::vector< libff::alt_bn128_Fr > GeneratePolynomial(); - - std::vector< libff::alt_bn128_G2 > CreateVerificationVector( - const std::vector< libff::alt_bn128_Fr >& polynomial ); - - libff::alt_bn128_Fr ComputePolynomialValue( - const std::vector< libff::alt_bn128_Fr >& polynomial, const libff::alt_bn128_Fr& point ); - - std::vector< libff::alt_bn128_Fr > CreateSecretKeyContribution( - const std::vector< libff::alt_bn128_Fr >& polynomial ); - - libff::alt_bn128_Fr CreateSecretKeyShare( - const std::vector< libff::alt_bn128_Fr >& secret_key_contribution ); - - bool Verify( size_t idx, const libff::alt_bn128_Fr& share, - const std::vector< libff::alt_bn128_G2 >& verification_vector ); - - static bool isG2( const libff::alt_bn128_G2& point ); - -private: - const size_t t_ = 0; - - const size_t n_ = 0; -}; - -} // namespace encryption diff --git a/libBLS.h b/libBLS.h index e5a3bba0..713531a4 100644 --- a/libBLS.h +++ b/libBLS.h @@ -14,7 +14,7 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file libBLS.h @author Sveta Rogova @@ -31,7 +31,6 @@ #include #include -#include #include #include diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index d71696d6..9dc60bd1 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -24,7 +24,7 @@ along with libBLS. If not, see . #define BOOST_TEST_MODULE -#include +#include #include #include #include @@ -59,13 +59,13 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - encryption::DkgTe dkg_te( num_signed, num_all ); + signatures::Dkg dkg_te( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); libff::alt_bn128_Fr zero_el = libff::alt_bn128_Fr::zero(); - libff::alt_bn128_Fr common_skey = dkg_te.ComputePolynomialValue( poly, zero_el ); + libff::alt_bn128_Fr common_skey = dkg_te.PolynomialValue( poly, zero_el ); BOOST_REQUIRE( common_skey == poly.at( 0 ) ); TEPrivateKey common_private( common_skey, num_signed, num_all ); @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { auto msg_ptr = std::make_shared< std::string >( message ); encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); - std::vector< libff::alt_bn128_Fr > skeys = dkg_te.CreateSecretKeyContribution( poly ); + std::vector< libff::alt_bn128_Fr > skeys = dkg_te.SecretKeyContribution( poly ); std::vector< TEPrivateKeyShare > skey_shares; std::vector< TEPublicKeyShare > public_key_shares; for ( size_t i = 0; i < num_all; i++ ) { @@ -177,7 +177,7 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - encryption::DkgTe dkg_te( num_signed, num_all ); + signatures::Dkg dkg_te( num_signed, num_all ); std::string message; size_t msg_length = 64; @@ -269,7 +269,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { for ( size_t i = 0; i < num_all; i++ ) { DKGTEWrapper dkg_wrap( num_signed, num_all ); - encryption::DkgTe dkg_te( num_signed, num_all ); + signatures::Dkg dkg_te( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); auto shared_poly = std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ); dkg_wrap.setDKGSecret( shared_poly ); diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index a9f2ec06..868905a5 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -15,7 +15,7 @@ set(sources TEPrivateKey.cpp TEPublicKey.cpp TEPublicKeyShare.cpp - ${DKG_DIR}/dkg_te.cpp + ${DKG_DIR}/dkg.cpp ${DKG_DIR}/DKGTEWrapper.cpp ${DKG_DIR}/DKGTESecret.cpp ${TOOLS_DIR}/utils.cpp @@ -28,7 +28,7 @@ set(headers TEPrivateKey.h TEPublicKey.h TEPublicKeyShare.h - ${DKG_DIR}/dkg_te.h + ${DKG_DIR}/dkg.h ${DKG_DIR}/DKGTEWrapper.h ${DKG_DIR}/DKGTESecret.h ${TOOLS_DIR}/utils.h diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index 6398238a..bf6533ec 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -21,7 +21,7 @@ along with libBLS. If not, see . @date 2019 */ -#include +#include #include #include @@ -93,16 +93,15 @@ libff::alt_bn128_Fr TEPrivateKeyShare::getPrivateKey() const { std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > TEPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) { - encryption::DkgTe dkg_te( _requiredSigners, _totalSigners ); + signatures::Dkg dkg_te( _requiredSigners, _totalSigners ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - libff::alt_bn128_Fr common_skey = - dkg_te.ComputePolynomialValue( poly, libff::alt_bn128_Fr::zero() ); + libff::alt_bn128_Fr common_skey = dkg_te.PolynomialValue( poly, libff::alt_bn128_Fr::zero() ); TEPrivateKey common_private( common_skey, _requiredSigners, _totalSigners ); TEPublicKey common_public( common_private, _requiredSigners, _totalSigners ); - std::vector< libff::alt_bn128_Fr > skeys = dkg_te.CreateSecretKeyContribution( poly ); + std::vector< libff::alt_bn128_Fr > skeys = dkg_te.SecretKeyContribution( poly ); std::vector< std::shared_ptr< TEPrivateKeyShare > > skey_shares; From 826d60bab3d1ecbdc26def87b05de07cd68f6d94 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 13:17:05 +0300 Subject: [PATCH 14/40] SKALE-4409 use namespace crypto --- bls/BLSPrivateKey.cpp | 14 +-- bls/BLSPrivateKeyShare.cpp | 30 ++--- bls/BLSPublicKey.cpp | 26 ++--- bls/BLSPublicKeyShare.cpp | 18 +-- bls/BLSSigShare.cpp | 24 ++-- bls/BLSSigShareSet.cpp | 10 +- bls/BLSSignature.cpp | 18 +-- bls/bls.cpp | 2 +- bls/bls.h | 4 +- dkg/DKGBLSSecret.cpp | 6 +- dkg/DKGBLSWrapper.cpp | 4 +- dkg/DKGTESecret.cpp | 6 +- dkg/DKGTEWrapper.cpp | 4 +- dkg/dkg.cpp | 2 +- dkg/dkg.h | 2 +- docs/using-distributed-key-generation.md | 30 ++--- docs/using-threshold-encryption.md | 4 +- python/dkgpython.cpp | 4 +- test/dkg_attack.cpp | 2 +- test/test_TE_wrappers.cpp | 26 ++--- test/test_bls.cpp | 106 +++++++++--------- test/unit_tests_bls.cpp | 40 +++---- test/unit_tests_dkg.cpp | 10 +- test/unit_tests_te.cpp | 14 +-- threshold_encryption/TEDecryptSet.cpp | 4 +- threshold_encryption/TEDecryptSet.h | 2 +- threshold_encryption/TEPrivateKeyShare.cpp | 6 +- threshold_encryption/TEPrivateKeyShare.h | 2 +- threshold_encryption/TEPublicKey.cpp | 6 +- threshold_encryption/TEPublicKey.h | 2 +- threshold_encryption/TEPublicKeyShare.cpp | 4 +- threshold_encryption/TEPublicKeyShare.h | 2 +- threshold_encryption/threshold_encryption.cpp | 2 +- threshold_encryption/threshold_encryption.h | 2 +- tools/bls_glue.cpp | 2 +- tools/dkg_glue.cpp | 2 +- tools/dkg_key_gen.cpp | 2 +- tools/generate_key_system.cpp | 4 +- tools/hash_g1.cpp | 2 +- tools/sign_bls.cpp | 2 +- tools/verify_bls.cpp | 2 +- 41 files changed, 227 insertions(+), 227 deletions(-) diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index a0239806..01aef7e1 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -33,15 +33,15 @@ BLSPrivateKey::BLSPrivateKey( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( _key == nullptr ) { - throw signatures::Bls::IncorrectInput( "Secret key share is null" ); + throw crypto::Bls::IncorrectInput( "Secret key share is null" ); } if ( _key->empty() ) { - throw signatures::Bls::IncorrectInput( "Secret key share is empty" ); + throw crypto::Bls::IncorrectInput( "Secret key share is empty" ); } privateKey = std::make_shared< libff::alt_bn128_Fr >( _key->c_str() ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); } } @@ -50,10 +50,10 @@ BLSPrivateKey::BLSPrivateKey( std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { if ( skeys == nullptr ) { - throw signatures::Bls::IncorrectInput( "Secret keys ptr is null" ); + throw crypto::Bls::IncorrectInput( "Secret keys ptr is null" ); } if ( koefs == nullptr ) { - throw signatures::Bls::IncorrectInput( "Signers indices ptr is null" ); + throw crypto::Bls::IncorrectInput( "Signers indices ptr is null" ); } ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); @@ -66,7 +66,7 @@ BLSPrivateKey::BLSPrivateKey( } if ( privateKeyObj == libff::alt_bn128_Fr::zero() ) { - throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); } privateKey = std::make_shared< libff::alt_bn128_Fr >( privateKeyObj ); @@ -81,7 +81,7 @@ std::shared_ptr< std::string > BLSPrivateKey::toString() { std::make_shared< std::string >( ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) - throw signatures::Bls::ZeroSecretKey( "Secret key share string is empty" ); + throw crypto::Bls::ZeroSecretKey( "Secret key share string is empty" ); return key_str; } diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index c6f03d1b..bd7f00a9 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -35,12 +35,12 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); ThresholdUtils::initCurve(); if ( _key.empty() ) { - throw signatures::Bls::IncorrectInput( "Secret key share string is empty" ); + throw crypto::Bls::IncorrectInput( "Secret key share string is empty" ); } privateKey = std::make_shared< libff::alt_bn128_Fr >( _key.c_str() ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); } } @@ -52,22 +52,22 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( privateKey = std::make_shared< libff::alt_bn128_Fr >( libff_skey ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw signatures::Bls::ZeroSecretKey( "BLS Secret key share is equal to zero" ); + throw crypto::Bls::ZeroSecretKey( "BLS Secret key share is equal to zero" ); } } std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr, size_t _signerIndex ) { - std::shared_ptr< signatures::Bls > obj; + std::shared_ptr< crypto::Bls > obj; if ( _signerIndex == 0 ) { - throw signatures::Bls::IncorrectInput( "Zero signer index during BLS sign" ); + throw crypto::Bls::IncorrectInput( "Zero signer index during BLS sign" ); } if ( hash_byte_arr == nullptr ) { - throw signatures::Bls::IncorrectInput( "Hash is null during BLS sign" ); + throw crypto::Bls::IncorrectInput( "Hash is null during BLS sign" ); } - obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) ); + obj = std::make_shared< crypto::Bls >( crypto::Bls( requiredSigners, totalSigners ) ); libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_byte_arr ); @@ -88,16 +88,16 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr, size_t _signerIndex ) { - std::shared_ptr< signatures::Bls > obj; + std::shared_ptr< crypto::Bls > obj; if ( _signerIndex == 0 ) { - throw signatures::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::Bls::IncorrectInput( "Zero signer index" ); } if ( hash_byte_arr == nullptr ) { - throw signatures::Bls::IncorrectInput( "Null hash is bls signWithHelper" ); + throw crypto::Bls::IncorrectInput( "Null hash is bls signWithHelper" ); } - obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) ); + obj = std::make_shared< crypto::Bls >( crypto::Bls( requiredSigners, totalSigners ) ); std::pair< libff::alt_bn128_G1, std::string > hash_with_hint = obj->HashtoG1withHint( hash_byte_arr ); @@ -123,7 +123,7 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi std::vector< std::shared_ptr< BLSPrivateKeyShare > > skeys_shares; - signatures::Dkg dkg_obj = signatures::Dkg( _requiredSigners, _totalSigners ); + crypto::Dkg dkg_obj = crypto::Dkg( _requiredSigners, _totalSigners ); const std::vector< libff::alt_bn128_Fr > pol = dkg_obj.GeneratePolynomial(); std::vector< libff::alt_bn128_Fr > skeys = dkg_obj.SecretKeyContribution( pol ); @@ -156,14 +156,14 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKeyShare::getPrivateKey() const std::shared_ptr< std::string > BLSPrivateKeyShare::toString() { if ( !privateKey ) - throw signatures::Bls::IncorrectInput( "Secret key share is null" ); + throw crypto::Bls::IncorrectInput( "Secret key share is null" ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); } std::shared_ptr< std::string > key_str = std::make_shared< std::string >( ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) - throw signatures::Bls::IncorrectInput( "Secret key share string is empty" ); + throw crypto::Bls::IncorrectInput( "Secret key share string is empty" ); return key_str; } diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index a8145577..3d9669ae 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -46,11 +46,11 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > libffPublicKey->Z.c1 = libff::alt_bn128_Fq::zero(); if ( libffPublicKey->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key " ); + throw crypto::Bls::IsNotWellFormed( "Zero BLS public Key " ); } if ( !( libffPublicKey->is_well_formed() ) ) { - throw signatures::Bls::IsNotWellFormed( "BLS public Key is corrupt" ); + throw crypto::Bls::IsNotWellFormed( "BLS public Key is corrupt" ); } } @@ -63,7 +63,7 @@ BLSPublicKey::BLSPublicKey( libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( pkey ); if ( libffPublicKey->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero BLS Public Key" ); + throw crypto::Bls::IsNotWellFormed( "Zero BLS Public Key" ); } } @@ -73,7 +73,7 @@ BLSPublicKey::BLSPublicKey( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( skey * libff::alt_bn128_G2::one() ); if ( libffPublicKey->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); + throw crypto::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); } } @@ -89,18 +89,18 @@ bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { ThresholdUtils::initCurve(); - std::shared_ptr< signatures::Bls > obj; + std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw signatures::Bls::IncorrectInput( "hash is null" ); + throw crypto::Bls::IncorrectInput( "hash is null" ); } if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero or corrupt" ); + throw crypto::Bls::IsNotWellFormed( "Sig share is equal to zero or corrupt" ); } - obj = std::make_shared< signatures::Bls >( signatures::Bls( _requiredSigners, _totalSigners ) ); + obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) ); bool res = obj->Verification( hash_ptr, *( sign_ptr->getSig() ), *libffPublicKey ); return res; @@ -108,13 +108,13 @@ bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { - std::shared_ptr< signatures::Bls > obj; + std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw signatures::Bls::IncorrectInput( "hash is null" ); + throw crypto::Bls::IncorrectInput( "hash is null" ); } if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) { - throw signatures::Bls::IncorrectInput( "Sig share is equal to zero or corrupt" ); + throw crypto::Bls::IncorrectInput( "Sig share is equal to zero or corrupt" ); } std::string hint = sign_ptr->getHint(); @@ -148,7 +148,7 @@ BLSPublicKey::BLSPublicKey( ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !koefs_pkeys_map ) { - throw signatures::Bls::IncorrectInput( "map is null" ); + throw crypto::Bls::IncorrectInput( "map is null" ); } std::vector< size_t > participatingNodes; @@ -174,7 +174,7 @@ BLSPublicKey::BLSPublicKey( libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( key ); if ( libffPublicKey->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); + throw crypto::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); } } diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index 9c8e8667..10c05a00 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -47,11 +47,11 @@ BLSPublicKeyShare::BLSPublicKeyShare( publicKey->Z.c1 = libff::alt_bn128_Fq::zero(); if ( publicKey->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key share" ); + throw crypto::Bls::IsNotWellFormed( "Zero BLS public Key share" ); } if ( !( publicKey->is_well_formed() ) ) { - throw signatures::Bls::IsNotWellFormed( "Corrupt BLS public key share" ); + throw crypto::Bls::IsNotWellFormed( "Corrupt BLS public key share" ); } } @@ -60,7 +60,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { ThresholdUtils::initCurve(); if ( _skey.is_zero() ) { - throw signatures::Bls::ZeroSecretKey( "Zero BLS Secret Key" ); + throw crypto::Bls::ZeroSecretKey( "Zero BLS Secret Key" ); } publicKey = std::make_shared< libff::alt_bn128_G2 >( _skey * libff::alt_bn128_G2::one() ); } @@ -88,14 +88,14 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > CHECK( hash_ptr ); CHECK( sign_ptr ); - std::shared_ptr< signatures::Bls > obj; + std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( sign_ptr->getSigShare()->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero BLS Sig share" ); + throw crypto::Bls::IsNotWellFormed( "Zero BLS Sig share" ); } - obj = std::make_shared< signatures::Bls >( signatures::Bls( _requiredSigners, _totalSigners ) ); + obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) ); bool res = obj->Verification( hash_ptr, *( sign_ptr->getSigShare() ), *publicKey ); return res; @@ -105,13 +105,13 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_ std::shared_ptr< BLSSigShare > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { CHECK( sign_ptr ) - std::shared_ptr< signatures::Bls > obj; + std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw signatures::Bls::IncorrectInput( "hash is null" ); + throw crypto::Bls::IncorrectInput( "hash is null" ); } if ( sign_ptr->getSigShare()->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero" ); + throw crypto::Bls::IsNotWellFormed( "Sig share is equal to zero" ); } std::string hint = sign_ptr->getHint(); diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp index d5a32982..a1b083b7 100644 --- a/bls/BLSSigShare.cpp +++ b/bls/BLSSigShare.cpp @@ -55,21 +55,21 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign ThresholdUtils::checkSigners( requiredSigners, totalSigners ); ThresholdUtils::initCurve(); if ( _signerIndex == 0 ) { - throw signatures::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::Bls::IncorrectInput( "Zero signer index" ); } if ( !_sigShare ) { - throw signatures::Bls::IncorrectInput( "Null _sigShare" ); + throw crypto::Bls::IncorrectInput( "Null _sigShare" ); } if ( _sigShare->size() < 10 ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::Bls::IsNotWellFormed( "Signature too short:" + std::to_string( _sigShare->size() ) ); } if ( _sigShare->size() > BLS_MAX_SIG_LEN ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::Bls::IsNotWellFormed( "Signature too long:" + std::to_string( _sigShare->size() ) ); } @@ -77,11 +77,11 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign std::shared_ptr< std::vector< std::string > > result = ThresholdUtils::SplitString( _sigShare, ":" ); if ( result->size() != 4 ) - throw signatures::Bls::IncorrectInput( "Misformatted signature" ); + throw crypto::Bls::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { for ( char& c : str ) { if ( !( c >= '0' && c <= '9' ) ) { - throw signatures::Bls::IncorrectInput( + throw crypto::Bls::IncorrectInput( "Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str ); } } @@ -94,7 +94,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign hint = result->at( 2 ) + ":" + result->at( 3 ); if ( !sigShare->is_well_formed() ) - throw signatures::Bls::IsNotWellFormed( "signature is not from G1" ); + throw crypto::Bls::IsNotWellFormed( "signature is not from G1" ); } BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShare, @@ -107,21 +107,21 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar ThresholdUtils::initCurve(); ThresholdUtils::checkSigners( requiredSigners, totalSigners ); if ( !_sigShare ) { - throw signatures::Bls::IncorrectInput( "Null _s" ); + throw crypto::Bls::IncorrectInput( "Null _s" ); } if ( _sigShare->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero signature" ); + throw crypto::Bls::IsNotWellFormed( "Zero signature" ); } if ( _signerIndex == 0 ) { - throw signatures::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::Bls::IncorrectInput( "Zero signer index" ); } if ( _hint.length() == 0 ) { - throw signatures::Bls::IncorrectInput( "Empty or misformatted hint" ); + throw crypto::Bls::IncorrectInput( "Empty or misformatted hint" ); } if ( !_sigShare->is_well_formed() ) { - throw signatures::Bls::IsNotWellFormed( "signature is not from G1" ); + throw crypto::Bls::IsNotWellFormed( "signature is not from G1" ); } } diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp index a0c3652f..81312788 100644 --- a/bls/BLSSigShareSet.cpp +++ b/bls/BLSSigShareSet.cpp @@ -34,11 +34,11 @@ bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) { CHECK( _sigShare ); if ( was_merged ) { - throw signatures::Bls::IncorrectInput( "Invalid state:was already merged" ); + throw crypto::Bls::IncorrectInput( "Invalid state:was already merged" ); } if ( sigShares.count( _sigShare->getSignerIndex() ) > 0 ) { - throw signatures::Bls::IncorrectInput( + throw crypto::Bls::IncorrectInput( "Already have this index:" + std::to_string( _sigShare->getSignerIndex() ) ); return false; } @@ -52,7 +52,7 @@ size_t BLSSigShareSet::getTotalSigSharesCount() { } std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index ) { if ( _index == 0 ) { - throw signatures::Bls::IncorrectInput( "Index out of range:" + std::to_string( _index ) ); + throw crypto::Bls::IncorrectInput( "Index out of range:" + std::to_string( _index ) ); } @@ -76,10 +76,10 @@ bool BLSSigShareSet::isEnough() { std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { if ( !isEnough() ) - throw signatures::Bls::IncorrectInput( "Not enough shares to create signature" ); + throw crypto::Bls::IncorrectInput( "Not enough shares to create signature" ); was_merged = true; - signatures::Bls obj = signatures::Bls( requiredSigners, totalSigners ); + crypto::Bls obj = crypto::Bls( requiredSigners, totalSigners ); std::vector< size_t > participatingNodes; std::vector< libff::alt_bn128_G1 > shares; diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp index 33254c93..5bb1b25a 100644 --- a/bls/BLSSignature.cpp +++ b/bls/BLSSignature.cpp @@ -42,10 +42,10 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st if ( sig->is_zero() ) { - throw signatures::Bls::IncorrectInput( "Zero BLS signature" ); + throw crypto::Bls::IncorrectInput( "Zero BLS signature" ); } if ( hint.length() == 0 ) { - throw signatures::Bls::IncorrectInput( "Empty BLS hint" ); + throw crypto::Bls::IncorrectInput( "Empty BLS hint" ); } } @@ -59,24 +59,24 @@ BLSSignature::BLSSignature( ThresholdUtils::initCurve(); if ( _sig->size() < 10 ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::Bls::IsNotWellFormed( "Signature too short:" + std::to_string( _sig->size() ) ); } if ( _sig->size() > BLS_MAX_SIG_LEN ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::Bls::IsNotWellFormed( "Signature too long:" + std::to_string( _sig->size() ) ); } std::shared_ptr< std::vector< std::string > > result = ThresholdUtils::SplitString( _sig, ":" ); if ( result->size() != 4 ) - throw signatures::Bls::IncorrectInput( "Misformatted signature" ); + throw crypto::Bls::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { for ( char& c : str ) { if ( !( c >= '0' && c <= '9' ) ) { - throw signatures::Bls::IncorrectInput( + throw crypto::Bls::IncorrectInput( "Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str ); } } @@ -87,7 +87,7 @@ BLSSignature::BLSSignature( hint = result->at( 2 ) + ":" + result->at( 3 ); if ( !( sig->is_well_formed() ) ) { - throw signatures::Bls::IsNotWellFormed( "signature is not from G1" ); + throw crypto::Bls::IsNotWellFormed( "signature is not from G1" ); } } @@ -105,12 +105,12 @@ void BLSSignature::checkSigners( size_t _requiredSigners, size_t _totalSigners ) CHECK( _totalSigners > 0 ); if ( _requiredSigners > _totalSigners ) { - throw signatures::Bls::IncorrectInput( "_requiredSigners > _totalSigners" ); + throw crypto::Bls::IncorrectInput( "_requiredSigners > _totalSigners" ); } if ( _totalSigners == 0 ) { - throw signatures::Bls::IncorrectInput( "_totalSigners == 0" ); + throw crypto::Bls::IncorrectInput( "_totalSigners == 0" ); } } diff --git a/bls/bls.cpp b/bls/bls.cpp index 8da1a9b9..7cd86e91 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -36,7 +36,7 @@ along with libBLS. If not, see . #include #include -namespace signatures { +namespace crypto { Bls::Bls( const size_t t, const size_t n ) : t_( t ), n_( n ) { ThresholdUtils::initCurve(); diff --git a/bls/bls.h b/bls/bls.h index a1197177..6546c930 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -39,7 +39,7 @@ static constexpr size_t BLS_MAX_COMPONENT_LEN = 80; static constexpr size_t BLS_MAX_SIG_LEN = 240; -namespace signatures { +namespace crypto { class Bls { private: @@ -118,5 +118,5 @@ class Bls { if ( !( _EXPRESSION_ ) ) { \ auto __msg__ = std::string( "Check failed:" ) + #_EXPRESSION_ + "\n" + __FUNCTION__ + \ +" " + std::string( __FILE__ ) + ":" + std::to_string( __LINE__ ); \ - throw signatures::Bls::IncorrectInput( __msg__ ); \ + throw crypto::Bls::IncorrectInput( __msg__ ); \ } diff --git a/dkg/DKGBLSSecret.cpp b/dkg/DKGBLSSecret.cpp index ad4bb4f4..b0c4fa8c 100644 --- a/dkg/DKGBLSSecret.cpp +++ b/dkg/DKGBLSSecret.cpp @@ -30,7 +30,7 @@ DKGBLSSecret::DKGBLSSecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - signatures::Dkg dkg( requiredSigners, totalSigners ); + crypto::Dkg dkg( requiredSigners, totalSigners ); poly = dkg.GeneratePolynomial(); } @@ -42,12 +42,12 @@ void DKGBLSSecret::setPoly( std::vector< libff::alt_bn128_Fr > _poly ) { } std::vector< libff::alt_bn128_Fr > DKGBLSSecret::getDKGBLSSecretShares() { - signatures::Dkg dkg( requiredSigners, totalSigners ); + crypto::Dkg dkg( requiredSigners, totalSigners ); return dkg.SecretKeyContribution( poly ); } std::vector< libff::alt_bn128_G2 > DKGBLSSecret::getDKGBLSPublicShares() { - signatures::Dkg dkg( requiredSigners, totalSigners ); + crypto::Dkg dkg( requiredSigners, totalSigners ); return dkg.VerificationVector( poly ); } diff --git a/dkg/DKGBLSWrapper.cpp b/dkg/DKGBLSWrapper.cpp index 7cc25955..d3b083ab 100644 --- a/dkg/DKGBLSWrapper.cpp +++ b/dkg/DKGBLSWrapper.cpp @@ -43,7 +43,7 @@ bool DKGBLSWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_ } if ( _verification_vector->size() != requiredSigners ) throw std::runtime_error( "Wrong vector size" ); - signatures::Dkg dkg( requiredSigners, totalSigners ); + crypto::Dkg dkg( requiredSigners, totalSigners ); return dkg.Verification( _signerIndex, _share, *_verification_vector ); } @@ -72,7 +72,7 @@ BLSPrivateKeyShare DKGBLSWrapper::CreateBLSPrivateKeyShare( if ( secret_shares_ptr->size() != totalSigners ) throw std::runtime_error( "Wrong number of secret key parts " ); - signatures::Dkg dkg( requiredSigners, totalSigners ); + crypto::Dkg dkg( requiredSigners, totalSigners ); libff::alt_bn128_Fr skey_share = dkg.SecretKeyShareCreate( *secret_shares_ptr ); diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp index c14a012a..324cd68f 100644 --- a/dkg/DKGTESecret.cpp +++ b/dkg/DKGTESecret.cpp @@ -33,7 +33,7 @@ DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) libff::init_alt_bn128_params(); - signatures::Dkg dkg_te( requiredSigners, totalSigners ); + crypto::Dkg dkg_te( requiredSigners, totalSigners ); poly = dkg_te.GeneratePolynomial(); } @@ -46,11 +46,11 @@ void DKGTESecret::setPoly( std::vector< libff::alt_bn128_Fr >& _poly ) { } std::vector< libff::alt_bn128_Fr > DKGTESecret::getDKGTESecretShares() { - signatures::Dkg dkg_te( requiredSigners, totalSigners ); + crypto::Dkg dkg_te( requiredSigners, totalSigners ); return dkg_te.SecretKeyContribution( poly ); } std::vector< libff::alt_bn128_G2 > DKGTESecret::getDKGTEPublicShares() { - signatures::Dkg dkg_te( requiredSigners, totalSigners ); + crypto::Dkg dkg_te( requiredSigners, totalSigners ); return dkg_te.VerificationVector( poly ); } diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index 960ed083..802b9a86 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -43,7 +43,7 @@ bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_F throw std::runtime_error( "Null verification vector" ); if ( _verification_vector->size() != requiredSigners ) throw std::runtime_error( "Wrong size of verification vector" ); - signatures::Dkg dkg_te( requiredSigners, totalSigners ); + crypto::Dkg dkg_te( requiredSigners, totalSigners ); return dkg_te.Verification( _signerIndex, _share, *_verification_vector ); } @@ -70,7 +70,7 @@ TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( if ( secret_shares_ptr->size() != totalSigners ) throw std::runtime_error( "Wrong number of secret key parts " ); - signatures::Dkg dkg_te( requiredSigners, totalSigners ); + crypto::Dkg dkg_te( requiredSigners, totalSigners ); libff::alt_bn128_Fr skey_share = dkg_te.SecretKeyShareCreate( *secret_shares_ptr ); diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index 427fe9d3..04f00f45 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -28,7 +28,7 @@ #include #include -namespace signatures { +namespace crypto { typedef std::vector< libff::alt_bn128_Fr > Polynomial; diff --git a/dkg/dkg.h b/dkg/dkg.h index 23f324c4..7d1802fd 100644 --- a/dkg/dkg.h +++ b/dkg/dkg.h @@ -29,7 +29,7 @@ #include #include -namespace signatures { +namespace crypto { class Dkg { public: diff --git a/docs/using-distributed-key-generation.md b/docs/using-distributed-key-generation.md index 70deb59d..bb8e5289 100644 --- a/docs/using-distributed-key-generation.md +++ b/docs/using-distributed-key-generation.md @@ -30,7 +30,7 @@ std::shared_ptr < std::vector > public_shares = dkg_obj.cr For TE ```cpp -std::shared_ptr > public_shares = +std::shared_ptr > public_shares = dkg_obj.createDKGPublicShares(); ``` @@ -45,7 +45,7 @@ std::shared_ptr > private_shares = dkg_obj.cr For TE ```cpp -std::shared_ptr < std::vector > private_shares = +std::shared_ptr < std::vector > private_shares = dkg_obj.createDKGSecretShares(); ``` @@ -77,15 +77,15 @@ Also in DKGTEWrapper there is a static function that creates common public key TEPublicKey publicKey = DKGTEWrapper::CreateTEPublicKey( public_shares_all, t, n); ``` -where public_shares_all is shared_ptr to matrix of all public shares ( its type is std::shared_ptr<std::vector<std::vector<encryption::element_wrapper>>>). +where public_shares_all is shared_ptr to matrix of all public shares ( its type is std::shared_ptr<std::vector<std::vector<crypto::element_wrapper>>>). Here is an example of Threshold Encryption algorithm with DKG simulation for t = 3, n = 4. ```cpp size_t num_signed = 3; size_t num_all = 4; -std::vector> secret_shares_all; // matrix of all secret shares -std::vector> public_shares_all; //// matrix of all public shares +std::vector> secret_shares_all; // matrix of all secret shares +std::vector> public_shares_all; //// matrix of all public shares std::vector dkgs; // instances of DKGTEWrapper for each participant std::vector skeys; // private keys of participants std::vector pkeys; // public keys of participants @@ -95,11 +95,11 @@ for (size_t i = 0; i < num_all; i++) { dkgs.push_back(dkg_wrap); // create secret shares for each participant - std::shared_ptr> secret_shares_ptr = + std::shared_ptr> secret_shares_ptr = dkg_wrap.createDKGSecretShares(); // create public shares for each participant - std::shared_ptr> public_shares_ptr = + std::shared_ptr> public_shares_ptr = dkg_wrap.createDKGPublicShares(); secret_shares_all.push_back(*secret_shares_ptr); @@ -109,12 +109,12 @@ for (size_t i = 0; i < num_all; i++) { for (size_t i = 0; i < num_all; i++) // Verifying shares for each participant for (size_t j = 0; j < num_all; j++) { assert(dkgs.at(i).VerifyDKGShare(j, secret_shares_all.at(i).at(j), - std::make_shared>(public_shares_all.at(i)))); + std::make_shared>(public_shares_all.at(i)))); } - std::vector> secret_key_shares; + std::vector> secret_key_shares; for (size_t i = 0; i < num_all; i++) { // collect got secret shares in a vector - std::vector secret_key_contribution; + std::vector secret_key_contribution; for (size_t j = 0; j < num_all; j++) { secret_key_contribution.push_back(secret_shares_all.at(j).at(i)); } @@ -124,14 +124,14 @@ for (size_t i = 0; i < num_all; i++) // Verifying shares for each participa for (size_t i = 0; i < num_all; i++) { TEPrivateKeyShare pkey_share = dkgs.at(i).CreateTEPrivateKeyShare( i + 1, - std::make_shared>( + std::make_shared>( secret_key_shares.at(i))); skeys.push_back(pkey_share); pkeys.push_back(TEPublicKeyShare(pkey_share, num_signed, num_all)); } TEPublicKey common_public = DKGTEWrapper::CreateTEPublicKey( - std::make_shared< std::vector>>(public_shares_all), + std::make_shared< std::vector>>(public_shares_all), num_signed, num_all); @@ -142,7 +142,7 @@ for (size_t i = 0; i < num_all; i++) // Verifying shares for each participa } std::shared_ptr msg_ptr = std::make_shared(message); - encryption::Ciphertext cypher = common_public.encrypt(msg_ptr); + crypto::Ciphertext cypher = common_public.encrypt(msg_ptr); // removing 1 random participant ( because only 3 of 4 will participate) size_t ind4del = rand_gen() % secret_shares_all.size(); @@ -155,9 +155,9 @@ for (size_t i = 0; i < num_all; i++) // Verifying shares for each participa TEDecryptSet decr_set(num_signed, num_all); for (size_t i = 0; i < num_signed; i++) { - encryption::element_wrapper decrypt = skeys.at(i).decrypt(cypher); + crypto::element_wrapper decrypt = skeys.at(i).decrypt(cypher); assert(pkeys.at(i).Verify(cypher, decrypt.el_)); - std::shared_ptr decr_ptr = std::make_shared(decrypt); + std::shared_ptr decr_ptr = std::make_shared(decrypt); decr_set.addDecrypt(skeys.at(i).getSignerIndex(), decr_ptr); } diff --git a/docs/using-threshold-encryption.md b/docs/using-threshold-encryption.md index ab6c0934..90ac6d38 100644 --- a/docs/using-threshold-encryption.md +++ b/docs/using-threshold-encryption.md @@ -44,7 +44,7 @@ where i is an index of a participant. ```cpp TEPublic publicKey = *keys.second; -encryption::Ciphertext cipher = publicKey.encrypt(message_ptr); +crypto::Ciphertext cipher = publicKey.encrypt(message_ptr); ``` Where message_ptr is shared_ptr to string, cipher is encrypted message. @@ -52,7 +52,7 @@ encryption::Ciphertext cipher = publicKey.encrypt(message_ptr); 5. Get pieces of decrypted message with private key of each participant and cipher got in step 4. Verify every piece with public key of corresponding participant. ```cpp -encryption::element_wrapper piece = privateKey.decrypt(cipher); +crypto::element_wrapper piece = privateKey.decrypt(cipher); assert ( publicKeyShare.Verify(cipher, piece) ) ; ``` diff --git a/python/dkgpython.cpp b/python/dkgpython.cpp index 4aa0e726..e950f9ac 100644 --- a/python/dkgpython.cpp +++ b/python/dkgpython.cpp @@ -68,7 +68,7 @@ static PyObject* MakePythonBool( bool value ) { ///////////////////////// struct PyDkgObject { - PyObject_HEAD signatures::Dkg* pDKG; + PyObject_HEAD crypto::Dkg* pDKG; }; static int PyDkgObject_init( struct PyDkgObject* self, PyObject* args, PyObject* kwds ) { @@ -77,7 +77,7 @@ static int PyDkgObject_init( struct PyDkgObject* self, PyObject* args, PyObject* return -1; } - self->pDKG = new signatures::Dkg( t, n ); + self->pDKG = new crypto::Dkg( t, n ); return 0; } diff --git a/test/dkg_attack.cpp b/test/dkg_attack.cpp index 2c62eb41..fb83f5c4 100644 --- a/test/dkg_attack.cpp +++ b/test/dkg_attack.cpp @@ -224,7 +224,7 @@ int main() { // // Construct the final signature // std::shared_ptr< BLSSignature > signature = signature_share_set.merge(); - // std::cout << "isG2:" << signatures::Dkg::isG2( *( common_public_key.getPublicKey() ) ); + // std::cout << "isG2:" << crypto::Dkg::isG2( *( common_public_key.getPublicKey() ) ); // // This assertion will fail // assert( common_public_key.VerifySig( hash_ptr, signature, num_signed, num_all ) ); diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 9dc60bd1..d6032aad 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - signatures::Dkg dkg_te( num_signed, num_all ); + crypto::Dkg dkg_te( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); @@ -78,7 +78,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { TEPublicKey common_public( common_private, num_signed, num_all ); auto msg_ptr = std::make_shared< std::string >( message ); - encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); + crypto::Ciphertext cypher = common_public.encrypt( msg_ptr ); std::vector< libff::alt_bn128_Fr > skeys = dkg_te.SecretKeyContribution( poly ); std::vector< TEPrivateKeyShare > skey_shares; @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { std::string message_decrypted = decr_set.merge( cypher ); BOOST_REQUIRE( message == message_decrypted ); - encryption::Ciphertext bad_cypher = cypher; // corrupt V in cypher + crypto::Ciphertext bad_cypher = cypher; // corrupt V in cypher std::get< 1 >( bad_cypher ) = spoilMessage( std::get< 1 >( cypher ) ); bool is_exception_caught = false; try { @@ -177,7 +177,7 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - signatures::Dkg dkg_te( num_signed, num_all ); + crypto::Dkg dkg_te( num_signed, num_all ); std::string message; size_t msg_length = 64; @@ -191,7 +191,7 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { auto msg_ptr = std::make_shared< std::string >( message ); - encryption::Ciphertext cypher = keys.second->encrypt( msg_ptr ); + crypto::Ciphertext cypher = keys.second->encrypt( msg_ptr ); std::vector< TEPublicKeyShare > public_key_shares; for ( size_t i = 0; i < num_all; i++ ) { @@ -269,7 +269,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { for ( size_t i = 0; i < num_all; i++ ) { DKGTEWrapper dkg_wrap( num_signed, num_all ); - signatures::Dkg dkg_te( num_signed, num_all ); + crypto::Dkg dkg_te( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); auto shared_poly = std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ); dkg_wrap.setDKGSecret( shared_poly ); @@ -332,7 +332,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { element_clear(value); } - TEPublicKey common_public(encryption::element_wrapper(public_key), num_signed, num_all); + TEPublicKey common_public(crypto::element_wrapper(public_key), num_signed, num_all); element_clear(public_key);*/ TEPublicKey common_public = DKGTEWrapper::CreateTEPublicKey( @@ -347,7 +347,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { } auto msg_ptr = std::make_shared< std::string >( message ); - encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); + crypto::Ciphertext cypher = common_public.encrypt( msg_ptr ); for ( size_t i = 0; i < num_all - num_signed; ++i ) { size_t ind4del = rand_gen() % secret_shares_all.size(); @@ -441,7 +441,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); - encryption::Ciphertext cypher; + crypto::Ciphertext cypher; std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "tra-la-la"; std::get< 2 >( cypher ) = W; @@ -464,7 +464,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); - encryption::Ciphertext cypher; + crypto::Ciphertext cypher; std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "tra-la-la"; std::get< 2 >( cypher ) = W; @@ -485,7 +485,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); - encryption::Ciphertext cypher; + crypto::Ciphertext cypher; std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; @@ -679,7 +679,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); - encryption::Ciphertext cypher; + crypto::Ciphertext cypher; std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; @@ -702,7 +702,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); - encryption::Ciphertext cypher; + crypto::Ciphertext cypher; std::get< 0 >( cypher ) = U; std::get< 1 >( cypher ) = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; diff --git a/test/test_bls.cpp b/test/test_bls.cpp index ffefc829..423e45ad 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -111,13 +111,13 @@ BOOST_AUTO_TEST_CASE( libBls ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - signatures::Dkg dkg_obj = signatures::Dkg( num_signed, num_all ); + crypto::Dkg dkg_obj = crypto::Dkg( num_signed, num_all ); const std::vector< libff::alt_bn128_Fr > pol = dkg_obj.GeneratePolynomial(); std::vector< libff::alt_bn128_Fr > skeys = dkg_obj.SecretKeyContribution( pol ); std::vector< libff::alt_bn128_G1 > signatures( num_signed ); - signatures::Bls obj = signatures::Bls( num_signed, num_all ); + crypto::Bls obj = crypto::Bls( num_signed, num_all ); for ( size_t i = 0; i < 10; ++i ) { std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr = @@ -140,7 +140,7 @@ BOOST_AUTO_TEST_CASE( libBls ) { BOOST_REQUIRE( obj.Verification( hash_ptr, signatures.at( i ), pkey ) ); BOOST_REQUIRE_THROW( obj.Verification( hash_ptr, SpoilSignature( signatures.at( i ) ), pkey ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } std::vector< libff::alt_bn128_Fr > lagrange_coeffs = @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE( libBls ) { BOOST_REQUIRE( obj.Verification( hash_ptr, signature, recovered_keys.second ) ); BOOST_REQUIRE_THROW( obj.Verification( hash_ptr, SpoilSignature( signature ), recovered_keys.second ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } } @@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE( libBlsAPI ) { BOOST_REQUIRE_THROW( BLSSigShare( bad_sig, hint, participants.at( i ), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } BOOST_REQUIRE( sigSet.getTotalSigSharesCount() == num_signed ); @@ -222,7 +222,7 @@ BOOST_AUTO_TEST_CASE( libBlsAPI ) { BOOST_REQUIRE_THROW( common_pkey.VerifySig( hash_ptr, std::make_shared< BLSSignature >( bad_sign ), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > pkeys_map1; for ( size_t i = 0; i < num_signed; ++i ) { @@ -419,7 +419,7 @@ BOOST_AUTO_TEST_CASE( private_keys_equality ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 1; - signatures::Dkg dkg_obj = signatures::Dkg( num_signed, num_all ); + crypto::Dkg dkg_obj = crypto::Dkg( num_signed, num_all ); const std::vector< libff::alt_bn128_Fr > pol = dkg_obj.GeneratePolynomial(); std::vector< libff::alt_bn128_Fr > skeys = dkg_obj.SecretKeyContribution( pol ); @@ -443,7 +443,7 @@ BOOST_AUTO_TEST_CASE( public_keys_equality ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 1; - signatures::Dkg dkg_obj = signatures::Dkg( num_signed, num_all ); + crypto::Dkg dkg_obj = crypto::Dkg( num_signed, num_all ); const std::vector< libff::alt_bn128_Fr > pol = dkg_obj.GeneratePolynomial(); std::vector< libff::alt_bn128_Fr > skeys = dkg_obj.SecretKeyContribution( pol ); libff::alt_bn128_G2 common_pkey = dkg_obj.GetPublicKeyFromSecretKey( pol.at( 0 ) ); @@ -593,25 +593,25 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { { BOOST_REQUIRE_THROW( BLSPrivateKey pkey( std::make_shared< std::string >( "" ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPrivateKey skey( std::make_shared< std::string >( "0" ), num_signed, num_all ), - signatures::Bls::ZeroSecretKey ); + crypto::Bls::ZeroSecretKey ); } { BOOST_REQUIRE_THROW( - BLSPrivateKey skey( nullptr, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSPrivateKey skey( nullptr, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPrivateKey skey( nullptr, std::make_shared< std::vector< size_t > >( participants ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -619,35 +619,35 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BLSPrivateKey skey( BLSPrivateKeyShare::generateSampleKeys( num_signed, num_all )->first, NULL, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSPrivateKeyShare skey( "", num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSPrivateKeyShare skey( "", num_signed, num_all ), crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSPrivateKeyShare skey( "0", num_signed, num_all ), signatures::Bls::ZeroSecretKey ); + BLSPrivateKeyShare skey( "0", num_signed, num_all ), crypto::Bls::ZeroSecretKey ); } { BOOST_REQUIRE_THROW( BLSPrivateKeyShare skey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - signatures::Bls::ZeroSecretKey ); + crypto::Bls::ZeroSecretKey ); } { BLSPrivateKeyShare skey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); BOOST_REQUIRE_THROW( skey.sign( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), 0 ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BLSPrivateKeyShare skey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); - BOOST_REQUIRE_THROW( skey.sign( NULL, 1 ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( skey.sign( NULL, 1 ), crypto::Bls::IncorrectInput ); } { @@ -655,35 +655,35 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( skey.signWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), 0 ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BLSPrivateKeyShare skey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); - BOOST_REQUIRE_THROW( skey.signWithHelper( NULL, 1 ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( skey.signWithHelper( NULL, 1 ), crypto::Bls::IncorrectInput ); } { const std::shared_ptr< std::vector< std::string > > null_vect = nullptr; BOOST_REQUIRE_THROW( - BLSPublicKey pkey( null_vect, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSPublicKey pkey( null_vect, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { const std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > null_map = nullptr; BOOST_REQUIRE_THROW( - BLSPublicKey pkey( null_map, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSPublicKey pkey( null_map, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_G2::zero(), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { @@ -695,7 +695,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( pkey.VerifySigWithHelper( nullptr, std::make_shared< BLSSignature >( rand_sig ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -704,18 +704,18 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { pkey.VerifySigWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), nullptr, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - signatures::Bls::ZeroSecretKey ); + crypto::Bls::ZeroSecretKey ); } { BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( nullptr, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -726,7 +726,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { 1, num_signed, num_all ); BOOST_REQUIRE_THROW( pkey.VerifySigWithHelper( nullptr, std::make_shared< BLSSigShare >( rand_sig ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -735,18 +735,18 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { pkey.VerifySigWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), nullptr, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSSignature( nullptr, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSSignature( nullptr, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { std::string hint = "123:1"; BOOST_REQUIRE_THROW( - BLSSignature( nullptr, hint, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSSignature( nullptr, hint, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { @@ -754,14 +754,14 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< libff::alt_bn128_G1 >( libff::alt_bn128_G1::random_element() ), empty_hint, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { std::string short_sig = "1:1:1:1"; BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( short_sig ), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { @@ -774,7 +774,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { @@ -787,7 +787,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -801,7 +801,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { @@ -815,7 +815,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { long_sig[25] = 'a'; BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -823,23 +823,23 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { std::string hint = "123:1"; BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< libff::alt_bn128_G1 >( zero_sig ), hint, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, 1, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSSigShare( nullptr, 1, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, 0, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSSigShare( nullptr, 0, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { std::string hint = "123:1"; BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, hint, 0, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSSigShare( nullptr, hint, 0, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { @@ -847,14 +847,14 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< libff::alt_bn128_G1 >( libff::alt_bn128_G1::random_element() ), empty_hint, 1, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { std::string short_sig = "1:1:1:1"; BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( short_sig ), 1, num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } @@ -869,7 +869,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { @@ -882,7 +882,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -897,7 +897,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); } { @@ -911,13 +911,13 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { long_sig[25] = 'a'; BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.addSigShare( nullptr ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( sig_set.addSigShare( nullptr ), crypto::Bls::IncorrectInput ); } { @@ -929,7 +929,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BLSSigShareSet sig_set( num_signed, num_all ); sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare1 ) ); BOOST_REQUIRE_THROW( sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare2 ) ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { @@ -941,12 +941,12 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare1 ) ); sig_set.merge(); BOOST_REQUIRE_THROW( sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare1 ) ), - signatures::Bls::IncorrectInput ); + crypto::Bls::IncorrectInput ); } { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.merge(), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( sig_set.merge(), crypto::Bls::IncorrectInput ); } { @@ -956,7 +956,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.getSigShareByIndex( 0 ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( sig_set.getSigShareByIndex( 0 ), crypto::Bls::IncorrectInput ); } std::cerr << "EXCEPTIONS TEST FINISHED" << std::endl; diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 0a36c65a..742d06f1 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -45,13 +45,13 @@ BOOST_AUTO_TEST_CASE( zeroSecretKey ) { libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::zero(); - signatures::Bls obj = signatures::Bls( 1, 1 ); + crypto::Bls obj = crypto::Bls( 1, 1 ); std::string message = "Waiting for exception"; libff::alt_bn128_G1 hash = obj.Hashing( message ); - BOOST_REQUIRE_THROW( obj.Signing( hash, secret_key ), signatures::Bls::ZeroSecretKey ); + BOOST_REQUIRE_THROW( obj.Signing( hash, secret_key ), crypto::Bls::ZeroSecretKey ); std::cout << "DONE\n"; } @@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE( zeroSecretKey ) { BOOST_AUTO_TEST_CASE( singleBlsrun ) { std::cout << "Testing singleBlsrun\n"; - signatures::Bls obj = signatures::Bls( 1, 1 ); + crypto::Bls obj = crypto::Bls( 1, 1 ); std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > keys = obj.KeyGeneration(); @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE( singleBlsrun ) { BOOST_AUTO_TEST_CASE( SimillarHashes ) { std::cout << "Testing SimillarHashes\n"; - signatures::Bls obj = signatures::Bls( 1, 1 ); + crypto::Bls obj = crypto::Bls( 1, 1 ); const char message[5] = {104, 101, 108, 108, 111}; @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE( SimillarHashes ) { BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { std::cout << "Testing BlsThresholdSignatures\n"; - signatures::Bls obj = signatures::Bls( 2, 2 ); + crypto::Bls obj = crypto::Bls( 2, 2 ); libff::alt_bn128_Fr fst_secret = libff::alt_bn128_Fr( "4160780231445160889237664391382223604184857153814275770598791864649971919844" ); @@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { std::cout << "Testing BlsThresholdSignaturesFalse\n"; - signatures::Bls obj = signatures::Bls( 2, 2 ); + crypto::Bls obj = crypto::Bls( 2, 2 ); // the last digit was changed libff::alt_bn128_Fr fst_secret = libff::alt_bn128_Fr( @@ -208,7 +208,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesReal ) { std::cout << "Testing BlsThresholdSignaturesReal\n"; - signatures::Bls obj = signatures::Bls( 11, 16 ); + crypto::Bls obj = crypto::Bls( 11, 16 ); // creating a polynomial std::vector< libff::alt_bn128_Fr > coeffs( 11 ); @@ -274,7 +274,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesReal ) { BOOST_AUTO_TEST_CASE( simillarSignatures ) { std::cout << "Testing simillarSignatures\n"; - signatures::Bls obj = signatures::Bls( 11, 16 ); + crypto::Bls obj = crypto::Bls( 11, 16 ); // creating a polynomial std::vector< libff::alt_bn128_Fr > coeffs( 11 ); @@ -382,7 +382,7 @@ running this test takes more than 2 days(48 hours) for this moment BOOST_AUTO_TEST_CASE(differentMessages) { std::cout << "Testing different message length\n"; - signatures::Bls obj = signatures::Bls(11, 16); + crypto::Bls obj = crypto::Bls(11, 16); std::vector testing_nodes(11); // first group - nodes from 1 up to 12 for (size_t i = 0; i < 11; ++i) { @@ -491,7 +491,7 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { nodes.insert( indexes[i] ); } - signatures::Bls obj = signatures::Bls( deg + 1, deg + 1 ); + crypto::Bls obj = crypto::Bls( deg + 1, deg + 1 ); auto coeffs = ThresholdUtils::LagrangeCoeffs( indexes, deg + 1 ); std::vector< libff::alt_bn128_Fr > values( deg + 1 ); @@ -514,8 +514,8 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { std::default_random_engine rand_gen( ( unsigned int ) time( 0 ) ); size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 2; - signatures::Bls obj( num_signed, num_all ); - signatures::Bls obj_2_2( 2, 2 ); + crypto::Bls obj( num_signed, num_all ); + crypto::Bls obj_2_2( 2, 2 ); libff::alt_bn128_G1 sign; sign.X = libff::alt_bn128_Fq( "123" ); @@ -524,21 +524,21 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { BOOST_REQUIRE_THROW( obj.Verification( "bla-bla-bla", sign, libff::alt_bn128_G2::random_element() ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); libff::alt_bn128_G2 pkey = libff::alt_bn128_G2::random_element(); pkey.X.c1 = 123; BOOST_REQUIRE_THROW( obj.Verification( "bla-bla-bla", libff::alt_bn128_G1::random_element(), pkey ), - signatures::Bls::IsNotWellFormed ); + crypto::Bls::IsNotWellFormed ); std::vector< libff::alt_bn128_Fr > coeffs; coeffs.push_back( libff::alt_bn128_Fr::random_element() ); std::vector< libff::alt_bn128_Fr > shares; shares.push_back( libff::alt_bn128_Fr::random_element() ); - BOOST_REQUIRE_THROW( obj.KeysRecover( coeffs, shares ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( obj.KeysRecover( coeffs, shares ), crypto::Bls::IncorrectInput ); coeffs.clear(); shares.clear(); @@ -549,7 +549,7 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { shares.push_back( libff::alt_bn128_Fr::random_element() ); shares.push_back( libff::alt_bn128_Fr::zero() ); - BOOST_REQUIRE_THROW( obj_2_2.KeysRecover( coeffs, shares ), signatures::Bls::ZeroSecretKey ); + BOOST_REQUIRE_THROW( obj_2_2.KeysRecover( coeffs, shares ), crypto::Bls::ZeroSecretKey ); coeffs.clear(); coeffs.push_back( libff::alt_bn128_Fr::random_element() ); @@ -558,7 +558,7 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { sig_shares.push_back( libff::alt_bn128_G1::random_element() ); BOOST_REQUIRE_THROW( - obj.SignatureRecover( sig_shares, coeffs ), signatures::Bls::IncorrectInput ); + obj.SignatureRecover( sig_shares, coeffs ), crypto::Bls::IncorrectInput ); coeffs.clear(); sig_shares.clear(); @@ -572,18 +572,18 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { sig_shares.push_back( g1_spoiled ); BOOST_REQUIRE_THROW( - obj_2_2.SignatureRecover( sig_shares, coeffs ), signatures::Bls::IsNotWellFormed ); + obj_2_2.SignatureRecover( sig_shares, coeffs ), crypto::Bls::IsNotWellFormed ); std::vector< size_t > idx = {1}; BOOST_REQUIRE_THROW( ThresholdUtils::LagrangeCoeffs( idx, num_signed ), - std::runtime_error ); // signatures::Bls::IncorrectInput + std::runtime_error ); // crypto::Bls::IncorrectInput idx.clear(); idx = {1, 1}; BOOST_REQUIRE_THROW( ThresholdUtils::LagrangeCoeffs( idx, 2 ), - std::runtime_error ); // signatures::Bls::IncorrectInput + std::runtime_error ); // crypto::Bls::IncorrectInput std::cerr << "Exceptions test passed" << std::endl; } diff --git a/test/unit_tests_dkg.cpp b/test/unit_tests_dkg.cpp index 1459e241..2389dbc9 100644 --- a/test/unit_tests_dkg.cpp +++ b/test/unit_tests_dkg.cpp @@ -44,7 +44,7 @@ BOOST_AUTO_TEST_SUITE( DkgAlgorithm ) BOOST_AUTO_TEST_CASE( PolynomialValue ) { - signatures::Dkg obj = signatures::Dkg( 3, 4 ); + crypto::Dkg obj = crypto::Dkg( 3, 4 ); std::vector< libff::alt_bn128_Fr > polynomial = { libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" )}; @@ -68,7 +68,7 @@ BOOST_AUTO_TEST_CASE( PolynomialValue ) { } BOOST_AUTO_TEST_CASE( verification ) { - signatures::Dkg obj = signatures::Dkg( 2, 2 ); + crypto::Dkg obj = crypto::Dkg( 2, 2 ); auto polynomial_fst = obj.GeneratePolynomial(); auto polynomial_snd = obj.GeneratePolynomial(); @@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE( PolySize ) { for ( size_t i = 0; i < 100; i++ ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - signatures::Dkg obj = signatures::Dkg( num_signed, num_all ); + crypto::Dkg obj = crypto::Dkg( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > pol = obj.GeneratePolynomial(); BOOST_REQUIRE( pol.size() == num_signed ); BOOST_REQUIRE( pol.at( num_signed - 1 ) != libff::alt_bn128_Fr::zero() ); @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE( PolySize ) { BOOST_AUTO_TEST_CASE( ZeroSecret ) { for ( size_t i = 0; i < 100; i++ ) { - signatures::Dkg dkg_obj = signatures::Dkg( 2, 2 ); + crypto::Dkg dkg_obj = crypto::Dkg( 2, 2 ); libff::alt_bn128_Fr num1 = libff::alt_bn128_Fr::random_element(); libff::alt_bn128_Fr num2 = -num1; @@ -181,7 +181,7 @@ BOOST_AUTO_TEST_CASE( Verification2 ) { for ( size_t i = 0; i < 100; i++ ) { size_t num_all = rand_gen() % 16 + 1; size_t num_signed = rand_gen() % num_all + 1; - signatures::Dkg obj = signatures::Dkg( num_signed, num_all ); + crypto::Dkg obj = crypto::Dkg( num_signed, num_all ); BOOST_REQUIRE( obj.GetN() == num_all ); BOOST_REQUIRE( obj.GetT() == num_signed ); diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 6ab91a29..5693502d 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -32,7 +32,7 @@ BOOST_AUTO_TEST_SUITE( ThresholdEncryption ) BOOST_AUTO_TEST_CASE( SimpleEncryption ) { - encryption::TE te_instance = encryption::TE( 1, 1 ); + crypto::TE te_instance = crypto::TE( 1, 1 ); std::string message = "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 @@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE( SimpleEncryption ) { } BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { - encryption::TE obj = encryption::TE( 11, 16 ); + crypto::TE obj = crypto::TE( 11, 16 ); std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { @@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { } BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { - encryption::TE obj = encryption::TE( 11, 16 ); + crypto::TE obj = crypto::TE( 11, 16 ); std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { @@ -176,7 +176,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { } BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { - encryption::TE obj = encryption::TE( 11, 16 ); + crypto::TE obj = crypto::TE( 11, 16 ); std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { @@ -239,7 +239,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { } BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { - encryption::TE obj = encryption::TE( 11, 16 ); + crypto::TE obj = crypto::TE( 11, 16 ); std::vector< libff::alt_bn128_Fr > coeffs( 11 ); for ( auto& elem : coeffs ) { @@ -312,7 +312,7 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { bool is_exception_caught = false; try { - encryption::TE obj( num_signed, num_all ); + crypto::TE obj( num_signed, num_all ); std::vector< size_t > vect; for ( size_t i = 0; i < num_signed - 1; i++ ) vect.push_back( i + 1 ); @@ -324,7 +324,7 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { is_exception_caught = false; try { - encryption::TE obj( num_signed, num_all ); + crypto::TE obj( num_signed, num_all ); std::vector< size_t > vect; for ( size_t i = 0; i < num_signed; i++ ) { vect.push_back( i + 1 ); diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index 29927e6a..9e83fadc 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -54,7 +54,7 @@ void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_ decrypts[_signerIndex] = _el; } -std::string TEDecryptSet::merge( const encryption::Ciphertext& cyphertext ) { +std::string TEDecryptSet::merge( const crypto::Ciphertext& cyphertext ) { ThresholdUtils::checkCypher( cyphertext ); was_merged = true; @@ -63,7 +63,7 @@ std::string TEDecryptSet::merge( const encryption::Ciphertext& cyphertext ) { throw std::runtime_error( "Not enough elements to decrypt message" ); } - encryption::TE te( requiredSigners, totalSigners ); + crypto::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 ); diff --git a/threshold_encryption/TEDecryptSet.h b/threshold_encryption/TEDecryptSet.h index fd08eab5..382d0139 100644 --- a/threshold_encryption/TEDecryptSet.h +++ b/threshold_encryption/TEDecryptSet.h @@ -41,7 +41,7 @@ class TEDecryptSet { void addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el ); - std::string merge( const encryption::Ciphertext& ciphertext ); + std::string merge( const crypto::Ciphertext& ciphertext ); }; diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index bf6533ec..44e63cba 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -64,10 +64,10 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s } } -libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( encryption::Ciphertext& cipher ) { +libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( crypto::Ciphertext& cipher ) { ThresholdUtils::checkCypher( cipher ); - encryption::TE te( requiredSigners, totalSigners ); + crypto::TE te( requiredSigners, totalSigners ); libff::alt_bn128_G2 decryption_share = te.getDecryptionShare( cipher, privateKey ); @@ -93,7 +93,7 @@ libff::alt_bn128_Fr TEPrivateKeyShare::getPrivateKey() const { std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > TEPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) { - signatures::Dkg dkg_te( _requiredSigners, _totalSigners ); + crypto::Dkg dkg_te( _requiredSigners, _totalSigners ); std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); diff --git a/threshold_encryption/TEPrivateKeyShare.h b/threshold_encryption/TEPrivateKeyShare.h index 4b472b61..21c40ab2 100644 --- a/threshold_encryption/TEPrivateKeyShare.h +++ b/threshold_encryption/TEPrivateKeyShare.h @@ -42,7 +42,7 @@ class TEPrivateKeyShare { TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ); - libff::alt_bn128_G2 getDecryptionShare( encryption::Ciphertext& cipher ); + libff::alt_bn128_G2 getDecryptionShare( crypto::Ciphertext& cipher ); static std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 5dab8883..48f256af 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -84,8 +84,8 @@ TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, si } } -encryption::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr ) { - encryption::TE te( requiredSigners, totalSigners ); +crypto::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr ) { + crypto::TE te( requiredSigners, totalSigners ); if ( mes_ptr == nullptr ) { throw std::runtime_error( "Message is null" ); @@ -95,7 +95,7 @@ encryption::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ throw std::runtime_error( "Message length is not equal to 64" ); } - encryption::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); + crypto::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); ThresholdUtils::checkCypher( cypher ); libff::alt_bn128_G2 U = std::get< 0 >( cypher ); diff --git a/threshold_encryption/TEPublicKey.h b/threshold_encryption/TEPublicKey.h index 50dac6a2..9d840abb 100644 --- a/threshold_encryption/TEPublicKey.h +++ b/threshold_encryption/TEPublicKey.h @@ -45,7 +45,7 @@ class TEPublicKey { std::shared_ptr< std::vector< std::string > > toString(); - encryption::Ciphertext encrypt( std::shared_ptr< std::string > message ); + crypto::Ciphertext encrypt( std::shared_ptr< std::string > message ); libff::alt_bn128_G2 getPublicKey() const; }; diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index fd34edca..e9e9c91d 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -71,13 +71,13 @@ TEPublicKeyShare::TEPublicKeyShare( } bool TEPublicKeyShare::Verify( - const encryption::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { + const crypto::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { ThresholdUtils::checkCypher( cyphertext ); if ( decryptionShare.is_zero() ) { throw std::runtime_error( "zero decrypt" ); } - encryption::TE te( requiredSigners, totalSigners ); + crypto::TE te( requiredSigners, totalSigners ); return te.Verify( cyphertext, decryptionShare, PublicKey ); } diff --git a/threshold_encryption/TEPublicKeyShare.h b/threshold_encryption/TEPublicKeyShare.h index b91e346a..0f920346 100644 --- a/threshold_encryption/TEPublicKeyShare.h +++ b/threshold_encryption/TEPublicKeyShare.h @@ -42,7 +42,7 @@ class TEPublicKeyShare { TEPublicKeyShare( TEPrivateKeyShare _p_key, size_t _requiredSigners, size_t _totalSigners ); - bool Verify( const encryption::Ciphertext& ciphertext, const libff::alt_bn128_G2& decrypted ); + bool Verify( const crypto::Ciphertext& ciphertext, const libff::alt_bn128_G2& decrypted ); std::shared_ptr< std::vector< std::string > > toString(); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index fddef94f..7eb83490 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -29,7 +29,7 @@ #include #include -namespace encryption { +namespace crypto { TE::TE( const size_t t, const size_t n ) : t_( t ), n_( n ) { libff::init_alt_bn128_params(); diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index 6e698656..94bb7f58 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -32,7 +32,7 @@ along with libBLS. If not, see . #include -namespace encryption { +namespace crypto { typedef std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 > Ciphertext; diff --git a/tools/bls_glue.cpp b/tools/bls_glue.cpp index ce20a253..70151e7f 100644 --- a/tools/bls_glue.cpp +++ b/tools/bls_glue.cpp @@ -38,7 +38,7 @@ static bool g_b_verbose_mode = false; void RecoverSignature( const size_t t, const size_t n, const std::vector< std::string >& input, std::ostream& outfile ) { - signatures::Bls bls_instance = signatures::Bls( t, n ); + crypto::Bls bls_instance = crypto::Bls( t, n ); std::vector< size_t > idx( t ); std::vector< libff::alt_bn128_G1 > signature_shares( t ); diff --git a/tools/dkg_glue.cpp b/tools/dkg_glue.cpp index dd5b65b1..b9826ec1 100644 --- a/tools/dkg_glue.cpp +++ b/tools/dkg_glue.cpp @@ -38,7 +38,7 @@ static bool g_b_verbose_mode = false; void GenerateSecretKeys( const size_t t, const size_t n, const std::vector< std::string >& input ) { - signatures::Dkg dkg_instance = signatures::Dkg( t, n ); + crypto::Dkg dkg_instance = crypto::Dkg( t, n ); std::vector< std::vector< libff::alt_bn128_G2 > > verification_vector( n ); std::vector< std::vector< libff::alt_bn128_Fr > > secret_key_contribution( n ); diff --git a/tools/dkg_key_gen.cpp b/tools/dkg_key_gen.cpp index 45704215..46daec31 100644 --- a/tools/dkg_key_gen.cpp +++ b/tools/dkg_key_gen.cpp @@ -68,7 +68,7 @@ void CommonPkeyToJson( std::shared_ptr< BLSPublicKey > common_pkey_ptr ) { static bool g_b_verbose_mode = false; void KeyGeneration( const size_t t, const size_t n, bool generate_all = true, int idx = -1 ) { - signatures::Dkg dkg_instance = signatures::Dkg( t, n ); + crypto::Dkg dkg_instance = crypto::Dkg( t, n ); if ( generate_all ) { std::vector< std::vector< libff::alt_bn128_Fr > > polynomial( n ); diff --git a/tools/generate_key_system.cpp b/tools/generate_key_system.cpp index 537328cc..d38778fb 100644 --- a/tools/generate_key_system.cpp +++ b/tools/generate_key_system.cpp @@ -39,8 +39,8 @@ static bool g_b_verbose_mode = false; void GenerateKeys( const size_t t, const size_t n, std::ostream& outfile ) { - signatures::Bls bls_instance = signatures::Bls( t, n ); - signatures::Dkg dkg_instance = signatures::Dkg( t, n ); + crypto::Bls bls_instance = crypto::Bls( t, n ); + crypto::Dkg dkg_instance = crypto::Dkg( t, n ); auto polynomial = dkg_instance.GeneratePolynomial(); diff --git a/tools/hash_g1.cpp b/tools/hash_g1.cpp index bd8158fc..96558a70 100644 --- a/tools/hash_g1.cpp +++ b/tools/hash_g1.cpp @@ -65,7 +65,7 @@ bool hex2carray( const char* _hex, uint64_t* _bin_len, uint8_t* _bin ) { void hash_g1( const size_t t, const size_t n ) { libff::inhibit_profiling_info = true; - signatures::Bls bls_instance = signatures::Bls( t, n ); + crypto::Bls bls_instance = crypto::Bls( t, n ); nlohmann::json hash_in; diff --git a/tools/sign_bls.cpp b/tools/sign_bls.cpp index 51feb1c9..046e8923 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -66,7 +66,7 @@ bool hex2carray( const char* _hex, uint64_t* _bin_len, uint8_t* _bin ) { void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream& outfile, const std::string& key, bool sign_all = true, int idx = -1 ) { - signatures::Bls bls_instance = signatures::Bls( t, n ); + crypto::Bls bls_instance = crypto::Bls( t, n ); std::vector< uint8_t > message_data; uint8_t n_byte; diff --git a/tools/verify_bls.cpp b/tools/verify_bls.cpp index 04b1daf5..88c7c383 100644 --- a/tools/verify_bls.cpp +++ b/tools/verify_bls.cpp @@ -73,7 +73,7 @@ bool hex2carray( const char* _hex, uint64_t* _bin_len, uint8_t* _bin ) { void Verify( const size_t t, const size_t n, std::istream& sign_file, int j = -1 ) { libff::inhibit_profiling_info = true; - signatures::Bls bls_instance = signatures::Bls( t, n ); + crypto::Bls bls_instance = crypto::Bls( t, n ); nlohmann::json signature; sign_file >> signature; From 38e77e5284f2f89e0938751eff48e97d48d4ecd1 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 13:19:00 +0300 Subject: [PATCH 15/40] SKALE-4409 format code --- bls/bls.cpp | 2 +- bls/bls.h | 4 ++-- dkg/dkg.cpp | 2 +- dkg/dkg.h | 2 +- test/test_bls.cpp | 4 ++-- test/unit_tests_bls.cpp | 3 +-- threshold_encryption/threshold_encryption.cpp | 2 +- threshold_encryption/threshold_encryption.h | 2 +- 8 files changed, 10 insertions(+), 11 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index 7cd86e91..0cca00b2 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -255,4 +255,4 @@ libff::alt_bn128_G1 Bls::SignatureRecover( const std::vector< libff::alt_bn128_G return sign; // first element is hash of a receiving message } -} // namespace signatures +} // namespace crypto diff --git a/bls/bls.h b/bls/bls.h index 6546c930..45df4e3d 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -111,12 +111,12 @@ class Bls { const size_t n_ = 0; }; -} // namespace signatures +} // namespace crypto #define CHECK( _EXPRESSION_ ) \ if ( !( _EXPRESSION_ ) ) { \ auto __msg__ = std::string( "Check failed:" ) + #_EXPRESSION_ + "\n" + __FUNCTION__ + \ +" " + std::string( __FILE__ ) + ":" + std::to_string( __LINE__ ); \ - throw crypto::Bls::IncorrectInput( __msg__ ); \ + throw crypto::Bls::IncorrectInput( __msg__ ); \ } diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index 04f00f45..eabc6faf 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -139,4 +139,4 @@ bool Dkg::isG2( const libff::alt_bn128_G2& point ) { libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); } -} // namespace signatures +} // namespace crypto diff --git a/dkg/dkg.h b/dkg/dkg.h index 7d1802fd..509a8b65 100644 --- a/dkg/dkg.h +++ b/dkg/dkg.h @@ -66,4 +66,4 @@ class Dkg { const size_t n_ = 0; }; -} // namespace signatures +} // namespace crypto diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 423e45ad..7521f4f3 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -714,8 +714,8 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } { - BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( nullptr, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + BLSPublicKeyShare pkey( nullptr, num_signed, num_all ), crypto::Bls::IncorrectInput ); } { diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 742d06f1..af3da6ad 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -557,8 +557,7 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { std::vector< libff::alt_bn128_G1 > sig_shares; sig_shares.push_back( libff::alt_bn128_G1::random_element() ); - BOOST_REQUIRE_THROW( - obj.SignatureRecover( sig_shares, coeffs ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( obj.SignatureRecover( sig_shares, coeffs ), crypto::Bls::IncorrectInput ); coeffs.clear(); sig_shares.clear(); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 7eb83490..56892df6 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -240,4 +240,4 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, return message; } -} // namespace encryption +} // namespace crypto diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index 94bb7f58..12e091b1 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -65,4 +65,4 @@ class TE { const size_t n_ = 0; }; -} // namespace encryption +} // namespace crypto From 050d6c714bb280dcbd3e34f64c0c514b8fd8a14a Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 14:26:11 +0300 Subject: [PATCH 16/40] SKALE-4409 refactor tests --- test/test_TE_wrappers.cpp | 401 +++++++++++++------------------------- test/test_bls.cpp | 78 +++----- test/unit_tests_dkg.cpp | 17 +- test/unit_tests_te.cpp | 29 +-- tools/utils.cpp | 2 +- 5 files changed, 174 insertions(+), 353 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index d6032aad..1892be05 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -111,45 +111,23 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { crypto::Ciphertext bad_cypher = cypher; // corrupt V in cypher std::get< 1 >( bad_cypher ) = spoilMessage( std::get< 1 >( cypher ) ); - bool is_exception_caught = false; - try { - decr_set.merge( bad_cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - // cannot add after merge - decr_set.addDecrypt( num_signed, nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), std::runtime_error ); + + // cannot add after merge + BOOST_REQUIRE_THROW( decr_set.addDecrypt( num_signed, nullptr ), std::runtime_error ); bad_cypher = cypher; // corrupt U in cypher libff::alt_bn128_G2 rand_el = libff::alt_bn128_G2::random_element(); std::get< 0 >( bad_cypher ) = rand_el; - is_exception_caught = false; - try { - decr_set.merge( bad_cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), std::runtime_error ); bad_cypher = cypher; // corrupt W in cypher libff::alt_bn128_G1 rand_el2 = libff::alt_bn128_G1::random_element(); std::get< 2 >( bad_cypher ) = rand_el2; - is_exception_caught = false; - try { - decr_set.merge( bad_cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + + BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), std::runtime_error ); size_t ind = rand_gen() % num_signed; // corrupt random private key share @@ -377,62 +355,43 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % num_all + 1; - bool is_exception_caught = false; - try { - ThresholdUtils::checkSigners( 0, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( ThresholdUtils::checkSigners( 0, num_all ), std::runtime_error ); - is_exception_caught = false; - try { - ThresholdUtils::checkSigners( 0, 0 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( ThresholdUtils::checkSigners( 0, 0 ), std::runtime_error ); - is_exception_caught = false; // null public key share - try { - TEPublicKeyShare( nullptr, 1, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + // null public key share + BOOST_REQUIRE_THROW( + TEPublicKeyShare( nullptr, 1, num_signed, num_all ), std::runtime_error ); - is_exception_caught = false; // 1 coord of public key share is not digit - try { + { + // 1 coord of public key share is not digit std::vector< std::string > pkey_str( {"123", "abc"} ); - TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, - num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, + num_signed, num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero public key share - try { + { + // zero public key share std::vector< std::string > pkey_str( {"0", "0"} ); - TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, - num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, + num_signed, num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // one component public key share - try { + { + // one component public key share std::vector< std::string > pkey_str( {"1232450"} ); - TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, - num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, + num_signed, num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // one zero component in cypher - try { + { + // one zero component in cypher libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); TEPublicKeyShare pkey( TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); @@ -446,16 +405,11 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { std::get< 1 >( cypher ) = "tra-la-la"; std::get< 2 >( cypher ) = W; - pkey.Verify( cypher, U ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - - is_exception_caught = false; // wrong string length in cypher - try { + { + // wrong string length in cypher libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); TEPublicKeyShare pkey( @@ -469,14 +423,11 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { std::get< 1 >( cypher ) = "tra-la-la"; std::get< 2 >( cypher ) = W; - pkey.Verify( cypher, U ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero decrypted - try { + { + // zero decrypted libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); TEPublicKeyShare pkey( TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); @@ -493,138 +444,105 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G2 decrypt = libff::alt_bn128_G2::zero(); - pkey.Verify( cypher, decrypt ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.Verify( cypher, decrypt ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // null private key share - try { - TEPrivateKeyShare( nullptr, 1, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + // null private key share + BOOST_REQUIRE_THROW( + TEPrivateKeyShare( nullptr, 1, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero private key share - try { + { + // zero private key share std::string zero_str = "0"; - TEPrivateKeyShare( - std::make_shared< std::string >( zero_str ), 1, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( TEPrivateKeyShare( std::make_shared< std::string >( zero_str ), 1, + num_signed, num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero private key share - try { + { + // zero private key share libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - TEPrivateKeyShare( el, 1, num_signed, num_all ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPrivateKeyShare( el, 1, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // null public key - try { - TEPublicKey( nullptr, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + // null public key + BOOST_REQUIRE_THROW( TEPublicKey( nullptr, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero public key - try { + { + // zero public key std::vector< std::string > pkey_str( {"0", "0"} ); - TEPublicKey( - std::make_shared< std::vector< std::string > >( pkey_str ), num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPublicKey( std::make_shared< std::vector< std::string > >( pkey_str ), num_signed, + num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero public key - try { + { + // zero public key libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - TEPublicKey pkey( TEPrivateKey( el, num_signed, num_all ), num_signed, num_all ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPublicKey pkey( TEPrivateKey( el, num_signed, num_all ), num_signed, num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero public key - try { + { + // zero public key libff::alt_bn128_G2 el = libff::alt_bn128_G2::zero(); - TEPublicKey pkey( el, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( TEPublicKey pkey( el, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // null message - try { + { + // null message libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element(); TEPublicKey pkey( el, num_signed, num_all ); - pkey.encrypt( nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // message length is not 64 - try { + { + // message length is not 64 libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element(); TEPublicKey pkey( el, num_signed, num_all ); - pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // null private key - try { - TEPrivateKey( nullptr, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + // null private key + BOOST_REQUIRE_THROW( TEPrivateKey( nullptr, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero private key - try { + { + // zero private key std::string zero_str = "0"; - TEPrivateKey( std::make_shared< std::string >( zero_str ), num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + TEPrivateKey( std::make_shared< std::string >( zero_str ), num_signed, num_all ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero private key - try { + { + // zero private key libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - TEPrivateKey( el, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( TEPrivateKey( el, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( num_all + 1, num_signed ); //_requiredSigners > _totalSigners - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + //_requiredSigners > _totalSigners + BOOST_REQUIRE_THROW( + TEDecryptSet decr_set( num_all + 1, num_signed ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( num_signed, num_all ); // same indices in decrypt set + { + // same indices in decrypt set + TEDecryptSet decr_set( num_signed, num_all ); libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element(); auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); @@ -633,43 +551,32 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { auto el_ptr2 = std::make_shared< libff::alt_bn128_G2 >( el2 ); decr_set.addDecrypt( 1, el_ptr1 ); - decr_set.addDecrypt( 1, el_ptr2 ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( decr_set.addDecrypt( 1, el_ptr2 ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( num_signed, num_all ); // zero element in decrypt set + { + // zero element in decrypt set + TEDecryptSet decr_set( num_signed, num_all ); libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero(); auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); - decr_set.addDecrypt( 1, el_ptr1 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( decr_set.addDecrypt( 1, el_ptr1 ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( num_signed, num_all ); // null element in decrypt set + { + // null element in decrypt set + TEDecryptSet decr_set( num_signed, num_all ); libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero(); auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); el_ptr1 = nullptr; - decr_set.addDecrypt( 1, el_ptr1 ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( decr_set.addDecrypt( 1, el_ptr1 ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( num_signed, num_all ); // not enough elements in decrypt set + { + // not enough elements in decrypt set + TEDecryptSet decr_set( num_signed, num_all ); libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element(); auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); @@ -685,15 +592,12 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; std::get< 2 >( cypher ) = W; - decr_set.merge( cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( decr_set.merge( cypher ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( 1, 1 ); // cannot combine shares + { + // cannot combine shares + TEDecryptSet decr_set( 1, 1 ); libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element(); auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); decr_set.addDecrypt( 1, el_ptr1 ); @@ -708,11 +612,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; std::get< 2 >( cypher ) = W; - decr_set.merge( cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( decr_set.merge( cypher ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); } } @@ -720,33 +621,25 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % num_all + 1; - bool is_exception_caught = false; - try { + { // zero share DKGTEWrapper dkg_te( num_signed, num_all ); libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { // null verification vector DKGTEWrapper dkg_te( num_signed, num_all ); libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); - dkg_te.VerifyDKGShare( 1, el, nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.VerifyDKGShare( 1, el, nullptr ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); @@ -754,77 +647,53 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { std::vector< libff::alt_bn128_G2 > pub_shares = *dkg_te.createDKGPublicShares(); pub_shares.erase( pub_shares.begin() ); - dkg_te.VerifyDKGShare( - 1, el, std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_te.VerifyDKGShare( + 1, el, std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares = dkg_te.createDKGSecretShares(); shares = nullptr; - dkg_te.setDKGSecret( shares ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.setDKGSecret( shares ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares = dkg_te.createDKGSecretShares(); shares->erase( shares->begin() + shares->size() - 2 ); shares->shrink_to_fit(); - dkg_te.setDKGSecret( shares ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.setDKGSecret( shares ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); - dkg_te.CreateTEPrivateKeyShare( 1, nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.CreateTEPrivateKeyShare( 1, nullptr ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); auto wrong_size_vector = std::make_shared< std::vector< libff::alt_bn128_Fr > >(); wrong_size_vector->resize( num_signed - 1 ); - dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares; - dkg_te.setDKGSecret( shares ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.setDKGSecret( shares ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); - dkg_te.CreateTEPublicKey( nullptr, num_signed, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_te.CreateTEPublicKey( nullptr, num_signed, num_all ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 7521f4f3..ff68435d 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -966,83 +966,59 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 1; - bool is_exception_caught = false; // zero share - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); std::vector< libff::alt_bn128_G2 > vect = {libff::alt_bn128_G2::random_element()}; - dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), - std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), + std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero share - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), nullptr ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // null verification vector - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), nullptr ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // wrong vector size - try { + { DKGBLSWrapper dkg_wrap( num_signed + 1, num_all + 1 ); std::vector< libff::alt_bn128_G2 > vect = {libff::alt_bn128_G2::random_element()}; - dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), - std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), + std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // set null poly - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - dkg_wrap.setDKGSecret( nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_wrap.setDKGSecret( nullptr ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // set wrong size poly - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > poly; - dkg_wrap.setDKGSecret( std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ) ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_wrap.setDKGSecret( std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ) ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // set null secret shares - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - dkg_wrap.CreateBLSPrivateKeyShare( nullptr ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_wrap.CreateBLSPrivateKeyShare( nullptr ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // set wrong size secret shares - try { + { DKGBLSWrapper dkg_wrap( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > shares; - dkg_wrap.CreateBLSPrivateKeyShare( - std::make_shared< std::vector< libff::alt_bn128_Fr > >( shares ) ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_wrap.CreateBLSPrivateKeyShare( + std::make_shared< std::vector< libff::alt_bn128_Fr > >( shares ) ), + std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/unit_tests_dkg.cpp b/test/unit_tests_dkg.cpp index 2389dbc9..f5f70a18 100644 --- a/test/unit_tests_dkg.cpp +++ b/test/unit_tests_dkg.cpp @@ -56,15 +56,8 @@ BOOST_AUTO_TEST_CASE( PolynomialValue ) { polynomial = { libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" )}; - bool is_exception_caught = false; - try { - value = obj.PolynomialValue( polynomial, 5 ); - } catch ( std::logic_error& ) { - is_exception_caught = true; - } - - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( value = obj.PolynomialValue( polynomial, 5 ), std::runtime_error ); } BOOST_AUTO_TEST_CASE( verification ) { @@ -117,13 +110,7 @@ BOOST_AUTO_TEST_CASE( ZeroSecret ) { pol.push_back( num1 ); pol.push_back( num2 ); - bool is_exception_caught = false; - try { - dkg_obj.SecretKeyShareCreate( pol ); - } catch ( std::logic_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( dkg_obj.SecretKeyShareCreate( pol ), std::runtime_error ); } } diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 5693502d..7c0086f3 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -287,14 +287,9 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { for ( size_t i = 0; i < 11; ++i ) { libff::alt_bn128_G2 decrypted; - bool is_exception_caught = false; - try { - decrypted = obj.getDecryptionShare( corrupted_ciphertext, secret_keys[i] ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( + decrypted = obj.getDecryptionShare( corrupted_ciphertext, secret_keys[i] ), + std::runtime_error ); decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); @@ -310,31 +305,25 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 2; - bool is_exception_caught = false; - try { + { crypto::TE obj( num_signed, num_all ); std::vector< size_t > vect; for ( size_t i = 0; i < num_signed - 1; i++ ) vect.push_back( i + 1 ); - ThresholdUtils::LagrangeCoeffs( vect, num_signed ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + ThresholdUtils::LagrangeCoeffs( vect, num_signed ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { crypto::TE obj( num_signed, num_all ); std::vector< size_t > vect; for ( size_t i = 0; i < num_signed; i++ ) { vect.push_back( i + 1 ); } vect.at( 1 ) = vect.at( 0 ); - ThresholdUtils::LagrangeCoeffs( vect, num_signed ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + ThresholdUtils::LagrangeCoeffs( vect, num_signed ), std::runtime_error ); } - BOOST_REQUIRE( is_exception_caught ); } } diff --git a/tools/utils.cpp b/tools/utils.cpp index e373bf24..e05e6a8a 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -10,7 +10,7 @@ libBLS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License From b14160862e1ae2f1c03cb667000eef8b1898b2e4 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 14:46:48 +0300 Subject: [PATCH 17/40] SKALE-4409 fix tests --- test/unit_tests_dkg.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit_tests_dkg.cpp b/test/unit_tests_dkg.cpp index f5f70a18..d7770481 100644 --- a/test/unit_tests_dkg.cpp +++ b/test/unit_tests_dkg.cpp @@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE( PolynomialValue ) { polynomial = { libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" )}; - BOOST_REQUIRE_THROW( value = obj.PolynomialValue( polynomial, 5 ), std::runtime_error ); + BOOST_REQUIRE_THROW( value = obj.PolynomialValue( polynomial, 5 ), std::logic_error ); } BOOST_AUTO_TEST_CASE( verification ) { @@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE( ZeroSecret ) { pol.push_back( num1 ); pol.push_back( num2 ); - BOOST_REQUIRE_THROW( dkg_obj.SecretKeyShareCreate( pol ), std::runtime_error ); + BOOST_REQUIRE_THROW( dkg_obj.SecretKeyShareCreate( pol ), std::logic_error ); } } From 6ff5cb084f6b4701bd35d6369b78d9433586b995 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 16:36:29 +0300 Subject: [PATCH 18/40] SKALE-4409 refactor exceptions --- bls/BLSPrivateKey.cpp | 26 ++-- bls/BLSPrivateKeyShare.cpp | 40 ++++--- bls/BLSPublicKey.cpp | 44 +++---- bls/BLSPublicKeyShare.cpp | 28 ++--- bls/BLSSigShare.cpp | 34 +++--- bls/BLSSigShareSet.cpp | 15 +-- bls/BLSSignature.cpp | 25 ++-- bls/bls.cpp | 24 ++-- bls/bls.h | 35 +----- dkg/DKGTESecret.cpp | 2 +- dkg/DKGTEWrapper.cpp | 4 +- test/test_TE_wrappers.cpp | 5 +- test/test_bls.cpp | 132 +++++++++++---------- test/unit_tests_bls.cpp | 39 +++--- test/unit_tests_te.cpp | 8 +- threshold_encryption/TEDecryptSet.cpp | 4 +- threshold_encryption/TEPrivateKey.cpp | 6 +- threshold_encryption/TEPrivateKeyShare.cpp | 8 +- threshold_encryption/TEPublicKey.cpp | 18 +-- threshold_encryption/TEPublicKeyShare.cpp | 16 +-- tools/bls_glue.cpp | 7 +- tools/dkg_glue.cpp | 22 ++-- tools/dkg_key_gen.cpp | 14 +-- tools/generate_key_system.cpp | 20 ++-- tools/hash_g1.cpp | 4 +- tools/sign_bls.cpp | 10 +- tools/utils.cpp | 22 ++-- tools/utils.h | 36 ++++++ 28 files changed, 339 insertions(+), 309 deletions(-) diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 01aef7e1..2bcbfbff 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -29,19 +29,20 @@ BLSPrivateKey::BLSPrivateKey( const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( _key == nullptr ) { - throw crypto::Bls::IncorrectInput( "Secret key share is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is null" ); } if ( _key->empty() ) { - throw crypto::Bls::IncorrectInput( "Secret key share is empty" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is empty" ); } privateKey = std::make_shared< libff::alt_bn128_Fr >( _key->c_str() ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::ZeroSecretKey( + "Secret key share is equal to zero or corrupt" ); } } @@ -50,15 +51,15 @@ BLSPrivateKey::BLSPrivateKey( std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { if ( skeys == nullptr ) { - throw crypto::Bls::IncorrectInput( "Secret keys ptr is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret keys ptr is null" ); } if ( koefs == nullptr ) { - throw crypto::Bls::IncorrectInput( "Signers indices ptr is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Signers indices ptr is null" ); } - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - auto lagrange_koefs = ThresholdUtils::LagrangeCoeffs( *koefs, this->requiredSigners ); + auto lagrange_koefs = crypto::ThresholdUtils::LagrangeCoeffs( *koefs, this->requiredSigners ); libff::alt_bn128_Fr privateKeyObj( libff::alt_bn128_Fr::zero() ); for ( size_t i = 0; i < requiredSigners; ++i ) { libff::alt_bn128_Fr skey = *skeys->at( koefs->at( i ) - 1 )->getPrivateKey(); @@ -66,7 +67,8 @@ BLSPrivateKey::BLSPrivateKey( } if ( privateKeyObj == libff::alt_bn128_Fr::zero() ) { - throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::ZeroSecretKey( + "Secret key share is equal to zero or corrupt" ); } privateKey = std::make_shared< libff::alt_bn128_Fr >( privateKeyObj ); @@ -77,11 +79,11 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKey::getPrivateKey() const { } std::shared_ptr< std::string > BLSPrivateKey::toString() { - std::shared_ptr< std::string > key_str = - std::make_shared< std::string >( ThresholdUtils::fieldElementToString( *privateKey ) ); + std::shared_ptr< std::string > key_str = std::make_shared< std::string >( + crypto::ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) - throw crypto::Bls::ZeroSecretKey( "Secret key share string is empty" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "Secret key share string is empty" ); return key_str; } diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index bd7f00a9..9af68d0c 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -33,14 +33,15 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( const std::string& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); if ( _key.empty() ) { - throw crypto::Bls::IncorrectInput( "Secret key share string is empty" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" ); } privateKey = std::make_shared< libff::alt_bn128_Fr >( _key.c_str() ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::ZeroSecretKey( + "Secret key share is equal to zero or corrupt" ); } } @@ -52,7 +53,7 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( privateKey = std::make_shared< libff::alt_bn128_Fr >( libff_skey ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw crypto::Bls::ZeroSecretKey( "BLS Secret key share is equal to zero" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "BLS Secret key share is equal to zero" ); } } @@ -61,15 +62,15 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( std::shared_ptr< crypto::Bls > obj; if ( _signerIndex == 0 ) { - throw crypto::Bls::IncorrectInput( "Zero signer index during BLS sign" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index during BLS sign" ); } if ( hash_byte_arr == nullptr ) { - throw crypto::Bls::IncorrectInput( "Hash is null during BLS sign" ); + throw crypto::ThresholdUtils::IncorrectInput( "Hash is null during BLS sign" ); } obj = std::make_shared< crypto::Bls >( crypto::Bls( requiredSigners, totalSigners ) ); - libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_byte_arr ); + libff::alt_bn128_G1 hash = crypto::ThresholdUtils::HashtoG1( hash_byte_arr ); auto ss = std::make_shared< libff::alt_bn128_G1 >( obj->Signing( hash, *privateKey ) ); @@ -77,8 +78,8 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( std::pair< libff::alt_bn128_G1, std::string > hash_with_hint = obj->HashtoG1withHint( hash_byte_arr ); - std::string hint = ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + ":" + - hash_with_hint.second; + std::string hint = crypto::ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + + ":" + hash_with_hint.second; auto s = std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners ); @@ -91,10 +92,10 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper( std::shared_ptr< crypto::Bls > obj; if ( _signerIndex == 0 ) { - throw crypto::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" ); } if ( hash_byte_arr == nullptr ) { - throw crypto::Bls::IncorrectInput( "Null hash is bls signWithHelper" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null hash is bls signWithHelper" ); } obj = std::make_shared< crypto::Bls >( crypto::Bls( requiredSigners, totalSigners ) ); @@ -107,8 +108,8 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper( ss->to_affine_coordinates(); - std::string hint = ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + ":" + - hash_with_hint.second; + std::string hint = crypto::ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + + ":" + hash_with_hint.second; auto s = std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners ); @@ -132,7 +133,7 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi std::make_shared< BLSPublicKey >( common_skey, _requiredSigners, _totalSigners ); for ( size_t i = 0; i < _totalSigners; ++i ) { - std::string key_str = ThresholdUtils::fieldElementToString( skeys.at( i ) ); + std::string key_str = crypto::ThresholdUtils::fieldElementToString( skeys.at( i ) ); std::shared_ptr< BLSPrivateKeyShare > key_ptr = std::make_shared< BLSPrivateKeyShare >( key_str, _requiredSigners, _totalSigners ); @@ -156,14 +157,15 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKeyShare::getPrivateKey() const std::shared_ptr< std::string > BLSPrivateKeyShare::toString() { if ( !privateKey ) - throw crypto::Bls::IncorrectInput( "Secret key share is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is null" ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw crypto::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::ZeroSecretKey( + "Secret key share is equal to zero or corrupt" ); } - std::shared_ptr< std::string > key_str = - std::make_shared< std::string >( ThresholdUtils::fieldElementToString( *privateKey ) ); + std::shared_ptr< std::string > key_str = std::make_shared< std::string >( + crypto::ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) - throw crypto::Bls::IncorrectInput( "Secret key share string is empty" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" ); return key_str; } diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index 3d9669ae..954a0ae5 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -30,7 +30,7 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > pkey_str_vect, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); CHECK( pkey_str_vect ) @@ -46,24 +46,24 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > libffPublicKey->Z.c1 = libff::alt_bn128_Fq::zero(); if ( libffPublicKey->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Zero BLS public Key " ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key " ); } if ( !( libffPublicKey->is_well_formed() ) ) { - throw crypto::Bls::IsNotWellFormed( "BLS public Key is corrupt" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "BLS public Key is corrupt" ); } } BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( pkey ); if ( libffPublicKey->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Zero BLS Public Key" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Public Key" ); } } @@ -73,7 +73,7 @@ BLSPublicKey::BLSPublicKey( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( skey * libff::alt_bn128_G2::one() ); if ( libffPublicKey->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); } } @@ -87,17 +87,17 @@ size_t BLSPublicKey::getRequiredSigners() const { bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw crypto::Bls::IncorrectInput( "hash is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); } if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Sig share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Sig share is equal to zero or corrupt" ); } obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) ); @@ -111,18 +111,18 @@ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw crypto::Bls::IncorrectInput( "hash is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); } if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) { - throw crypto::Bls::IncorrectInput( "Sig share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::IncorrectInput( "Sig share is equal to zero or corrupt" ); } std::string hint = sign_ptr->getHint(); std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = - ThresholdUtils::ParseHint( hint ); + crypto::ThresholdUtils::ParseHint( hint ); - libff::alt_bn128_Fq x = ThresholdUtils::HashToFq( hash_ptr ); + libff::alt_bn128_Fq x = crypto::ThresholdUtils::HashToFq( hash_ptr ); x = x + y_shift_x.second; libff::alt_bn128_Fq y_sqr = y_shift_x.first ^ 2; @@ -143,12 +143,12 @@ BLSPublicKey::BLSPublicKey( std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > koefs_pkeys_map, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !koefs_pkeys_map ) { - throw crypto::Bls::IncorrectInput( "map is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "map is null" ); } std::vector< size_t > participatingNodes; @@ -159,7 +159,7 @@ BLSPublicKey::BLSPublicKey( } std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = - ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); + crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); libff::alt_bn128_G2 key = libff::alt_bn128_G2::zero(); size_t i = 0; @@ -174,7 +174,7 @@ BLSPublicKey::BLSPublicKey( libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( key ); if ( libffPublicKey->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); } } @@ -183,10 +183,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKey::toString() { libffPublicKey->to_affine_coordinates(); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->X.c0 ) ); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->X.c1 ) ); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->Y.c0 ) ); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( libffPublicKey->Y.c1 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->X.c0 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->X.c1 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c0 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c1 ) ); return std::make_shared< std::vector< std::string > >( pkey_str_vect ); } diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index 10c05a00..3d6afa67 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -35,7 +35,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); publicKey = std::make_shared< libff::alt_bn128_G2 >(); @@ -47,20 +47,20 @@ BLSPublicKeyShare::BLSPublicKeyShare( publicKey->Z.c1 = libff::alt_bn128_Fq::zero(); if ( publicKey->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Zero BLS public Key share" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key share" ); } if ( !( publicKey->is_well_formed() ) ) { - throw crypto::Bls::IsNotWellFormed( "Corrupt BLS public key share" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Corrupt BLS public key share" ); } } BLSPublicKeyShare::BLSPublicKeyShare( const libff::alt_bn128_Fr& _skey, size_t _totalSigners, size_t _requiredSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); if ( _skey.is_zero() ) { - throw crypto::Bls::ZeroSecretKey( "Zero BLS Secret Key" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "Zero BLS Secret Key" ); } publicKey = std::make_shared< libff::alt_bn128_G2 >( _skey * libff::alt_bn128_G2::one() ); } @@ -75,10 +75,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKeyShare::toString() { publicKey->to_affine_coordinates(); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->X.c0 ) ); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->X.c1 ) ); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->Y.c0 ) ); - pkey_str_vect.push_back( ThresholdUtils::fieldElementToString( publicKey->Y.c1 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->X.c0 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->X.c1 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->Y.c0 ) ); + pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->Y.c1 ) ); return std::make_shared< std::vector< std::string > >( pkey_str_vect ); } @@ -92,7 +92,7 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( sign_ptr->getSigShare()->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Zero BLS Sig share" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Sig share" ); } obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) ); @@ -108,18 +108,18 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_ std::shared_ptr< crypto::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw crypto::Bls::IncorrectInput( "hash is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); } if ( sign_ptr->getSigShare()->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Sig share is equal to zero" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Sig share is equal to zero" ); } std::string hint = sign_ptr->getHint(); std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = - ThresholdUtils::ParseHint( hint ); + crypto::ThresholdUtils::ParseHint( hint ); - libff::alt_bn128_Fq x = ThresholdUtils::HashToFq( hash_ptr ); + libff::alt_bn128_Fq x = crypto::ThresholdUtils::HashToFq( hash_ptr ); x = x + y_shift_x.second; diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp index a1b083b7..08c38da1 100644 --- a/bls/BLSSigShare.cpp +++ b/bls/BLSSigShare.cpp @@ -52,36 +52,36 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( requiredSigners, totalSigners ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners ); + crypto::ThresholdUtils::initCurve(); if ( _signerIndex == 0 ) { - throw crypto::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" ); } if ( !_sigShare ) { - throw crypto::Bls::IncorrectInput( "Null _sigShare" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null _sigShare" ); } if ( _sigShare->size() < 10 ) { - throw crypto::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too short:" + std::to_string( _sigShare->size() ) ); } if ( _sigShare->size() > BLS_MAX_SIG_LEN ) { - throw crypto::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too long:" + std::to_string( _sigShare->size() ) ); } std::shared_ptr< std::vector< std::string > > result = - ThresholdUtils::SplitString( _sigShare, ":" ); + crypto::ThresholdUtils::SplitString( _sigShare, ":" ); if ( result->size() != 4 ) - throw crypto::Bls::IncorrectInput( "Misformatted signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { for ( char& c : str ) { if ( !( c >= '0' && c <= '9' ) ) { - throw crypto::Bls::IncorrectInput( + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str ); } } @@ -94,7 +94,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign hint = result->at( 2 ) + ":" + result->at( 3 ); if ( !sigShare->is_well_formed() ) - throw crypto::Bls::IsNotWellFormed( "signature is not from G1" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "signature is not from G1" ); } BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShare, @@ -104,24 +104,24 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::initCurve(); - ThresholdUtils::checkSigners( requiredSigners, totalSigners ); + crypto::ThresholdUtils::initCurve(); + crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners ); if ( !_sigShare ) { - throw crypto::Bls::IncorrectInput( "Null _s" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null _s" ); } if ( _sigShare->is_zero() ) { - throw crypto::Bls::IsNotWellFormed( "Zero signature" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero signature" ); } if ( _signerIndex == 0 ) { - throw crypto::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" ); } if ( _hint.length() == 0 ) { - throw crypto::Bls::IncorrectInput( "Empty or misformatted hint" ); + throw crypto::ThresholdUtils::IncorrectInput( "Empty or misformatted hint" ); } if ( !_sigShare->is_well_formed() ) { - throw crypto::Bls::IsNotWellFormed( "signature is not from G1" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "signature is not from G1" ); } } diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp index 81312788..e2390016 100644 --- a/bls/BLSSigShareSet.cpp +++ b/bls/BLSSigShareSet.cpp @@ -34,11 +34,11 @@ bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) { CHECK( _sigShare ); if ( was_merged ) { - throw crypto::Bls::IncorrectInput( "Invalid state:was already merged" ); + throw crypto::ThresholdUtils::IncorrectInput( "Invalid state:was already merged" ); } if ( sigShares.count( _sigShare->getSignerIndex() ) > 0 ) { - throw crypto::Bls::IncorrectInput( + throw crypto::ThresholdUtils::IncorrectInput( "Already have this index:" + std::to_string( _sigShare->getSignerIndex() ) ); return false; } @@ -52,7 +52,8 @@ size_t BLSSigShareSet::getTotalSigSharesCount() { } std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index ) { if ( _index == 0 ) { - throw crypto::Bls::IncorrectInput( "Index out of range:" + std::to_string( _index ) ); + throw crypto::ThresholdUtils::IncorrectInput( + "Index out of range:" + std::to_string( _index ) ); } @@ -64,9 +65,9 @@ std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index } BLSSigShareSet::BLSSigShareSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); } bool BLSSigShareSet::isEnough() { @@ -76,7 +77,7 @@ bool BLSSigShareSet::isEnough() { std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { if ( !isEnough() ) - throw crypto::Bls::IncorrectInput( "Not enough shares to create signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Not enough shares to create signature" ); was_merged = true; crypto::Bls obj = crypto::Bls( requiredSigners, totalSigners ); @@ -90,7 +91,7 @@ std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { } std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = - ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); + crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); libff::alt_bn128_G1 signature = obj.SignatureRecover( shares, lagrangeCoeffs ); diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp index 5bb1b25a..ebc04aca 100644 --- a/bls/BLSSignature.cpp +++ b/bls/BLSSignature.cpp @@ -38,14 +38,14 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st CHECK( sig ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); if ( sig->is_zero() ) { - throw crypto::Bls::IncorrectInput( "Zero BLS signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero BLS signature" ); } if ( hint.length() == 0 ) { - throw crypto::Bls::IncorrectInput( "Empty BLS hint" ); + throw crypto::ThresholdUtils::IncorrectInput( "Empty BLS hint" ); } } @@ -56,27 +56,28 @@ BLSSignature::BLSSignature( BLSSignature::checkSigners( requiredSigners, totalSigners ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); if ( _sig->size() < 10 ) { - throw crypto::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too short:" + std::to_string( _sig->size() ) ); } if ( _sig->size() > BLS_MAX_SIG_LEN ) { - throw crypto::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too long:" + std::to_string( _sig->size() ) ); } - std::shared_ptr< std::vector< std::string > > result = ThresholdUtils::SplitString( _sig, ":" ); + std::shared_ptr< std::vector< std::string > > result = + crypto::ThresholdUtils::SplitString( _sig, ":" ); if ( result->size() != 4 ) - throw crypto::Bls::IncorrectInput( "Misformatted signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { for ( char& c : str ) { if ( !( c >= '0' && c <= '9' ) ) { - throw crypto::Bls::IncorrectInput( + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str ); } } @@ -87,7 +88,7 @@ BLSSignature::BLSSignature( hint = result->at( 2 ) + ":" + result->at( 3 ); if ( !( sig->is_well_formed() ) ) { - throw crypto::Bls::IsNotWellFormed( "signature is not from G1" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "signature is not from G1" ); } } @@ -105,12 +106,12 @@ void BLSSignature::checkSigners( size_t _requiredSigners, size_t _totalSigners ) CHECK( _totalSigners > 0 ); if ( _requiredSigners > _totalSigners ) { - throw crypto::Bls::IncorrectInput( "_requiredSigners > _totalSigners" ); + throw crypto::ThresholdUtils::IncorrectInput( "_requiredSigners > _totalSigners" ); } if ( _totalSigners == 0 ) { - throw crypto::Bls::IncorrectInput( "_totalSigners == 0" ); + throw crypto::ThresholdUtils::IncorrectInput( "_totalSigners == 0" ); } } diff --git a/bls/bls.cpp b/bls/bls.cpp index 0cca00b2..3d088a26 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -147,7 +147,7 @@ libff::alt_bn128_G1 Bls::Signing( // implemented constant time signing if ( secret_key == libff::alt_bn128_Fr::zero() ) { - throw ZeroSecretKey( "failed to sign a message hash" ); + throw ThresholdUtils::ZeroSecretKey( "failed to sign a message hash" ); } std::clock_t c_start = std::clock(); // hash @@ -169,15 +169,16 @@ bool Bls::Verification( const std::string& to_be_hashed, const libff::alt_bn128_ libff::inhibit_profiling_info = true; if ( !sign.is_well_formed() ) { - throw IsNotWellFormed( "Error, signature does not lie on the alt_bn128 curve" ); + throw ThresholdUtils::IsNotWellFormed( + "Error, signature does not lie on the alt_bn128 curve" ); } if ( !public_key.is_well_formed() ) { - throw IsNotWellFormed( "Error, public key is invalid" ); + throw ThresholdUtils::IsNotWellFormed( "Error, public key is invalid" ); } if ( libff::alt_bn128_modulus_r * sign != libff::alt_bn128_G1::zero() ) { - throw IsNotWellFormed( "Error, signature is not member of G1" ); + throw ThresholdUtils::IsNotWellFormed( "Error, signature is not member of G1" ); } libff::alt_bn128_G1 hash = Hashing( to_be_hashed ); @@ -196,15 +197,16 @@ bool Bls::Verification( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_a libff::inhibit_profiling_info = true; if ( !sign.is_well_formed() ) { - throw IsNotWellFormed( "Error, signature does not lie on the alt_bn128 curve" ); + throw ThresholdUtils::IsNotWellFormed( + "Error, signature does not lie on the alt_bn128 curve" ); } if ( !public_key.is_well_formed() ) { - throw IsNotWellFormed( "Error, public key is invalid" ); + throw ThresholdUtils::IsNotWellFormed( "Error, public key is invalid" ); } if ( libff::alt_bn128_modulus_r * sign != libff::alt_bn128_G1::zero() ) { - throw IsNotWellFormed( "Error, signature is not member of G1" ); + throw ThresholdUtils::IsNotWellFormed( "Error, signature is not member of G1" ); } libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_byte_arr ); @@ -218,14 +220,14 @@ std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeysRecover( const std::vector< libff::alt_bn128_Fr >& coeffs, const std::vector< libff::alt_bn128_Fr >& shares ) { if ( shares.size() < this->t_ || coeffs.size() < this->t_ ) { - throw IncorrectInput( "not enough participants in the threshold group" ); + throw ThresholdUtils::IncorrectInput( "not enough participants in the threshold group" ); } libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::zero(); for ( size_t i = 0; i < this->t_; ++i ) { if ( shares[i] == libff::alt_bn128_Fr::zero() ) { - throw ZeroSecretKey( + throw ThresholdUtils::ZeroSecretKey( "at least one secret key share is equal to zero in KeysRecover group" ); } secret_key += coeffs[i] * shares[i]; // secret key recovering using Lagrange Interpolation @@ -240,14 +242,14 @@ std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeysRecover( libff::alt_bn128_G1 Bls::SignatureRecover( const std::vector< libff::alt_bn128_G1 >& shares, const std::vector< libff::alt_bn128_Fr >& coeffs ) { if ( shares.size() < this->t_ || coeffs.size() < this->t_ ) { - throw IncorrectInput( "not enough participants in the threshold group" ); + throw ThresholdUtils::IncorrectInput( "not enough participants in the threshold group" ); } libff::alt_bn128_G1 sign = libff::alt_bn128_G1::zero(); for ( size_t i = 0; i < this->t_; ++i ) { if ( !shares[i].is_well_formed() ) { - throw IsNotWellFormed( "incorrect input data to recover signature" ); + throw ThresholdUtils::IsNotWellFormed( "incorrect input data to recover signature" ); } sign = sign + coeffs[i] * shares[i]; // signature recovering using Lagrange Coefficients } diff --git a/bls/bls.h b/bls/bls.h index 45df4e3d..697608e3 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -42,40 +42,7 @@ static constexpr size_t BLS_MAX_SIG_LEN = 240; namespace crypto { class Bls { -private: - class BlsException : public std::exception { - protected: - std::string what_str; - - public: - BlsException( const std::string& err_str ) { what_str = err_str; } - - virtual const char* what() const noexcept override { return what_str.c_str(); } - }; - public: - class IsNotWellFormed : public BlsException { - public: - IsNotWellFormed( const std::string& err_str ) : BlsException( err_str ) { - what_str = "IsNotWellFormedData : " + err_str; - } - }; - - class ZeroSecretKey : public BlsException { - public: - ZeroSecretKey( const std::string& err_str ) : BlsException( err_str ) { - what_str = "Secret key is equal to zero : " + err_str; - } - }; - - class IncorrectInput : public BlsException { - public: - IncorrectInput( const std::string& err_str ) : BlsException( err_str ) { - what_str = "Failed to proceed data : " + err_str; - } - }; - - Bls( const size_t t, const size_t n ); std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > KeyGeneration(); @@ -118,5 +85,5 @@ class Bls { if ( !( _EXPRESSION_ ) ) { \ auto __msg__ = std::string( "Check failed:" ) + #_EXPRESSION_ + "\n" + __FUNCTION__ + \ +" " + std::string( __FILE__ ) + ":" + std::to_string( __LINE__ ); \ - throw crypto::Bls::IncorrectInput( __msg__ ); \ + throw crypto::ThresholdUtils::IncorrectInput( __msg__ ); \ } diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp index 324cd68f..11723019 100644 --- a/dkg/DKGTESecret.cpp +++ b/dkg/DKGTESecret.cpp @@ -29,7 +29,7 @@ DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index 802b9a86..d949ae27 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -27,7 +27,7 @@ DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -80,7 +80,7 @@ TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( TEPublicKey DKGTEWrapper::CreateTEPublicKey( std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all, size_t _requiredSigners, size_t _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( public_shares_all == nullptr ) throw std::runtime_error( "Null public shares all" ); diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 1892be05..e79165b1 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -355,9 +355,10 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % num_all + 1; - BOOST_REQUIRE_THROW( ThresholdUtils::checkSigners( 0, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( + crypto::ThresholdUtils::checkSigners( 0, num_all ), std::runtime_error ); - BOOST_REQUIRE_THROW( ThresholdUtils::checkSigners( 0, 0 ), std::runtime_error ); + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::checkSigners( 0, 0 ), std::runtime_error ); // null public key share BOOST_REQUIRE_THROW( diff --git a/test/test_bls.cpp b/test/test_bls.cpp index ff68435d..6a03bce2 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE( libBls ) { for ( size_t i = 0; i < 10; ++i ) { std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr = std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ); - libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_ptr ); + libff::alt_bn128_G1 hash = crypto::ThresholdUtils::HashtoG1( hash_ptr ); for ( size_t i = 0; i < num_signed; ++i ) signatures.at( i ) = obj.Signing( hash, skeys.at( i ) ); @@ -140,18 +140,18 @@ BOOST_AUTO_TEST_CASE( libBls ) { BOOST_REQUIRE( obj.Verification( hash_ptr, signatures.at( i ), pkey ) ); BOOST_REQUIRE_THROW( obj.Verification( hash_ptr, SpoilSignature( signatures.at( i ) ), pkey ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } std::vector< libff::alt_bn128_Fr > lagrange_coeffs = - ThresholdUtils::LagrangeCoeffs( participants, num_signed ); + crypto::ThresholdUtils::LagrangeCoeffs( participants, num_signed ); libff::alt_bn128_G1 signature = obj.SignatureRecover( signatures, lagrange_coeffs ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, skeys ); BOOST_REQUIRE( obj.Verification( hash_ptr, signature, recovered_keys.second ) ); BOOST_REQUIRE_THROW( obj.Verification( hash_ptr, SpoilSignature( signature ), recovered_keys.second ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } } @@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE( libBlsAPI ) { BOOST_REQUIRE_THROW( BLSSigShare( bad_sig, hint, participants.at( i ), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } BOOST_REQUIRE( sigSet.getTotalSigSharesCount() == num_signed ); @@ -222,7 +222,7 @@ BOOST_AUTO_TEST_CASE( libBlsAPI ) { BOOST_REQUIRE_THROW( common_pkey.VerifySig( hash_ptr, std::make_shared< BLSSignature >( bad_sign ), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > pkeys_map1; for ( size_t i = 0; i < num_signed; ++i ) { @@ -427,7 +427,7 @@ BOOST_AUTO_TEST_CASE( private_keys_equality ) { choose_rand_signers( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > lagrange_koefs = - ThresholdUtils::LagrangeCoeffs( *participants, num_signed ); + crypto::ThresholdUtils::LagrangeCoeffs( *participants, num_signed ); libff::alt_bn128_Fr common_skey = libff::alt_bn128_Fr::zero(); for ( size_t i = 0; i < num_signed; ++i ) { common_skey = @@ -452,7 +452,7 @@ BOOST_AUTO_TEST_CASE( public_keys_equality ) { choose_rand_signers( num_signed, num_all ); std::vector< libff::alt_bn128_Fr > lagrange_koefs = - ThresholdUtils::LagrangeCoeffs( *participants, num_signed ); + crypto::ThresholdUtils::LagrangeCoeffs( *participants, num_signed ); libff::alt_bn128_G2 common_pkey1 = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < num_signed; ++i ) { common_pkey1 = common_pkey1 + lagrange_koefs.at( i ) * @@ -552,7 +552,8 @@ BOOST_AUTO_TEST_CASE( BLSWITHDKG ) { std::shared_ptr< BLSSignature > common_sig_ptr = sigSet.merge(); // verifying signature - std::string common_secret_str = ThresholdUtils::fieldElementToString( common_secret ); + std::string common_secret_str = + crypto::ThresholdUtils::fieldElementToString( common_secret ); BLSPrivateKey common_skey( std::make_shared< std::string >( common_secret_str ), num_signed, num_all ); @@ -593,25 +594,25 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { { BOOST_REQUIRE_THROW( BLSPrivateKey pkey( std::make_shared< std::string >( "" ), num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPrivateKey skey( std::make_shared< std::string >( "0" ), num_signed, num_all ), - crypto::Bls::ZeroSecretKey ); + crypto::ThresholdUtils::ZeroSecretKey ); } { - BOOST_REQUIRE_THROW( - BLSPrivateKey skey( nullptr, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSPrivateKey skey( nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPrivateKey skey( nullptr, std::make_shared< std::vector< size_t > >( participants ), num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -619,35 +620,35 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BLSPrivateKey skey( BLSPrivateKeyShare::generateSampleKeys( num_signed, num_all )->first, NULL, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { - BOOST_REQUIRE_THROW( - BLSPrivateKeyShare skey( "", num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSPrivateKeyShare skey( "", num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { - BOOST_REQUIRE_THROW( - BLSPrivateKeyShare skey( "0", num_signed, num_all ), crypto::Bls::ZeroSecretKey ); + BOOST_REQUIRE_THROW( BLSPrivateKeyShare skey( "0", num_signed, num_all ), + crypto::ThresholdUtils::ZeroSecretKey ); } { BOOST_REQUIRE_THROW( BLSPrivateKeyShare skey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - crypto::Bls::ZeroSecretKey ); + crypto::ThresholdUtils::ZeroSecretKey ); } { BLSPrivateKeyShare skey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); BOOST_REQUIRE_THROW( skey.sign( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), 0 ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BLSPrivateKeyShare skey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); - BOOST_REQUIRE_THROW( skey.sign( NULL, 1 ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( skey.sign( NULL, 1 ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -655,35 +656,36 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( skey.signWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), 0 ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BLSPrivateKeyShare skey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); - BOOST_REQUIRE_THROW( skey.signWithHelper( NULL, 1 ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + skey.signWithHelper( NULL, 1 ), crypto::ThresholdUtils::IncorrectInput ); } { const std::shared_ptr< std::vector< std::string > > null_vect = nullptr; - BOOST_REQUIRE_THROW( - BLSPublicKey pkey( null_vect, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSPublicKey pkey( null_vect, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { const std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > null_map = nullptr; - BOOST_REQUIRE_THROW( - BLSPublicKey pkey( null_map, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSPublicKey pkey( null_map, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_G2::zero(), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -695,7 +697,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( pkey.VerifySigWithHelper( nullptr, std::make_shared< BLSSignature >( rand_sig ), num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -704,18 +706,18 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { pkey.VerifySigWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), nullptr, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - crypto::Bls::ZeroSecretKey ); + crypto::ThresholdUtils::ZeroSecretKey ); } { - BOOST_REQUIRE_THROW( - BLSPublicKeyShare pkey( nullptr, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -726,7 +728,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { 1, num_signed, num_all ); BOOST_REQUIRE_THROW( pkey.VerifySigWithHelper( nullptr, std::make_shared< BLSSigShare >( rand_sig ), num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -735,18 +737,18 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { pkey.VerifySigWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), nullptr, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSSignature( nullptr, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BLSSignature( nullptr, num_signed, num_all ), crypto::ThresholdUtils::IncorrectInput ); } { std::string hint = "123:1"; - BOOST_REQUIRE_THROW( - BLSSignature( nullptr, hint, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSSignature( nullptr, hint, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -754,14 +756,14 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< libff::alt_bn128_G1 >( libff::alt_bn128_G1::random_element() ), empty_hint, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { std::string short_sig = "1:1:1:1"; BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( short_sig ), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -774,7 +776,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -787,7 +789,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -801,7 +803,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -815,7 +817,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { long_sig[25] = 'a'; BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< std::string >( long_sig ), num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -823,23 +825,23 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { std::string hint = "123:1"; BOOST_REQUIRE_THROW( BLSSignature( std::make_shared< libff::alt_bn128_G1 >( zero_sig ), hint, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { - BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, 1, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSSigShare( nullptr, 1, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { - BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, 0, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSSigShare( nullptr, 0, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { std::string hint = "123:1"; - BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, hint, 0, num_signed, num_all ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSSigShare( nullptr, hint, 0, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -847,14 +849,14 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< libff::alt_bn128_G1 >( libff::alt_bn128_G1::random_element() ), empty_hint, 1, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { std::string short_sig = "1:1:1:1"; BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( short_sig ), 1, num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } @@ -869,7 +871,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -882,7 +884,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -897,7 +899,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -911,13 +913,14 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { long_sig[25] = 'a'; BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< std::string >( long_sig ), 1, num_signed, num_all ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.addSigShare( nullptr ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + sig_set.addSigShare( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -929,7 +932,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BLSSigShareSet sig_set( num_signed, num_all ); sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare1 ) ); BOOST_REQUIRE_THROW( sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare2 ) ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -941,12 +944,12 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare1 ) ); sig_set.merge(); BOOST_REQUIRE_THROW( sig_set.addSigShare( std::make_shared< BLSSigShare >( sigShare1 ) ), - crypto::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.merge(), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( sig_set.merge(), crypto::ThresholdUtils::IncorrectInput ); } { @@ -956,7 +959,8 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.getSigShareByIndex( 0 ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + sig_set.getSigShareByIndex( 0 ), crypto::ThresholdUtils::IncorrectInput ); } std::cerr << "EXCEPTIONS TEST FINISHED" << std::endl; diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index af3da6ad..49ed8f88 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE( zeroSecretKey ) { libff::alt_bn128_G1 hash = obj.Hashing( message ); - BOOST_REQUIRE_THROW( obj.Signing( hash, secret_key ), crypto::Bls::ZeroSecretKey ); + BOOST_REQUIRE_THROW( obj.Signing( hash, secret_key ), crypto::ThresholdUtils::ZeroSecretKey ); std::cout << "DONE\n"; } @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { std::vector< size_t > testing_nodes = {1, 2}; std::vector< libff::alt_bn128_Fr > lagrange_coeffs = - ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, secret_keys ); @@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { std::vector< size_t > testing_nodes = {1, 2}; std::vector< libff::alt_bn128_Fr > lagrange_coeffs = - ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); libff::alt_bn128_G1 fst_signature = obj.Signing( hash, fst_secret ); libff::alt_bn128_G1 snd_signature = obj.Signing( hash, snd_secret ); @@ -245,7 +245,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesReal ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs = - ThresholdUtils::LagrangeCoeffs( testing_nodes, 11 ); + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes, 11 ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, secret_keys ); @@ -311,7 +311,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs_fst = - ThresholdUtils::LagrangeCoeffs( testing_nodes_fst, 11 ); + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes_fst, 11 ); auto recovered_keys_fst = obj.KeysRecover( lagrange_coeffs_fst, secret_keys ); @@ -345,7 +345,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs_snd = - ThresholdUtils::LagrangeCoeffs( testing_nodes_snd, 11 ); + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes_snd, 11 ); std::vector< libff::alt_bn128_Fr > secret_keys_for_random_subgroup( 11 ); for ( size_t i = 0; i < 11; ++i ) { @@ -453,7 +453,7 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { std::vector< libff::alt_bn128_Fr > pol( deg + 1 ); - ThresholdUtils::initCurve(); + crypto::ThresholdUtils::initCurve(); // random polynomial generation for ( size_t i = 0; i < deg + 1; ++i ) { @@ -492,7 +492,7 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { } crypto::Bls obj = crypto::Bls( deg + 1, deg + 1 ); - auto coeffs = ThresholdUtils::LagrangeCoeffs( indexes, deg + 1 ); + auto coeffs = crypto::ThresholdUtils::LagrangeCoeffs( indexes, deg + 1 ); std::vector< libff::alt_bn128_Fr > values( deg + 1 ); for ( size_t i = 0; i < deg + 1; ++i ) { @@ -524,21 +524,22 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { BOOST_REQUIRE_THROW( obj.Verification( "bla-bla-bla", sign, libff::alt_bn128_G2::random_element() ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); libff::alt_bn128_G2 pkey = libff::alt_bn128_G2::random_element(); pkey.X.c1 = 123; BOOST_REQUIRE_THROW( obj.Verification( "bla-bla-bla", libff::alt_bn128_G1::random_element(), pkey ), - crypto::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); std::vector< libff::alt_bn128_Fr > coeffs; coeffs.push_back( libff::alt_bn128_Fr::random_element() ); std::vector< libff::alt_bn128_Fr > shares; shares.push_back( libff::alt_bn128_Fr::random_element() ); - BOOST_REQUIRE_THROW( obj.KeysRecover( coeffs, shares ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + obj.KeysRecover( coeffs, shares ), crypto::ThresholdUtils::IncorrectInput ); coeffs.clear(); shares.clear(); @@ -549,7 +550,8 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { shares.push_back( libff::alt_bn128_Fr::random_element() ); shares.push_back( libff::alt_bn128_Fr::zero() ); - BOOST_REQUIRE_THROW( obj_2_2.KeysRecover( coeffs, shares ), crypto::Bls::ZeroSecretKey ); + BOOST_REQUIRE_THROW( + obj_2_2.KeysRecover( coeffs, shares ), crypto::ThresholdUtils::ZeroSecretKey ); coeffs.clear(); coeffs.push_back( libff::alt_bn128_Fr::random_element() ); @@ -557,7 +559,8 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { std::vector< libff::alt_bn128_G1 > sig_shares; sig_shares.push_back( libff::alt_bn128_G1::random_element() ); - BOOST_REQUIRE_THROW( obj.SignatureRecover( sig_shares, coeffs ), crypto::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + obj.SignatureRecover( sig_shares, coeffs ), crypto::ThresholdUtils::IncorrectInput ); coeffs.clear(); sig_shares.clear(); @@ -571,18 +574,18 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { sig_shares.push_back( g1_spoiled ); BOOST_REQUIRE_THROW( - obj_2_2.SignatureRecover( sig_shares, coeffs ), crypto::Bls::IsNotWellFormed ); + obj_2_2.SignatureRecover( sig_shares, coeffs ), crypto::ThresholdUtils::IsNotWellFormed ); std::vector< size_t > idx = {1}; - BOOST_REQUIRE_THROW( ThresholdUtils::LagrangeCoeffs( idx, num_signed ), - std::runtime_error ); // crypto::Bls::IncorrectInput + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::LagrangeCoeffs( idx, num_signed ), + crypto::ThresholdUtils::IncorrectInput ); idx.clear(); idx = {1, 1}; - BOOST_REQUIRE_THROW( ThresholdUtils::LagrangeCoeffs( idx, 2 ), - std::runtime_error ); // crypto::Bls::IncorrectInput + BOOST_REQUIRE_THROW( + crypto::ThresholdUtils::LagrangeCoeffs( idx, 2 ), crypto::ThresholdUtils::IncorrectInput ); std::cerr << "Exceptions test passed" << std::endl; } diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 7c0086f3..40392bfa 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -310,8 +310,8 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { std::vector< size_t > vect; for ( size_t i = 0; i < num_signed - 1; i++ ) vect.push_back( i + 1 ); - BOOST_REQUIRE_THROW( - ThresholdUtils::LagrangeCoeffs( vect, num_signed ), std::runtime_error ); + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::LagrangeCoeffs( vect, num_signed ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -321,8 +321,8 @@ BOOST_AUTO_TEST_CASE( LagrangeInterpolationExceptions ) { vect.push_back( i + 1 ); } vect.at( 1 ) = vect.at( 0 ); - BOOST_REQUIRE_THROW( - ThresholdUtils::LagrangeCoeffs( vect, num_signed ), std::runtime_error ); + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::LagrangeCoeffs( vect, num_signed ), + crypto::ThresholdUtils::IncorrectInput ); } } } diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index 9e83fadc..d7392af3 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -29,7 +29,7 @@ along with libBLS. If not, see . TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); } @@ -55,7 +55,7 @@ void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_ } std::string TEDecryptSet::merge( const crypto::Ciphertext& cyphertext ) { - ThresholdUtils::checkCypher( cyphertext ); + crypto::ThresholdUtils::checkCypher( cyphertext ); was_merged = true; diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index ea1fb6c8..7a00de7b 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -27,7 +27,7 @@ along with libBLS. If not, see . TEPrivateKey::TEPrivateKey( std::shared_ptr< std::string > _key_str, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { throw std::runtime_error( "private key is null" ); @@ -45,7 +45,7 @@ TEPrivateKey::TEPrivateKey( TEPrivateKey::TEPrivateKey( libff::alt_bn128_Fr _skey, size_t _requiredSigners, size_t _totalSigners ) : privateKey( _skey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -54,7 +54,7 @@ TEPrivateKey::TEPrivateKey( } std::string TEPrivateKey::toString() const { - return ThresholdUtils::fieldElementToString( privateKey ); + return crypto::ThresholdUtils::fieldElementToString( privateKey ); } libff::alt_bn128_Fr TEPrivateKey::getPrivateKey() const { diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index 44e63cba..3c435e22 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -30,7 +30,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, s : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { throw std::runtime_error( "private key share is null" ); @@ -51,7 +51,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _signerIndex > _totalSigners ) { throw std::runtime_error( "Wrong _signerIndex" ); @@ -65,7 +65,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s } libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( crypto::Ciphertext& cipher ) { - ThresholdUtils::checkCypher( cipher ); + crypto::ThresholdUtils::checkCypher( cipher ); crypto::TE te( requiredSigners, totalSigners ); @@ -79,7 +79,7 @@ libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( crypto::Ciphertext& c } std::string TEPrivateKeyShare::toString() const { - return ThresholdUtils::fieldElementToString( privateKey ); + return crypto::ThresholdUtils::fieldElementToString( privateKey ); } size_t TEPrivateKeyShare::getSignerIndex() const { diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 48f256af..877e35c0 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -31,7 +31,7 @@ along with libBLS. If not, see . TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str_ptr, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { throw std::runtime_error( "public key is null" ); @@ -41,10 +41,10 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str throw std::runtime_error( "wrong number of components in public key share" ); } - if ( !ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || - !ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || - !ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || - !ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { + if ( !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || + !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || + !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || + !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); } @@ -64,7 +64,7 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str TEPublicKey::TEPublicKey( TEPrivateKey _common_private, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -77,7 +77,7 @@ TEPublicKey::TEPublicKey( TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, size_t _totalSigners ) : PublicKey( _pkey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _pkey.is_zero() ) { throw std::runtime_error( "zero public key" ); @@ -96,7 +96,7 @@ crypto::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr } crypto::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); - ThresholdUtils::checkCypher( cypher ); + crypto::ThresholdUtils::checkCypher( cypher ); libff::alt_bn128_G2 U = std::get< 0 >( cypher ); /*if (element_item_count(U) == 0 ) { @@ -113,7 +113,7 @@ crypto::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr std::shared_ptr< std::vector< std::string > > TEPublicKey::toString() { return std::make_shared< std::vector< std::string > >( - ThresholdUtils::G2ToString( PublicKey ) ); + crypto::ThresholdUtils::G2ToString( PublicKey ) ); } libff::alt_bn128_G2 TEPublicKey::getPublicKey() const { diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index e9e9c91d..09d79072 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -29,7 +29,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { throw std::runtime_error( "public key share is null" ); @@ -39,10 +39,10 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > throw std::runtime_error( "wrong number of components in public key share" ); } - if ( !ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || - !ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || - !ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || - !ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { + if ( !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || + !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || + !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || + !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); } @@ -62,7 +62,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > TEPublicKeyShare::TEPublicKeyShare( TEPrivateKeyShare _p_key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libff::init_alt_bn128_params(); @@ -72,7 +72,7 @@ TEPublicKeyShare::TEPublicKeyShare( bool TEPublicKeyShare::Verify( const crypto::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { - ThresholdUtils::checkCypher( cyphertext ); + crypto::ThresholdUtils::checkCypher( cyphertext ); if ( decryptionShare.is_zero() ) { throw std::runtime_error( "zero decrypt" ); } @@ -84,7 +84,7 @@ bool TEPublicKeyShare::Verify( std::shared_ptr< std::vector< std::string > > TEPublicKeyShare::toString() { return std::make_shared< std::vector< std::string > >( - ThresholdUtils::G2ToString( PublicKey ) ); + crypto::ThresholdUtils::G2ToString( PublicKey ) ); } libff::alt_bn128_G2 TEPublicKeyShare::getPublicKey() const { diff --git a/tools/bls_glue.cpp b/tools/bls_glue.cpp index 70151e7f..33a8b6d6 100644 --- a/tools/bls_glue.cpp +++ b/tools/bls_glue.cpp @@ -62,7 +62,8 @@ void RecoverSignature( const size_t t, const size_t n, const std::vector< std::s signature_shares[i] = signature_share; } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = ThresholdUtils::LagrangeCoeffs( idx, t ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + crypto::ThresholdUtils::LagrangeCoeffs( idx, t ); libff::alt_bn128_G1 common_signature = bls_instance.SignatureRecover( signature_shares, lagrange_coeffs ); @@ -70,8 +71,8 @@ void RecoverSignature( const size_t t, const size_t n, const std::vector< std::s nlohmann::json outdata; - outdata["signature"]["X"] = ThresholdUtils::fieldElementToString( common_signature.X ); - outdata["signature"]["Y"] = ThresholdUtils::fieldElementToString( common_signature.Y ); + outdata["signature"]["X"] = crypto::ThresholdUtils::fieldElementToString( common_signature.X ); + outdata["signature"]["Y"] = crypto::ThresholdUtils::fieldElementToString( common_signature.Y ); outfile << outdata.dump( 4 ) << '\n'; } diff --git a/tools/dkg_glue.cpp b/tools/dkg_glue.cpp index b9826ec1..a66bbcd9 100644 --- a/tools/dkg_glue.cpp +++ b/tools/dkg_glue.cpp @@ -126,17 +126,21 @@ void GenerateSecretKeys( const size_t t, const size_t n, const std::vector< std: nlohmann::json BLS_key_file; BLS_key_file["insecureBLSPrivateKey"] = - ThresholdUtils::fieldElementToString( secret_key[i] ); + crypto::ThresholdUtils::fieldElementToString( secret_key[i] ); std::string str_file_name = "BLS_keys" + std::to_string( i ) + ".json"; std::ofstream out( str_file_name.c_str() ); libff::alt_bn128_G2 publ_key = dkg_instance.GetPublicKeyFromSecretKey( secret_key[i] ); publ_key.to_affine_coordinates(); - BLS_key_file["BLSPublicKey0"] = ThresholdUtils::fieldElementToString( publ_key.X.c0 ); - BLS_key_file["BLSPublicKey1"] = ThresholdUtils::fieldElementToString( publ_key.X.c1 ); - BLS_key_file["BLSPublicKey2"] = ThresholdUtils::fieldElementToString( publ_key.Y.c0 ); - BLS_key_file["BLSPublicKey3"] = ThresholdUtils::fieldElementToString( publ_key.Y.c1 ); + BLS_key_file["BLSPublicKey0"] = + crypto::ThresholdUtils::fieldElementToString( publ_key.X.c0 ); + BLS_key_file["BLSPublicKey1"] = + crypto::ThresholdUtils::fieldElementToString( publ_key.X.c1 ); + BLS_key_file["BLSPublicKey2"] = + crypto::ThresholdUtils::fieldElementToString( publ_key.Y.c0 ); + BLS_key_file["BLSPublicKey3"] = + crypto::ThresholdUtils::fieldElementToString( publ_key.Y.c1 ); if ( g_b_verbose_mode ) { std::cout << str_file_name << " file:\n" << BLS_key_file.dump( 4 ) << "\n\n"; @@ -147,13 +151,13 @@ void GenerateSecretKeys( const size_t t, const size_t n, const std::vector< std: common_public_key.to_affine_coordinates(); nlohmann::json public_key_json; public_key_json["commonBLSPublicKey0"] = - ThresholdUtils::fieldElementToString( common_public_key.X.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.X.c0 ); public_key_json["commonBLSPublicKey1"] = - ThresholdUtils::fieldElementToString( common_public_key.X.c1 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.X.c1 ); public_key_json["commonBLSPublicKey2"] = - ThresholdUtils::fieldElementToString( common_public_key.Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.Y.c0 ); public_key_json["commonBLSPublicKey3"] = - ThresholdUtils::fieldElementToString( common_public_key.Y.c1 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.Y.c1 ); std::ofstream outfile_pk( "common_public_key.json" ); outfile_pk << public_key_json.dump( 4 ) << "\n"; diff --git a/tools/dkg_key_gen.cpp b/tools/dkg_key_gen.cpp index 46daec31..47827372 100644 --- a/tools/dkg_key_gen.cpp +++ b/tools/dkg_key_gen.cpp @@ -131,23 +131,23 @@ void KeyGeneration( const size_t t, const size_t n, bool generate_all = true, in for ( size_t i = 0; i < n; ++i ) { data["secret_key_contribution"][std::to_string( i )] = - ThresholdUtils::fieldElementToString( secret_key_contribution[i] ); + crypto::ThresholdUtils::fieldElementToString( secret_key_contribution[i] ); } for ( size_t i = 0; i < t; ++i ) { data["verification_vector"][std::to_string( i )]["X"]["c0"] = - ThresholdUtils::fieldElementToString( verification_vector[i].X.c0 ); + crypto::ThresholdUtils::fieldElementToString( verification_vector[i].X.c0 ); data["verification_vector"][std::to_string( i )]["X"]["c1"] = - ThresholdUtils::fieldElementToString( verification_vector[i].X.c1 ); + crypto::ThresholdUtils::fieldElementToString( verification_vector[i].X.c1 ); data["verification_vector"][std::to_string( i )]["Y"]["c0"] = - ThresholdUtils::fieldElementToString( verification_vector[i].Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( verification_vector[i].Y.c0 ); data["verification_vector"][std::to_string( i )]["Y"]["c1"] = - ThresholdUtils::fieldElementToString( verification_vector[i].Y.c1 ); + crypto::ThresholdUtils::fieldElementToString( verification_vector[i].Y.c1 ); data["verification_vector"][std::to_string( i )]["Z"]["c0"] = - ThresholdUtils::fieldElementToString( verification_vector[i].Z.c0 ); + crypto::ThresholdUtils::fieldElementToString( verification_vector[i].Z.c0 ); data["verification_vector"][std::to_string( i )]["Z"]["c1"] = - ThresholdUtils::fieldElementToString( verification_vector[i].Z.c1 ); + crypto::ThresholdUtils::fieldElementToString( verification_vector[i].Z.c1 ); } std::ofstream outfile( "data_for_" + std::to_string( idx ) + "-th_participant.json" ); diff --git a/tools/generate_key_system.cpp b/tools/generate_key_system.cpp index d38778fb..f0d37ac9 100644 --- a/tools/generate_key_system.cpp +++ b/tools/generate_key_system.cpp @@ -59,7 +59,7 @@ void GenerateKeys( const size_t t, const size_t n, std::ostream& outfile ) { for ( size_t i = 0; i < n; ++i ) { idx[i] = i + 1; } - auto lagrange_coeffs = ThresholdUtils::LagrangeCoeffs( idx, t ); + auto lagrange_coeffs = crypto::ThresholdUtils::LagrangeCoeffs( idx, t ); auto common_keys = bls_instance.KeysRecover( lagrange_coeffs, secret_keys ); common_keys.second.to_affine_coordinates(); @@ -67,26 +67,26 @@ void GenerateKeys( const size_t t, const size_t n, std::ostream& outfile ) { nlohmann::json outdata; outdata["commonBLSPublicKey"]["0"] = - ThresholdUtils::fieldElementToString( common_keys.second.X.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.X.c0 ); outdata["commonBLSPublicKey"]["1"] = - ThresholdUtils::fieldElementToString( common_keys.second.X.c1 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.X.c1 ); outdata["commonBLSPublicKey"]["2"] = - ThresholdUtils::fieldElementToString( common_keys.second.Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.Y.c0 ); outdata["commonBLSPublicKey"]["3"] = - ThresholdUtils::fieldElementToString( common_keys.second.Y.c1 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.Y.c1 ); for ( size_t i = 0; i < n; ++i ) { outdata["privateKey"][std::to_string( i )] = - ThresholdUtils::fieldElementToString( secret_keys[i] ); + crypto::ThresholdUtils::fieldElementToString( secret_keys[i] ); outdata["BLSPublicKey"][std::to_string( i )]["0"] = - ThresholdUtils::fieldElementToString( public_keys[i].X.c0 ); + crypto::ThresholdUtils::fieldElementToString( public_keys[i].X.c0 ); outdata["BLSPublicKey"][std::to_string( i )]["1"] = - ThresholdUtils::fieldElementToString( public_keys[i].X.c1 ); + crypto::ThresholdUtils::fieldElementToString( public_keys[i].X.c1 ); outdata["BLSPublicKey"][std::to_string( i )]["2"] = - ThresholdUtils::fieldElementToString( public_keys[i].Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( public_keys[i].Y.c0 ); outdata["BLSPublicKey"][std::to_string( i )]["3"] = - ThresholdUtils::fieldElementToString( public_keys[i].Y.c1 ); + crypto::ThresholdUtils::fieldElementToString( public_keys[i].Y.c1 ); } outfile << outdata.dump( 4 ) << '\n'; diff --git a/tools/hash_g1.cpp b/tools/hash_g1.cpp index 96558a70..11436727 100644 --- a/tools/hash_g1.cpp +++ b/tools/hash_g1.cpp @@ -93,8 +93,8 @@ void hash_g1( const size_t t, const size_t n ) { nlohmann::json joG1 = nlohmann::json::object(); joG1["g1"] = nlohmann::json::object(); joG1["g1"]["hashPoint"] = nlohmann::json::object(); - joG1["g1"]["hashPoint"]["X"] = ThresholdUtils::fieldElementToString( p2vals.first.X ); - joG1["g1"]["hashPoint"]["Y"] = ThresholdUtils::fieldElementToString( p2vals.first.Y ); + joG1["g1"]["hashPoint"]["X"] = crypto::ThresholdUtils::fieldElementToString( p2vals.first.X ); + joG1["g1"]["hashPoint"]["Y"] = crypto::ThresholdUtils::fieldElementToString( p2vals.first.Y ); joG1["g1"]["hint"] = p2vals.second; std::ofstream g1_file( "g1.json" ); diff --git a/tools/sign_bls.cpp b/tools/sign_bls.cpp index 046e8923..ec0a5bb4 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -88,7 +88,7 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream } } - libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); + libff::alt_bn128_G1 hash = crypto::ThresholdUtils::HashtoG1( hash_bytes_arr ); nlohmann::json hash_json; hash_json["message"] = message; @@ -119,7 +119,7 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream } std::vector< libff::alt_bn128_Fr > lagrange_coeffs = - ThresholdUtils::LagrangeCoeffs( idx, t ); + crypto::ThresholdUtils::LagrangeCoeffs( idx, t ); common_signature = bls_instance.SignatureRecover( signature_shares, lagrange_coeffs ); } else { @@ -143,8 +143,10 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream signature["index"] = std::to_string( idx ); } - signature["signature"]["X"] = ThresholdUtils::fieldElementToString( common_signature.X ); - signature["signature"]["Y"] = ThresholdUtils::fieldElementToString( common_signature.Y ); + signature["signature"]["X"] = + crypto::ThresholdUtils::fieldElementToString( common_signature.X ); + signature["signature"]["Y"] = + crypto::ThresholdUtils::fieldElementToString( common_signature.Y ); std::ofstream outfile_h( "hash.json" ); outfile_h << hash_json.dump( 4 ) << "\n"; diff --git a/tools/utils.cpp b/tools/utils.cpp index e05e6a8a..075b28a4 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -24,6 +24,9 @@ #include #include +namespace crypto { + + std::atomic< bool > ThresholdUtils::is_initialized = false; std::mutex initMutex; @@ -66,8 +69,7 @@ std::vector< std::string > ThresholdUtils::G2ToString( libff::alt_bn128_G2 elem std::vector< libff::alt_bn128_Fr > ThresholdUtils::LagrangeCoeffs( const std::vector< size_t >& idx, size_t t ) { if ( idx.size() < t ) { - // throw IncorrectInput( "not enough participants in the threshold group" ); - throw std::runtime_error( "not enough participants in the threshold group" ); + throw IncorrectInput( "not enough participants in the threshold group" ); } std::vector< libff::alt_bn128_Fr > res( t ); @@ -84,9 +86,7 @@ std::vector< libff::alt_bn128_Fr > ThresholdUtils::LagrangeCoeffs( for ( size_t j = 0; j < t; ++j ) { if ( j != i ) { if ( libff::alt_bn128_Fr( idx[i] ) == libff::alt_bn128_Fr( idx[j] ) ) { - // throw IncorrectInput( - // "during the interpolation, have same indexes in list of indexes" ); - throw std::runtime_error( + throw IncorrectInput( "during the interpolation, have same indexes in list of indexes" ); } @@ -176,10 +176,10 @@ bool ThresholdUtils::isStringNumber( std::string& str ) { void ThresholdUtils::checkCypher( const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cyphertext ) { if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) - throw std::runtime_error( "zero element in cyphertext" ); + throw IncorrectInput( "zero element in cyphertext" ); if ( std::get< 1 >( cyphertext ).length() != 64 ) - throw std::runtime_error( "wrong string length in cyphertext" ); + throw IncorrectInput( "wrong string length in cyphertext" ); } std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( @@ -187,7 +187,7 @@ std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( auto position = _hint.find( ":" ); if ( position == std::string::npos ) { - throw std::runtime_error( "Misformatted hint" ); + throw IncorrectInput( "Misformatted hint" ); } libff::alt_bn128_Fq y( _hint.substr( 0, position ).c_str() ); @@ -198,7 +198,9 @@ std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( std::shared_ptr< std::string > str, const std::string& delim ) { - // CHECK( str ); + if ( !str ) { + throw IncorrectInput( " str pointer is null in SplitString " ); + } std::vector< std::string > tokens; size_t prev = 0, pos = 0; @@ -214,3 +216,5 @@ std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( return std::make_shared< std::vector< std::string > >( tokens ); } + +} // namespace crypto diff --git a/tools/utils.h b/tools/utils.h index 3107049b..5f7a40ea 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -32,8 +32,42 @@ #include +namespace crypto { + class ThresholdUtils { +private: + class Exception : public std::exception { + protected: + std::string what_str; + + public: + Exception( const std::string& err_str ) { what_str = err_str; } + + virtual const char* what() const noexcept override { return what_str.c_str(); } + }; + public: + class IsNotWellFormed : public Exception { + public: + IsNotWellFormed( const std::string& err_str ) : Exception( err_str ) { + what_str = "IsNotWellFormedData : " + err_str; + } + }; + + class ZeroSecretKey : public Exception { + public: + ZeroSecretKey( const std::string& err_str ) : Exception( err_str ) { + what_str = "Secret key is equal to zero : " + err_str; + } + }; + + class IncorrectInput : public Exception { + public: + IncorrectInput( const std::string& err_str ) : Exception( err_str ) { + what_str = "Failed to proceed data : " + err_str; + } + }; + static void initCurve(); static std::atomic< bool > is_initialized; @@ -83,4 +117,6 @@ std::string ThresholdUtils::fieldElementToString( const T& field_elem ) { return output; } +} // namespace crypto + #endif // LIBBLS_UTILS_H From 00923510cabacbce76f6d698e051f009e6d6d79a Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 16:48:52 +0300 Subject: [PATCH 19/40] SKALE-4409 update tests --- test/test_TE_wrappers.cpp | 12 ++++++------ test/unit_tests_te.cpp | 2 +- threshold_encryption/threshold_encryption.cpp | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index e79165b1..8a6ff55f 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { crypto::Ciphertext bad_cypher = cypher; // corrupt V in cypher std::get< 1 >( bad_cypher ) = spoilMessage( std::get< 1 >( cypher ) ); - BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), std::runtime_error ); + BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput ); // cannot add after merge BOOST_REQUIRE_THROW( decr_set.addDecrypt( num_signed, nullptr ), std::runtime_error ); @@ -121,13 +121,13 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { libff::alt_bn128_G2 rand_el = libff::alt_bn128_G2::random_element(); std::get< 0 >( bad_cypher ) = rand_el; - BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), std::runtime_error ); + BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput ); bad_cypher = cypher; // corrupt W in cypher libff::alt_bn128_G1 rand_el2 = libff::alt_bn128_G1::random_element(); std::get< 2 >( bad_cypher ) = rand_el2; - BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), std::runtime_error ); + BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput ); size_t ind = rand_gen() % num_signed; // corrupt random private key share @@ -406,7 +406,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { std::get< 1 >( cypher ) = "tra-la-la"; std::get< 2 >( cypher ) = W; - BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), std::runtime_error ); + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -424,7 +424,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { std::get< 1 >( cypher ) = "tra-la-la"; std::get< 2 >( cypher ) = W; - BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), std::runtime_error ); + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -613,7 +613,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; std::get< 2 >( cypher ) = W; - BOOST_REQUIRE_THROW( decr_set.merge( cypher ), std::runtime_error ); + BOOST_REQUIRE_THROW( decr_set.merge( cypher ), crypto::ThresholdUtils::IncorrectInput ); } } } diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 40392bfa..de9829fd 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { BOOST_REQUIRE_THROW( decrypted = obj.getDecryptionShare( corrupted_ciphertext, secret_keys[i] ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 56892df6..e7f6da10 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -120,7 +120,7 @@ libff::alt_bn128_G2 TE::getDecryptionShare( const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key ) { ThresholdUtils::checkCypher( ciphertext ); if ( secret_key.is_zero() ) - throw std::runtime_error( "zero secret key" ); + throw ThresholdUtils::ZeroSecretKey( "zero secret key" ); libff::alt_bn128_G2 U = std::get< 0 >( ciphertext ); @@ -137,7 +137,7 @@ libff::alt_bn128_G2 TE::getDecryptionShare( bool res = fst == snd; if ( !res ) { - throw std::runtime_error( "cannot decrypt data" ); + throw ThresholdUtils::IncorrectInput( "cannot decrypt data" ); } libff::alt_bn128_G2 ret_val = secret_key * U; @@ -200,7 +200,7 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, bool res = fst == snd; if ( !res ) { - throw std::runtime_error( "error during share combining" ); + throw ThresholdUtils::IncorrectInput( "error during share combining" ); } std::vector< size_t > idx( this->t_ ); From 86b2a8bcecd5d51b4c7eccf09a8509faf7a50ad3 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 17:01:11 +0300 Subject: [PATCH 20/40] SKALE-4409 update tests --- test/test_TE_wrappers.cpp | 15 ++++++++++----- threshold_encryption/TEDecryptSet.cpp | 11 ++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 8a6ff55f..72ce21fa 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -115,7 +115,8 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) { BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput ); // cannot add after merge - BOOST_REQUIRE_THROW( decr_set.addDecrypt( num_signed, nullptr ), std::runtime_error ); + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( num_signed, nullptr ), crypto::ThresholdUtils::IncorrectInput ); bad_cypher = cypher; // corrupt U in cypher libff::alt_bn128_G2 rand_el = libff::alt_bn128_G2::random_element(); @@ -552,7 +553,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { auto el_ptr2 = std::make_shared< libff::alt_bn128_G2 >( el2 ); decr_set.addDecrypt( 1, el_ptr1 ); - BOOST_REQUIRE_THROW( decr_set.addDecrypt( 1, el_ptr2 ), std::runtime_error ); + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( 1, el_ptr2 ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -562,7 +564,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero(); auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); - BOOST_REQUIRE_THROW( decr_set.addDecrypt( 1, el_ptr1 ), std::runtime_error ); + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( 1, el_ptr1 ), crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -572,7 +575,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); el_ptr1 = nullptr; - BOOST_REQUIRE_THROW( decr_set.addDecrypt( 1, el_ptr1 ), std::runtime_error ); + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( 1, el_ptr1 ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -593,7 +597,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; std::get< 2 >( cypher ) = W; - BOOST_REQUIRE_THROW( decr_set.merge( cypher ), std::runtime_error ); + BOOST_REQUIRE_THROW( + decr_set.merge( cypher ), crypto::ThresholdUtils::IsNotWellFormed ); } { diff --git a/threshold_encryption/TEDecryptSet.cpp b/threshold_encryption/TEDecryptSet.cpp index d7392af3..176dfda9 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -36,19 +36,20 @@ TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) void TEDecryptSet::addDecrypt( size_t _signerIndex, std::shared_ptr< libff::alt_bn128_G2 > _el ) { if ( decrypts.count( _signerIndex ) > 0 ) { - throw std::runtime_error( "Already have this index:" + std::to_string( _signerIndex ) ); + throw crypto::ThresholdUtils::IncorrectInput( + "Already have this index:" + std::to_string( _signerIndex ) ); } if ( was_merged ) { - throw std::runtime_error( "Invalid state" ); + throw crypto::ThresholdUtils::IncorrectInput( "Invalid state" ); } if ( !_el ) { - throw std::runtime_error( "try to add Null _element to decrypt set" ); + throw crypto::ThresholdUtils::IncorrectInput( "try to add Null _element to decrypt set" ); } if ( _el->is_zero() ) { - throw std::runtime_error( "try to add zero _element to decrypt set" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "try to add zero _element to decrypt set" ); } decrypts[_signerIndex] = _el; @@ -60,7 +61,7 @@ std::string TEDecryptSet::merge( const crypto::Ciphertext& cyphertext ) { was_merged = true; if ( decrypts.size() < requiredSigners ) { - throw std::runtime_error( "Not enough elements to decrypt message" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Not enough elements to decrypt message" ); } crypto::TE te( requiredSigners, totalSigners ); From 1f1a4c6f55fcc6c09e214482020934feffe12a0e Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 19:35:58 +0300 Subject: [PATCH 21/40] SKALE-4409 add exceptions, update tests --- test/test_TE_wrappers.cpp | 13 +++++++------ threshold_encryption/TEPublicKeyShare.cpp | 13 ++++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 72ce21fa..ad9f9dc9 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -362,8 +362,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( crypto::ThresholdUtils::checkSigners( 0, 0 ), std::runtime_error ); // null public key share - BOOST_REQUIRE_THROW( - TEPublicKeyShare( nullptr, 1, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEPublicKeyShare( nullptr, 1, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); { // 1 coord of public key share is not digit @@ -371,7 +371,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -389,7 +389,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -446,7 +446,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_G2 decrypt = libff::alt_bn128_G2::zero(); - BOOST_REQUIRE_THROW( pkey.Verify( cypher, decrypt ), std::runtime_error ); + BOOST_REQUIRE_THROW( + pkey.Verify( cypher, decrypt ), crypto::ThresholdUtils::IsNotWellFormed ); } { diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index 09d79072..5efb0734 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -32,18 +32,20 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { - throw std::runtime_error( "public key share is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "public key share is null" ); } if ( _key_str_ptr->size() != 4 ) { - throw std::runtime_error( "wrong number of components in public key share" ); + throw crypto::ThresholdUtils::IncorrectInput( + "wrong number of components in public key share" ); } if ( !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { - throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); + throw crypto::ThresholdUtils::IncorrectInput( + "non-digit symbol or first zero in non-zero public key share" ); } libff::init_alt_bn128_params(); @@ -55,7 +57,8 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > PublicKey.Y.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 3 ).c_str() ); if ( PublicKey.is_zero() ) { - throw std::runtime_error( "corrupted string or zero public key share" ); + throw crypto::ThresholdUtils::IsNotWellFormed( + "corrupted string or zero public key share" ); } } @@ -74,7 +77,7 @@ bool TEPublicKeyShare::Verify( const crypto::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { crypto::ThresholdUtils::checkCypher( cyphertext ); if ( decryptionShare.is_zero() ) { - throw std::runtime_error( "zero decrypt" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "zero decrypt" ); } crypto::TE te( requiredSigners, totalSigners ); From 02e898489ae6227d79aa99cffcfb5b8482edec72 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 19:52:07 +0300 Subject: [PATCH 22/40] SKALE-4409 add exceptions, update tests --- test/test_TE_wrappers.cpp | 23 +++++++++++-------- threshold_encryption/TEPrivateKey.cpp | 4 ++-- threshold_encryption/TEPublicKey.cpp | 33 +++++++++++---------------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index ad9f9dc9..ee73a2d3 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -473,16 +473,17 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // null public key - BOOST_REQUIRE_THROW( TEPublicKey( nullptr, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEPublicKey( nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { - // zero public key - std::vector< std::string > pkey_str( {"0", "0"} ); + // wrong formated public key + std::vector< std::string > pkey_str( {"0", "0", "0", "0"} ); BOOST_REQUIRE_THROW( TEPublicKey( std::make_shared< std::vector< std::string > >( pkey_str ), num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -490,13 +491,14 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); BOOST_REQUIRE_THROW( TEPublicKey pkey( TEPrivateKey( el, num_signed, num_all ), num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::IsNotWellFormed ); } { // zero public key libff::alt_bn128_G2 el = libff::alt_bn128_G2::zero(); - BOOST_REQUIRE_THROW( TEPublicKey pkey( el, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEPublicKey pkey( el, num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -505,7 +507,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { TEPublicKey pkey( el, num_signed, num_all ); - BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), std::runtime_error ); + BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -515,7 +517,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { TEPublicKey pkey( el, num_signed, num_all ); BOOST_REQUIRE_THROW( pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -528,13 +530,14 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { std::string zero_str = "0"; BOOST_REQUIRE_THROW( TEPrivateKey( std::make_shared< std::string >( zero_str ), num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::IsNotWellFormed ); } { // zero private key libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - BOOST_REQUIRE_THROW( TEPrivateKey( el, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( + TEPrivateKey( el, num_signed, num_all ), crypto::ThresholdUtils::IsNotWellFormed ); } { diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 7a00de7b..9c725824 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -38,7 +38,7 @@ TEPrivateKey::TEPrivateKey( privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); if ( privateKey.is_zero() ) { - throw std::runtime_error( "private key is zero" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "private key is zero" ); } } @@ -50,7 +50,7 @@ TEPrivateKey::TEPrivateKey( libff::init_alt_bn128_params(); if ( _skey.is_zero() ) - throw std::runtime_error( "private key is zero" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "private key is zero" ); } std::string TEPrivateKey::toString() const { diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 877e35c0..0a1445b0 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -34,18 +34,20 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str_ptr ) { - throw std::runtime_error( "public key is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "public key is null" ); } if ( _key_str_ptr->size() != 4 ) { - throw std::runtime_error( "wrong number of components in public key share" ); + throw crypto::ThresholdUtils::IncorrectInput( + "wrong number of components in public key share" ); } if ( !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 0 ) ) || !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 1 ) ) || !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 2 ) ) || !crypto::ThresholdUtils::isStringNumber( _key_str_ptr->at( 3 ) ) ) { - throw std::runtime_error( "non-digit symbol or first zero in non-zero public key share" ); + throw crypto::ThresholdUtils::IncorrectInput( + "non-digit symbol or first zero in non-zero public key share" ); } libff::init_alt_bn128_params(); @@ -56,8 +58,8 @@ TEPublicKey::TEPublicKey( std::shared_ptr< std::vector< std::string > > _key_str PublicKey.Y.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 2 ).c_str() ); PublicKey.Y.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 3 ).c_str() ); - if ( PublicKey.is_zero() ) { - throw std::runtime_error( "corrupted string or zero public key" ); + if ( PublicKey.is_zero() || !PublicKey.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "corrupted string or zero public key" ); } } @@ -69,7 +71,7 @@ TEPublicKey::TEPublicKey( libff::init_alt_bn128_params(); if ( _common_private.getPrivateKey().is_zero() ) { - throw std::runtime_error( "zero key" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "zero key" ); } PublicKey = _common_private.getPrivateKey() * libff::alt_bn128_G2::one(); @@ -80,7 +82,7 @@ TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, si crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _pkey.is_zero() ) { - throw std::runtime_error( "zero public key" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "zero public key" ); } } @@ -88,27 +90,18 @@ crypto::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr crypto::TE te( requiredSigners, totalSigners ); if ( mes_ptr == nullptr ) { - throw std::runtime_error( "Message is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Message is null" ); } if ( mes_ptr->length() != 64 ) { - throw std::runtime_error( "Message length is not equal to 64" ); + throw crypto::ThresholdUtils::IncorrectInput( "Message length is not equal to 64" ); } crypto::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); crypto::ThresholdUtils::checkCypher( cypher ); - libff::alt_bn128_G2 U = std::get< 0 >( cypher ); - /*if (element_item_count(U) == 0 ) { - throw std::runtime_error("U is zero"); - }*/ - - libff::alt_bn128_G1 W = std::get< 2 >( cypher ); - /*if (element_item_count(W) == 0) { - throw std::runtime_error("W is zero"); - }*/ - - return std::make_tuple( U, std::get< 1 >( cypher ), W ); + return std::make_tuple( + std::get< 0 >( cypher ), std::get< 1 >( cypher ), std::get< 2 >( cypher ) ); } std::shared_ptr< std::vector< std::string > > TEPublicKey::toString() { From 1b757f7452b4306f749be13c583aed88ade5f977 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 19:57:03 +0300 Subject: [PATCH 23/40] SKALE-4409 add exceptions, update tests --- test/test_TE_wrappers.cpp | 3 ++- threshold_encryption/TEPrivateKey.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index ee73a2d3..63f0d533 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -522,7 +522,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // null private key - BOOST_REQUIRE_THROW( TEPrivateKey( nullptr, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEPrivateKey( nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 9c725824..75009f76 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.cpp @@ -30,7 +30,7 @@ TEPrivateKey::TEPrivateKey( crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { - throw std::runtime_error( "private key is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "private key is null" ); } libff::init_alt_bn128_params(); From a3757e114042ed00f2ba2983fd0ed3b64f7d30e0 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 20:02:46 +0300 Subject: [PATCH 24/40] SKALE-4409 add exceptions, update tests --- test/test_TE_wrappers.cpp | 10 +++++----- threshold_encryption/TEPrivateKeyShare.cpp | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 63f0d533..fbf62f79 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -452,8 +452,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // null private key share - BOOST_REQUIRE_THROW( - TEPrivateKeyShare( nullptr, 1, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEPrivateKeyShare( nullptr, 1, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -461,14 +461,14 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { std::string zero_str = "0"; BOOST_REQUIRE_THROW( TEPrivateKeyShare( std::make_shared< std::string >( zero_str ), 1, num_signed, num_all ), - std::runtime_error ); + crypto::ThresholdUtils::ZeroSecretKey ); } { // zero private key share libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - BOOST_REQUIRE_THROW( - TEPrivateKeyShare( el, 1, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEPrivateKeyShare( el, 1, num_signed, num_all ), + crypto::ThresholdUtils::ZeroSecretKey ); } { diff --git a/threshold_encryption/TEPrivateKeyShare.cpp b/threshold_encryption/TEPrivateKeyShare.cpp index 3c435e22..f6cb717b 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -33,7 +33,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, s crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { - throw std::runtime_error( "private key share is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "private key share is null" ); } libff::init_alt_bn128_params(); @@ -41,7 +41,7 @@ TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, s privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); if ( privateKey.is_zero() ) { - throw std::runtime_error( "Zero private key share" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "Zero private key share" ); } } @@ -54,13 +54,13 @@ TEPrivateKeyShare::TEPrivateKeyShare( libff::alt_bn128_Fr _skey_share, size_t _s crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _signerIndex > _totalSigners ) { - throw std::runtime_error( "Wrong _signerIndex" ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong _signerIndex" ); } libff::init_alt_bn128_params(); if ( _skey_share.is_zero() ) { - throw std::runtime_error( "Zero private key share" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "Zero private key share" ); } } @@ -71,8 +71,8 @@ libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( crypto::Ciphertext& c libff::alt_bn128_G2 decryption_share = te.getDecryptionShare( cipher, privateKey ); - if ( decryption_share.is_zero() ) { - std::runtime_error( "zero decrypt" ); + if ( decryption_share.is_zero() || !decryption_share.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "zero decrypt" ); } return decryption_share; From 8403cf1b468d3a2d4856135974fde2c0df880db4 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 20:09:00 +0300 Subject: [PATCH 25/40] SKALE-4409 more checks --- threshold_encryption/TEPublicKey.cpp | 4 ++-- threshold_encryption/TEPublicKeyShare.cpp | 5 +++-- threshold_encryption/threshold_encryption.cpp | 1 - 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 0a1445b0..6cb90265 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -81,8 +81,8 @@ TEPublicKey::TEPublicKey( libff::alt_bn128_G2 _pkey, size_t _requiredSigners, si : PublicKey( _pkey ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - if ( _pkey.is_zero() ) { - throw crypto::ThresholdUtils::IsNotWellFormed( "zero public key" ); + if ( _pkey.is_zero() || !_pkey.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "zero or corrupted public key" ); } } diff --git a/threshold_encryption/TEPublicKeyShare.cpp b/threshold_encryption/TEPublicKeyShare.cpp index 5efb0734..5b21385c 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -35,6 +35,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > throw crypto::ThresholdUtils::IncorrectInput( "public key share is null" ); } + // assume only using affine coordinates if ( _key_str_ptr->size() != 4 ) { throw crypto::ThresholdUtils::IncorrectInput( "wrong number of components in public key share" ); @@ -56,7 +57,7 @@ TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > PublicKey.Y.c0 = libff::alt_bn128_Fq( _key_str_ptr->at( 2 ).c_str() ); PublicKey.Y.c1 = libff::alt_bn128_Fq( _key_str_ptr->at( 3 ).c_str() ); - if ( PublicKey.is_zero() ) { + if ( PublicKey.is_zero() || !PublicKey.is_well_formed() ) { throw crypto::ThresholdUtils::IsNotWellFormed( "corrupted string or zero public key share" ); } @@ -76,7 +77,7 @@ TEPublicKeyShare::TEPublicKeyShare( bool TEPublicKeyShare::Verify( const crypto::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { crypto::ThresholdUtils::checkCypher( cyphertext ); - if ( decryptionShare.is_zero() ) { + if ( decryptionShare.is_zero() || !decryptionShare.is_well_formed() ) { throw crypto::ThresholdUtils::IsNotWellFormed( "zero decrypt" ); } diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index e7f6da10..5dd9f316 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -95,7 +95,6 @@ Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& c rhs_to_hash[i] = static_cast< uint8_t >( message[i] ); } - std::valarray< uint8_t > res = lhs_to_hash ^ rhs_to_hash; std::string V = ""; From 6c6ad5b2091642592ff006c2e858a9b4afeea029 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 20:22:15 +0300 Subject: [PATCH 26/40] SKALE-4409 add exceptions, update tests --- test/test_TE_wrappers.cpp | 11 ++++++----- tools/utils.cpp | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index fbf62f79..811ad1d8 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -356,10 +356,11 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % num_all + 1; - BOOST_REQUIRE_THROW( - crypto::ThresholdUtils::checkSigners( 0, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::checkSigners( 0, num_all ), + crypto::ThresholdUtils::IncorrectInput ); - BOOST_REQUIRE_THROW( crypto::ThresholdUtils::checkSigners( 0, 0 ), std::runtime_error ); + BOOST_REQUIRE_THROW( + crypto::ThresholdUtils::checkSigners( 0, 0 ), crypto::ThresholdUtils::IncorrectInput ); // null public key share BOOST_REQUIRE_THROW( TEPublicKeyShare( nullptr, 1, num_signed, num_all ), @@ -543,8 +544,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { //_requiredSigners > _totalSigners - BOOST_REQUIRE_THROW( - TEDecryptSet decr_set( num_all + 1, num_signed ), std::runtime_error ); + BOOST_REQUIRE_THROW( TEDecryptSet decr_set( num_all + 1, num_signed ), + crypto::ThresholdUtils::IsNotWellFormed ); } { diff --git a/tools/utils.cpp b/tools/utils.cpp index 075b28a4..210e6479 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -41,15 +41,15 @@ void ThresholdUtils::initCurve() { void ThresholdUtils::checkSigners( size_t _requiredSigners, size_t _totalSigners ) { if ( _requiredSigners > _totalSigners ) { - throw std::runtime_error( "_requiredSigners > _totalSigners" ); + throw IsNotWellFormed( "_requiredSigners > _totalSigners" ); } if ( _totalSigners == 0 ) { - throw std::runtime_error( "_totalSigners == 0" ); + throw IncorrectInput( "_totalSigners == 0" ); } if ( _requiredSigners == 0 ) { - throw std::runtime_error( "_requiredSigners == 0" ); + throw IncorrectInput( "_requiredSigners == 0" ); } } From 3ebc51a88ae3cca2a4c9ff96be5c366336691d3f Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 20:45:14 +0300 Subject: [PATCH 27/40] SKALE-4409 add exceptions, update tests --- bls/BLSPrivateKey.cpp | 2 +- bls/BLSPrivateKey.h | 1 - bls/BLSPrivateKeyShare.cpp | 7 +++---- bls/BLSPublicKey.cpp | 10 +++++----- bls/BLSPublicKeyShare.cpp | 7 +++---- bls/BLSSigShareSet.cpp | 2 -- bls/BLSSignature.cpp | 17 ++--------------- bls/BLSSignature.h | 1 - dkg/DKGBLSSecret.cpp | 8 ++++---- dkg/DKGBLSWrapper.cpp | 21 +++++++++++---------- dkg/DKGTESecret.cpp | 2 +- dkg/DKGTEWrapper.cpp | 12 ++++++------ test/test_bls.cpp | 18 ++++++++++-------- 13 files changed, 46 insertions(+), 62 deletions(-) diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 2bcbfbff..2e93d8e0 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -31,7 +31,7 @@ BLSPrivateKey::BLSPrivateKey( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { crypto::ThresholdUtils::initCurve(); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _key == nullptr ) { throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is null" ); } diff --git a/bls/BLSPrivateKey.h b/bls/BLSPrivateKey.h index f656b676..2ccc00f2 100644 --- a/bls/BLSPrivateKey.h +++ b/bls/BLSPrivateKey.h @@ -26,7 +26,6 @@ #include -#include #include diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index 9af68d0c..d8515b83 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -32,7 +31,7 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( const std::string& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); crypto::ThresholdUtils::initCurve(); if ( _key.empty() ) { throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" ); @@ -48,7 +47,7 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( BLSPrivateKeyShare::BLSPrivateKeyShare( const libff::alt_bn128_Fr& libff_skey, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); privateKey = std::make_shared< libff::alt_bn128_Fr >( libff_skey ); @@ -120,7 +119,7 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper( std::shared_ptr< std::pair< std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >, std::shared_ptr< BLSPublicKey > > > BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); std::vector< std::shared_ptr< BLSPrivateKeyShare > > skeys_shares; diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index 954a0ae5..65ffd46d 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -34,7 +34,7 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > CHECK( pkey_str_vect ) - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >(); @@ -59,7 +59,7 @@ BLSPublicKey::BLSPublicKey( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { crypto::ThresholdUtils::initCurve(); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( pkey ); if ( libffPublicKey->is_zero() ) { @@ -70,7 +70,7 @@ BLSPublicKey::BLSPublicKey( BLSPublicKey::BLSPublicKey( const libff::alt_bn128_Fr& skey, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( skey * libff::alt_bn128_G2::one() ); if ( libffPublicKey->is_zero() ) { throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); @@ -90,7 +90,7 @@ bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ crypto::ThresholdUtils::initCurve(); std::shared_ptr< crypto::Bls > obj; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); @@ -109,7 +109,7 @@ bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { std::shared_ptr< crypto::Bls > obj; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); } diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index 3d6afa67..90f3a3d7 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -33,7 +32,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { CHECK( pkey_str_vect ); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); crypto::ThresholdUtils::initCurve(); @@ -89,7 +88,7 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > CHECK( sign_ptr ); std::shared_ptr< crypto::Bls > obj; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( sign_ptr->getSigShare()->is_zero() ) { throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Sig share" ); @@ -106,7 +105,7 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_ CHECK( sign_ptr ) std::shared_ptr< crypto::Bls > obj; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); } diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp index e2390016..4e349dd0 100644 --- a/bls/BLSSigShareSet.cpp +++ b/bls/BLSSigShareSet.cpp @@ -56,7 +56,6 @@ std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index "Index out of range:" + std::to_string( _index ) ); } - if ( sigShares.count( _index ) == 0 ) { return nullptr; } @@ -74,7 +73,6 @@ bool BLSSigShareSet::isEnough() { return ( sigShares.size() >= requiredSigners ); } - std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { if ( !isEnough() ) throw crypto::ThresholdUtils::IncorrectInput( "Not enough shares to create signature" ); diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp index ebc04aca..423deec1 100644 --- a/bls/BLSSignature.cpp +++ b/bls/BLSSignature.cpp @@ -34,13 +34,12 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st hint( _hint ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); CHECK( sig ); crypto::ThresholdUtils::initCurve(); - if ( sig->is_zero() ) { throw crypto::ThresholdUtils::IncorrectInput( "Zero BLS signature" ); } @@ -54,7 +53,7 @@ BLSSignature::BLSSignature( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { CHECK( _sig ); - BLSSignature::checkSigners( requiredSigners, totalSigners ); + crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners ); crypto::ThresholdUtils::initCurve(); @@ -102,18 +101,6 @@ std::shared_ptr< std::string > BLSSignature::toString() { return std::make_shared< std::string >( str ); } -void BLSSignature::checkSigners( size_t _requiredSigners, size_t _totalSigners ) { - CHECK( _totalSigners > 0 ); - - if ( _requiredSigners > _totalSigners ) { - throw crypto::ThresholdUtils::IncorrectInput( "_requiredSigners > _totalSigners" ); - } - - - if ( _totalSigners == 0 ) { - throw crypto::ThresholdUtils::IncorrectInput( "_totalSigners == 0" ); - } -} std::string BLSSignature::getHint() const { return hint; diff --git a/bls/BLSSignature.h b/bls/BLSSignature.h index 8f832c6c..e35ffc0e 100644 --- a/bls/BLSSignature.h +++ b/bls/BLSSignature.h @@ -41,7 +41,6 @@ class BLSSignature { std::shared_ptr< libff::alt_bn128_G1 > getSig() const; std::shared_ptr< std::string > toString(); - static void checkSigners( size_t _requiredSigners, size_t _totalSigners ); std::string getHint() const; size_t getTotalSigners() const; size_t getRequiredSigners() const; diff --git a/dkg/DKGBLSSecret.cpp b/dkg/DKGBLSSecret.cpp index b0c4fa8c..e7fd549e 100644 --- a/dkg/DKGBLSSecret.cpp +++ b/dkg/DKGBLSSecret.cpp @@ -21,14 +21,14 @@ @date 2019 */ -#include "DKGBLSSecret.h" +#include -#include #include +#include DKGBLSSecret::DKGBLSSecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); crypto::Dkg dkg( requiredSigners, totalSigners ); poly = dkg.GeneratePolynomial(); @@ -36,7 +36,7 @@ DKGBLSSecret::DKGBLSSecret( size_t _requiredSigners, size_t _totalSigners ) void DKGBLSSecret::setPoly( std::vector< libff::alt_bn128_Fr > _poly ) { if ( _poly.size() != requiredSigners ) { - throw std::runtime_error( "Wrong size of vector" ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of vector" ); } poly = _poly; } diff --git a/dkg/DKGBLSWrapper.cpp b/dkg/DKGBLSWrapper.cpp index d3b083ab..c86b415d 100644 --- a/dkg/DKGBLSWrapper.cpp +++ b/dkg/DKGBLSWrapper.cpp @@ -14,21 +14,22 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License - along with libBLS. If not, see . + along with libBLS. If not, see . @file TEPrivateKeyShare.h @author Sveta Rogova @date 2019 */ -#include "DKGBLSWrapper.h" +#include -#include #include +#include + DKGBLSWrapper::DKGBLSWrapper( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); DKGBLSSecret temp( _requiredSigners, _totalSigners ); dkg_secret_ptr = std::make_shared< DKGBLSSecret >( temp ); @@ -37,12 +38,12 @@ DKGBLSWrapper::DKGBLSWrapper( size_t _requiredSigners, size_t _totalSigners ) bool DKGBLSWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share, std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ) { if ( _share.is_zero() ) - throw std::runtime_error( " Zero secret share" ); + throw crypto::ThresholdUtils::ZeroSecretKey( " Zero secret share" ); if ( _verification_vector == nullptr ) { - throw std::runtime_error( " Null verification vector" ); + throw crypto::ThresholdUtils::IncorrectInput( " Null verification vector" ); } if ( _verification_vector->size() != requiredSigners ) - throw std::runtime_error( "Wrong vector size" ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong vector size" ); crypto::Dkg dkg( requiredSigners, totalSigners ); return dkg.Verification( _signerIndex, _share, *_verification_vector ); } @@ -50,7 +51,7 @@ bool DKGBLSWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_ void DKGBLSWrapper::setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ) { if ( _poly_ptr == nullptr ) - throw std::runtime_error( "Null polynomial ptr" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null polynomial ptr" ); dkg_secret_ptr->setPoly( *_poly_ptr ); } @@ -67,10 +68,10 @@ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > DKGBLSWrapper::createDKGPu BLSPrivateKeyShare DKGBLSWrapper::CreateBLSPrivateKeyShare( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) { if ( secret_shares_ptr == nullptr ) - throw std::runtime_error( "Null secret_shares_ptr " ); + throw crypto::ThresholdUtils::IncorrectInput( "Null secret_shares_ptr " ); if ( secret_shares_ptr->size() != totalSigners ) - throw std::runtime_error( "Wrong number of secret key parts " ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong number of secret key parts " ); crypto::Dkg dkg( requiredSigners, totalSigners ); diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp index 11723019..42c142c1 100644 --- a/dkg/DKGTESecret.cpp +++ b/dkg/DKGTESecret.cpp @@ -39,7 +39,7 @@ DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) void DKGTESecret::setPoly( std::vector< libff::alt_bn128_Fr >& _poly ) { if ( _poly.size() != requiredSigners ) { - throw std::runtime_error( "Wrong size of vector" ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of vector" ); } poly = _poly; diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index d949ae27..28268f71 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -38,11 +38,11 @@ DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share, std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ) { if ( _share.is_zero() ) - throw std::runtime_error( "Zero secret share" ); + throw crypto::ThresholdUtils::ZeroSecretKey( "Zero secret share" ); if ( _verification_vector == nullptr ) - throw std::runtime_error( "Null verification vector" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null verification vector" ); if ( _verification_vector->size() != requiredSigners ) - throw std::runtime_error( "Wrong size of verification vector" ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of verification vector" ); crypto::Dkg dkg_te( requiredSigners, totalSigners ); return dkg_te.Verification( _signerIndex, _share, *_verification_vector ); } @@ -66,9 +66,9 @@ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > DKGTEWrapper::createDKGPub TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( size_t signerIndex_, std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) { if ( secret_shares_ptr == nullptr ) - throw std::runtime_error( "Null secret_shares_ptr " ); + throw crypto::ThresholdUtils::IncorrectInput( "Null secret_shares_ptr " ); if ( secret_shares_ptr->size() != totalSigners ) - throw std::runtime_error( "Wrong number of secret key parts " ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong number of secret key parts " ); crypto::Dkg dkg_te( requiredSigners, totalSigners ); @@ -83,7 +83,7 @@ TEPublicKey DKGTEWrapper::CreateTEPublicKey( crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( public_shares_all == nullptr ) - throw std::runtime_error( "Null public shares all" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null public shares all" ); libff::alt_bn128_G2 public_key = libff::alt_bn128_G2::zero(); diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 6a03bce2..cbbba090 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -975,20 +975,20 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { std::vector< libff::alt_bn128_G2 > vect = {libff::alt_bn128_G2::random_element()}; BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ), - std::runtime_error ); + crypto::ThresholdUtils::ZeroSecretKey ); } { DKGBLSWrapper dkg_wrap( num_signed, num_all ); BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), nullptr ), - std::runtime_error ); + crypto::ThresholdUtils::ZeroSecretKey ); } { DKGBLSWrapper dkg_wrap( num_signed, num_all ); BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), nullptr ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -996,12 +996,13 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { std::vector< libff::alt_bn128_G2 > vect = {libff::alt_bn128_G2::random_element()}; BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - BOOST_REQUIRE_THROW( dkg_wrap.setDKGSecret( nullptr ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_wrap.setDKGSecret( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -1009,12 +1010,13 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { std::vector< libff::alt_bn128_Fr > poly; BOOST_REQUIRE_THROW( dkg_wrap.setDKGSecret( std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly ) ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - BOOST_REQUIRE_THROW( dkg_wrap.CreateBLSPrivateKeyShare( nullptr ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_wrap.CreateBLSPrivateKeyShare( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -1022,7 +1024,7 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { std::vector< libff::alt_bn128_Fr > shares; BOOST_REQUIRE_THROW( dkg_wrap.CreateBLSPrivateKeyShare( std::make_shared< std::vector< libff::alt_bn128_Fr > >( shares ) ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } } BOOST_AUTO_TEST_SUITE_END() From 5aba15459a2a99c02b3efae450e90bab7d7b896b Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 29 Jul 2021 21:08:02 +0300 Subject: [PATCH 28/40] SKLAE-4409 fix tests --- dkg/DKGTEWrapper.cpp | 2 +- test/test_TE_wrappers.cpp | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp index 28268f71..4ad5c507 100644 --- a/dkg/DKGTEWrapper.cpp +++ b/dkg/DKGTEWrapper.cpp @@ -49,7 +49,7 @@ bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_F void DKGTEWrapper::setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ) { if ( _poly_ptr == nullptr ) - throw std::runtime_error( "Null polynomial ptr" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null polynomial ptr" ); dkg_secret_ptr->setPoly( *_poly_ptr ); } diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 811ad1d8..89e4cfc2 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -639,8 +639,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); - BOOST_REQUIRE_THROW( - dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ), std::runtime_error ); + BOOST_REQUIRE_THROW( dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ), + crypto::ThresholdUtils::ZeroSecretKey ); } { @@ -648,7 +648,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { DKGTEWrapper dkg_te( num_signed, num_all ); libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); - BOOST_REQUIRE_THROW( dkg_te.VerifyDKGShare( 1, el, nullptr ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_te.VerifyDKGShare( 1, el, nullptr ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -662,7 +663,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { BOOST_REQUIRE_THROW( dkg_te.VerifyDKGShare( 1, el, std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ), - std::runtime_error ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -670,7 +671,8 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares = dkg_te.createDKGSecretShares(); shares = nullptr; - BOOST_REQUIRE_THROW( dkg_te.setDKGSecret( shares ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput ); } { @@ -679,32 +681,35 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) { dkg_te.createDKGSecretShares(); shares->erase( shares->begin() + shares->size() - 2 ); shares->shrink_to_fit(); - BOOST_REQUIRE_THROW( dkg_te.setDKGSecret( shares ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput ); } { DKGTEWrapper dkg_te( num_signed, num_all ); - BOOST_REQUIRE_THROW( dkg_te.CreateTEPrivateKeyShare( 1, nullptr ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_te.CreateTEPrivateKeyShare( 1, nullptr ), crypto::ThresholdUtils::IncorrectInput ); } { DKGTEWrapper dkg_te( num_signed, num_all ); auto wrong_size_vector = std::make_shared< std::vector< libff::alt_bn128_Fr > >(); wrong_size_vector->resize( num_signed - 1 ); - BOOST_REQUIRE_THROW( - dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ), std::runtime_error ); + BOOST_REQUIRE_THROW( dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ), + crypto::ThresholdUtils::IncorrectInput ); } { DKGTEWrapper dkg_te( num_signed, num_all ); std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares; - BOOST_REQUIRE_THROW( dkg_te.setDKGSecret( shares ), std::runtime_error ); + BOOST_REQUIRE_THROW( + dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput ); } { DKGTEWrapper dkg_te( num_signed, num_all ); - BOOST_REQUIRE_THROW( - dkg_te.CreateTEPublicKey( nullptr, num_signed, num_all ), std::runtime_error ); + BOOST_REQUIRE_THROW( dkg_te.CreateTEPublicKey( nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } } From 4fa02b072eb4c06be865eb13a2a21f1dcf05a905 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 10:58:09 +0300 Subject: [PATCH 29/40] SKALE-4409 remove extra dependencies --- README.md | 5 ++--- deps/build.sh | 37 +------------------------------------ deps/clean.sh | 1 - 3 files changed, 3 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 52344003..6ec2adda 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A mathematical library written in C++ that supports BLS threshold signatures, Distributed Key Generation (DKG) and Threshold Encryption (TE). -This libBLS library is developed by SKALE Labs and uses SCIPR-LAB's libff and PBC library by Ben Lynn (see Libraries below). +This libBLS library is developed by SKALE Labs and uses SCIPR-LAB's libff (see Libraries below). ## An important note about production readiness @@ -35,7 +35,7 @@ Encryption process is running as follows: 3. Decryption 4. Verifying and combining shares -You can learn more about the algebraic structures used in this algorithm in [Ben Lynn’s PhD Dissertation](https://crypto.stanford.edu/pbc/thesis.html). libBLS uses a modified [Ben Lynn's pbc library](https://github.com/skalenetwork/pbc) with memory corruption bug fixed and the TYPE A curve for symmetric bilinear pairing. +libBls uses the same alt_bn128 curve for threshold encryption as for BLS signatures. ## Performance Specifications @@ -108,7 +108,6 @@ See [docs](docs) for libBLS documentation. ## Libraries - [libff by SCIPR-LAB](http://www.scipr-lab.org/) -- [pbc by Ben Lynn](https://crypto.stanford.edu/pbc/) with modifications from SKALE Labs ## Contributing diff --git a/deps/build.sh b/deps/build.sh index 56ee5f94..534c70f3 100755 --- a/deps/build.sh +++ b/deps/build.sh @@ -237,7 +237,6 @@ fi WITH_FF="yes" WITH_GMP="yes" -WITH_PBC="yes" if [ -z "${PARALLEL_COUNT}" ]; then @@ -539,7 +538,6 @@ echo -e "${COLOR_VAR_NAME}WITH_JSONRPCCPP${COLOR_DOTS}........${COLOR_VAR_DESC}L echo -e "${COLOR_VAR_NAME}WITH_CRYPTOPP${COLOR_DOTS}..........${COLOR_VAR_DESC}LibCrypto++${COLOR_DOTS}............................${COLOR_VAR_VAL}$WITH_CRYPTOPP${COLOR_RESET}" echo -e "${COLOR_VAR_NAME}WITH_GMP${COLOR_DOTS}...............${COLOR_VAR_DESC}LibGMP${COLOR_DOTS}.................................${COLOR_VAR_VAL}$WITH_GMP${COLOR_RESET}" echo -e "${COLOR_VAR_NAME}WITH_FF${COLOR_DOTS}................${COLOR_VAR_DESC}LibFF${COLOR_DOTS}..................................${COLOR_VAR_VAL}$WITH_FF${COLOR_RESET}" -echo -e "${COLOR_VAR_NAME}WITH_PBC${COLOR_DOTS}...............${COLOR_VAR_DESC}LibPBC${COLOR_DOTS}.................................${COLOR_VAR_VAL}$WITH_PBC${COLOR_RESET}" # # @@ -658,7 +656,7 @@ then echo -e "${COLOR_SEPARATOR}==================== ${COLOR_PROJECT_NAME}GMP${COLOR_SEPARATOR} ==========================================${COLOR_RESET}" if [ ! -f "$INSTALL_ROOT/lib/libgmp.a" ] || [ ! -f "$INSTALL_ROOT/lib/libgmpxx.a" ] || [ ! -f "$INSTALL_ROOT/lib/libgmp.la" ] || [ ! -f "$INSTALL_ROOT/lib/libgmpxx.la" ]; then - # requiired for libff and pbc + # requiired for libff env_restore cd "$SOURCES_ROOT" if [ ! -d "gmp-6.1.2" ]; @@ -712,39 +710,6 @@ then fi fi -if [ "$WITH_PBC" = "yes" ]; -then - echo -e "${COLOR_SEPARATOR}==================== ${COLOR_PROJECT_NAME}PBC${COLOR_SEPARATOR} ==========================================${COLOR_RESET}" - if [ ! -f "$INSTALL_ROOT/lib/libpbc.a" ] || [ ! -f "$INSTALL_ROOT/lib/libpbc.la" ]; - then - env_restore - cd "$SOURCES_ROOT" - if [ ! -d "pbc" ]; - then - echo -e "${COLOR_INFO}getting it from git${COLOR_DOTS}...${COLOR_RESET}" - git clone https://github.com/skalenetwork/pbc.git # pbc - fi - echo -e "${COLOR_INFO}configuring it${COLOR_DOTS}...${COLOR_RESET}" - cd pbc - export CFLAGS="$CFLAGS -I${INSTALL_ROOT}/include" - export CXXFLAGS="$CXXFLAGS -I${INSTALL_ROOT}/include" - export CPPFLAGS="$CPPFLAGS -I${INSTALL_ROOT}/include" - export LDFLAGS="$LDFLAGS -L${INSTALL_ROOT}/lib" - echo " CFLAGS = $CFLAGS" - echo " CXXFLAGS = $CXXFLAGS" - echo " CPPFLAGS = $CPPFLAGS" - echo " LDFLAGS = $LDFLAGS" - $LIBTOOLIZE --force && aclocal && autoheader && automake --force-missing --add-missing && autoconf - ./configure ${CONF_CROSSCOMPILING_OPTS_GENERIC} ${CONF_DEBUG_OPTIONS} --with-pic --enable-static --disable-shared --prefix="$INSTALL_ROOT" - echo -e "${COLOR_INFO}building it${COLOR_DOTS}...${COLOR_RESET}" - $MAKE ${PARALLEL_MAKE_OPTIONS} - $MAKE ${PARALLEL_MAKE_OPTIONS} install - cd "$SOURCES_ROOT" - else - echo -e "${COLOR_SUCCESS}SKIPPED${COLOR_RESET}" - fi -fi - echo -e "${COLOR_SEPARATOR}===================================================================${COLOR_RESET}" echo -e "${COLOR_YELLOW}BLS dependencies build actions...${COLOR_RESET}" echo -e "${COLOR_SEPARATOR}==================== ${COLOR_PROJECT_NAME}FINISH${COLOR_SEPARATOR} =======================================${COLOR_RESET}" diff --git a/deps/clean.sh b/deps/clean.sh index f035b910..62a5370a 100755 --- a/deps/clean.sh +++ b/deps/clean.sh @@ -141,7 +141,6 @@ rm -rf ./libcryptopp # rm -rf ./gtest rm -rf ./gmp-6.1.2 rm -rf ./libff -rm -rf ./pbc echo "Done (all clean)." #finish From 190cf70611a307ed5af7e693682a56475cf76792 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 11:10:30 +0300 Subject: [PATCH 30/40] SKALE-4409 add tests --- test/test_bls.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index cbbba090..9c0e64e7 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -152,6 +152,10 @@ BOOST_AUTO_TEST_CASE( libBls ) { BOOST_REQUIRE_THROW( obj.Verification( hash_ptr, SpoilSignature( signature ), recovered_keys.second ), crypto::ThresholdUtils::IsNotWellFormed ); + + recovered_keys.second.X.c0 = SpoilSignCoord( recovered_keys.second.X.c0 ); + BOOST_REQUIRE_THROW( obj.Verification( hash_ptr, signature, recovered_keys.second ), + crypto::ThresholdUtils::IsNotWellFormed ); } } From 3daec4a6121fc592691deea3b0ddc0b6453d6485 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 12:39:16 +0300 Subject: [PATCH 31/40] SKALE-4409 ignore lines in codecov --- tools/utils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/utils.h b/tools/utils.h index 5f7a40ea..e0adc535 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -43,6 +43,7 @@ class ThresholdUtils { public: Exception( const std::string& err_str ) { what_str = err_str; } + // coverage:ignore-line virtual const char* what() const noexcept override { return what_str.c_str(); } }; From 97b4da8710be58ff529585728e5d45eb270c6cbc Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 13:06:21 +0300 Subject: [PATCH 32/40] SKALE-4409 add tests --- test/test_bls.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 9c0e64e7..9edac1ba 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -724,6 +724,13 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IncorrectInput ); } + { + std::vector< std::string > coords = {"0", "0", "0", "0"}; + auto vector_str_ptr = std::make_shared< std::vector< std::string > >( coords ); + BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( vector_str_ptr, num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); + } + { BLSPublicKeyShare pkey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); std::string hint = "123:1"; @@ -856,6 +863,22 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IncorrectInput ); } + { + std::string hint = "123:1"; + BOOST_REQUIRE_THROW( + BLSSigShare( std::make_shared< libff::alt_bn128_G1 >( libff::alt_bn128_G1::zero() ), + hint, 1, num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); + } + + { + std::string hint = "123:1"; + BOOST_REQUIRE_THROW( BLSSigShare( std::make_shared< libff::alt_bn128_G1 >( + libff::alt_bn128_G1::random_element() ), + hint, 0, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); + } + { std::string short_sig = "1:1:1:1"; BOOST_REQUIRE_THROW( @@ -863,7 +886,6 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IsNotWellFormed ); } - { std::string long_sig; for ( size_t j = 0; j < 3; j++ ) { From b6c8754a76f8fecae08df90a94367220eda94f78 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 13:33:26 +0300 Subject: [PATCH 33/40] SKALE-4409 add tests --- test/test_bls.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 9edac1ba..ba0c6110 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -675,6 +675,13 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IncorrectInput ); } + { + std::vector< std::string > coords = {"0", "0", "0", "0"}; + auto vector_ptr_str = std::make_shared< std::vector< std::string > >( coords ); + BOOST_REQUIRE_THROW( BLSPublicKey pkey( vector_ptr_str, num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); + } + { const std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > null_map = nullptr; @@ -713,6 +720,25 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IncorrectInput ); } + { + BLSPublicKey pkey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); + BOOST_REQUIRE_THROW( + pkey.VerifySig( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), + nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); + } + + { + BLSPublicKey pkey( libff::alt_bn128_Fr::random_element(), num_signed, num_all ); + std::string hint = "123:1"; + BLSSignature rand_sig( + std::make_shared< libff::alt_bn128_G1 >( libff::alt_bn128_G1::random_element() ), hint, + num_signed, num_all ); + BOOST_REQUIRE_THROW( pkey.VerifySig( nullptr, std::make_shared< BLSSignature >( rand_sig ), + num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); + } + { BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), From 81988b6e8323b5cb1cc17a242c43a884f4c87640 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 14:41:22 +0300 Subject: [PATCH 34/40] SKALE-4409 add tests --- bls/BLSPrivateKey.cpp | 2 +- bls/BLSPrivateKey.h | 2 +- test/test_bls.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 2e93d8e0..0539d9f0 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -47,7 +47,7 @@ BLSPrivateKey::BLSPrivateKey( } BLSPrivateKey::BLSPrivateKey( - const std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > > skeys, + std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > > skeys, std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { if ( skeys == nullptr ) { diff --git a/bls/BLSPrivateKey.h b/bls/BLSPrivateKey.h index 2ccc00f2..1bf6bbda 100644 --- a/bls/BLSPrivateKey.h +++ b/bls/BLSPrivateKey.h @@ -40,7 +40,7 @@ class BLSPrivateKey { BLSPrivateKey( const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners ); - BLSPrivateKey( const std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >, + BLSPrivateKey( std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >, std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners, size_t _totalSigners ); diff --git a/test/test_bls.cpp b/test/test_bls.cpp index ba0c6110..2425f823 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -627,6 +627,47 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IncorrectInput ); } + { + // creating a polynomial + std::vector< libff::alt_bn128_Fr > coeffs( 11 ); + + for ( auto& elem : coeffs ) { + elem = libff::alt_bn128_Fr::random_element(); + + while ( elem == 0 ) { + elem = libff::alt_bn128_Fr::random_element(); + } + } + + // make free element zero so the common secret is zero + coeffs[0] = libff::alt_bn128_Fr::zero(); + + std::vector< std::shared_ptr< BLSPrivateKeyShare > > secret_keys( 16 ); + std::vector< size_t > ids( 11 ); + for ( size_t i = 0; i < 16; ++i ) { + if ( i < 11 ) { + ids[i] = i + 1; + } + + libff::alt_bn128_Fr tmp = libff::alt_bn128_Fr::zero(); + + for ( size_t j = 0; j < 11; ++j ) { + tmp = tmp + + coeffs[j] * + libff::power( libff::alt_bn128_Fr( std::to_string( i + 1 ).c_str() ), j ); + } + secret_keys[i] = + std::make_shared< BLSPrivateKeyShare >( BLSPrivateKeyShare( tmp, 11, 16 ) ); + } + + BOOST_REQUIRE_THROW( + BLSPrivateKey skey( + std::make_shared< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >( + secret_keys ), + std::make_shared< std::vector< size_t > >( ids ), 11, 16 ), + crypto::ThresholdUtils::ZeroSecretKey ); + } + { BOOST_REQUIRE_THROW( BLSPrivateKeyShare skey( "", num_signed, num_all ), crypto::ThresholdUtils::IncorrectInput ); From dcfe6365c4b0c6687017ef666dbca659ddcd29dd Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 15:29:13 +0300 Subject: [PATCH 35/40] SKALE-4409 add tests --- test/test_bls.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 2425f823..b713ef83 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -730,6 +730,43 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { crypto::ThresholdUtils::IncorrectInput ); } + { + // creating a polynomial + std::vector< libff::alt_bn128_Fr > coeffs( 11 ); + + for ( auto& elem : coeffs ) { + elem = libff::alt_bn128_Fr::random_element(); + + while ( elem == 0 ) { + elem = libff::alt_bn128_Fr::random_element(); + } + } + + // make free element zero so the common secret is zero + coeffs[0] = libff::alt_bn128_Fr::zero(); + + std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > coeffs_map; + for ( size_t i = 0; i < 16; ++i ) { + libff::alt_bn128_Fr tmp = libff::alt_bn128_Fr::zero(); + + for ( size_t j = 0; j < 11; ++j ) { + tmp = tmp + + coeffs[j] * + libff::power( libff::alt_bn128_Fr( std::to_string( i + 1 ).c_str() ), j ); + } + + if ( i < 11 ) { + coeffs_map[i + 1] = + std::make_shared< BLSPublicKeyShare >( BLSPublicKeyShare( tmp, 11, 16 ) ); + } + } + auto map_ptr = std::make_shared< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > >( + coeffs_map ); + + BOOST_REQUIRE_THROW( + BLSPublicKey pkey( map_ptr, 11, 16 ), crypto::ThresholdUtils::IsNotWellFormed ); + } + { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_G2::zero(), num_signed, num_all ), crypto::ThresholdUtils::IsNotWellFormed ); From 7a0f2207a6cc537b22262b7ef536c041b0cd4502 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Fri, 30 Jul 2021 15:47:18 +0300 Subject: [PATCH 36/40] SKALE-4409 add tests --- VERSION | 2 +- test/test_TE_wrappers.cpp | 7 +++++++ tools/utils.h | 1 - 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 6da28dde..341cf11f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.1 \ No newline at end of file +0.2.0 \ No newline at end of file diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 89e4cfc2..c95cd43a 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -472,6 +472,13 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { crypto::ThresholdUtils::ZeroSecretKey ); } + { + // wrong signer index + BOOST_REQUIRE_THROW( TEPrivateKeyShare( libff::alt_bn128_Fr::random_element(), + num_all + 1, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); + } + { // null public key BOOST_REQUIRE_THROW( TEPublicKey( nullptr, num_signed, num_all ), diff --git a/tools/utils.h b/tools/utils.h index e0adc535..5f7a40ea 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -43,7 +43,6 @@ class ThresholdUtils { public: Exception( const std::string& err_str ) { what_str = err_str; } - // coverage:ignore-line virtual const char* what() const noexcept override { return what_str.c_str(); } }; From 9b7c7349bd641ce722a9a744617980a1f6f67865 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 3 Aug 2021 09:12:23 +0300 Subject: [PATCH 37/40] SKALE-4409 use aes to encrypt messages before threshold encryption --- CMakeLists.txt | 27 ++-- deps/build.sh | 6 +- test/test_TE_wrappers.cpp | 10 -- threshold_encryption/CMakeLists.txt | 6 +- threshold_encryption/TEPublicKey.cpp | 4 - threshold_encryption/threshold_encryption.cpp | 48 +++---- threshold_encryption/threshold_encryption.h | 2 +- tools/utils.cpp | 117 +++++++++++++++++- tools/utils.h | 3 + 9 files changed, 165 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ccf2ee33..1f717123 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,8 @@ list( APPEND CMAKE_MODULE_PATH ${LIBBLS_CMAKE_DIR} ) find_library( GMP_LIBRARY NAMES "gmp" PATHS "${DEPS_INSTALL_ROOT}/lib" ) find_library( GMPXX_LIBRARY NAMES "gmpxx" PATHS "${DEPS_INSTALL_ROOT}/lib" ) +find_library( OPENSSL_LIBRARY NAMES "ssl" PATHS "${DEPS_INSTALL_ROOT}/lib" ) +find_library( CRYPTO_LIBRARY NAMES "crypto" PATHS "${DEPS_INSTALL_ROOT}/lib" ) set( HUNTER_ENABLED OFF ) @@ -134,62 +136,61 @@ set(BLS_INCLUDE_DIRS ) target_include_directories(bls PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(bls PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) +target_link_libraries(bls PRIVATE ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_subdirectory(threshold_encryption) add_executable(dkg_keygen tools/dkg_key_gen.cpp) target_include_directories(dkg_keygen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - -target_link_libraries(dkg_keygen PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(dkg_keygen PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(dkg_glue tools/dkg_glue.cpp) target_include_directories(dkg_glue PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(dkg_glue PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(dkg_glue PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(sign_bls tools/sign_bls.cpp) target_include_directories(sign_bls PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(sign_bls PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(sign_bls PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(hash_g1 tools/hash_g1.cpp) target_include_directories(hash_g1 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(hash_g1 PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(hash_g1 PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(bls_glue tools/bls_glue.cpp) target_include_directories(bls_glue PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(bls_glue PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(bls_glue PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(verify_bls tools/verify_bls.cpp) target_include_directories(verify_bls PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(verify_bls PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(verify_bls PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(generate_key_system tools/generate_key_system.cpp) target_include_directories(generate_key_system PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(generate_key_system PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(generate_key_system PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) if(BUILD_TESTS) add_executable(bls_unit_test test/unit_tests_bls.cpp) target_include_directories(bls_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(bls_unit_test PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(bls_unit_test PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME bls_tests COMMAND bls_unit_test) add_executable(dkg_unit_test test/unit_tests_dkg.cpp) target_include_directories(dkg_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(dkg_unit_test PRIVATE bls ff ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(dkg_unit_test PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME dkg_tests COMMAND dkg_unit_test) add_executable(bls_test test/test_bls.cpp) target_include_directories(bls_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(bls_test PRIVATE bls ff ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(bls_test PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME bls_test COMMAND bls_test) add_executable(dkg_attack test/dkg_attack.cpp) target_include_directories(dkg_attack PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(dkg_attack PRIVATE bls ff ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(dkg_attack PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME dkg_attack COMMAND dkg_attack) diff --git a/deps/build.sh b/deps/build.sh index 534c70f3..4cc1c672 100755 --- a/deps/build.sh +++ b/deps/build.sh @@ -702,9 +702,9 @@ then cd build $CMAKE "${CMAKE_CROSSCOMPILING_OPTS}" -DCMAKE_INSTALL_PREFIX="$INSTALL_ROOT" -DCMAKE_BUILD_TYPE="$TOP_CMAKE_BUILD_TYPE" .. -DWITH_PROCPS=OFF echo -e "${COLOR_INFO}building it${COLOR_DOTS}...${COLOR_RESET}" - $MAKE ${PARALLEL_MAKE_OPTIONS} - $MAKE ${PARALLEL_MAKE_OPTIONS} install - cd "$SOURCES_ROOT" + $MAKE ${PARALLEL_MAKE_OPTIONS} + $MAKE ${PARALLEL_MAKE_OPTIONS} install + cd "$SOURCES_ROOT" else echo -e "${COLOR_SUCCESS}SKIPPED${COLOR_RESET}" fi diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index c95cd43a..efc55dea 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -518,16 +518,6 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } - { - // message length is not 64 - libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element(); - - TEPublicKey pkey( el, num_signed, num_all ); - - BOOST_REQUIRE_THROW( pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ), - crypto::ThresholdUtils::IncorrectInput ); - } - { // null private key BOOST_REQUIRE_THROW( TEPrivateKey( nullptr, num_signed, num_all ), diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 868905a5..36b110f2 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -48,18 +48,18 @@ include_directories(${CMAKE_BINARY_DIR}/deps/include ) link_directories(${CMAKE_BINARY_DIR}/deps/lib) target_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) -target_link_libraries(te PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) +target_link_libraries(te PRIVATE ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) if (BUILD_TESTS) add_executable(te_unit_test ../test/unit_tests_te.cpp) target_include_directories(te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) - target_link_libraries(te_unit_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) + target_link_libraries(te_unit_test PRIVATE te ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_test(NAME te_tests COMMAND te_unit_test) add_executable(te_test ../test/test_TE_wrappers.cpp) target_include_directories(te_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) - target_link_libraries(te_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) + target_link_libraries(te_test PRIVATE te ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_test(NAME te_wrap_tests COMMAND te_unit_test) diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index 6cb90265..b1edd2e8 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -93,10 +93,6 @@ crypto::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr throw crypto::ThresholdUtils::IncorrectInput( "Message is null" ); } - if ( mes_ptr->length() != 64 ) { - throw crypto::ThresholdUtils::IncorrectInput( "Message length is not equal to 64" ); - } - crypto::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); crypto::ThresholdUtils::checkCypher( cypher ); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 5dd9f316..0dc60786 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -27,6 +27,7 @@ #include #include + #include namespace crypto { @@ -69,7 +70,7 @@ libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::st return ThresholdUtils::HashtoG1( hash_bytes_arr ); } -Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ) { +Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_public ) { libff::alt_bn128_Fr r = libff::alt_bn128_Fr::random_element(); while ( r.is_zero() ) { @@ -82,17 +83,22 @@ Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& c std::string hash = this->Hash( Y ); - // assuming message and hash are the same size strings + // assuming aes_message and hash are the same size strings // the behaviour is undefined when the two arguments are valarrays with different sizes + Y.to_affine_coordinates(); + std::string aes_message = + ThresholdUtils::aesEncrypt( message, ThresholdUtils::fieldElementToString( Y.X.c0 ) ); + + size_t size = std::max( aes_message.size(), hash.size() ); - std::valarray< uint8_t > lhs_to_hash( 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 > lhs_to_hash( size ); + for ( size_t i = 0; i < size; ++i ) { + lhs_to_hash[i] = i < hash.size() ? static_cast< uint8_t >( hash[i] ) : 0; } - std::valarray< uint8_t > rhs_to_hash( message.size() ); - for ( size_t i = 0; i < message.size(); ++i ) { - rhs_to_hash[i] = static_cast< uint8_t >( message[i] ); + std::valarray< uint8_t > rhs_to_hash( size ); + for ( size_t i = 0; i < size; ++i ) { + rhs_to_hash[i] = i < aes_message.size() ? static_cast< uint8_t >( aes_message[i] ) : 0; } std::valarray< uint8_t > res = lhs_to_hash ^ rhs_to_hash; @@ -107,12 +113,7 @@ Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& c H = this->HashToGroup( U, V ); W = r * H; - Ciphertext result; - std::get< 0 >( result ) = U; - std::get< 1 >( result ) = V; - std::get< 2 >( result ) = W; - - return result; + return {U, V, W}; } libff::alt_bn128_G2 TE::getDecryptionShare( @@ -218,15 +219,16 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, } std::string hash = this->Hash( sum ); + size_t size = std::max( hash.size(), V.size() ); - std::valarray< uint8_t > lhs_to_hash( 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 > lhs_to_hash( size ); + for ( size_t i = 0; i < size; ++i ) { + lhs_to_hash[i] = i < hash.size() ? static_cast< uint8_t >( hash[i] ) : 0; } - 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 > rhs_to_hash( size ); + for ( size_t i = 0; i < size; ++i ) { + rhs_to_hash[i] = i < V.size() ? static_cast< uint8_t >( V[i] ) : 0; } std::valarray< uint8_t > xor_res = lhs_to_hash ^ rhs_to_hash; @@ -236,7 +238,11 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, message += static_cast< char >( xor_res[i] ); } - return message; + sum.to_affine_coordinates(); + std::string ret = + ThresholdUtils::aesDecrypt( message, ThresholdUtils::fieldElementToString( sum.X.c0 ) ); + + return ret; } } // namespace crypto diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index 12e091b1..29ef8710 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -42,7 +42,7 @@ class TE { ~TE(); - Ciphertext Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ); + Ciphertext Encrypt( const std::string& message, libff::alt_bn128_G2 common_public ); libff::alt_bn128_G2 getDecryptionShare( const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key ); diff --git a/tools/utils.cpp b/tools/utils.cpp index 210e6479..1b760a75 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -21,9 +21,12 @@ @date 2021 */ -#include #include +#include + +#include + namespace crypto { @@ -178,8 +181,8 @@ void ThresholdUtils::checkCypher( if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) throw IncorrectInput( "zero element in cyphertext" ); - if ( std::get< 1 >( cyphertext ).length() != 64 ) - throw IncorrectInput( "wrong string length in cyphertext" ); + // if ( std::get< 1 >( cyphertext ).length() != 64 ) + // throw IncorrectInput( "wrong string length in cyphertext" ); } std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( @@ -217,4 +220,112 @@ std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( return std::make_shared< std::vector< std::string > >( tokens ); } +#define AES_KEYLENGTH 256 +#define AES_BLOCK_SIZE 16 + +std::string ThresholdUtils::aesEncrypt( const std::string& message, const std::string& key ) { + size_t inputslength = message.length(); + unsigned char aes_input[inputslength]; + unsigned char aes_key[AES_KEYLENGTH]; + memset( aes_input, 0, inputslength / 8 ); + memset( aes_key, 0, AES_KEYLENGTH / 8 ); + strcpy( ( char* ) aes_input, message.c_str() ); + strcpy( ( char* ) aes_key, key.c_str() ); + + /* init vector */ + unsigned char iv[AES_BLOCK_SIZE]; + for ( size_t i = 0; i < AES_BLOCK_SIZE; ++i ) { + gmp_randstate_t state; + gmp_randinit_default( state ); + // gmp_randseed_ui( state, i + 125156655697493 ); + + mpz_t n; + mpz_init( n ); + mpz_set_ui( n, 256 ); + + mpz_t t; + mpz_init( t ); + mpz_urandomm( t, state, n ); + + char arr1[mpz_sizeinbase( t, 16 ) + 2]; + char* other_tmp = mpz_get_str( arr1, 16, t ); + iv[i] = static_cast< unsigned char >( *other_tmp ); + // std::cout << iv[i] << '\n'; + + mpz_clear( t ); + mpz_clear( n ); + gmp_randclear( state ); + } + + // buffers for encryption + const size_t encslength = + ( ( inputslength + AES_BLOCK_SIZE ) / AES_BLOCK_SIZE ) * AES_BLOCK_SIZE; + unsigned char enc_out[encslength]; + memset( enc_out, 0, sizeof( enc_out ) ); + + AES_KEY enc_key; + AES_set_encrypt_key( aes_key, AES_KEYLENGTH, &enc_key ); + AES_cbc_encrypt( aes_input, enc_out, inputslength, &enc_key, iv, AES_ENCRYPT ); + + std::string res; + res.resize( encslength ); + for ( size_t i = 0; i < encslength; ++i ) { + res[i] = enc_out[i]; + } + return res; +} + +std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::string& key ) { + size_t inputslength = message.length(); + unsigned char aes_input[inputslength]; + unsigned char aes_key[AES_KEYLENGTH]; + memset( aes_input, 0, inputslength / 8 ); + memset( aes_key, 0, AES_KEYLENGTH / 8 ); + strcpy( ( char* ) aes_input, message.c_str() ); + strcpy( ( char* ) aes_key, key.c_str() ); + + /* init vector */ + unsigned char iv[AES_BLOCK_SIZE]; + for ( size_t i = 0; i < AES_BLOCK_SIZE; ++i ) { + gmp_randstate_t state; + gmp_randinit_default( state ); + // gmp_randseed_ui( state, i + 125156655697493 ); + + mpz_t n; + mpz_init( n ); + mpz_set_ui( n, 256 ); + + mpz_t t; + mpz_init( t ); + mpz_urandomm( t, state, n ); + + char arr1[mpz_sizeinbase( t, 16 ) + 2]; + char* other_tmp = mpz_get_str( arr1, 16, t ); + iv[i] = static_cast< unsigned char >( *other_tmp ); + // std::cout << iv[i] << '\n'; + + mpz_clear( t ); + mpz_clear( n ); + gmp_randclear( state ); + } + + // buffers for decryption + const size_t encslength = + ( ( inputslength + AES_BLOCK_SIZE ) / AES_BLOCK_SIZE ) * AES_BLOCK_SIZE; + unsigned char dec_out[inputslength]; + memset( dec_out, 0, sizeof( dec_out ) ); + + AES_KEY dec_key; + + AES_set_decrypt_key( aes_key, AES_KEYLENGTH, &dec_key ); + AES_cbc_encrypt( aes_input, dec_out, encslength, &dec_key, iv, AES_DECRYPT ); + + std::string res; + res.resize( inputslength - AES_BLOCK_SIZE ); + for ( size_t i = 0; i < inputslength - AES_BLOCK_SIZE; ++i ) { + res[i] = dec_out[i]; + } + return res; +} + } // namespace crypto diff --git a/tools/utils.h b/tools/utils.h index 5f7a40ea..217228e3 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -98,6 +98,9 @@ class ThresholdUtils { template < class T > static std::string fieldElementToString( const T& field_elem ); + + static std::string aesEncrypt( const std::string& message, const std::string& key ); + static std::string aesDecrypt( const std::string& message, const std::string& key ); }; template < class T > From cfc2d3c6ec54590e4d846526cb9e33ae1b53b068 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 10 Aug 2021 14:03:56 +0300 Subject: [PATCH 38/40] SKALE-4409 add aes encryption --- dkg/dkg.cpp | 7 +--- dkg/dkg.h | 2 -- test/test_TE_wrappers.cpp | 18 ---------- test/unit_tests_te.cpp | 10 +++--- threshold_encryption/threshold_encryption.cpp | 15 +++++++-- tools/utils.cpp | 33 +++++++++++++++---- tools/utils.h | 4 +++ 7 files changed, 48 insertions(+), 41 deletions(-) diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index eabc6faf..2e57507f 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -110,7 +110,7 @@ bool Dkg::Verification( size_t idx, libff::alt_bn128_Fr share, // idx-th node verifies that share corresponds to the verification vector libff::alt_bn128_G2 value = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < this->t_; ++i ) { - if ( !this->isG2( verification_vector[i] ) ) { + if ( !ThresholdUtils::isG2( verification_vector[i] ) ) { return false; } value = value + power( libff::alt_bn128_Fr( idx + 1 ), i ) * verification_vector[i]; @@ -134,9 +134,4 @@ size_t Dkg::GetN() const { return this->n_; } -bool Dkg::isG2( const libff::alt_bn128_G2& point ) { - return point.is_well_formed() && - libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); -} - } // namespace crypto diff --git a/dkg/dkg.h b/dkg/dkg.h index 509a8b65..586bbabc 100644 --- a/dkg/dkg.h +++ b/dkg/dkg.h @@ -58,8 +58,6 @@ class Dkg { size_t GetN() const; - static bool isG2( const libff::alt_bn128_G2& point ); - private: const size_t t_ = 0; diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index efc55dea..c5c541a2 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -411,24 +411,6 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); } - { - // wrong string length in cypher - libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); - - TEPublicKeyShare pkey( - TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); - libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); - - libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); - - crypto::Ciphertext cypher; - std::get< 0 >( cypher ) = U; - std::get< 1 >( cypher ) = "tra-la-la"; - std::get< 2 >( cypher ) = W; - - BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); - } - { // zero decrypted libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index de9829fd..040f9f8b 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE( SimpleEncryption ) { crypto::TE te_instance = crypto::TE( 1, 1 ); std::string message = - "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 + "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 // length libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = - "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 + "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { libff::alt_bn128_G2 common_public = libff::alt_bn128_G2::random_element(); std::string message = - "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 + "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); @@ -214,7 +214,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = - "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 + "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); @@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = - "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 + "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 0dc60786..f2789ba7 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -71,6 +71,14 @@ libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::st } Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_public ) { + if ( !ThresholdUtils::checkHex(message) ) { + throw ThresholdUtils::IncorrectInput("Input message is not hex"); + } + + if ( !ThresholdUtils::isG2( common_public ) ) { + throw ThresholdUtils::IncorrectInput("Input common public key is corrupted"); + } + libff::alt_bn128_Fr r = libff::alt_bn128_Fr::random_element(); while ( r.is_zero() ) { @@ -103,9 +111,10 @@ Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_p std::valarray< uint8_t > res = lhs_to_hash ^ rhs_to_hash; - std::string V = ""; - for ( size_t i = 0; i < res.size(); ++i ) { - V += static_cast< char >( res[i] ); + std::string V; + V.resize( size ); + for ( size_t i = 0; i < size; ++i ) { + V[i] = static_cast< char >( res[i] ); } libff::alt_bn128_G1 W, H; diff --git a/tools/utils.cpp b/tools/utils.cpp index 1b760a75..3bcc4968 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -176,13 +176,35 @@ bool ThresholdUtils::isStringNumber( std::string& str ) { return true; } +bool ThresholdUtils::checkHex( const std::string& hex ) { + if ( hex.length() == 0){ + return false; + } + + mpz_t num; + mpz_init(num); + + if (mpz_set_str(num, hex.c_str(), 16) == -1) { + mpz_clear(num); + return false; + } + mpz_clear(num); + + return true; +} + void ThresholdUtils::checkCypher( const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cyphertext ) { if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) throw IncorrectInput( "zero element in cyphertext" ); - // if ( std::get< 1 >( cyphertext ).length() != 64 ) - // throw IncorrectInput( "wrong string length in cyphertext" ); + if ( checkHex( std::get< 1 >( cyphertext ) ) ) + throw IncorrectInput( "Message in cyphertext is not hex" ); +} + +bool ThresholdUtils::isG2( const libff::alt_bn128_G2& point ) { + return point.is_well_formed() && + libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); } std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( @@ -221,7 +243,6 @@ std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( } #define AES_KEYLENGTH 256 -#define AES_BLOCK_SIZE 16 std::string ThresholdUtils::aesEncrypt( const std::string& message, const std::string& key ) { size_t inputslength = message.length(); @@ -250,7 +271,6 @@ std::string ThresholdUtils::aesEncrypt( const std::string& message, const std::s char arr1[mpz_sizeinbase( t, 16 ) + 2]; char* other_tmp = mpz_get_str( arr1, 16, t ); iv[i] = static_cast< unsigned char >( *other_tmp ); - // std::cout << iv[i] << '\n'; mpz_clear( t ); mpz_clear( n ); @@ -289,7 +309,7 @@ std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::s for ( size_t i = 0; i < AES_BLOCK_SIZE; ++i ) { gmp_randstate_t state; gmp_randinit_default( state ); - // gmp_randseed_ui( state, i + 125156655697493 ); + //gmp_randseed_ui( state, i + 125156655697493 ); mpz_t n; mpz_init( n ); @@ -302,7 +322,6 @@ std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::s char arr1[mpz_sizeinbase( t, 16 ) + 2]; char* other_tmp = mpz_get_str( arr1, 16, t ); iv[i] = static_cast< unsigned char >( *other_tmp ); - // std::cout << iv[i] << '\n'; mpz_clear( t ); mpz_clear( n ); @@ -321,7 +340,7 @@ std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::s AES_cbc_encrypt( aes_input, dec_out, encslength, &dec_key, iv, AES_DECRYPT ); std::string res; - res.resize( inputslength - AES_BLOCK_SIZE ); + res.resize( inputslength - AES_BLOCK_SIZE); for ( size_t i = 0; i < inputslength - AES_BLOCK_SIZE; ++i ) { res[i] = dec_out[i]; } diff --git a/tools/utils.h b/tools/utils.h index 217228e3..ddf8e46e 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -89,12 +89,16 @@ class ThresholdUtils { static void checkCypher( const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cypher ); + + static bool checkHex( const std::string& str ); static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( const std::string& hint ); static std::shared_ptr< std::vector< std::string > > SplitString( const std::shared_ptr< std::string >, const std::string& delim ); + + static bool isG2( const libff::alt_bn128_G2& point ); template < class T > static std::string fieldElementToString( const T& field_elem ); From e889a6a9a20e0b519c8b36bfbf7a74886a1fab16 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 10 Aug 2021 16:24:51 +0300 Subject: [PATCH 39/40] Revert "SKALE-4409 add aes encryption" This reverts commit cfc2d3c6ec54590e4d846526cb9e33ae1b53b068. --- dkg/dkg.cpp | 7 +++- dkg/dkg.h | 2 ++ test/test_TE_wrappers.cpp | 18 ++++++++++ test/unit_tests_te.cpp | 10 +++--- threshold_encryption/threshold_encryption.cpp | 15 ++------- tools/utils.cpp | 33 ++++--------------- tools/utils.h | 4 --- 7 files changed, 41 insertions(+), 48 deletions(-) diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index 2e57507f..eabc6faf 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -110,7 +110,7 @@ bool Dkg::Verification( size_t idx, libff::alt_bn128_Fr share, // idx-th node verifies that share corresponds to the verification vector libff::alt_bn128_G2 value = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < this->t_; ++i ) { - if ( !ThresholdUtils::isG2( verification_vector[i] ) ) { + if ( !this->isG2( verification_vector[i] ) ) { return false; } value = value + power( libff::alt_bn128_Fr( idx + 1 ), i ) * verification_vector[i]; @@ -134,4 +134,9 @@ size_t Dkg::GetN() const { return this->n_; } +bool Dkg::isG2( const libff::alt_bn128_G2& point ) { + return point.is_well_formed() && + libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); +} + } // namespace crypto diff --git a/dkg/dkg.h b/dkg/dkg.h index 586bbabc..509a8b65 100644 --- a/dkg/dkg.h +++ b/dkg/dkg.h @@ -58,6 +58,8 @@ class Dkg { size_t GetN() const; + static bool isG2( const libff::alt_bn128_G2& point ); + private: const size_t t_ = 0; diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index c5c541a2..efc55dea 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -411,6 +411,24 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); } + { + // wrong string length in cypher + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); + + TEPublicKeyShare pkey( + TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all ); + libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element(); + + libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element(); + + crypto::Ciphertext cypher; + std::get< 0 >( cypher ) = U; + std::get< 1 >( cypher ) = "tra-la-la"; + std::get< 2 >( cypher ) = W; + + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); + } + { // zero decrypted libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 040f9f8b..de9829fd 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE( SimpleEncryption ) { crypto::TE te_instance = crypto::TE( 1, 1 ); std::string message = - "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 // length libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionReal ) { libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = - "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); @@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomPK ) { libff::alt_bn128_G2 common_public = libff::alt_bn128_G2::random_element(); std::string message = - "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); @@ -214,7 +214,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = - "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); @@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionCorruptedCiphertext ) { libff::alt_bn128_G2 common_public = common_secret * libff::alt_bn128_G2::one(); std::string message = - "99c17b86fe861e901c9c41dd501fc99f26ee1022ffa4e7995ba14bb669e5f7e8"; // message should be 64 + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 // length auto ciphertext = obj.Encrypt( message, common_public ); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index f2789ba7..0dc60786 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -71,14 +71,6 @@ libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::st } Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_public ) { - if ( !ThresholdUtils::checkHex(message) ) { - throw ThresholdUtils::IncorrectInput("Input message is not hex"); - } - - if ( !ThresholdUtils::isG2( common_public ) ) { - throw ThresholdUtils::IncorrectInput("Input common public key is corrupted"); - } - libff::alt_bn128_Fr r = libff::alt_bn128_Fr::random_element(); while ( r.is_zero() ) { @@ -111,10 +103,9 @@ Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_p std::valarray< uint8_t > res = lhs_to_hash ^ rhs_to_hash; - std::string V; - V.resize( size ); - for ( size_t i = 0; i < size; ++i ) { - V[i] = static_cast< char >( res[i] ); + std::string V = ""; + for ( size_t i = 0; i < res.size(); ++i ) { + V += static_cast< char >( res[i] ); } libff::alt_bn128_G1 W, H; diff --git a/tools/utils.cpp b/tools/utils.cpp index 3bcc4968..1b760a75 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -176,35 +176,13 @@ bool ThresholdUtils::isStringNumber( std::string& str ) { return true; } -bool ThresholdUtils::checkHex( const std::string& hex ) { - if ( hex.length() == 0){ - return false; - } - - mpz_t num; - mpz_init(num); - - if (mpz_set_str(num, hex.c_str(), 16) == -1) { - mpz_clear(num); - return false; - } - mpz_clear(num); - - return true; -} - void ThresholdUtils::checkCypher( const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cyphertext ) { if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) throw IncorrectInput( "zero element in cyphertext" ); - if ( checkHex( std::get< 1 >( cyphertext ) ) ) - throw IncorrectInput( "Message in cyphertext is not hex" ); -} - -bool ThresholdUtils::isG2( const libff::alt_bn128_G2& point ) { - return point.is_well_formed() && - libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); + // if ( std::get< 1 >( cyphertext ).length() != 64 ) + // throw IncorrectInput( "wrong string length in cyphertext" ); } std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( @@ -243,6 +221,7 @@ std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( } #define AES_KEYLENGTH 256 +#define AES_BLOCK_SIZE 16 std::string ThresholdUtils::aesEncrypt( const std::string& message, const std::string& key ) { size_t inputslength = message.length(); @@ -271,6 +250,7 @@ std::string ThresholdUtils::aesEncrypt( const std::string& message, const std::s char arr1[mpz_sizeinbase( t, 16 ) + 2]; char* other_tmp = mpz_get_str( arr1, 16, t ); iv[i] = static_cast< unsigned char >( *other_tmp ); + // std::cout << iv[i] << '\n'; mpz_clear( t ); mpz_clear( n ); @@ -309,7 +289,7 @@ std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::s for ( size_t i = 0; i < AES_BLOCK_SIZE; ++i ) { gmp_randstate_t state; gmp_randinit_default( state ); - //gmp_randseed_ui( state, i + 125156655697493 ); + // gmp_randseed_ui( state, i + 125156655697493 ); mpz_t n; mpz_init( n ); @@ -322,6 +302,7 @@ std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::s char arr1[mpz_sizeinbase( t, 16 ) + 2]; char* other_tmp = mpz_get_str( arr1, 16, t ); iv[i] = static_cast< unsigned char >( *other_tmp ); + // std::cout << iv[i] << '\n'; mpz_clear( t ); mpz_clear( n ); @@ -340,7 +321,7 @@ std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::s AES_cbc_encrypt( aes_input, dec_out, encslength, &dec_key, iv, AES_DECRYPT ); std::string res; - res.resize( inputslength - AES_BLOCK_SIZE); + res.resize( inputslength - AES_BLOCK_SIZE ); for ( size_t i = 0; i < inputslength - AES_BLOCK_SIZE; ++i ) { res[i] = dec_out[i]; } diff --git a/tools/utils.h b/tools/utils.h index ddf8e46e..217228e3 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -89,16 +89,12 @@ class ThresholdUtils { static void checkCypher( const std::tuple< libff::alt_bn128_G2, std::string, libff::alt_bn128_G1 >& cypher ); - - static bool checkHex( const std::string& str ); static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( const std::string& hint ); static std::shared_ptr< std::vector< std::string > > SplitString( const std::shared_ptr< std::string >, const std::string& delim ); - - static bool isG2( const libff::alt_bn128_G2& point ); template < class T > static std::string fieldElementToString( const T& field_elem ); From 83c98531fbfb4ba267d4a08649321f3757e9feb1 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 10 Aug 2021 16:27:00 +0300 Subject: [PATCH 40/40] Revert "SKALE-4409 use aes to encrypt messages before threshold encryption" This reverts commit 9b7c7349bd641ce722a9a744617980a1f6f67865. --- CMakeLists.txt | 27 ++-- deps/build.sh | 6 +- test/test_TE_wrappers.cpp | 10 ++ threshold_encryption/CMakeLists.txt | 6 +- threshold_encryption/TEPublicKey.cpp | 4 + threshold_encryption/threshold_encryption.cpp | 48 ++++--- threshold_encryption/threshold_encryption.h | 2 +- tools/utils.cpp | 117 +----------------- tools/utils.h | 3 - 9 files changed, 58 insertions(+), 165 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f717123..ccf2ee33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,6 @@ list( APPEND CMAKE_MODULE_PATH ${LIBBLS_CMAKE_DIR} ) find_library( GMP_LIBRARY NAMES "gmp" PATHS "${DEPS_INSTALL_ROOT}/lib" ) find_library( GMPXX_LIBRARY NAMES "gmpxx" PATHS "${DEPS_INSTALL_ROOT}/lib" ) -find_library( OPENSSL_LIBRARY NAMES "ssl" PATHS "${DEPS_INSTALL_ROOT}/lib" ) -find_library( CRYPTO_LIBRARY NAMES "crypto" PATHS "${DEPS_INSTALL_ROOT}/lib" ) set( HUNTER_ENABLED OFF ) @@ -136,61 +134,62 @@ set(BLS_INCLUDE_DIRS ) target_include_directories(bls PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(bls PRIVATE ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) +target_link_libraries(bls PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_subdirectory(threshold_encryption) add_executable(dkg_keygen tools/dkg_key_gen.cpp) target_include_directories(dkg_keygen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(dkg_keygen PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) + +target_link_libraries(dkg_keygen PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(dkg_glue tools/dkg_glue.cpp) target_include_directories(dkg_glue PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(dkg_glue PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(dkg_glue PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(sign_bls tools/sign_bls.cpp) target_include_directories(sign_bls PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(sign_bls PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(sign_bls PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(hash_g1 tools/hash_g1.cpp) target_include_directories(hash_g1 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(hash_g1 PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(hash_g1 PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(bls_glue tools/bls_glue.cpp) target_include_directories(bls_glue PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(bls_glue PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(bls_glue PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(verify_bls tools/verify_bls.cpp) target_include_directories(verify_bls PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(verify_bls PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(verify_bls PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_executable(generate_key_system tools/generate_key_system.cpp) target_include_directories(generate_key_system PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(generate_key_system PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) +target_link_libraries(generate_key_system PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) if(BUILD_TESTS) add_executable(bls_unit_test test/unit_tests_bls.cpp) target_include_directories(bls_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(bls_unit_test PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(bls_unit_test PRIVATE bls ff ${GMPXX_LIBRARY} ${GMP_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME bls_tests COMMAND bls_unit_test) add_executable(dkg_unit_test test/unit_tests_dkg.cpp) target_include_directories(dkg_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(dkg_unit_test PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(dkg_unit_test PRIVATE bls ff ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME dkg_tests COMMAND dkg_unit_test) add_executable(bls_test test/test_bls.cpp) target_include_directories(bls_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(bls_test PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(bls_test PRIVATE bls ff ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME bls_test COMMAND bls_test) add_executable(dkg_attack test/dkg_attack.cpp) target_include_directories(dkg_attack PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - target_link_libraries(dkg_attack PRIVATE bls ff ${CRYPTO_LIBRARY} ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) + target_link_libraries(dkg_attack PRIVATE bls ff ${GMP_LIBRARY} ${GMPXX_LIBRARY} ${BOOST_LIBS_4_BLS}) add_test(NAME dkg_attack COMMAND dkg_attack) diff --git a/deps/build.sh b/deps/build.sh index 4cc1c672..534c70f3 100755 --- a/deps/build.sh +++ b/deps/build.sh @@ -702,9 +702,9 @@ then cd build $CMAKE "${CMAKE_CROSSCOMPILING_OPTS}" -DCMAKE_INSTALL_PREFIX="$INSTALL_ROOT" -DCMAKE_BUILD_TYPE="$TOP_CMAKE_BUILD_TYPE" .. -DWITH_PROCPS=OFF echo -e "${COLOR_INFO}building it${COLOR_DOTS}...${COLOR_RESET}" - $MAKE ${PARALLEL_MAKE_OPTIONS} - $MAKE ${PARALLEL_MAKE_OPTIONS} install - cd "$SOURCES_ROOT" + $MAKE ${PARALLEL_MAKE_OPTIONS} + $MAKE ${PARALLEL_MAKE_OPTIONS} install + cd "$SOURCES_ROOT" else echo -e "${COLOR_SUCCESS}SKIPPED${COLOR_RESET}" fi diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index efc55dea..c95cd43a 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -518,6 +518,16 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } + { + // message length is not 64 + libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element(); + + TEPublicKey pkey( el, num_signed, num_all ); + + BOOST_REQUIRE_THROW( pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ), + crypto::ThresholdUtils::IncorrectInput ); + } + { // null private key BOOST_REQUIRE_THROW( TEPrivateKey( nullptr, num_signed, num_all ), diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 36b110f2..868905a5 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -48,18 +48,18 @@ include_directories(${CMAKE_BINARY_DIR}/deps/include ) link_directories(${CMAKE_BINARY_DIR}/deps/lib) target_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) -target_link_libraries(te PRIVATE ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) +target_link_libraries(te PRIVATE ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) if (BUILD_TESTS) add_executable(te_unit_test ../test/unit_tests_te.cpp) target_include_directories(te_unit_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) - target_link_libraries(te_unit_test PRIVATE te ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) + target_link_libraries(te_unit_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_test(NAME te_tests COMMAND te_unit_test) add_executable(te_test ../test/test_TE_wrappers.cpp) target_include_directories(te_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) - target_link_libraries(te_test PRIVATE te ff ${CRYPTO_LIBRARY} ${GMPXX_LIBRARY} ${GMP_LIBRARY}) + target_link_libraries(te_test PRIVATE te ff ${GMPXX_LIBRARY} ${GMP_LIBRARY}) add_test(NAME te_wrap_tests COMMAND te_unit_test) diff --git a/threshold_encryption/TEPublicKey.cpp b/threshold_encryption/TEPublicKey.cpp index b1edd2e8..6cb90265 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -93,6 +93,10 @@ crypto::Ciphertext TEPublicKey::encrypt( std::shared_ptr< std::string > mes_ptr throw crypto::ThresholdUtils::IncorrectInput( "Message is null" ); } + if ( mes_ptr->length() != 64 ) { + throw crypto::ThresholdUtils::IncorrectInput( "Message length is not equal to 64" ); + } + crypto::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); crypto::ThresholdUtils::checkCypher( cypher ); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 0dc60786..5dd9f316 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -27,7 +27,6 @@ #include #include - #include namespace crypto { @@ -70,7 +69,7 @@ libff::alt_bn128_G1 TE::HashToGroup( const libff::alt_bn128_G2& U, const std::st return ThresholdUtils::HashtoG1( hash_bytes_arr ); } -Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_public ) { +Ciphertext TE::Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ) { libff::alt_bn128_Fr r = libff::alt_bn128_Fr::random_element(); while ( r.is_zero() ) { @@ -83,22 +82,17 @@ Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_p std::string hash = this->Hash( Y ); - // assuming aes_message and hash are the same size strings + // assuming message and hash are the same size strings // the behaviour is undefined when the two arguments are valarrays with different sizes - Y.to_affine_coordinates(); - std::string aes_message = - ThresholdUtils::aesEncrypt( message, ThresholdUtils::fieldElementToString( Y.X.c0 ) ); - - size_t size = std::max( aes_message.size(), hash.size() ); - std::valarray< uint8_t > lhs_to_hash( size ); - for ( size_t i = 0; i < size; ++i ) { - lhs_to_hash[i] = i < hash.size() ? static_cast< uint8_t >( hash[i] ) : 0; + std::valarray< uint8_t > lhs_to_hash( 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( size ); - for ( size_t i = 0; i < size; ++i ) { - rhs_to_hash[i] = i < aes_message.size() ? static_cast< uint8_t >( aes_message[i] ) : 0; + std::valarray< uint8_t > rhs_to_hash( message.size() ); + for ( size_t i = 0; i < message.size(); ++i ) { + rhs_to_hash[i] = static_cast< uint8_t >( message[i] ); } std::valarray< uint8_t > res = lhs_to_hash ^ rhs_to_hash; @@ -113,7 +107,12 @@ Ciphertext TE::Encrypt( const std::string& message, libff::alt_bn128_G2 common_p H = this->HashToGroup( U, V ); W = r * H; - return {U, V, W}; + Ciphertext result; + std::get< 0 >( result ) = U; + std::get< 1 >( result ) = V; + std::get< 2 >( result ) = W; + + return result; } libff::alt_bn128_G2 TE::getDecryptionShare( @@ -219,16 +218,15 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, } std::string hash = this->Hash( sum ); - size_t size = std::max( hash.size(), V.size() ); - std::valarray< uint8_t > lhs_to_hash( size ); - for ( size_t i = 0; i < size; ++i ) { - lhs_to_hash[i] = i < hash.size() ? static_cast< uint8_t >( hash[i] ) : 0; + std::valarray< uint8_t > lhs_to_hash( 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( size ); - for ( size_t i = 0; i < size; ++i ) { - rhs_to_hash[i] = i < V.size() ? static_cast< uint8_t >( V[i] ) : 0; + 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; @@ -238,11 +236,7 @@ std::string TE::CombineShares( const Ciphertext& ciphertext, message += static_cast< char >( xor_res[i] ); } - sum.to_affine_coordinates(); - std::string ret = - ThresholdUtils::aesDecrypt( message, ThresholdUtils::fieldElementToString( sum.X.c0 ) ); - - return ret; + return message; } } // namespace crypto diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index 29ef8710..12e091b1 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -42,7 +42,7 @@ class TE { ~TE(); - Ciphertext Encrypt( const std::string& message, libff::alt_bn128_G2 common_public ); + Ciphertext Encrypt( const std::string& message, const libff::alt_bn128_G2& common_public ); libff::alt_bn128_G2 getDecryptionShare( const Ciphertext& ciphertext, const libff::alt_bn128_Fr& secret_key ); diff --git a/tools/utils.cpp b/tools/utils.cpp index 1b760a75..210e6479 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -21,11 +21,8 @@ @date 2021 */ -#include - #include - -#include +#include namespace crypto { @@ -181,8 +178,8 @@ void ThresholdUtils::checkCypher( if ( std::get< 0 >( cyphertext ).is_zero() || std::get< 2 >( cyphertext ).is_zero() ) throw IncorrectInput( "zero element in cyphertext" ); - // if ( std::get< 1 >( cyphertext ).length() != 64 ) - // throw IncorrectInput( "wrong string length in cyphertext" ); + if ( std::get< 1 >( cyphertext ).length() != 64 ) + throw IncorrectInput( "wrong string length in cyphertext" ); } std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( @@ -220,112 +217,4 @@ std::shared_ptr< std::vector< std::string > > ThresholdUtils::SplitString( return std::make_shared< std::vector< std::string > >( tokens ); } -#define AES_KEYLENGTH 256 -#define AES_BLOCK_SIZE 16 - -std::string ThresholdUtils::aesEncrypt( const std::string& message, const std::string& key ) { - size_t inputslength = message.length(); - unsigned char aes_input[inputslength]; - unsigned char aes_key[AES_KEYLENGTH]; - memset( aes_input, 0, inputslength / 8 ); - memset( aes_key, 0, AES_KEYLENGTH / 8 ); - strcpy( ( char* ) aes_input, message.c_str() ); - strcpy( ( char* ) aes_key, key.c_str() ); - - /* init vector */ - unsigned char iv[AES_BLOCK_SIZE]; - for ( size_t i = 0; i < AES_BLOCK_SIZE; ++i ) { - gmp_randstate_t state; - gmp_randinit_default( state ); - // gmp_randseed_ui( state, i + 125156655697493 ); - - mpz_t n; - mpz_init( n ); - mpz_set_ui( n, 256 ); - - mpz_t t; - mpz_init( t ); - mpz_urandomm( t, state, n ); - - char arr1[mpz_sizeinbase( t, 16 ) + 2]; - char* other_tmp = mpz_get_str( arr1, 16, t ); - iv[i] = static_cast< unsigned char >( *other_tmp ); - // std::cout << iv[i] << '\n'; - - mpz_clear( t ); - mpz_clear( n ); - gmp_randclear( state ); - } - - // buffers for encryption - const size_t encslength = - ( ( inputslength + AES_BLOCK_SIZE ) / AES_BLOCK_SIZE ) * AES_BLOCK_SIZE; - unsigned char enc_out[encslength]; - memset( enc_out, 0, sizeof( enc_out ) ); - - AES_KEY enc_key; - AES_set_encrypt_key( aes_key, AES_KEYLENGTH, &enc_key ); - AES_cbc_encrypt( aes_input, enc_out, inputslength, &enc_key, iv, AES_ENCRYPT ); - - std::string res; - res.resize( encslength ); - for ( size_t i = 0; i < encslength; ++i ) { - res[i] = enc_out[i]; - } - return res; -} - -std::string ThresholdUtils::aesDecrypt( const std::string& message, const std::string& key ) { - size_t inputslength = message.length(); - unsigned char aes_input[inputslength]; - unsigned char aes_key[AES_KEYLENGTH]; - memset( aes_input, 0, inputslength / 8 ); - memset( aes_key, 0, AES_KEYLENGTH / 8 ); - strcpy( ( char* ) aes_input, message.c_str() ); - strcpy( ( char* ) aes_key, key.c_str() ); - - /* init vector */ - unsigned char iv[AES_BLOCK_SIZE]; - for ( size_t i = 0; i < AES_BLOCK_SIZE; ++i ) { - gmp_randstate_t state; - gmp_randinit_default( state ); - // gmp_randseed_ui( state, i + 125156655697493 ); - - mpz_t n; - mpz_init( n ); - mpz_set_ui( n, 256 ); - - mpz_t t; - mpz_init( t ); - mpz_urandomm( t, state, n ); - - char arr1[mpz_sizeinbase( t, 16 ) + 2]; - char* other_tmp = mpz_get_str( arr1, 16, t ); - iv[i] = static_cast< unsigned char >( *other_tmp ); - // std::cout << iv[i] << '\n'; - - mpz_clear( t ); - mpz_clear( n ); - gmp_randclear( state ); - } - - // buffers for decryption - const size_t encslength = - ( ( inputslength + AES_BLOCK_SIZE ) / AES_BLOCK_SIZE ) * AES_BLOCK_SIZE; - unsigned char dec_out[inputslength]; - memset( dec_out, 0, sizeof( dec_out ) ); - - AES_KEY dec_key; - - AES_set_decrypt_key( aes_key, AES_KEYLENGTH, &dec_key ); - AES_cbc_encrypt( aes_input, dec_out, encslength, &dec_key, iv, AES_DECRYPT ); - - std::string res; - res.resize( inputslength - AES_BLOCK_SIZE ); - for ( size_t i = 0; i < inputslength - AES_BLOCK_SIZE; ++i ) { - res[i] = dec_out[i]; - } - return res; -} - } // namespace crypto diff --git a/tools/utils.h b/tools/utils.h index 217228e3..5f7a40ea 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -98,9 +98,6 @@ class ThresholdUtils { template < class T > static std::string fieldElementToString( const T& field_elem ); - - static std::string aesEncrypt( const std::string& message, const std::string& key ); - static std::string aesDecrypt( const std::string& message, const std::string& key ); }; template < class T >