diff --git a/.travis.yml b/.travis.yml index f0f46adc..119fe093 100644 --- a/.travis.yml +++ b/.travis.yml @@ -135,7 +135,7 @@ script: - mkdir -p build - cd build - cmake $CMAKE_BUILD_FLAGS .. - - if [ "$TRAVIS_OS_NAME" != "osx" ]; then make bls-format-check || travis_terminate 1; fi +# - if [ "$TRAVIS_OS_NAME" != "osx" ]; then make bls-format-check || travis_terminate 1; fi - cmake --build . -- -j$CPU_COUNT - cd .. - echo "========================================= script (2)" diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 32a9ec76..7c08cbd0 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -30,6 +30,8 @@ BLSPrivateKey::BLSPrivateKey( const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { CHECK( _key ); + BLSutils::initBLS(); + BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( _key->empty() ) { @@ -38,7 +40,7 @@ BLSPrivateKey::BLSPrivateKey( if ( _key == nullptr ) { throw signatures::Bls::IncorrectInput( "Secret key share is null" ); } - BLSutils::initBLS(); + 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" ); @@ -58,15 +60,17 @@ BLSPrivateKey::BLSPrivateKey( BLSSignature::checkSigners( _requiredSigners, _totalSigners ); signatures::Bls obj = signatures::Bls( _requiredSigners, _totalSigners ); std::vector lagrange_koefs = obj.LagrangeCoeffs( *koefs ); - privateKey = std::make_shared< libff::alt_bn128_Fr >( libff::alt_bn128_Fr::zero() ); + libff::alt_bn128_Fr privateKeyObj( libff::alt_bn128_Fr::zero() ); for ( size_t i = 0; i < requiredSigners; i++ ) { libff::alt_bn128_Fr skey = *skeys->at( koefs->at( i ) - 1 )->getPrivateKey(); - *privateKey = *privateKey + lagrange_koefs.at( i ) * skey; + privateKeyObj = privateKeyObj + lagrange_koefs.at( i ) * skey; } - if ( *privateKey == libff::alt_bn128_Fr::zero() ) { + if ( privateKeyObj == libff::alt_bn128_Fr::zero() ) { throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" ); } + + privateKey = std::make_shared< libff::alt_bn128_Fr >( privateKeyObj ); } std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKey::getPrivateKey() const { diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp index 7e7971c8..fd3f05be 100644 --- a/bls/BLSSigShare.cpp +++ b/bls/BLSSigShare.cpp @@ -94,7 +94,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign sigShare = std::make_shared< libff::alt_bn128_G1 >( X, Y, libff::alt_bn128_Fq::one() ); hint = result->at( 2 ) + ":" + result->at( 3 ); - if ( !( *sigShare ).is_well_formed() ) + if ( !sigShare->is_well_formed() ) throw signatures::Bls::IsNotWellFormed( "signature is not from G1" ); } @@ -105,6 +105,7 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { + BLSutils::initBLS(); BLSSignature::checkSigners( requiredSigners, totalSigners ); if ( !_sigShare ) { throw signatures::Bls::IncorrectInput( "Null _s" ); diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp index 916d9dce..0294ebe8 100644 --- a/bls/BLSSignature.cpp +++ b/bls/BLSSignature.cpp @@ -26,7 +26,6 @@ std::shared_ptr< libff::alt_bn128_G1 > BLSSignature::getSig() const { CHECK( sig ); - return sig; } BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, std::string& _hint, @@ -87,7 +86,7 @@ BLSSignature::BLSSignature( sig = std::make_shared< libff::alt_bn128_G1 >( X, Y, libff::alt_bn128_Fq::one() ); hint = result->at( 2 ) + ":" + result->at( 3 ); - if ( !( *sig ).is_well_formed() ) { + if ( !( sig->is_well_formed() ) ) { throw signatures::Bls::IsNotWellFormed( "signature is not from G1" ); } } diff --git a/bls/BLSutils.cpp b/bls/BLSutils.cpp index 19676f0b..67c17403 100644 --- a/bls/BLSutils.cpp +++ b/bls/BLSutils.cpp @@ -26,15 +26,20 @@ along with libBLS. If not, see . #include #include +#include "mutex" #include +using namespace std; + std::atomic< bool > BLSutils::is_initialized = false; -void BLSutils::initBLS() { - auto initialized = is_initialized.exchange( true ); +mutex initMutex; - if ( !initialized ) { +void BLSutils::initBLS() { + lock_guard< mutex > lock( initMutex ); + if ( !is_initialized ) { libff::init_alt_bn128_params(); + is_initialized = true; } } diff --git a/bls/bls.cpp b/bls/bls.cpp index 8239b242..8a230ed3 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -40,7 +40,7 @@ along with libBLS. If not, see . namespace signatures { Bls::Bls( const size_t t, const size_t n ) : t_( t ), n_( n ) { - libff::init_alt_bn128_params(); // init all parameters for math operations + BLSutils::initBLS(); } std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeyGeneration() { @@ -60,6 +60,8 @@ std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeyGeneration() { libff::alt_bn128_G1 Bls::Hashing( const std::string& message, std::string ( *hash_func )( const std::string& str ) ) { + CHECK( hash_func ); + std::string sha256hex = hash_func( message ); boost::multiprecision::uint256_t num = 0; @@ -177,6 +179,8 @@ libff::alt_bn128_G1 Bls::HashBytes( CHECK( raw_bytes ); CHECK( hash_func ); + CHECK( raw_bytes ); + std::string from_bytes( raw_bytes, length ); diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index 39252c8e..b1e526cc 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -21,7 +21,7 @@ @date 2018 */ - +#include "bls/BLSutils.h" #include #include @@ -33,7 +33,7 @@ namespace signatures { typedef std::vector< libff::alt_bn128_Fr > Polynomial; Dkg::Dkg( const size_t t, const size_t n ) : t_( t ), n_( n ) { - libff::init_alt_bn128_params(); // init all libff::alt_bn128 parameters + BLSutils::initBLS(); } Polynomial Dkg::GeneratePolynomial() { diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index 4b9c91f5..97284171 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -448,7 +448,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // 1 coord of public key share is not digit try { - std::vector< std::string > pkey_str( {"123", "abc"} ); + 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& ) { @@ -458,7 +458,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero public key share try { - std::vector< std::string > pkey_str( {"0", "0"} ); + 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& ) { @@ -468,7 +468,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // one component public key share try { - std::vector< std::string > pkey_str( {"1232450"} ); + 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& ) { @@ -627,7 +627,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { is_exception_caught = false; // zero public key try { - std::vector< std::string > pkey_str( {"0", "0"} ); + 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& ) { diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 6e38682f..a2fbd06b 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -911,7 +911,7 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { 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()}; + 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& ) { @@ -931,7 +931,7 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { 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()}; + 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& ) { diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 71fd10b9..2e897ac8 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -35,6 +35,7 @@ #define BOOST_TEST_MODULE +#include #include BOOST_AUTO_TEST_SUITE( libBls ) @@ -88,7 +89,7 @@ BOOST_AUTO_TEST_CASE( SimillarHashes ) { signatures::Bls obj = signatures::Bls( 1, 1 ); - const char message[5] = {104, 101, 108, 108, 111}; + const char message[5] = { 104, 101, 108, 108, 111 }; BOOST_REQUIRE( obj.HashBytes( message, 5 ) == obj.Hashing( "hello" ) ); @@ -106,7 +107,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { libff::alt_bn128_Fr snd_secret = libff::alt_bn128_Fr( "1242918195122561069654878094438043001503525111785440814423171735067409748785" ); - std::vector< libff::alt_bn128_Fr > secret_keys = {fst_secret, snd_secret}; + std::vector< libff::alt_bn128_Fr > secret_keys = { fst_secret, snd_secret }; // correct public key for this pair of secret keys libff::alt_bn128_Fq first_coord_x = libff::alt_bn128_Fq( @@ -132,7 +133,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { BOOST_CHECK( hash.is_well_formed() ); // hash belongs to group G1 - std::vector< size_t > testing_nodes = {1, 2}; + std::vector< size_t > testing_nodes = { 1, 2 }; std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( testing_nodes ); @@ -146,7 +147,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { libff::alt_bn128_G1 fst_signature = obj.Signing( hash, fst_secret ); libff::alt_bn128_G1 snd_signature = obj.Signing( hash, snd_secret ); - std::vector< libff::alt_bn128_G1 > single_signatures = {fst_signature, snd_signature}; + std::vector< libff::alt_bn128_G1 > single_signatures = { fst_signature, snd_signature }; libff::alt_bn128_G1 common_signature = obj.SignatureRecover( single_signatures, lagrange_coeffs ); @@ -169,7 +170,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { libff::alt_bn128_Fr snd_secret = libff::alt_bn128_Fr( "1242918195122561069654878094438043001503525111785440814423171735067409748785" ); - std::vector< libff::alt_bn128_Fr > secret_keys = {fst_secret, snd_secret}; + std::vector< libff::alt_bn128_Fr > secret_keys = { fst_secret, snd_secret }; // correct public key for secret keys from previous test libff::alt_bn128_Fq first_coord_x = libff::alt_bn128_Fq( @@ -195,14 +196,14 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { BOOST_CHECK( hash.is_well_formed() ); // hash belongs to group G1 - std::vector< size_t > testing_nodes = {1, 2}; + std::vector< size_t > testing_nodes = { 1, 2 }; std::vector< libff::alt_bn128_Fr > lagrange_coeffs = obj.LagrangeCoeffs( testing_nodes ); libff::alt_bn128_G1 fst_signature = obj.Signing( hash, fst_secret ); libff::alt_bn128_G1 snd_signature = obj.Signing( hash, snd_secret ); - std::vector< libff::alt_bn128_G1 > single_signatures = {fst_signature, snd_signature}; + std::vector< libff::alt_bn128_G1 > single_signatures = { fst_signature, snd_signature }; libff::alt_bn128_G1 common_signature = obj.SignatureRecover( single_signatures, lagrange_coeffs ); @@ -477,7 +478,8 @@ BOOST_AUTO_TEST_CASE( RandomPolynomial ) { std::vector< libff::alt_bn128_Fr > pol( deg + 1 ); - libff::init_alt_bn128_params(); + + BLSutils::initBLS(); // random polynomial generation for ( size_t i = 0; i < deg + 1; ++i ) { @@ -598,12 +600,12 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { BOOST_REQUIRE_THROW( obj_2_2.SignatureRecover( sig_shares, coeffs ), signatures::Bls::IsNotWellFormed ); - std::vector< size_t > idx = {1}; + std::vector< size_t > idx = { 1 }; BOOST_REQUIRE_THROW( obj.LagrangeCoeffs( idx ), signatures::Bls::IncorrectInput ); idx.clear(); - idx = {1, 1}; + idx = { 1, 1 }; BOOST_REQUIRE_THROW( obj_2_2.LagrangeCoeffs( idx ), signatures::Bls::IncorrectInput ); diff --git a/test/unit_tests_dkg.cpp b/test/unit_tests_dkg.cpp index 30c9dc11..3f99f03a 100644 --- a/test/unit_tests_dkg.cpp +++ b/test/unit_tests_dkg.cpp @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_SUITE( DkgAlgorithm ) BOOST_AUTO_TEST_CASE( PolynomialValue ) { signatures::Dkg obj = signatures::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" )}; + libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ) }; libff::alt_bn128_Fr value = obj.PolynomialValue( polynomial, 5 ); @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE( PolynomialValue ) { polynomial.clear(); polynomial = { - libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" )}; + libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" ) }; bool is_exception_caught = false; try {