Skip to content

Commit

Permalink
Merge pull request #97 from skalenetwork/SKALE-2848-consensus-crashes
Browse files Browse the repository at this point in the history
Skale 2848 consensus crashes
  • Loading branch information
kladkogex authored Jul 9, 2020
2 parents 3d13380 + 6e22906 commit 85631dc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 39 deletions.
9 changes: 5 additions & 4 deletions bls/BLSPrivateKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ BLSPrivateKeyShare::BLSPrivateKeyShare(
privateKey = std::make_shared< libff::alt_bn128_Fr >( libff_skey );

if ( *privateKey == libff::alt_bn128_Fr::zero() ) {
throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" );
throw signatures::Bls::ZeroSecretKey( "BLS Secret key share is equal to zero" );
}
}

Expand All @@ -65,10 +65,10 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign(
std::shared_ptr< signatures::Bls > obj;

if ( _signerIndex == 0 ) {
throw signatures::Bls::IncorrectInput( "Zero signer index" );
throw signatures::Bls::IncorrectInput( "Zero signer index during BLS sign" );
}
if ( hash_byte_arr == nullptr ) {
throw signatures::Bls::IncorrectInput( "Hash is null" );
throw signatures::Bls::IncorrectInput( "Hash is null during BLS sign" );
}

obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) );
Expand Down Expand Up @@ -98,7 +98,7 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper(
throw signatures::Bls::IncorrectInput( "Zero signer index" );
}
if ( hash_byte_arr == nullptr ) {
throw signatures::Bls::IncorrectInput( "Hash is null" );
throw signatures::Bls::IncorrectInput( "Null hash is bls signWithHelper" );
}

obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) );
Expand Down Expand Up @@ -154,6 +154,7 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi
}

std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKeyShare::getPrivateKey() const {
CHECK(privateKey);
return privateKey;
}

Expand Down
31 changes: 24 additions & 7 deletions bls/BLSPublicKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > >
size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {

BLSutils::initBLS();

CHECK(pkey_str_vect);

BLSSignature::checkSigners( _requiredSigners, _totalSigners );

if ( pkey_str_vect == nullptr ) {
throw signatures::Bls::IncorrectInput( "Public Key ptr is null" );
}

libffPublicKey = std::make_shared< libff::alt_bn128_G2 >();

libffPublicKey->X.c0 = libff::alt_bn128_Fq( pkey_str_vect->at( 0 ).c_str() );
Expand All @@ -48,18 +46,30 @@ 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() || !( libffPublicKey->is_well_formed() ) ) {
throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
if ( libffPublicKey->is_zero()) {
throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key " );
}

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 );


libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( pkey );
if ( libffPublicKey->is_zero() ) {
throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
throw signatures::Bls::IsNotWellFormed( "Zero BLS Public Key" );
}
}

Expand All @@ -85,8 +95,12 @@ bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_
std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) {




CHECK(sign_ptr);

BLSutils::initBLS();

std::shared_ptr< signatures::Bls > obj;
BLSSignature::checkSigners( _requiredSigners, _totalSigners );
if ( !hash_ptr ) {
Expand Down Expand Up @@ -142,6 +156,9 @@ 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 );

signatures::Bls obj = signatures::Bls( requiredSigners, totalSigners );
Expand Down
31 changes: 18 additions & 13 deletions bls/BLSPublicKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ BLSPublicKeyShare::BLSPublicKeyShare(

BLSSignature::checkSigners( _requiredSigners, _totalSigners );

if ( pkey_str_vect == nullptr ) {
throw signatures::Bls::IncorrectInput( "Public Key ptr is null" );
}
BLSutils::initBLS();

publicKey = std::make_shared< libff::alt_bn128_G2 >();

publicKey->X.c0 = libff::alt_bn128_Fq( pkey_str_vect->at( 0 ).c_str() );
Expand All @@ -49,8 +47,12 @@ BLSPublicKeyShare::BLSPublicKeyShare(
publicKey->Z.c0 = libff::alt_bn128_Fq::one();
publicKey->Z.c1 = libff::alt_bn128_Fq::zero();

if ( publicKey->is_zero() || !( publicKey->is_well_formed() ) ) {
throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
if ( publicKey->is_zero()) {
throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key share" );
}

if (!( publicKey->is_well_formed() ) ) {
throw signatures::Bls::IsNotWellFormed( "Corrupt BLS public key share" );
}
}

Expand All @@ -59,12 +61,13 @@ BLSPublicKeyShare::BLSPublicKeyShare(
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
BLSutils::initBLS();
if ( _skey.is_zero() ) {
throw signatures::Bls::ZeroSecretKey( "Secret Key is equal to zero or corrupt" );
throw signatures::Bls::ZeroSecretKey( "Zero BLS Secret Key" );
}
publicKey = std::make_shared< libff::alt_bn128_G2 >( _skey * libff::alt_bn128_G2::one() );
}

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

Expand All @@ -85,14 +88,15 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > >
std::shared_ptr< BLSSigShare > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) {

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 || sign_ptr->getSigShare()->is_zero() ) {
throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero or corrupt" );

if ( sign_ptr->getSigShare()->is_zero() ) {
throw signatures::Bls::IsNotWellFormed( "Zero BLS Sig share" );
}

obj = std::make_shared< signatures::Bls >( signatures::Bls( _requiredSigners, _totalSigners ) );
Expand All @@ -113,15 +117,16 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_
if ( !hash_ptr ) {
throw signatures::Bls::IncorrectInput( "hash is null" );
}
if ( !sign_ptr || sign_ptr->getSigShare()->is_zero() ) {
throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero or corrupt" );
if (sign_ptr->getSigShare()->is_zero() ) {
throw signatures::Bls::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 );

libff::alt_bn128_Fq x = BLSutils::HashToFq( hash_ptr );

x = x + y_shift_x.second;

libff::alt_bn128_Fq y_sqr = y_shift_x.first ^ 2;
Expand Down
6 changes: 3 additions & 3 deletions bls/BLSPublicKeyShare.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class BLSPublicKeyShare {
size_t totalSigners;

public:
BLSPublicKeyShare( const std::shared_ptr< std::vector< std::string > >, size_t _totalSigners,
size_t _requiredSigners );
BLSPublicKeyShare( const std::shared_ptr< std::vector< std::string > >, size_t _requiredSigners,
size_t _totalSigners );

BLSPublicKeyShare(
const libff::alt_bn128_Fr& skey, size_t _totalSigners, size_t _requiredSigners );
const libff::alt_bn128_Fr& skey, size_t _requiredSigners, size_t _totalSigners );

std::shared_ptr< libff::alt_bn128_G2 > getPublicKey() const;

Expand Down
1 change: 1 addition & 0 deletions bls/BLSSigShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <string>

std::shared_ptr< libff::alt_bn128_G1 > BLSSigShare::getSigShare() const {
CHECK(sigShare);
return sigShare;
}
size_t BLSSigShare::getSignerIndex() const {
Expand Down
8 changes: 4 additions & 4 deletions bls/BLSSigShareSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) {
CHECK(_sigShare);

if ( was_merged ) {
throw signatures::Bls::IncorrectInput( "Invalid state" );
throw signatures::Bls::IncorrectInput( "Invalid state:was already merged" );
}

if ( !_sigShare ) {
throw signatures::Bls::IncorrectInput( "Null _sigShare" );
}

if ( sigShares.count( _sigShare->getSignerIndex() ) > 0 ) {
throw signatures::Bls::IncorrectInput(
Expand Down Expand Up @@ -71,6 +68,9 @@ std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index
BLSSigShareSet::BLSSigShareSet( size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ), was_merged( false ) {
BLSSignature::checkSigners( _requiredSigners, _totalSigners );

BLSutils::initBLS();

}

bool BLSSigShareSet::isEnough() {
Expand Down
26 changes: 18 additions & 8 deletions bls/BLSSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <bls/BLSutils.h>

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,
Expand All @@ -34,12 +37,17 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st
requiredSigners( _requiredSigners ),
totalSigners( _totalSigners ) {
checkSigners( _requiredSigners, _totalSigners );
BLSSignature::checkSigners( requiredSigners, totalSigners );
if ( sig == nullptr || sig->is_zero() ) {
throw signatures::Bls::IncorrectInput( "Zero or null signature" );

CHECK(sig);

BLSutils::initBLS();


if (sig->is_zero() ) {
throw signatures::Bls::IncorrectInput( "Zero BLS signature" );
}
if ( hint.length() == 0 ) {
throw signatures::Bls::IncorrectInput( "Empty or misformatted hint" );
throw signatures::Bls::IncorrectInput( "Empty BLS hint" );
}
}

Expand All @@ -50,11 +58,8 @@ BLSSignature::BLSSignature(
CHECK(_sig);

BLSSignature::checkSigners( requiredSigners, totalSigners );
BLSutils::initBLS();

if ( !_sig ) {
throw signatures::Bls::IncorrectInput( "Null signature" );
}
BLSutils::initBLS();

if ( _sig->size() < 10 ) {
throw signatures::Bls::IsNotWellFormed(
Expand All @@ -67,8 +72,10 @@ BLSSignature::BLSSignature(
}

std::shared_ptr< std::vector< std::string > > result = BLSutils::SplitString( _sig, ":" );

if ( result->size() != 4 )
throw signatures::Bls::IncorrectInput( "Misformatted signature" );

for ( auto&& str : *result ) {
for ( char& c : str ) {
if ( !( c >= '0' && c <= '9' ) ) {
Expand Down Expand Up @@ -98,6 +105,9 @@ 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" );
}
Expand Down

0 comments on commit 85631dc

Please sign in to comment.