Skip to content

Commit

Permalink
Merge pull request #150 from skalenetwork/feature/SKALE-4409-move-to-…
Browse files Browse the repository at this point in the history
…asymmetric-encryption-scheme

Feature/skale 4409 move to asymmetric encryption scheme
  • Loading branch information
olehnikolaiev authored Aug 11, 2021
2 parents e23f24c + 83c9853 commit 45f2517
Show file tree
Hide file tree
Showing 70 changed files with 1,678 additions and 3,162 deletions.
1 change: 0 additions & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ jobs:
$NIGHTLY_BUILD_FLAGS ./bls_test
$NIGHTLY_BUILD_FLAGS ./threshold_encryption/te_unit_test
$NIGHTLY_BUILD_FLAGS ./threshold_encryption/te_test
$NIGHTLY_BUILD_FLAGS ./threshold_encryption/dkg_te_unit_test
$NIGHTLY_BUILD_FLAGS ./dkg_attack
# - name: Run python test
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ jobs:
./bls_unit_test
./dkg_unit_test
./bls_test
./threshold_encryption/dkg_te_unit_test
./threshold_encryption/te_unit_test
./threshold_encryption/te_test
./dkg_attack
Expand Down
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ set( CLANG_FORMAT_EXCLUDE_PATTERNS
)
include( BlsFindClangFormat )

set( TOOLS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tools/")
set( DKG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dkg/")

set(sourses_bls
bls/bls.cpp
bls/BLSPrivateKeyShare.cpp
Expand All @@ -70,11 +73,11 @@ set(sourses_bls
bls/BLSSignature.cpp
bls/BLSSigShare.cpp
bls/BLSSigShareSet.cpp
bls/BLSutils.cpp
dkg/dkg.cpp
dkg/DKGBLSWrapper.cpp
dkg/DKGBLSSecret.cpp
third_party/cryptlite/base64.cpp
tools/utils.cpp
)

set(headers_bls
Expand All @@ -87,7 +90,6 @@ set(headers_bls
bls/BLSSignature.h
bls/BLSSigShare.h
bls/BLSSigShareSet.h
bls/BLSutils.h
dkg/dkg.h
dkg/DKGBLSWrapper.h
dkg/DKGBLSSecret.h
Expand All @@ -96,9 +98,10 @@ set(headers_bls
third_party/cryptlite/sha1.h
third_party/cryptlite/hmac.h
third_party/cryptlite/base64.h
tools/utils.h
)

set(PROJECT_VERSION 0.1.1)
set(PROJECT_VERSION 0.2.0)
add_definitions(-DBLS_VERSION=${PROJECT_VERSION})
if ( APPLE )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wparentheses -Wunused -Wunreachable-code -Wextra -fPIC -std=c++17")
Expand All @@ -113,7 +116,6 @@ add_library(bls ${sourses_bls} ${headers_bls})

include_directories(${Boost_INCLUDE_DIRS})


if( SKALE_HAVE_BOOST_FROM_HUNTER )
set( BOOST_LIBS_4_BLS Boost::program_options )
else()
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

A mathematical library written in C++ that supports BLS threshold signatures, Distributed Key Generation (DKG) and Threshold Encryption (TE).

This libBLS library is developed by SKALE Labs and uses SCIPR-LAB's libff and PBC library by Ben Lynn (see Libraries below).
This libBLS library is developed by SKALE Labs and uses SCIPR-LAB's libff (see Libraries below).

## An important note about production readiness

Expand Down Expand Up @@ -35,7 +35,7 @@ Encryption process is running as follows:
3. Decryption
4. Verifying and combining shares

You can learn more about the algebraic structures used in this algorithm in [Ben Lynn’s PhD Dissertation](https://crypto.stanford.edu/pbc/thesis.html). libBLS uses a modified [Ben Lynn's pbc library](https://github.com/skalenetwork/pbc) with memory corruption bug fixed and the TYPE A curve for symmetric bilinear pairing.
libBls uses the same alt_bn128 curve for threshold encryption as for BLS signatures.

## Performance Specifications

Expand Down Expand Up @@ -108,7 +108,6 @@ See [docs](docs) for libBLS documentation.
## Libraries

- [libff by SCIPR-LAB](http://www.scipr-lab.org/)
- [pbc by Ben Lynn](https://crypto.stanford.edu/pbc/) with modifications from SKALE Labs

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.2.0
41 changes: 22 additions & 19 deletions bls/BLSPrivateKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,61 @@
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file BLSPrivateKey.cpp
@author Sveta Rogova
@date 2019
*/

#include "bls.h"
#include <bls/BLSPrivateKey.h>
#include <bls/BLSutils.h>
#include <bls/bls.h>
#include <tools/utils.h>


BLSPrivateKey::BLSPrivateKey(
const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
BLSutils::initBLS();
crypto::ThresholdUtils::initCurve();

BLSSignature::checkSigners( _requiredSigners, _totalSigners );
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
if ( _key == nullptr ) {
throw signatures::Bls::IncorrectInput( "Secret key share is null" );
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is null" );
}
if ( _key->empty() ) {
throw signatures::Bls::IncorrectInput( "Secret key share is empty" );
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is empty" );
}

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" );
throw crypto::ThresholdUtils::ZeroSecretKey(
"Secret key share is equal to zero or corrupt" );
}
}

BLSPrivateKey::BLSPrivateKey(
const std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > > skeys,
std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > > skeys,
std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
if ( skeys == nullptr ) {
throw signatures::Bls::IncorrectInput( "Secret keys ptr is null" );
throw crypto::ThresholdUtils::IncorrectInput( "Secret keys ptr is null" );
}
if ( koefs == nullptr ) {
throw signatures::Bls::IncorrectInput( "Signers indices ptr is null" );
throw crypto::ThresholdUtils::IncorrectInput( "Signers indices ptr is null" );
}
BLSSignature::checkSigners( _requiredSigners, _totalSigners );
signatures::Bls obj = signatures::Bls( _requiredSigners, _totalSigners );
std::vector lagrange_koefs = obj.LagrangeCoeffs( *koefs );

crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );

auto lagrange_koefs = crypto::ThresholdUtils::LagrangeCoeffs( *koefs, this->requiredSigners );
libff::alt_bn128_Fr privateKeyObj( libff::alt_bn128_Fr::zero() );
for ( size_t i = 0; i < requiredSigners; i++ ) {
for ( size_t i = 0; i < requiredSigners; ++i ) {
libff::alt_bn128_Fr skey = *skeys->at( koefs->at( i ) - 1 )->getPrivateKey();
privateKeyObj = privateKeyObj + lagrange_koefs.at( i ) * skey;
}

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

privateKey = std::make_shared< libff::alt_bn128_Fr >( privateKeyObj );
Expand All @@ -76,11 +79,11 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKey::getPrivateKey() const {
}

std::shared_ptr< std::string > BLSPrivateKey::toString() {
std::shared_ptr< std::string > key_str =
std::make_shared< std::string >( BLSutils::ConvertToString( *privateKey ) );
std::shared_ptr< std::string > key_str = std::make_shared< std::string >(
crypto::ThresholdUtils::fieldElementToString( *privateKey ) );

if ( key_str->empty() )
throw signatures::Bls::ZeroSecretKey( "Secret key share string is empty" );
throw crypto::ThresholdUtils::ZeroSecretKey( "Secret key share string is empty" );

return key_str;
}
3 changes: 1 addition & 2 deletions bls/BLSPrivateKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@


#include <bls/BLSPrivateKeyShare.h>
#include <bls/BLSSignature.h>
#include <bls/bls.h>


Expand All @@ -41,7 +40,7 @@ class BLSPrivateKey {
BLSPrivateKey(
const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners );

BLSPrivateKey( const std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >,
BLSPrivateKey( std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >,
std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners,
size_t _totalSigners );

Expand Down
59 changes: 30 additions & 29 deletions bls/BLSPrivateKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,62 @@

#include <bls/BLSPrivateKeyShare.h>
#include <bls/BLSSigShare.h>
#include <bls/BLSSignature.h>
#include <tools/utils.h>

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


BLSPrivateKeyShare::BLSPrivateKeyShare(
const std::string& _key, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
BLSSignature::checkSigners( _requiredSigners, _totalSigners );
BLSutils::initBLS();
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
crypto::ThresholdUtils::initCurve();
if ( _key.empty() ) {
throw signatures::Bls::IncorrectInput( "Secret key share string is empty" );
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" );
}
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" );
throw crypto::ThresholdUtils::ZeroSecretKey(
"Secret key share is equal to zero or corrupt" );
}
}

BLSPrivateKeyShare::BLSPrivateKeyShare(
const libff::alt_bn128_Fr& libff_skey, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
BLSSignature::checkSigners( _requiredSigners, _totalSigners );
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );

privateKey = std::make_shared< libff::alt_bn128_Fr >( libff_skey );

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

std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign(
std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr, size_t _signerIndex ) {
std::shared_ptr< signatures::Bls > obj;
std::shared_ptr< crypto::Bls > obj;

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

obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) );
obj = std::make_shared< crypto::Bls >( crypto::Bls( requiredSigners, totalSigners ) );

libff::alt_bn128_G1 hash = obj->HashtoG1( hash_byte_arr );
libff::alt_bn128_G1 hash = crypto::ThresholdUtils::HashtoG1( hash_byte_arr );

auto ss = std::make_shared< libff::alt_bn128_G1 >( obj->Signing( hash, *privateKey ) );

ss->to_affine_coordinates();

std::pair< libff::alt_bn128_G1, std::string > hash_with_hint =
obj->HashtoG1withHint( hash_byte_arr );
std::string hint =
BLSutils::ConvertToString( hash_with_hint.first.Y ) + ":" + hash_with_hint.second;
std::string hint = crypto::ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) +
":" + hash_with_hint.second;

auto s =
std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners );
Expand All @@ -88,16 +88,16 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign(

std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper(
std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr, size_t _signerIndex ) {
std::shared_ptr< signatures::Bls > obj;
std::shared_ptr< crypto::Bls > obj;

if ( _signerIndex == 0 ) {
throw signatures::Bls::IncorrectInput( "Zero signer index" );
throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" );
}
if ( hash_byte_arr == nullptr ) {
throw signatures::Bls::IncorrectInput( "Null hash is bls signWithHelper" );
throw crypto::ThresholdUtils::IncorrectInput( "Null hash is bls signWithHelper" );
}

obj = std::make_shared< signatures::Bls >( signatures::Bls( requiredSigners, totalSigners ) );
obj = std::make_shared< crypto::Bls >( crypto::Bls( requiredSigners, totalSigners ) );

std::pair< libff::alt_bn128_G1, std::string > hash_with_hint =
obj->HashtoG1withHint( hash_byte_arr );
Expand All @@ -107,8 +107,8 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper(

ss->to_affine_coordinates();

std::string hint =
BLSutils::ConvertToString( hash_with_hint.first.Y ) + ":" + hash_with_hint.second;
std::string hint = crypto::ThresholdUtils::fieldElementToString( hash_with_hint.first.Y ) +
":" + hash_with_hint.second;

auto s =
std::make_shared< BLSSigShare >( ss, hint, _signerIndex, requiredSigners, totalSigners );
Expand All @@ -119,11 +119,11 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper(
std::shared_ptr< std::pair< std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >,
std::shared_ptr< BLSPublicKey > > >
BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) {
BLSSignature::checkSigners( _requiredSigners, _totalSigners );
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );

std::vector< std::shared_ptr< BLSPrivateKeyShare > > skeys_shares;

signatures::Dkg dkg_obj = signatures::Dkg( _requiredSigners, _totalSigners );
crypto::Dkg dkg_obj = crypto::Dkg( _requiredSigners, _totalSigners );
const std::vector< libff::alt_bn128_Fr > pol = dkg_obj.GeneratePolynomial();
std::vector< libff::alt_bn128_Fr > skeys = dkg_obj.SecretKeyContribution( pol );

Expand All @@ -132,7 +132,7 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi
std::make_shared< BLSPublicKey >( common_skey, _requiredSigners, _totalSigners );

for ( size_t i = 0; i < _totalSigners; ++i ) {
std::string key_str = BLSutils::ConvertToString( skeys.at( i ) );
std::string key_str = crypto::ThresholdUtils::fieldElementToString( skeys.at( i ) );

std::shared_ptr< BLSPrivateKeyShare > key_ptr =
std::make_shared< BLSPrivateKeyShare >( key_str, _requiredSigners, _totalSigners );
Expand All @@ -156,14 +156,15 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKeyShare::getPrivateKey() const

std::shared_ptr< std::string > BLSPrivateKeyShare::toString() {
if ( !privateKey )
throw signatures::Bls::IncorrectInput( "Secret key share is null" );
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share is null" );
if ( *privateKey == libff::alt_bn128_Fr::zero() ) {
throw signatures::Bls::ZeroSecretKey( "Secret key share is equal to zero or corrupt" );
throw crypto::ThresholdUtils::ZeroSecretKey(
"Secret key share is equal to zero or corrupt" );
}
std::shared_ptr< std::string > key_str =
std::make_shared< std::string >( BLSutils::ConvertToString( *privateKey ) );
std::shared_ptr< std::string > key_str = std::make_shared< std::string >(
crypto::ThresholdUtils::fieldElementToString( *privateKey ) );

if ( key_str->empty() )
throw signatures::Bls::IncorrectInput( "Secret key share string is empty" );
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" );
return key_str;
}
Loading

0 comments on commit 45f2517

Please sign in to comment.