Skip to content

Commit

Permalink
Merge pull request #105 from skalenetwork/SKALE-3120-fixed-libbls-init
Browse files Browse the repository at this point in the history
Skale 3120 fixed libbls init
  • Loading branch information
kladkogex authored Aug 18, 2020
2 parents e694fd8 + ceb0928 commit 6342bc9
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
12 changes: 8 additions & 4 deletions bls/BLSPrivateKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand All @@ -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" );
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion bls/BLSSigShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}

Expand All @@ -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" );
Expand Down
3 changes: 1 addition & 2 deletions bls/BLSSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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" );
}
}
Expand Down
11 changes: 8 additions & 3 deletions bls/BLSutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ along with libBLS. If not, see <https://www.gnu.org/licenses/>.
#include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>

#include "mutex"
#include <bitset>

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

Expand Down
6 changes: 5 additions & 1 deletion bls/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ along with libBLS. If not, see <https://www.gnu.org/licenses/>.
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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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 );


Expand Down
4 changes: 2 additions & 2 deletions dkg/dkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@date 2018
*/


#include "bls/BLSutils.h"
#include <dkg/dkg.h>

#include <boost/multiprecision/cpp_int.hpp>
Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions test/test_TE_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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& ) {
Expand All @@ -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& ) {
Expand All @@ -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& ) {
Expand Down Expand Up @@ -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& ) {
Expand Down
4 changes: 2 additions & 2 deletions test/test_bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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& ) {
Expand All @@ -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& ) {
Expand Down
22 changes: 12 additions & 10 deletions test/unit_tests_bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#define BOOST_TEST_MODULE

#include <bls/BLSutils.h>
#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_SUITE( libBls )
Expand Down Expand Up @@ -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" ) );

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

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

Expand Down
4 changes: 2 additions & 2 deletions test/unit_tests_dkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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 {
Expand Down

0 comments on commit 6342bc9

Please sign in to comment.