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/CMakeLists.txt b/CMakeLists.txt index cc93afb3..ccf2ee33 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 @@ -70,11 +73,11 @@ set(sourses_bls bls/BLSSignature.cpp bls/BLSSigShare.cpp bls/BLSSigShareSet.cpp - bls/BLSutils.cpp dkg/dkg.cpp dkg/DKGBLSWrapper.cpp dkg/DKGBLSSecret.cpp third_party/cryptlite/base64.cpp + tools/utils.cpp ) set(headers_bls @@ -87,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 @@ -96,9 +98,10 @@ 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.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") @@ -113,7 +116,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/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/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/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index d94e231c..0539d9f0 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -14,58 +14,61 @@ 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 @date 2019 */ -#include "bls.h" #include -#include +#include +#include BLSPrivateKey::BLSPrivateKey( const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( _key == nullptr ) { - throw signatures::Bls::IncorrectInput( "Secret key share is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is null" ); } if ( _key->empty() ) { - throw signatures::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 signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::ZeroSecretKey( + "Secret key share is equal to zero or corrupt" ); } } 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 ) { - throw signatures::Bls::IncorrectInput( "Secret keys ptr is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Secret keys ptr is null" ); } if ( koefs == nullptr ) { - throw signatures::Bls::IncorrectInput( "Signers indices ptr is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "Signers indices ptr is null" ); } - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - signatures::Bls obj = signatures::Bls( _requiredSigners, _totalSigners ); - std::vector lagrange_koefs = obj.LagrangeCoeffs( *koefs ); + + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + + 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++ ) { + 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; } if ( privateKeyObj == libff::alt_bn128_Fr::zero() ) { - throw signatures::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 ); @@ -76,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 >( BLSutils::ConvertToString( *privateKey ) ); + std::shared_ptr< std::string > key_str = std::make_shared< std::string >( + crypto::ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) - throw signatures::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/BLSPrivateKey.h b/bls/BLSPrivateKey.h index f656b676..1bf6bbda 100644 --- a/bls/BLSPrivateKey.h +++ b/bls/BLSPrivateKey.h @@ -26,7 +26,6 @@ #include -#include #include @@ -41,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/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index 557565bb..d8515b83 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -23,53 +23,53 @@ #include #include -#include +#include -#include #include BLSPrivateKeyShare::BLSPrivateKeyShare( const std::string& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); - BLSutils::initBLS(); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::initCurve(); if ( _key.empty() ) { - throw signatures::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 signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::ZeroSecretKey( + "Secret key share is equal to zero or corrupt" ); } } 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 ); if ( *privateKey == libff::alt_bn128_Fr::zero() ) { - throw signatures::Bls::ZeroSecretKey( "BLS Secret key share is equal to zero" ); + throw crypto::ThresholdUtils::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::ThresholdUtils::IncorrectInput( "Zero signer index during BLS sign" ); } if ( hash_byte_arr == nullptr ) { - throw signatures::Bls::IncorrectInput( "Hash is null during BLS sign" ); + throw crypto::ThresholdUtils::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 = obj->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 +77,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 = crypto::ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + + ":" + hash_with_hint.second; auto s = std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners ); @@ -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::ThresholdUtils::IncorrectInput( "Zero signer index" ); } if ( hash_byte_arr == nullptr ) { - throw signatures::Bls::IncorrectInput( "Null hash is bls signWithHelper" ); + throw crypto::ThresholdUtils::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 ); @@ -107,8 +107,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 = crypto::ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) + + ":" + hash_with_hint.second; auto s = std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners ); @@ -119,11 +119,11 @@ 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; - 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 ); @@ -132,7 +132,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 = crypto::ThresholdUtils::fieldElementToString( skeys.at( i ) ); std::shared_ptr< BLSPrivateKeyShare > key_ptr = std::make_shared< BLSPrivateKeyShare >( key_str, _requiredSigners, _totalSigners ); @@ -156,14 +156,15 @@ 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::ThresholdUtils::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::ThresholdUtils::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::shared_ptr< std::string > key_str = std::make_shared< std::string >( + crypto::ThresholdUtils::fieldElementToString( *privateKey ) ); if ( key_str->empty() ) - throw signatures::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 3eeb8e8e..65ffd46d 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,17 +24,17 @@ #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(); + crypto::ThresholdUtils::initCurve(); CHECK( pkey_str_vect ) - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); libffPublicKey = std::make_shared< libff::alt_bn128_G2 >(); @@ -46,34 +46,34 @@ 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::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key " ); } if ( !( libffPublicKey->is_well_formed() ) ) { - throw signatures::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 ) { - BLSutils::initBLS(); + 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() ) { - throw signatures::Bls::IsNotWellFormed( "Zero BLS Public Key" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Public Key" ); } } 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 signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); } } @@ -87,20 +87,20 @@ 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(); + crypto::ThresholdUtils::initCurve(); - std::shared_ptr< signatures::Bls > obj; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + std::shared_ptr< crypto::Bls > obj; + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw signatures::Bls::IncorrectInput( "hash is null" ); + throw crypto::ThresholdUtils::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::ThresholdUtils::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,20 +108,21 @@ 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; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + std::shared_ptr< crypto::Bls > obj; + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw signatures::Bls::IncorrectInput( "hash is null" ); + throw crypto::ThresholdUtils::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::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 = BLSutils::ParseHint( hint ); + std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = + crypto::ThresholdUtils::ParseHint( hint ); - libff::alt_bn128_Fq x = BLSutils::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; @@ -142,13 +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 ) { - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - signatures::Bls obj = signatures::Bls( requiredSigners, totalSigners ); if ( !koefs_pkeys_map ) { - throw signatures::Bls::IncorrectInput( "map is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "map is null" ); } std::vector< size_t > participatingNodes; @@ -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 = + crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners ); libff::alt_bn128_G2 key = libff::alt_bn128_G2::zero(); size_t i = 0; @@ -173,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::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" ); } } @@ -182,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( 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 45ac2aaf..90f3a3d7 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -23,9 +23,8 @@ #include #include -#include -#include #include +#include BLSPublicKeyShare::BLSPublicKeyShare( const std::shared_ptr< std::vector< std::string > > pkey_str_vect, size_t _requiredSigners, @@ -33,9 +32,9 @@ BLSPublicKeyShare::BLSPublicKeyShare( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { CHECK( pkey_str_vect ); - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); publicKey = std::make_shared< libff::alt_bn128_G2 >(); @@ -47,20 +46,20 @@ 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::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key share" ); } if ( !( publicKey->is_well_formed() ) ) { - throw signatures::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 ) { - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); if ( _skey.is_zero() ) { - throw signatures::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 +74,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( 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 ); } @@ -88,14 +87,14 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > CHECK( hash_ptr ); CHECK( sign_ptr ); - std::shared_ptr< signatures::Bls > obj; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + std::shared_ptr< crypto::Bls > obj; + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( sign_ptr->getSigShare()->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero BLS Sig share" ); + throw crypto::ThresholdUtils::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,20 +104,21 @@ 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; - BLSSignature::checkSigners( _requiredSigners, _totalSigners ); + std::shared_ptr< crypto::Bls > obj; + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { - throw signatures::Bls::IncorrectInput( "hash is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "hash is null" ); } if ( sign_ptr->getSigShare()->is_zero() ) { - throw signatures::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 = BLSutils::ParseHint( hint ); + std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = + crypto::ThresholdUtils::ParseHint( hint ); - libff::alt_bn128_Fq x = BLSutils::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 b4499e7a..08c38da1 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,7 @@ #include #include -#include +#include #include #include @@ -52,35 +52,36 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSSignature::checkSigners( requiredSigners, totalSigners ); - BLSutils::initBLS(); + crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners ); + crypto::ThresholdUtils::initCurve(); if ( _signerIndex == 0 ) { - throw signatures::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" ); } if ( !_sigShare ) { - throw signatures::Bls::IncorrectInput( "Null _sigShare" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null _sigShare" ); } if ( _sigShare->size() < 10 ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too short:" + std::to_string( _sigShare->size() ) ); } if ( _sigShare->size() > BLS_MAX_SIG_LEN ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too long:" + std::to_string( _sigShare->size() ) ); } - std::shared_ptr< std::vector< std::string > > result = BLSutils::SplitString( _sigShare, ":" ); + std::shared_ptr< std::vector< std::string > > result = + crypto::ThresholdUtils::SplitString( _sigShare, ":" ); if ( result->size() != 4 ) - throw signatures::Bls::IncorrectInput( "Misformatted signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { for ( char& c : str ) { if ( !( c >= '0' && c <= '9' ) ) { - throw signatures::Bls::IncorrectInput( + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str ); } } @@ -93,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::ThresholdUtils::IsNotWellFormed( "signature is not from G1" ); } BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShare, @@ -103,24 +104,24 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - BLSutils::initBLS(); - BLSSignature::checkSigners( requiredSigners, totalSigners ); + crypto::ThresholdUtils::initCurve(); + crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners ); if ( !_sigShare ) { - throw signatures::Bls::IncorrectInput( "Null _s" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null _s" ); } if ( _sigShare->is_zero() ) { - throw signatures::Bls::IsNotWellFormed( "Zero signature" ); + throw crypto::ThresholdUtils::IsNotWellFormed( "Zero signature" ); } if ( _signerIndex == 0 ) { - throw signatures::Bls::IncorrectInput( "Zero signer index" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" ); } if ( _hint.length() == 0 ) { - throw signatures::Bls::IncorrectInput( "Empty or misformatted hint" ); + throw crypto::ThresholdUtils::IncorrectInput( "Empty or misformatted hint" ); } if ( !_sigShare->is_well_formed() ) { - throw signatures::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 e792e8cd..4e349dd0 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,19 +27,18 @@ #include #include #include -#include +#include bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) { CHECK( _sigShare ); if ( was_merged ) { - throw signatures::Bls::IncorrectInput( "Invalid state:was already merged" ); + throw crypto::ThresholdUtils::IncorrectInput( "Invalid state:was already merged" ); } - if ( sigShares.count( _sigShare->getSignerIndex() ) > 0 ) { - throw signatures::Bls::IncorrectInput( + throw crypto::ThresholdUtils::IncorrectInput( "Already have this index:" + std::to_string( _sigShare->getSignerIndex() ) ); return false; } @@ -53,10 +52,10 @@ 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::ThresholdUtils::IncorrectInput( + "Index out of range:" + std::to_string( _index ) ); } - if ( sigShares.count( _index ) == 0 ) { return nullptr; } @@ -65,22 +64,21 @@ 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 ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); } bool BLSSigShareSet::isEnough() { return ( sigShares.size() >= requiredSigners ); } - std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { if ( !isEnough() ) - throw signatures::Bls::IncorrectInput( "Not enough shares to create signature" ); + throw crypto::ThresholdUtils::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; @@ -90,7 +88,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 = + 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 0294ebe8..423deec1 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 ); @@ -34,18 +34,17 @@ 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 ); - BLSutils::initBLS(); - + crypto::ThresholdUtils::initCurve(); if ( sig->is_zero() ) { - throw signatures::Bls::IncorrectInput( "Zero BLS signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Zero BLS signature" ); } if ( hint.length() == 0 ) { - throw signatures::Bls::IncorrectInput( "Empty BLS hint" ); + throw crypto::ThresholdUtils::IncorrectInput( "Empty BLS hint" ); } } @@ -54,29 +53,30 @@ BLSSignature::BLSSignature( : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { CHECK( _sig ); - BLSSignature::checkSigners( requiredSigners, totalSigners ); + crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners ); - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); if ( _sig->size() < 10 ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "Signature too short:" + std::to_string( _sig->size() ) ); } if ( _sig->size() > BLS_MAX_SIG_LEN ) { - throw signatures::Bls::IsNotWellFormed( + throw crypto::ThresholdUtils::IsNotWellFormed( "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 = + crypto::ThresholdUtils::SplitString( _sig, ":" ); if ( result->size() != 4 ) - throw signatures::Bls::IncorrectInput( "Misformatted signature" ); + throw crypto::ThresholdUtils::IncorrectInput( "Misformatted signature" ); for ( auto&& str : *result ) { for ( char& c : str ) { if ( !( c >= '0' && c <= '9' ) ) { - throw signatures::Bls::IncorrectInput( + throw crypto::ThresholdUtils::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::ThresholdUtils::IsNotWellFormed( "signature is not from G1" ); } } @@ -101,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 signatures::Bls::IncorrectInput( "_requiredSigners > _totalSigners" ); - } - - - if ( _totalSigners == 0 ) { - throw signatures::Bls::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/bls/BLSutils.cpp b/bls/BLSutils.cpp deleted file mode 100644 index 7c708907..00000000 --- a/bls/BLSutils.cpp +++ /dev/null @@ -1,90 +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 ); -} - -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 ); - - 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 695f895c..00000000 --- a/bls/BLSutils.h +++ /dev/null @@ -1,58 +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: - 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( - const std::shared_ptr< std::string >, const std::string& delim ); - static void initBLS(); - - - 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 8a230ed3..3d088a26 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -23,6 +23,7 @@ along with libBLS. If not, see . #include +#include #include #include @@ -35,12 +36,10 @@ along with libBLS. If not, see . #include #include -#include - -namespace signatures { +namespace crypto { 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() { @@ -79,59 +78,13 @@ 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 ); 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 ) { @@ -171,7 +124,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( @@ -183,7 +136,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; @@ -195,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 @@ -217,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 ); @@ -244,18 +197,19 @@ 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 = 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 ) ); @@ -266,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 @@ -288,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 } @@ -303,38 +257,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 +} // namespace crypto diff --git a/bls/bls.h b/bls/bls.h index 19ad5fa9..697608e3 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 @@ -39,43 +39,10 @@ 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: - 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(); @@ -86,8 +53,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 > > ); @@ -107,20 +72,18 @@ 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; 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 signatures::Bls::IncorrectInput( __msg__ ); \ + throw crypto::ThresholdUtils::IncorrectInput( __msg__ ); \ } 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 diff --git a/dkg/DKGBLSSecret.cpp b/dkg/DKGBLSSecret.cpp index ad4bb4f4..e7fd549e 100644 --- a/dkg/DKGBLSSecret.cpp +++ b/dkg/DKGBLSSecret.cpp @@ -21,33 +21,33 @@ @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 ); - signatures::Dkg dkg( requiredSigners, totalSigners ); + crypto::Dkg dkg( requiredSigners, totalSigners ); poly = dkg.GeneratePolynomial(); } 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; } 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/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..c86b415d 100644 --- a/dkg/DKGBLSWrapper.cpp +++ b/dkg/DKGBLSWrapper.cpp @@ -14,43 +14,44 @@ 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 ); } 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" ); + 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" ); - signatures::Dkg dkg( requiredSigners, totalSigners ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong vector size" ); + crypto::Dkg dkg( requiredSigners, totalSigners ); return dkg.Verification( _signerIndex, _share, *_verification_vector ); } 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,12 +68,12 @@ 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 " ); - 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/DKGBLSWrapper.h b/dkg/DKGBLSWrapper.h index 42b59d63..0ec8b335 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 @@ -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 f77986ba..42c142c1 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 @@ -22,33 +22,35 @@ */ -#include "DKGTESecret.h" +#include +#include -#include -#include +#include DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); + libff::init_alt_bn128_params(); + + crypto::Dkg 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" ); + throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of vector" ); } poly = _poly; } -std::vector< encryption::element_wrapper > DKGTESecret::getDKGTESecretShares() { - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - return dkg_te.CreateSecretKeyContribution( poly ); +std::vector< libff::alt_bn128_Fr > DKGTESecret::getDKGTESecretShares() { + crypto::Dkg dkg_te( requiredSigners, totalSigners ); + return dkg_te.SecretKeyContribution( poly ); } -std::vector< encryption::element_wrapper > DKGTESecret::getDKGTEPublicShares() { - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - return dkg_te.CreateVerificationVector( poly ); +std::vector< libff::alt_bn128_G2 > DKGTESecret::getDKGTEPublicShares() { + crypto::Dkg dkg_te( requiredSigners, totalSigners ); + return dkg_te.VerificationVector( 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..4ad5c507 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 @@ -22,95 +22,76 @@ */ -#include "DKGTEWrapper.h" - -#include +#include +#include DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::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_ ) ) ) - throw std::runtime_error( "Zero secret share" ); +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 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" ); - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); - return dkg_te.Verify( _signerIndex, _share, *_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 ); } -void DKGTEWrapper::setDKGSecret( - std::shared_ptr< std::vector< encryption::element_wrapper > >& _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" ); + throw crypto::ThresholdUtils::IncorrectInput( "Null polynomial ptr" ); dkg_secret_ptr->setPoly( *_poly_ptr ); } -std::shared_ptr< std::vector< encryption::element_wrapper > > -DKGTEWrapper::createDKGSecretShares() { - return std::make_shared< std::vector< encryption::element_wrapper > >( +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< encryption::element_wrapper > > -DKGTEWrapper::createDKGPublicShares() { - return std::make_shared< std::vector< encryption::element_wrapper > >( +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< encryption::element_wrapper > > 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 " ); + 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 " ); - encryption::DkgTe dkg_te( requiredSigners, totalSigners ); + crypto::Dkg dkg_te( requiredSigners, totalSigners ); - encryption::element_wrapper 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 ); } 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 ); + 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" ); - 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..e846edf3 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 @@ -26,7 +26,7 @@ #define LIBBLS_DKGTEWRAPPER_H #include -#include +#include #include class DKGTEWrapper { @@ -39,21 +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, + 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 > > > - 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/dkg/dkg.cpp b/dkg/dkg.cpp index 93701ccd..eabc6faf 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -21,19 +21,19 @@ @date 2018 */ -#include "bls/BLSutils.h" #include +#include #include #include #include -namespace signatures { +namespace crypto { 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() { @@ -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 23f324c4..509a8b65 100644 --- a/dkg/dkg.h +++ b/dkg/dkg.h @@ -29,7 +29,7 @@ #include #include -namespace signatures { +namespace crypto { class Dkg { public: @@ -66,4 +66,4 @@ class Dkg { const size_t n_ = 0; }; -} // namespace signatures +} // namespace crypto diff --git a/dkg/dkg_te.cpp b/dkg/dkg_te.cpp deleted file mode 100644 index 7ebadafc..00000000 --- a/dkg/dkg_te.cpp +++ /dev/null @@ -1,224 +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< element_wrapper > Polynomial; - -DkgTe::DkgTe( const size_t t, const size_t n ) : t_( t ), n_( n ) {} - -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 ); - - while ( i == this->t_ - 1 && element_is0( g ) ) { - element_random( g ); - } - - pol[i] = element_wrapper( g ); - - element_clear( g ); - } - - return pol; -} - -std::vector< element_wrapper > DkgTe::CreateVerificationVector( - const std::vector< element_wrapper >& polynomial ) { - std::vector< element_wrapper > 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 ); - } - - 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 ); - - 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" ); - } - - 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 ); - } - - element_clear( pow ); - - element_wrapper ret_val = element_wrapper( value ); - element_clear( value ); - - return ret_val; -} - -std::vector< element_wrapper > DkgTe::CreateSecretKeyContribution( - const std::vector< element_wrapper >& polynomial ) { - std::vector< element_wrapper > 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 ); - - 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 ); - - 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 ); - } - - if ( element_is0( secret_key_share ) ) { - 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; -} - -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_ ); - - 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 ); - } - - element_clear( tmp1 ); - element_clear( tmp2 ); - element_clear( tmp3 ); - element_clear( tmp4 ); - } - - 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 ret_val; -} -} // namespace encryption diff --git a/dkg/dkg_te.h b/dkg/dkg_te.h deleted file mode 100644 index 6af1dd60..00000000 --- a/dkg/dkg_te.h +++ /dev/null @@ -1,58 +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 -#include - -namespace encryption { - -class DkgTe { -public: - DkgTe( const size_t t, const size_t n ); - - std::vector< element_wrapper > GeneratePolynomial(); - - std::vector< element_wrapper > CreateVerificationVector( - const std::vector< element_wrapper >& polynomial ); - - element_wrapper ComputePolynomialValue( - const std::vector< element_wrapper >& polynomial, const element_wrapper& point ); - - std::vector< element_wrapper > CreateSecretKeyContribution( - const std::vector< element_wrapper >& polynomial ); - - element_wrapper CreateSecretKeyShare( - const std::vector< element_wrapper >& secret_key_contribution ); - - bool Verify( size_t idx, const element_wrapper& share, - const std::vector< element_wrapper >& verification_vector ); - -private: - const size_t t_ = 0; - - const size_t n_ = 0; -}; - -} // namespace encryption 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/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/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 248120af..fb83f5c4 100644 --- a/test/dkg_attack.cpp +++ b/test/dkg_attack.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -225,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 87b9806e..c95cd43a 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -24,14 +24,14 @@ along with libBLS. If not, see . #define BOOST_TEST_MODULE -#include +#include #include #include #include #include #include #include -#include +#include #include #include @@ -54,73 +54,19 @@ 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; size_t num_signed = rand_gen() % num_all + 1; - encryption::DkgTe dkg_te( num_signed, num_all ); + crypto::Dkg 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 ); + std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial(); - element_clear( zero ); + 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.PolynomialValue( poly, zero_el ); + BOOST_REQUIRE( common_skey == poly.at( 0 ) ); TEPrivateKey common_private( common_skey, num_signed, num_all ); @@ -131,16 +77,14 @@ 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 ); - encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); + auto msg_ptr = std::make_shared< std::string >( message ); + crypto::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.SecretKeyContribution( 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 ) ); + 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 ) ); } @@ -157,92 +101,62 @@ 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 ); 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 { - 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 ), crypto::ThresholdUtils::IncorrectInput ); + + // cannot add after merge + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( num_signed, nullptr ), crypto::ThresholdUtils::IncorrectInput ); 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; - 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 ), crypto::ThresholdUtils::IncorrectInput ); 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 { - 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 ), crypto::ThresholdUtils::IncorrectInput ); 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 ); - + crypto::Dkg dkg_te( num_signed, num_all ); std::string message; size_t msg_length = 64; @@ -255,8 +169,8 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) { keys = TEPrivateKeyShare::generateSampleKeys( num_signed, num_all ); - std::shared_ptr msg_ptr = std::make_shared< std::string >( message ); - encryption::Ciphertext cypher = keys.second->encrypt( msg_ptr ); + auto msg_ptr = std::make_shared< std::string >( message ); + crypto::Ciphertext cypher = keys.second->encrypt( msg_ptr ); std::vector< TEPublicKeyShare > public_key_shares; for ( size_t i = 0; i < num_all; i++ ) { @@ -276,9 +190,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 +205,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 ); + BOOST_REQUIRE( common_pkey.getPublicKey() == common_pkey_from_str.getPublicKey() ); - element_t test; - element_init_Zr( test, TEDataSingleton::getData().pairing_ ); - element_random( test ); - TEPrivateKey private_key( encryption::element_wrapper( test ), num_signed, num_all ); - - 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 +239,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; @@ -351,16 +248,15 @@ 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 ); - std::vector< encryption::element_wrapper > poly = dkg_te.GeneratePolynomial(); - auto shared_poly = - std::make_shared< std::vector< encryption::element_wrapper > >( poly ); + 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 ); 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 +266,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 +282,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 ) ); @@ -415,11 +311,11 @@ 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( - 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,8 +325,8 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) { message += char( rand_gen() % 128 ); } - std::shared_ptr msg_ptr = std::make_shared< std::string >( message ); - encryption::Ciphertext cypher = common_public.encrypt( msg_ptr ); + auto msg_ptr = std::make_shared< std::string >( message ); + 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(); @@ -444,9 +340,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 ); } @@ -460,439 +356,283 @@ 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 { - TEDataSingleton::checkSigners( 0, num_all ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::checkSigners( 0, num_all ), + crypto::ThresholdUtils::IncorrectInput ); - is_exception_caught = false; - try { - TEDataSingleton::checkSigners( 0, 0 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); + BOOST_REQUIRE_THROW( + crypto::ThresholdUtils::checkSigners( 0, 0 ), crypto::ThresholdUtils::IncorrectInput ); - 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 ), + crypto::ThresholdUtils::IncorrectInput ); - 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 ), + crypto::ThresholdUtils::IncorrectInput ); } - 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 ), + crypto::ThresholdUtils::IncorrectInput ); } - 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( is_exception_caught ); - - 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 ); - TEPublicKeyShare pkey( - TEPrivateKeyShare( el_wrap, 1, num_signed, num_all ), num_signed, num_all ); + BOOST_REQUIRE_THROW( + TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, + num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); + } - 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 ); + { + // 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 ); + 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; + crypto::Ciphertext cypher; + 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 ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - - 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 ); + { + // wrong string length in cypher + 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 ); + crypto::Ciphertext cypher; + 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 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero decrypted - try { - element_t el; - element_init_Zr( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); + { + // zero decrypted + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); TEPublicKeyShare pkey( - TEPrivateKeyShare( encryption::element_wrapper( el ), 1, num_signed, num_all ), - num_signed, num_all ); + 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 ); + crypto::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< 2 >( cypher ) = encryption::element_wrapper( W ); + std::get< 2 >( cypher ) = 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 ); + libff::alt_bn128_G2 decrypt = libff::alt_bn128_G2::zero(); - element_clear( el ); - element_clear( U ); - element_clear( W ); + BOOST_REQUIRE_THROW( + pkey.Verify( cypher, decrypt ), crypto::ThresholdUtils::IsNotWellFormed ); + } - pkey.Verify( cypher, decrypt.el_ ); + { + // null private key share + BOOST_REQUIRE_THROW( TEPrivateKeyShare( nullptr, 1, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); + } + { + // zero private key share + std::string zero_str = "0"; + BOOST_REQUIRE_THROW( TEPrivateKeyShare( std::make_shared< std::string >( zero_str ), 1, + num_signed, num_all ), + crypto::ThresholdUtils::ZeroSecretKey ); + } - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + // zero private key share + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); + BOOST_REQUIRE_THROW( TEPrivateKeyShare( el, 1, num_signed, num_all ), + crypto::ThresholdUtils::ZeroSecretKey ); } - 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; + { + // wrong signer index + BOOST_REQUIRE_THROW( TEPrivateKeyShare( libff::alt_bn128_Fr::random_element(), + num_all + 1, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero private key share - try { - 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; + { + // null public key + BOOST_REQUIRE_THROW( TEPublicKey( nullptr, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - 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 ); + { + // 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 ), + crypto::ThresholdUtils::IsNotWellFormed ); + } - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + // zero public key + 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 ), + crypto::ThresholdUtils::IsNotWellFormed ); } - 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; + { + // zero public key + libff::alt_bn128_G2 el = libff::alt_bn128_G2::zero(); + BOOST_REQUIRE_THROW( TEPublicKey pkey( el, num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // zero public key - try { - 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( is_exception_caught ); - - 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 ); - - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); - - 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 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); - - is_exception_caught = false; // null message - try { - element_t el; - element_init_G1( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); + { + // null message + 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& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; // message length is not 64 - try { - element_t el; - element_init_G1( el, TEDataSingleton::getData().pairing_ ); - element_random( el ); + { + // message length is not 64 + 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& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ), + crypto::ThresholdUtils::IncorrectInput ); } - 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 ), + crypto::ThresholdUtils::IncorrectInput ); } - 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( is_exception_caught ); - - 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 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - 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; - } - BOOST_REQUIRE( is_exception_caught ); - - is_exception_caught = false; - 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 ); - - 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 ); + BOOST_REQUIRE_THROW( + TEPrivateKey( std::make_shared< std::string >( zero_str ), num_signed, num_all ), + crypto::ThresholdUtils::IsNotWellFormed ); + } - decr_set.addDecrypt( 1, el_ptr1 ); - decr_set.addDecrypt( 1, el_ptr2 ); + { + // zero private key + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero(); + BOOST_REQUIRE_THROW( + TEPrivateKey( el, num_signed, num_all ), crypto::ThresholdUtils::IsNotWellFormed ); + } - } catch ( std::runtime_error& ) { - is_exception_caught = true; + { + //_requiredSigners > _totalSigners + BOOST_REQUIRE_THROW( TEDecryptSet decr_set( num_all + 1, num_signed ), + crypto::ThresholdUtils::IsNotWellFormed ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - TEDecryptSet decr_set( num_signed, num_all ); // zero element in decrypt set + { + // same indices in decrypt set + TEDecryptSet decr_set( num_signed, num_all ); - 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::random_element(); + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); + + 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 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); - - 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 ); - el_ptr1 = nullptr; - element_clear( el1 ); - decr_set.addDecrypt( 1, el_ptr1 ); + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( 1, el_ptr2 ), crypto::ThresholdUtils::IncorrectInput ); + } + + { + // 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 ); + + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( 1, el_ptr1 ), crypto::ThresholdUtils::IsNotWellFormed ); + } + + { + // null element in decrypt set + TEDecryptSet decr_set( num_signed, num_all ); + libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero(); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 ); + el_ptr1 = nullptr; + BOOST_REQUIRE_THROW( + decr_set.addDecrypt( 1, el_ptr1 ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - 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 ); + { + // 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 ); 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; + crypto::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< 2 >( cypher ) = W_wrap; - - decr_set.merge( cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - BOOST_REQUIRE( is_exception_caught ); - - 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 ); + std::get< 2 >( cypher ) = W; + + BOOST_REQUIRE_THROW( + decr_set.merge( cypher ), crypto::ThresholdUtils::IsNotWellFormed ); + } + + { + // 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 ); - 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; + crypto::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< 2 >( cypher ) = W_wrap; + std::get< 2 >( cypher ) = W; - decr_set.merge( cypher ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( decr_set.merge( cypher ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); } } @@ -900,123 +640,84 @@ 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 ); - 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() ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ), + crypto::ThresholdUtils::ZeroSecretKey ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { // 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 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element(); + BOOST_REQUIRE_THROW( + dkg_te.VerifyDKGShare( 1, el, nullptr ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - 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 ) ); - - } 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 ) ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - 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 ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - 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(); - dkg_te.setDKGSecret( shares ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( + dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput ); } - 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 ), crypto::ThresholdUtils::IncorrectInput ); } - 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< 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& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { + { DKGTEWrapper dkg_te( num_signed, num_all ); - std::shared_ptr< std::vector< encryption::element_wrapper > > shares; - dkg_te.setDKGSecret( shares ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares; + BOOST_REQUIRE_THROW( + dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput ); } - 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 ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/test_bls.cpp b/test/test_bls.cpp index e3695d4d..b713ef83 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include @@ -111,18 +111,18 @@ 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 = std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ); - libff::alt_bn128_G1 hash = obj.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,17 +140,22 @@ 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::ThresholdUtils::IsNotWellFormed ); } - std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( participants ); + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + 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 ), - signatures::Bls::IsNotWellFormed ); + 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 ); } } @@ -200,7 +205,7 @@ BOOST_AUTO_TEST_CASE( libBlsAPI ) { BOOST_REQUIRE_THROW( BLSSigShare( bad_sig, hint, participants.at( i ), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } BOOST_REQUIRE( sigSet.getTotalSigSharesCount() == num_signed ); @@ -221,7 +226,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::ThresholdUtils::IsNotWellFormed ); std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > pkeys_map1; for ( size_t i = 0; i < num_signed; ++i ) { @@ -418,15 +423,15 @@ 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 ); 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 = + 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 = @@ -442,7 +447,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 ) ); @@ -450,8 +455,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 = + 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 ) * @@ -551,7 +556,8 @@ 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 = + crypto::ThresholdUtils::fieldElementToString( common_secret ); BLSPrivateKey common_skey( std::make_shared< std::string >( common_secret_str ), num_signed, num_all ); @@ -592,25 +598,25 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { { BOOST_REQUIRE_THROW( BLSPrivateKey pkey( std::make_shared< std::string >( "" ), num_signed, num_all ), - signatures::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( BLSPrivateKey skey( std::make_shared< std::string >( "0" ), num_signed, num_all ), - signatures::Bls::ZeroSecretKey ); + crypto::ThresholdUtils::ZeroSecretKey ); } { - BOOST_REQUIRE_THROW( - BLSPrivateKey skey( nullptr, num_signed, num_all ), signatures::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 ), - signatures::Bls::IncorrectInput ); + crypto::ThresholdUtils::IncorrectInput ); } { @@ -618,35 +624,76 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BLSPrivateKey skey( BLSPrivateKeyShare::generateSampleKeys( num_signed, num_all )->first, NULL, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + 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( - BLSPrivateKeyShare skey( "", num_signed, num_all ), signatures::Bls::IncorrectInput ); + 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( "0", num_signed, num_all ), signatures::Bls::ZeroSecretKey ); + BOOST_REQUIRE_THROW( BLSPrivateKeyShare skey( "", num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); + } + + { + 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 ), - signatures::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 ), - signatures::Bls::IncorrectInput ); + crypto::ThresholdUtils::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::ThresholdUtils::IncorrectInput ); } { @@ -654,35 +701,80 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { BOOST_REQUIRE_THROW( skey.signWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), 0 ), - signatures::Bls::IncorrectInput ); + crypto::ThresholdUtils::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::ThresholdUtils::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 ); + BOOST_REQUIRE_THROW( BLSPublicKey pkey( null_vect, num_signed, num_all ), + 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; + BOOST_REQUIRE_THROW( BLSPublicKey pkey( null_map, num_signed, num_all ), + 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( null_map, num_signed, num_all ), signatures::Bls::IncorrectInput ); + BLSPublicKey pkey( map_ptr, 11, 16 ), crypto::ThresholdUtils::IsNotWellFormed ); } { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_G2::zero(), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { BOOST_REQUIRE_THROW( BLSPublicKey pkey( libff::alt_bn128_Fr::zero(), num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -694,7 +786,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::ThresholdUtils::IncorrectInput ); } { @@ -703,18 +795,44 @@ 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::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 ), - signatures::Bls::ZeroSecretKey ); + crypto::ThresholdUtils::ZeroSecretKey ); } { BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( nullptr, num_signed, num_all ), - signatures::Bls::IncorrectInput ); + 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 ); } { @@ -725,7 +843,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::ThresholdUtils::IncorrectInput ); } { @@ -734,18 +852,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::ThresholdUtils::IncorrectInput ); } { BOOST_REQUIRE_THROW( - BLSSignature( nullptr, num_signed, num_all ), signatures::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 ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSSignature( nullptr, hint, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -753,14 +871,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::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 ), - signatures::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } { @@ -773,7 +891,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::ThresholdUtils::IsNotWellFormed ); } { @@ -786,7 +904,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::ThresholdUtils::IncorrectInput ); } { @@ -800,7 +918,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::ThresholdUtils::IsNotWellFormed ); } { @@ -814,7 +932,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::ThresholdUtils::IncorrectInput ); } { @@ -822,23 +940,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::ThresholdUtils::IncorrectInput ); } { - BOOST_REQUIRE_THROW( - BLSSigShare( nullptr, 1, num_signed, num_all ), signatures::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 ), signatures::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 ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( BLSSigShare( nullptr, hint, 0, num_signed, num_all ), + crypto::ThresholdUtils::IncorrectInput ); } { @@ -846,17 +964,32 @@ 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::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( BLSSigShare( std::make_shared< std::string >( short_sig ), 1, num_signed, num_all ), - signatures::Bls::IsNotWellFormed ); + crypto::ThresholdUtils::IsNotWellFormed ); } - { std::string long_sig; for ( size_t j = 0; j < 3; j++ ) { @@ -868,7 +1001,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::ThresholdUtils::IsNotWellFormed ); } { @@ -881,7 +1014,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::ThresholdUtils::IncorrectInput ); } { @@ -896,7 +1029,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::ThresholdUtils::IsNotWellFormed ); } { @@ -910,13 +1043,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 ), - signatures::Bls::IncorrectInput ); + crypto::ThresholdUtils::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::ThresholdUtils::IncorrectInput ); } { @@ -928,7 +1062,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::ThresholdUtils::IncorrectInput ); } { @@ -940,12 +1074,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::ThresholdUtils::IncorrectInput ); } { BLSSigShareSet sig_set( num_signed, num_all ); - BOOST_REQUIRE_THROW( sig_set.merge(), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( sig_set.merge(), crypto::ThresholdUtils::IncorrectInput ); } { @@ -955,7 +1089,8 @@ 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::ThresholdUtils::IncorrectInput ); } std::cerr << "EXCEPTIONS TEST FINISHED" << std::endl; @@ -965,83 +1100,61 @@ 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 ) ), + crypto::ThresholdUtils::ZeroSecretKey ); } - 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 ), + crypto::ThresholdUtils::ZeroSecretKey ); } - 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 ), + crypto::ThresholdUtils::IncorrectInput ); } - 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 ) ), + crypto::ThresholdUtils::IncorrectInput ); } - 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 ), crypto::ThresholdUtils::IncorrectInput ); } - 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 ) ), + crypto::ThresholdUtils::IncorrectInput ); } - 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 ), crypto::ThresholdUtils::IncorrectInput ); } - 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 ) ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 0d52f439..49ed8f88 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 @@ -35,7 +36,6 @@ #define BOOST_TEST_MODULE -#include #include BOOST_AUTO_TEST_SUITE( libBls ) @@ -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::ThresholdUtils::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" ); @@ -129,7 +129,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 = + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, secret_keys ); @@ -154,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( @@ -188,7 +189,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 = + 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 ); @@ -206,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 ); @@ -242,7 +244,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 = + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes, 11 ); auto recovered_keys = obj.KeysRecover( lagrange_coeffs, secret_keys ); @@ -271,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 ); @@ -308,7 +311,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs_fst = - obj.LagrangeCoeffs( testing_nodes_fst ); + crypto::ThresholdUtils::LagrangeCoeffs( testing_nodes_fst, 11 ); auto recovered_keys_fst = obj.KeysRecover( lagrange_coeffs_fst, secret_keys ); @@ -342,7 +345,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } std::vector< libff::alt_bn128_Fr > lagrange_coeffs_snd = - obj.LagrangeCoeffs( testing_nodes_snd ); + 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 ) { @@ -379,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) { @@ -450,7 +453,7 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { std::vector< libff::alt_bn128_Fr > pol( deg + 1 ); - BLSutils::initBLS(); + crypto::ThresholdUtils::initCurve(); // random polynomial generation for ( size_t i = 0; i < deg + 1; ++i ) { @@ -488,8 +491,8 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { nodes.insert( indexes[i] ); } - signatures::Bls obj = signatures::Bls( deg + 1, deg + 1 ); - auto coeffs = obj.LagrangeCoeffs( indexes ); + crypto::Bls obj = crypto::Bls( deg + 1, 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 ) { @@ -511,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" ); @@ -521,21 +524,22 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { BOOST_REQUIRE_THROW( obj.Verification( "bla-bla-bla", sign, libff::alt_bn128_G2::random_element() ), - signatures::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 ), - signatures::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 ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( + obj.KeysRecover( coeffs, shares ), crypto::ThresholdUtils::IncorrectInput ); coeffs.clear(); shares.clear(); @@ -546,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 ), signatures::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() ); @@ -555,7 +560,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::ThresholdUtils::IncorrectInput ); coeffs.clear(); sig_shares.clear(); @@ -569,16 +574,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::ThresholdUtils::IsNotWellFormed ); std::vector< size_t > idx = {1}; - BOOST_REQUIRE_THROW( obj.LagrangeCoeffs( idx ), signatures::Bls::IncorrectInput ); + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::LagrangeCoeffs( idx, num_signed ), + crypto::ThresholdUtils::IncorrectInput ); idx.clear(); idx = {1, 1}; - BOOST_REQUIRE_THROW( obj_2_2.LagrangeCoeffs( idx ), signatures::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_dkg.cpp b/test/unit_tests_dkg.cpp index 3e2405f7..d7770481 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 @@ -45,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" )}; @@ -57,19 +56,12 @@ 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::logic_error ); } 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(); @@ -101,7 +93,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() ); @@ -110,7 +102,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; @@ -118,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::logic_error ); } } @@ -182,7 +168,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_dkg_te.cpp b/test/unit_tests_dkg_te.cpp deleted file mode 100644 index 71c83592..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() diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index eae99a0d..de9829fd 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 @@ -22,177 +22,72 @@ */ #include +#include #include #define BOOST_TEST_MODULE #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 ); + 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 // length - element_t secret_key; - element_init_Zr( secret_key, TEDataSingleton::getData().pairing_ ); - element_random( secret_key ); + libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); - 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_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 ); + crypto::TE obj = crypto::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; + + sk += tmp4; + } + + 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 +95,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 ); + crypto::TE obj = crypto::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_ ); + // 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,112 +156,62 @@ 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 ); } BOOST_AUTO_TEST_CASE( ThresholdEncryptionRandomSK ) { - encryption::TE obj = encryption::TE( 11, 16 ); + crypto::TE obj = crypto::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 ); + + 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 @@ -423,107 +219,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_ ); + libff::alt_bn128_G2 decrypted = obj.getDecryptionShare( ciphertext, secret_keys[i] ); - obj.Decrypt( decrypted, ciphertext, secret_keys[i].el_ ); - - 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 ); + crypto::TE obj = crypto::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; + } - element_t common_public; - element_init_G1( common_public, TEDataSingleton::getData().pairing_ ); - element_pow_zn( common_public, TEDataSingleton::getData().generator_, common_secret ); + secret_keys[i] = sk; + } + + 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 @@ -531,46 +277,25 @@ 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_ ); - - bool is_exception_caught = false; - try { - obj.Decrypt( decrypted, corrupted_ciphertext, secret_keys[i].el_ ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; - } - - element_clear( decrypted ); - BOOST_REQUIRE( is_exception_caught ); + libff::alt_bn128_G2 decrypted; - element_init_G1( decrypted, TEDataSingleton::getData().pairing_ ); + BOOST_REQUIRE_THROW( + decrypted = obj.getDecryptionShare( corrupted_ciphertext, secret_keys[i] ), + crypto::ThresholdUtils::IncorrectInput ); - 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 ); } } @@ -580,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 { - encryption::TE obj( num_signed, num_all ); - std::vector< int > vect; + { + 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 ); - obj.LagrangeCoeffs( vect ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::LagrangeCoeffs( vect, num_signed ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); - is_exception_caught = false; - try { - encryption::TE obj( num_signed, num_all ); - std::vector< int > vect; + { + 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 ); - obj.LagrangeCoeffs( vect ); - } catch ( std::runtime_error& ) { - is_exception_caught = true; + BOOST_REQUIRE_THROW( crypto::ThresholdUtils::LagrangeCoeffs( vect, num_signed ), + crypto::ThresholdUtils::IncorrectInput ); } - BOOST_REQUIRE( is_exception_caught ); } } diff --git a/threshold_encryption/CMakeLists.txt b/threshold_encryption/CMakeLists.txt index 25652139..868905a5 100644 --- a/threshold_encryption/CMakeLists.txt +++ b/threshold_encryption/CMakeLists.txt @@ -15,11 +15,10 @@ set(sources TEPrivateKey.cpp TEPublicKey.cpp TEPublicKeyShare.cpp - TEDataSingleton.h - ../dkg/dkg_te.cpp - ../dkg/DKGTEWrapper.cpp - ../dkg/DKGTESecret.cpp - utils.cpp + ${DKG_DIR}/dkg.cpp + ${DKG_DIR}/DKGTEWrapper.cpp + ${DKG_DIR}/DKGTESecret.cpp + ${TOOLS_DIR}/utils.cpp ) set(headers @@ -29,14 +28,13 @@ set(headers TEPrivateKey.h TEPublicKey.h TEPublicKeyShare.h - TEDataSingleton.cpp - ../dkg/dkg_te.h - ../dkg/DKGTEWrapper.h - ../dkg/DKGTESecret.h - utils.h + ${DKG_DIR}/dkg.h + ${DKG_DIR}/DKGTEWrapper.h + ${DKG_DIR}/DKGTESecret.h + ${TOOLS_DIR}/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}/../) @@ -46,31 +44,22 @@ add_library(te ${sources} ${headers}) include_directories(${CMAKE_BINARY_DIR}) link_directories(${CMAKE_BINARY_DIR}) -target_include_directories(te PRIVATE ${GMP_INCLUDE_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_include_directories(te PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${THIRD_PARTY_DIR}) +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 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) 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) @@ -80,5 +69,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/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 f66a2a14..176dfda9 100644 --- a/threshold_encryption/TEDecryptSet.cpp +++ b/threshold_encryption/TEDecryptSet.cpp @@ -14,59 +14,60 @@ 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 + TEDecryptSet::TEDecryptSet( size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); } -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 ) ); + 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 ( isG1Element0( _el->el_ ) ) { - throw std::runtime_error( "try to add zero _element to decrypt set" ); + if ( _el->is_zero() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "try to add zero _element to decrypt set" ); } decrypts[_signerIndex] = _el; } -std::string TEDecryptSet::merge( const encryption::Ciphertext& cyphertext ) { - checkCypher( cyphertext ); +std::string TEDecryptSet::merge( const crypto::Ciphertext& cyphertext ) { + crypto::ThresholdUtils::checkCypher( 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" ); } - encryption::TE te( requiredSigners, totalSigners ); - std::vector< std::pair< encryption::element_wrapper, size_t > > decrypted; + crypto::TE te( requiredSigners, totalSigners ); + 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..382d0139 100644 --- a/threshold_encryption/TEDecryptSet.h +++ b/threshold_encryption/TEDecryptSet.h @@ -34,14 +34,14 @@ 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 ); + std::string merge( const crypto::Ciphertext& ciphertext ); }; diff --git a/threshold_encryption/TEPrivateKey.cpp b/threshold_encryption/TEPrivateKey.cpp index 4ab3a828..75009f76 100644 --- a/threshold_encryption/TEPrivateKey.cpp +++ b/threshold_encryption/TEPrivateKey.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 TEPublicKey.h @author Sveta Rogova @@ -22,41 +22,41 @@ along with libBLS. If not, see . */ #include -#include +#include TEPrivateKey::TEPrivateKey( std::shared_ptr< std::string > _key_str, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); if ( !_key_str ) { - throw std::runtime_error( "private key is null" ); + throw crypto::ThresholdUtils::IncorrectInput( "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 ); + libff::init_alt_bn128_params(); - if ( element_is0( privateKey.el_ ) ) { - throw std::runtime_error( " private key is zero" ); + privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); + + if ( privateKey.is_zero() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "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 ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); + + libff::init_alt_bn128_params(); - if ( element_is0( _skey.el_ ) ) - throw std::runtime_error( " private key is zero" ); + if ( _skey.is_zero() ) + throw crypto::ThresholdUtils::IsNotWellFormed( "private key is zero" ); } -std::string TEPrivateKey::toString() { - return ElementZrToString( privateKey.el_ ); +std::string TEPrivateKey::toString() const { + return crypto::ThresholdUtils::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..af96ca7e 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 @@ -24,12 +24,11 @@ #ifndef LIBBLS_TEPRIVATEKEY_H #define LIBBLS_TEPRIVATEKEY_H -#include #include class TEPrivateKey { private: - encryption::element_wrapper privateKey; + libff::alt_bn128_Fr privateKey; size_t requiredSigners; size_t totalSigners; @@ -38,12 +37,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(); + std::string toString() const; - 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..f6cb717b 100644 --- a/threshold_encryption/TEPrivateKeyShare.cpp +++ b/threshold_encryption/TEPrivateKeyShare.cpp @@ -14,112 +14,103 @@ 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 #include -#include +#include TEPrivateKeyShare::TEPrivateKeyShare( std::shared_ptr< std::string > _key_str, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ) : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + 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" ); } - 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 ); + libff::init_alt_bn128_params(); - if ( element_is0( privateKey.el_ ) ) { - throw std::runtime_error( "Zero private key share" ); + privateKey = libff::alt_bn128_Fr( _key_str->c_str() ); + + if ( privateKey.is_zero() ) { + throw crypto::ThresholdUtils::ZeroSecretKey( "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 ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - /* if (_signerIndex > _totalSigners) { - throw std::runtime_error ("Wrong _signerIndex"); - }*/ - if ( element_is0( _skey_share.el_ ) ) { - throw std::runtime_error( "Zero private key share" ); + if ( _signerIndex > _totalSigners ) { + throw crypto::ThresholdUtils::IncorrectInput( "Wrong _signerIndex" ); } -} -encryption::element_wrapper TEPrivateKeyShare::decrypt( encryption::Ciphertext& cypher ) { - checkCypher( cypher ); + libff::init_alt_bn128_params(); - encryption::TE te( requiredSigners, totalSigners ); + if ( _skey_share.is_zero() ) { + throw crypto::ThresholdUtils::ZeroSecretKey( "Zero private key share" ); + } +} - element_t decrypt; - element_init_G1( decrypt, TEDataSingleton::getData().pairing_ ); +libff::alt_bn128_G2 TEPrivateKeyShare::getDecryptionShare( crypto::Ciphertext& cipher ) { + crypto::ThresholdUtils::checkCypher( cipher ); - te.Decrypt( decrypt, cypher, privateKey.el_ ); - encryption::element_wrapper decrypted( decrypt ); + crypto::TE te( requiredSigners, totalSigners ); - if ( isG1Element0( decrypt ) ) { - std::runtime_error( "zero decrypt" ); + libff::alt_bn128_G2 decryption_share = te.getDecryptionShare( cipher, privateKey ); + + if ( decryption_share.is_zero() || !decryption_share.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "zero decrypt" ); } - element_clear( decrypt ); - return decrypted; + + return decryption_share; } -std::string TEPrivateKeyShare::toString() { - return ElementZrToString( privateKey.el_ ); +std::string TEPrivateKeyShare::toString() const { + return crypto::ThresholdUtils::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 ); + crypto::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..21c40ab2 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 @@ -24,13 +24,12 @@ #ifndef LIBBLS_TEPRIVATEKEYSHARE_H #define LIBBLS_TEPRIVATEKEYSHARE_H -#include #include #include class TEPrivateKeyShare { private: - encryption::element_wrapper privateKey; + libff::alt_bn128_Fr privateKey; size_t signerIndex; size_t requiredSigners; @@ -40,20 +39,20 @@ 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( crypto::Ciphertext& cipher ); static std::pair< std::shared_ptr< std::vector< std::shared_ptr< TEPrivateKeyShare > > >, std::shared_ptr< TEPublicKey > > generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ); - std::string toString(); + std::string toString() const; 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..6cb90265 100644 --- a/threshold_encryption/TEPublicKey.cpp +++ b/threshold_encryption/TEPublicKey.cpp @@ -14,16 +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 TEPublicKey.h @author Sveta Rogova @date 2019 */ -#include #include -#include +#include #include #include @@ -32,93 +31,84 @@ 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 ); + 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" ); } - std::string key_str = "[" + _key_str_ptr->at( 0 ) + "," + _key_str_ptr->at( 1 ) + "]"; + if ( _key_str_ptr->size() != 4 ) { + 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 crypto::ThresholdUtils::IncorrectInput( + "non-digit symbol or first zero in non-zero public key share" ); + } + + libff::init_alt_bn128_params(); - 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_ ) ) { - 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" ); } } 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 ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - if ( element_is0( _comon_private.getPrivateKey().el_ ) ) { - throw std::runtime_error( "zero key" ); - } + libff::init_alt_bn128_params(); - element_t pkey; - element_init_G1( pkey, TEDataSingleton::getData().pairing_ ); - element_mul_zn( - pkey, TEDataSingleton::getData().generator_, _comon_private.getPrivateKey().el_ ); + if ( _common_private.getPrivateKey().is_zero() ) { + throw crypto::ThresholdUtils::ZeroSecretKey( "zero key" ); + } - 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 ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - if ( isG1Element0( _pkey.el_ ) ) { - throw std::runtime_error( "zero public key" ); + if ( _pkey.is_zero() || !_pkey.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "zero or corrupted public key" ); } } -encryption::Ciphertext TEPublicKey::encrypt( const 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" ); + 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" ); } - encryption::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey.el_ ); - 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 ); - /*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_ ); - /*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 ); + crypto::Ciphertext cypher = te.Encrypt( *mes_ptr, PublicKey ); + crypto::ThresholdUtils::checkCypher( cypher ); + + 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() { - return ElementG1ToString( PublicKey.el_ ); + return std::make_shared< std::vector< std::string > >( + crypto::ThresholdUtils::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..9d840abb 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 ); + crypto::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..5b21385c 100644 --- a/threshold_encryption/TEPublicKeyShare.cpp +++ b/threshold_encryption/TEPublicKeyShare.cpp @@ -14,77 +14,83 @@ 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 TEPublicKeyShare::TEPublicKeyShare( std::shared_ptr< std::vector< std::string > > _key_str_ptr, size_t _signerIndex, size_t _requiredSigners, size_t _totalSigners ) : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + 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() != 2 ) - throw std::runtime_error( "wrong number of components in public key share" ); + // assume only using affine coordinates + if ( _key_str_ptr->size() != 4 ) { + throw crypto::ThresholdUtils::IncorrectInput( + "wrong number of components in public key share" ); + } - if ( !isStringNumber( _key_str_ptr->at( 0 ) ) || !isStringNumber( _key_str_ptr->at( 1 ) ) ) - throw std::runtime_error( "non-digit symbol or first zero in non-zero 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 crypto::ThresholdUtils::IncorrectInput( + "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 ) + "]"; + libff::init_alt_bn128_params(); - 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_ ) ) { - throw std::runtime_error( "corrupted string or zero public key share" ); + if ( PublicKey.is_zero() || !PublicKey.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( + "corrupted string or zero public key share" ); } } TEPublicKeyShare::TEPublicKeyShare( TEPrivateKeyShare _p_key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - TEDataSingleton::checkSigners( _requiredSigners, _totalSigners ); + crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners ); - element_t pkey; - element_init_G1( pkey, TEDataSingleton::getData().pairing_ ); - element_mul_zn( pkey, TEDataSingleton::getData().generator_, _p_key.getPrivateKey().el_ ); + libff::init_alt_bn128_params(); - 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 ) { - checkCypher( cyphertext ); - if ( isG1Element0( const_cast< element_t& >( decrypted ) ) ) { - throw std::runtime_error( "zero decrypt" ); + const crypto::Ciphertext& cyphertext, const libff::alt_bn128_G2& decryptionShare ) { + crypto::ThresholdUtils::checkCypher( cyphertext ); + if ( decryptionShare.is_zero() || !decryptionShare.is_well_formed() ) { + throw crypto::ThresholdUtils::IsNotWellFormed( "zero decrypt" ); } - encryption::TE te( requiredSigners, totalSigners ); + crypto::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 > >( + crypto::ThresholdUtils::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..0f920346 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 crypto::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/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 75799fbe..5dd9f316 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 @@ -26,150 +26,62 @@ #include #include -#include +#include +#include -namespace encryption { +namespace crypto { -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() {} -std::string TE::Hash( const element_t& Y, std::string ( *hash_func )( const std::string& str ) ) { - // assumed that Y lies in from G1 - - 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 TE::Hash( + const libff::alt_bn128_G2& Y, std::string ( *hash_func )( const std::string& str ) ) { + auto vectorCoordinates = ThresholdUtils::G2ToString( Y ); - 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 ); - - int is_square = mpz_legendre( y_squared, modulus_q ); + // assumed that U lies in G2 - if ( is_square == -1 || is_square == 0 ) { - mpz_addmul_ui( x_coord, one, 1 ); - mpz_clear( y_squared ); + auto U_str = ThresholdUtils::G2ToString( U ); - } else { - MpzSquareRoot( y_coord, y_squared ); - - mpz_clear( y_squared ); - mpz_clear( one ); - - break; - } - } + const std::string sha256hex = hash_func( U_str[0] + U_str[1] + U_str[2] + U_str[3] + V ); - 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 ThresholdUtils::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 @@ -183,7 +95,6 @@ Ciphertext TE::Encrypt( const std::string& message, const element_t& common_publ rhs_to_hash[i] = static_cast< uint8_t >( message[i] ); } - std::valarray< uint8_t > res = lhs_to_hash ^ rhs_to_hash; std::string V = ""; @@ -191,203 +102,119 @@ 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 ) { - checkCypher( ciphertext ); - if ( element_is0( const_cast< element_t& >( secret_key ) ) ) - throw std::runtime_error( "zero secret key" ); +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 ThresholdUtils::ZeroSecretKey( "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 ); - throw std::runtime_error( "cannot decrypt data" ); + if ( !res ) { + throw ThresholdUtils::IncorrectInput( "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 ); - if ( check ) { + 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_ ) ); + 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; - element_clear( U ); - element_clear( W ); - element_clear( H ); - - bool res = element_cmp( fst, snd ); - - element_clear( fst ); - element_clear( snd ); - - if ( res ) { - throw std::runtime_error( "error during share combining" ); + if ( !res ) { + throw ThresholdUtils::IncorrectInput( "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] = decrypted[i].second; + idx[i] = decryptionShares[i].second; } + std::vector< libff::alt_bn128_Fr > lagrange_coeffs = + ThresholdUtils::LagrangeCoeffs( idx, this->t_ ); - 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_ ); + libff::alt_bn128_G2 temp = lagrange_coeffs[i] * decryptionShares[i].first; - element_t tmp1; - element_init_G1( tmp1, TEDataSingleton::getData().pairing_ ); - - element_add( tmp1, sum, temp ); - - 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,89 +236,7 @@ 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 ) { - if ( idx.size() < this->t_ ) { - throw std::runtime_error( "Error, not enough participants in the threshold group" ); - } - - std::vector< element_wrapper > 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_ ); - - 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 ); - } - - 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] ); - - for ( size_t j = 0; j < this->t_; ++j ) { - if ( j != i ) { - if ( idx[i] == idx[j] ) { - element_clear( w ); - element_clear( v ); - throw std::runtime_error( - "Error during the interpolation, have same indexes in the 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 ); - } - } - - 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 ); - } - - element_clear( w ); - - return res; -} - -} // namespace encryption +} // namespace crypto diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index cc937e25..12e091b1 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 { +namespace crypto { -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,22 @@ 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 ); - - std::vector< element_wrapper > LagrangeCoeffs( const std::vector< int >& idx ); + const std::vector< std::pair< libff::alt_bn128_G2, size_t > >& decryptionShare ); private: const size_t t_ = 0; @@ -114,4 +65,4 @@ class TE { const size_t n_ = 0; }; -} // namespace encryption +} // namespace crypto diff --git a/threshold_encryption/utils.cpp b/threshold_encryption/utils.cpp deleted file mode 100644 index d707b45c..00000000 --- a/threshold_encryption/utils.cpp +++ /dev/null @@ -1,107 +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 - -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 ); -} - -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; - for ( char& c : str ) { - if ( !( c >= '0' && c <= '9' ) ) { - return false; - } - } - 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_ ) ) ) - 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 f6ecc448..00000000 --- a/threshold_encryption/utils.h +++ /dev/null @@ -1,46 +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 -#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 ); - -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/bls_glue.cpp b/tools/bls_glue.cpp index 132df512..33a8b6d6 100644 --- a/tools/bls_glue.cpp +++ b/tools/bls_glue.cpp @@ -24,8 +24,8 @@ #include -#include #include +#include #include @@ -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 ); @@ -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 = bls_instance.LagrangeCoeffs( idx ); + 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,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"] = 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 fe83fbaf..a66bbcd9 100644 --- a/tools/dkg_glue.cpp +++ b/tools/dkg_glue.cpp @@ -22,8 +22,8 @@ */ -#include #include +#include #include @@ -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 ); @@ -126,7 +126,7 @@ 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] ); + 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() ); @@ -134,13 +134,13 @@ void GenerateSecretKeys( const size_t t, const size_t n, const std::vector< std: 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 ); + crypto::ThresholdUtils::fieldElementToString( publ_key.X.c0 ); BLS_key_file["BLSPublicKey1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.X.c1 ); + crypto::ThresholdUtils::fieldElementToString( publ_key.X.c1 ); BLS_key_file["BLSPublicKey2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( publ_key.Y.c0 ); BLS_key_file["BLSPublicKey3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( publ_key.Y.c1 ); + 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"; @@ -151,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"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.X.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.X.c0 ); public_key_json["commonBLSPublicKey1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.X.c1 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.X.c1 ); public_key_json["commonBLSPublicKey2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_public_key.Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_public_key.Y.c0 ); public_key_json["commonBLSPublicKey3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( 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 0a0eaad8..47827372 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 @@ -29,7 +30,6 @@ #include #include #include -#include #define EXPAND_AS_STR( x ) __EXPAND_AS_STR__( x ) @@ -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 ); @@ -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 )] = - BLSutils::ConvertToString< libff::alt_bn128_Fr >( 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"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( verification_vector[i].X.c0 ); + crypto::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 ); + crypto::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 ); + crypto::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 ); + crypto::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 ); + crypto::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 ); + 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 baddba8a..f0d37ac9 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 @@ -24,8 +24,8 @@ #include -#include #include +#include #include @@ -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(); @@ -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 = bls_instance.LagrangeCoeffs( idx ); + 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"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.X.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.X.c0 ); outdata["commonBLSPublicKey"]["1"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.X.c1 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.X.c1 ); outdata["commonBLSPublicKey"]["2"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_keys.second.Y.c0 ); + crypto::ThresholdUtils::fieldElementToString( common_keys.second.Y.c0 ); outdata["commonBLSPublicKey"]["3"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( 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 )] = - BLSutils::ConvertToString< libff::alt_bn128_Fr >( secret_keys[i] ); + crypto::ThresholdUtils::fieldElementToString( secret_keys[i] ); outdata["BLSPublicKey"][std::to_string( i )]["0"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( public_keys[i].X.c0 ); + crypto::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 ); + crypto::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 ); + crypto::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 ); + 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 4eb7f592..11436727 100644 --- a/tools/hash_g1.cpp +++ b/tools/hash_g1.cpp @@ -22,8 +22,8 @@ */ #include -#include #include +#include #include #include #include @@ -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; @@ -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"] = BLSutils::ConvertToString( p2vals.first.X ); - joG1["g1"]["hashPoint"]["Y"] = BLSutils::ConvertToString( 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 fd78d19a..ec0a5bb4 100644 --- a/tools/sign_bls.cpp +++ b/tools/sign_bls.cpp @@ -21,8 +21,8 @@ @date 2019 */ -#include #include +#include #include @@ -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; @@ -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 = bls_instance.HashtoG1( hash_bytes_arr ); + libff::alt_bn128_G1 hash = crypto::ThresholdUtils::HashtoG1( hash_bytes_arr ); nlohmann::json hash_json; hash_json["message"] = message; @@ -118,7 +118,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 = + crypto::ThresholdUtils::LagrangeCoeffs( idx, t ); common_signature = bls_instance.SignatureRecover( signature_shares, lagrange_coeffs ); } else { @@ -143,9 +144,9 @@ void Sign( const size_t t, const size_t n, std::istream& data_file, std::ostream } signature["signature"]["X"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_signature.X ); + crypto::ThresholdUtils::fieldElementToString( common_signature.X ); signature["signature"]["Y"] = - BLSutils::ConvertToString< libff::alt_bn128_Fq >( common_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 new file mode 100644 index 00000000..210e6479 --- /dev/null +++ b/tools/utils.cpp @@ -0,0 +1,220 @@ +/* + 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 +#include + +namespace crypto { + + +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 ) { + throw IsNotWellFormed( "_requiredSigners > _totalSigners" ); + } + + if ( _totalSigners == 0 ) { + throw IncorrectInput( "_totalSigners == 0" ); + } + + if ( _requiredSigners == 0 ) { + throw IncorrectInput( "_requiredSigners == 0" ); + } +} + +std::vector< std::string > ThresholdUtils::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 > ThresholdUtils::LagrangeCoeffs( + const std::vector< size_t >& idx, size_t t ) { + if ( idx.size() < t ) { + throw IncorrectInput( "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" ); + } + + 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 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 ); + 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 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; + + 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; +} + +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 IncorrectInput( "zero element 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( + const std::string& _hint ) { + auto position = _hint.find( ":" ); + + if ( position == std::string::npos ) { + throw IncorrectInput( "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 ) { + if ( !str ) { + throw IncorrectInput( " str pointer is null in SplitString " ); + } + + 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 ); +} + +} // namespace crypto diff --git a/tools/utils.h b/tools/utils.h new file mode 100644 index 00000000..5f7a40ea --- /dev/null +++ b/tools/utils.h @@ -0,0 +1,122 @@ +/* + 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 + +#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; + + static void checkSigners( size_t _requiredSigners, size_t _totalSigners ); + + static std::vector< std::string > G2ToString( libff::alt_bn128_G2 elem ); + + static std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( + 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 ); + + 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 ); + + 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 ); +}; + +template < class T > +std::string ThresholdUtils::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; +} + +} // namespace crypto + +#endif // LIBBLS_UTILS_H 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;