From d53b566db77cd64213095eff2eda88da67571c77 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 15 Jul 2020 10:24:39 +0300 Subject: [PATCH] fix bls-format nad improve tests --- VERSION | 2 +- bls/BLSPrivateKey.cpp | 3 +-- bls/BLSPrivateKeyShare.cpp | 5 ++--- bls/BLSPublicKey.cpp | 23 ++++++----------------- bls/BLSPublicKeyShare.cpp | 23 +++++++++-------------- bls/BLSSigShare.cpp | 5 ++--- bls/BLSSigShareSet.cpp | 7 +------ bls/BLSSignature.cpp | 13 +++++-------- bls/BLSutils.cpp | 12 +++++------- bls/BLSutils.h | 3 +-- bls/bls.cpp | 11 ++++------- bls/bls.h | 13 ++++++------- cmake/bls-clang-format.cmake | 14 +++----------- test/test_bls.cpp | 28 +++++++++++++++------------- 14 files changed, 61 insertions(+), 101 deletions(-) diff --git a/VERSION b/VERSION index 6c6aa7cb..6da28dde 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 \ No newline at end of file +0.1.1 \ No newline at end of file diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp index 024c20a6..32a9ec76 100644 --- a/bls/BLSPrivateKey.cpp +++ b/bls/BLSPrivateKey.cpp @@ -29,8 +29,7 @@ BLSPrivateKey::BLSPrivateKey( const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - - CHECK(_key); + CHECK( _key ); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( _key->empty() ) { diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index 29a89bf1..f83cd07c 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -58,8 +58,7 @@ BLSPrivateKeyShare::BLSPrivateKeyShare( std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr, size_t _signerIndex ) { - - CHECK(hash_byte_arr); + CHECK( hash_byte_arr ); std::shared_ptr< signatures::Bls > obj; @@ -154,7 +153,7 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi } std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKeyShare::getPrivateKey() const { - CHECK(privateKey); + CHECK( privateKey ); return privateKey; } diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index 435d7acb..868a834a 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -30,10 +30,9 @@ 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(); - CHECK(pkey_str_vect); + CHECK( pkey_str_vect ); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); @@ -46,22 +45,18 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > libffPublicKey->Z.c0 = libff::alt_bn128_Fq::one(); libffPublicKey->Z.c1 = libff::alt_bn128_Fq::zero(); - if ( libffPublicKey->is_zero()) { + if ( libffPublicKey->is_zero() ) { throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key " ); } - if (!( libffPublicKey->is_well_formed() ) ) { + if ( !( libffPublicKey->is_well_formed() ) ) { throw signatures::Bls::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(); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); @@ -93,11 +88,7 @@ size_t BLSPublicKey::getRequiredSigners() const { bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { - - - - - CHECK(sign_ptr); + CHECK( sign_ptr ); BLSutils::initBLS(); @@ -118,9 +109,8 @@ 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 ) { - - CHECK(hash_ptr); - CHECK(sign_ptr); + CHECK( hash_ptr ); + CHECK( sign_ptr ); std::shared_ptr< signatures::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); @@ -156,7 +146,6 @@ 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(); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp index 4b138644..10a44c97 100644 --- a/bls/BLSPublicKeyShare.cpp +++ b/bls/BLSPublicKeyShare.cpp @@ -31,8 +31,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( const std::shared_ptr< std::vector< std::string > > pkey_str_vect, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - - CHECK(pkey_str_vect); + CHECK( pkey_str_vect ); BLSSignature::checkSigners( _requiredSigners, _totalSigners ); @@ -47,11 +46,11 @@ BLSPublicKeyShare::BLSPublicKeyShare( publicKey->Z.c0 = libff::alt_bn128_Fq::one(); publicKey->Z.c1 = libff::alt_bn128_Fq::zero(); - if ( publicKey->is_zero()) { + if ( publicKey->is_zero() ) { throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key share" ); } - if (!( publicKey->is_well_formed() ) ) { + if ( !( publicKey->is_well_formed() ) ) { throw signatures::Bls::IsNotWellFormed( "Corrupt BLS public key share" ); } } @@ -67,7 +66,7 @@ BLSPublicKeyShare::BLSPublicKeyShare( } std::shared_ptr< libff::alt_bn128_G2 > BLSPublicKeyShare::getPublicKey() const { - CHECK(publicKey); + CHECK( publicKey ); return publicKey; } @@ -86,10 +85,8 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKeyShare::toString() { bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSigShare > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { - - CHECK(hash_ptr); - CHECK(sign_ptr); - + CHECK( hash_ptr ); + CHECK( sign_ptr ); std::shared_ptr< signatures::Bls > obj; @@ -107,17 +104,15 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr, std::shared_ptr< BLSSigShare > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) { - - - CHECK(hash_ptr); - CHECK(sign_ptr); + CHECK( hash_ptr ); + CHECK( sign_ptr ); std::shared_ptr< signatures::Bls > obj; BLSSignature::checkSigners( _requiredSigners, _totalSigners ); if ( !hash_ptr ) { throw signatures::Bls::IncorrectInput( "hash is null" ); } - if (sign_ptr->getSigShare()->is_zero() ) { + if ( sign_ptr->getSigShare()->is_zero() ) { throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero" ); } diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp index b7281f31..7e7971c8 100644 --- a/bls/BLSSigShare.cpp +++ b/bls/BLSSigShare.cpp @@ -29,7 +29,7 @@ #include std::shared_ptr< libff::alt_bn128_G1 > BLSSigShare::getSigShare() const { - CHECK(sigShare); + CHECK( sigShare ); return sigShare; } size_t BLSSigShare::getSignerIndex() const { @@ -52,8 +52,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign : signerIndex( _signerIndex ), requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - - CHECK(_sigShare); + CHECK( _sigShare ); BLSSignature::checkSigners( requiredSigners, totalSigners ); BLSutils::initBLS(); diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp index 171294ef..e792e8cd 100644 --- a/bls/BLSSigShareSet.cpp +++ b/bls/BLSSigShareSet.cpp @@ -31,9 +31,7 @@ bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) { - - - CHECK(_sigShare); + CHECK( _sigShare ); if ( was_merged ) { throw signatures::Bls::IncorrectInput( "Invalid state:was already merged" ); @@ -70,7 +68,6 @@ BLSSigShareSet::BLSSigShareSet( size_t _requiredSigners, size_t _totalSigners ) BLSSignature::checkSigners( _requiredSigners, _totalSigners ); BLSutils::initBLS(); - } bool BLSSigShareSet::isEnough() { @@ -78,8 +75,6 @@ bool BLSSigShareSet::isEnough() { } - - std::shared_ptr< BLSSignature > BLSSigShareSet::merge() { if ( !isEnough() ) throw signatures::Bls::IncorrectInput( "Not enough shares to create signature" ); diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp index c47b1cae..916d9dce 100644 --- a/bls/BLSSignature.cpp +++ b/bls/BLSSignature.cpp @@ -25,8 +25,7 @@ #include std::shared_ptr< libff::alt_bn128_G1 > BLSSignature::getSig() const { - - CHECK(sig); + CHECK( sig ); return sig; } @@ -38,12 +37,12 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st totalSigners( _totalSigners ) { checkSigners( _requiredSigners, _totalSigners ); - CHECK(sig); + CHECK( sig ); BLSutils::initBLS(); - if (sig->is_zero() ) { + if ( sig->is_zero() ) { throw signatures::Bls::IncorrectInput( "Zero BLS signature" ); } if ( hint.length() == 0 ) { @@ -54,8 +53,7 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st BLSSignature::BLSSignature( std::shared_ptr< std::string > _sig, size_t _requiredSigners, size_t _totalSigners ) : requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) { - - CHECK(_sig); + CHECK( _sig ); BLSSignature::checkSigners( requiredSigners, totalSigners ); @@ -105,8 +103,7 @@ 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); + CHECK( _totalSigners > 0 ); if ( _requiredSigners > _totalSigners ) { throw signatures::Bls::IncorrectInput( "_requiredSigners > _totalSigners" ); diff --git a/bls/BLSutils.cpp b/bls/BLSutils.cpp index 559e7c97..19676f0b 100644 --- a/bls/BLSutils.cpp +++ b/bls/BLSutils.cpp @@ -28,13 +28,12 @@ along with libBLS. If not, see . #include -std::atomic BLSutils::is_initialized = false; +std::atomic< bool > BLSutils::is_initialized = false; void BLSutils::initBLS() { + auto initialized = is_initialized.exchange( true ); - auto initialized = is_initialized.exchange(true); - - if (!initialized ) { + if ( !initialized ) { libff::init_alt_bn128_params(); } } @@ -56,7 +55,7 @@ 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 hex(64); + 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; @@ -70,8 +69,7 @@ libff::alt_bn128_Fq BLSutils::HashToFq( std::shared_ptr< std::vector< std::string > > BLSutils::SplitString( std::shared_ptr< std::string > str, const std::string& delim ) { - - CHECK(str); + CHECK( str ); std::vector< std::string > tokens; size_t prev = 0, pos = 0; diff --git a/bls/BLSutils.h b/bls/BLSutils.h index aea5a695..695f895c 100644 --- a/bls/BLSutils.h +++ b/bls/BLSutils.h @@ -37,8 +37,7 @@ class BLSutils { static void initBLS(); - static std::atomic is_initialized; - + static std::atomic< bool > is_initialized; }; template < class T > diff --git a/bls/bls.cpp b/bls/bls.cpp index 8a0c8001..8239b242 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -125,8 +125,7 @@ libff::alt_bn128_G1 Bls::HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > 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); + CHECK( hash_byte_arr ); libff::alt_bn128_G1 point; libff::alt_bn128_Fq counter = libff::alt_bn128_Fq::zero(); @@ -175,9 +174,8 @@ std::pair< libff::alt_bn128_G1, std::string > Bls::HashtoG1withHint( libff::alt_bn128_G1 Bls::HashBytes( const char* raw_bytes, size_t length, std::string ( *hash_func )( const std::string& str ) ) { - - CHECK(raw_bytes); - CHECK(hash_func); + CHECK( raw_bytes ); + CHECK( hash_func ); std::string from_bytes( raw_bytes, length ); @@ -235,8 +233,7 @@ bool Bls::Verification( const std::string& to_be_hashed, const libff::alt_bn128_ bool Bls::Verification( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr, const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key ) { - - CHECK(hash_byte_arr); + CHECK( hash_byte_arr ); // verifies that a given signature corresponds to given public key diff --git a/bls/bls.h b/bls/bls.h index 54ceacc8..19ad5fa9 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -118,10 +118,9 @@ class Bls { } // namespace signatures - -#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__);} - +#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__ ); \ + } diff --git a/cmake/bls-clang-format.cmake b/cmake/bls-clang-format.cmake index 7703e5a3..31fad4ff 100644 --- a/cmake/bls-clang-format.cmake +++ b/cmake/bls-clang-format.cmake @@ -14,22 +14,14 @@ endforeach() add_custom_target(bls-format COMMENT "Running clang-format to change files" - COMMAND ${CLANG_FORMAT_BIN} - -style=file - -i - ${ALL_SOURCE_FILES} + COMMAND ${BLS_CLANG_FORMAT_BIN} -style=file -i ${ALL_SOURCE_FILES} ) add_custom_target(bls-format-check COMMENT "Checking clang-format changes" # Use ! to negate the result for correct output - COMMAND ! - ${CLANG_FORMAT_BIN} - -style=file - -output-replacements-xml - ${ALL_SOURCE_FILES} - | grep -q "replacement offset" + COMMAND ! ${BLS_CLANG_FORMAT_BIN} -style=file -output-replacements-xml ${ALL_SOURCE_FILES} | grep -q "replacement offset" ) # Get the path to this file @@ -52,6 +44,6 @@ add_custom_target(bls-format-check-changed COMMAND ${_clangcheckpath}/../scripts/clang-format-check-changed.py --file-extensions \"${CHANGED_FILE_EXTENSIONS}\" ${EXCLUDE_PATTERN_ARGS} - --clang-format-bin ${CLANG_FORMAT_BIN} + --clang-format-bin ${BLS_CLANG_FORMAT_BIN} ) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 38fbaac2..6e38682f 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -53,22 +53,24 @@ std::default_random_engine rand_gen( ( unsigned int ) time( 0 ) ); libff::alt_bn128_Fq SpoilSignCoord( libff::alt_bn128_Fq& sign_coord ) { libff::alt_bn128_Fq bad_coord = sign_coord; - size_t n_bad_bit = rand_gen() % ( bad_coord.size_in_bits() ) + 1; + do { + size_t n_bad_bit = rand_gen() % ( bad_coord.size_in_bits() ) + 1; - mpz_t was_coord; - mpz_init( was_coord ); - bad_coord.as_bigint().to_mpz( was_coord ); + mpz_t was_coord; + mpz_init( was_coord ); + bad_coord.as_bigint().to_mpz( was_coord ); - mpz_t mask; - mpz_init( mask ); - mpz_set_si( mask, n_bad_bit ); + mpz_t mask; + mpz_init( mask ); + mpz_set_si( mask, n_bad_bit ); - mpz_t badCoord; - mpz_init( badCoord ); - mpz_xor( badCoord, was_coord, mask ); + mpz_t badCoord; + mpz_init( badCoord ); + mpz_xor( badCoord, was_coord, mask ); - bad_coord = libff::alt_bn128_Fq( badCoord ); - mpz_clears( badCoord, was_coord, mask, 0 ); + bad_coord = libff::alt_bn128_Fq( badCoord ); + mpz_clears( badCoord, was_coord, mask, 0 ); + } while ( bad_coord == libff::alt_bn128_Fq::zero() ); return bad_coord; } @@ -693,7 +695,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { pkey.VerifySigWithHelper( std::make_shared< std::array< uint8_t, 32 > >( GenerateRandHash() ), nullptr, num_signed, num_all ), - signatures::Bls::IncorrectInput); + signatures::Bls::IncorrectInput ); } {