Skip to content

Commit

Permalink
Merge pull request #102 from skalenetwork/bug/fix-format-and-improve-…
Browse files Browse the repository at this point in the history
…tests

fix bls-format and improve tests
  • Loading branch information
olehnikolaiev authored Jul 15, 2020
2 parents 81ce3c1 + d53b566 commit 6c863ec
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 101 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1
3 changes: 1 addition & 2 deletions bls/BLSPrivateKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand Down
5 changes: 2 additions & 3 deletions bls/BLSPrivateKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
23 changes: 6 additions & 17 deletions bls/BLSPublicKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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 );
Expand Down Expand Up @@ -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();

Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
23 changes: 9 additions & 14 deletions bls/BLSPublicKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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" );
}
}
Expand All @@ -67,7 +66,7 @@ BLSPublicKeyShare::BLSPublicKeyShare(
}

std::shared_ptr< libff::alt_bn128_G2 > BLSPublicKeyShare::getPublicKey() const {
CHECK(publicKey);
CHECK( publicKey );
return publicKey;
}

Expand All @@ -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;
Expand All @@ -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" );
}

Expand Down
5 changes: 2 additions & 3 deletions bls/BLSSigShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <string>

std::shared_ptr< libff::alt_bn128_G1 > BLSSigShare::getSigShare() const {
CHECK(sigShare);
CHECK( sigShare );
return sigShare;
}
size_t BLSSigShare::getSignerIndex() const {
Expand All @@ -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();
Expand Down
7 changes: 1 addition & 6 deletions bls/BLSSigShareSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down Expand Up @@ -70,16 +68,13 @@ BLSSigShareSet::BLSSigShareSet( size_t _requiredSigners, size_t _totalSigners )
BLSSignature::checkSigners( _requiredSigners, _totalSigners );

BLSutils::initBLS();

}

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" );
Expand Down
13 changes: 5 additions & 8 deletions bls/BLSSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
#include <bls/BLSutils.h>

std::shared_ptr< libff::alt_bn128_G1 > BLSSignature::getSig() const {

CHECK(sig);
CHECK( sig );

return sig;
}
Expand All @@ -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 ) {
Expand All @@ -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 );

Expand Down Expand Up @@ -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" );
Expand Down
12 changes: 5 additions & 7 deletions bls/BLSutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ along with libBLS. If not, see <https://www.gnu.org/licenses/>.

#include <bitset>

std::atomic<bool> 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();
}
}
Expand All @@ -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<uint8_t> 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;
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions bls/BLSutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class BLSutils {
static void initBLS();


static std::atomic<bool> is_initialized;

static std::atomic< bool > is_initialized;
};

template < class T >
Expand Down
11 changes: 4 additions & 7 deletions bls/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ ); \
}
14 changes: 3 additions & 11 deletions cmake/bls-clang-format.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
)

Loading

0 comments on commit 6c863ec

Please sign in to comment.