diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml
index 291339c4..6a6f163d 100644
--- a/.github/workflows/nightly.yml
+++ b/.github/workflows/nightly.yml
@@ -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
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 835304a5..1ab6aca2 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index cc93afb3..ccf2ee33 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
@@ -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
@@ -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
@@ -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")
@@ -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()
diff --git a/README.md b/README.md
index 52344003..6ec2adda 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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
@@ -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
diff --git a/VERSION b/VERSION
index 6da28dde..341cf11f 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.1.1
\ No newline at end of file
+0.2.0
\ No newline at end of file
diff --git a/bls/BLSPrivateKey.cpp b/bls/BLSPrivateKey.cpp
index d94e231c..0539d9f0 100644
--- a/bls/BLSPrivateKey.cpp
+++ b/bls/BLSPrivateKey.cpp
@@ -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 .
+ along with libBLS. If not, see .
@file BLSPrivateKey.cpp
@author Sveta Rogova
@date 2019
*/
-#include "bls.h"
#include
-#include
+#include
+#include
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 );
@@ -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;
}
diff --git a/bls/BLSPrivateKey.h b/bls/BLSPrivateKey.h
index f656b676..1bf6bbda 100644
--- a/bls/BLSPrivateKey.h
+++ b/bls/BLSPrivateKey.h
@@ -26,7 +26,6 @@
#include
-#include
#include
@@ -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 );
diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp
index 557565bb..d8515b83 100644
--- a/bls/BLSPrivateKeyShare.cpp
+++ b/bls/BLSPrivateKeyShare.cpp
@@ -23,53 +23,53 @@
#include
#include
-#include
+#include
-#include
#include
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 ) );
@@ -77,8 +77,8 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign(
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 );
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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;
}
diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp
index 3eeb8e8e..65ffd46d 100644
--- a/bls/BLSPublicKey.cpp
+++ b/bls/BLSPublicKey.cpp
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file BLSPublicKey.cpp
@author Sveta Rogova
@@ -24,17 +24,17 @@
#include
#include
-#include
+#include
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();
+ crypto::ThresholdUtils::initCurve();
CHECK( pkey_str_vect )
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
libffPublicKey = std::make_shared< libff::alt_bn128_G2 >();
@@ -46,34 +46,34 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > >
libffPublicKey->Z.c1 = libff::alt_bn128_Fq::zero();
if ( libffPublicKey->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key " );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key " );
}
if ( !( libffPublicKey->is_well_formed() ) ) {
- throw signatures::Bls::IsNotWellFormed( "BLS public Key is corrupt" );
+ throw crypto::ThresholdUtils::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();
+ crypto::ThresholdUtils::initCurve();
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( pkey );
if ( libffPublicKey->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Zero BLS Public Key" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Public Key" );
}
}
BLSPublicKey::BLSPublicKey(
const libff::alt_bn128_Fr& skey, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( skey * libff::alt_bn128_G2::one() );
if ( libffPublicKey->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
}
}
@@ -87,20 +87,20 @@ 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 ) {
- BLSutils::initBLS();
+ crypto::ThresholdUtils::initCurve();
- std::shared_ptr< signatures::Bls > obj;
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ std::shared_ptr< crypto::Bls > obj;
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
if ( !hash_ptr ) {
- throw signatures::Bls::IncorrectInput( "hash is null" );
+ throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
}
if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero or corrupt" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Sig share is equal to zero or corrupt" );
}
- obj = std::make_shared< signatures::Bls >( signatures::Bls( _requiredSigners, _totalSigners ) );
+ obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) );
bool res = obj->Verification( hash_ptr, *( sign_ptr->getSig() ), *libffPublicKey );
return res;
@@ -108,20 +108,21 @@ 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 ) {
- std::shared_ptr< signatures::Bls > obj;
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ std::shared_ptr< crypto::Bls > obj;
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
if ( !hash_ptr ) {
- throw signatures::Bls::IncorrectInput( "hash is null" );
+ throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
}
if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) {
- throw signatures::Bls::IncorrectInput( "Sig share is equal to zero or corrupt" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Sig share is equal to zero or corrupt" );
}
std::string hint = sign_ptr->getHint();
- std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x = BLSutils::ParseHint( hint );
+ std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x =
+ crypto::ThresholdUtils::ParseHint( hint );
- libff::alt_bn128_Fq x = BLSutils::HashToFq( hash_ptr );
+ libff::alt_bn128_Fq x = crypto::ThresholdUtils::HashToFq( hash_ptr );
x = x + y_shift_x.second;
libff::alt_bn128_Fq y_sqr = y_shift_x.first ^ 2;
@@ -142,13 +143,12 @@ 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();
+ crypto::ThresholdUtils::initCurve();
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
- signatures::Bls obj = signatures::Bls( requiredSigners, totalSigners );
if ( !koefs_pkeys_map ) {
- throw signatures::Bls::IncorrectInput( "map is null" );
+ throw crypto::ThresholdUtils::IncorrectInput( "map is null" );
}
std::vector< size_t > participatingNodes;
@@ -158,7 +158,8 @@ BLSPublicKey::BLSPublicKey(
participatingNodes.push_back( static_cast< uint64_t >( item.first ) );
}
- std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = obj.LagrangeCoeffs( participatingNodes );
+ std::vector< libff::alt_bn128_Fr > lagrangeCoeffs =
+ crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners );
libff::alt_bn128_G2 key = libff::alt_bn128_G2::zero();
size_t i = 0;
@@ -173,7 +174,7 @@ BLSPublicKey::BLSPublicKey(
libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( key );
if ( libffPublicKey->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
}
}
@@ -182,10 +183,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKey::toString() {
libffPublicKey->to_affine_coordinates();
- pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->X.c0 ) );
- pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->X.c1 ) );
- pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->Y.c0 ) );
- pkey_str_vect.push_back( BLSutils::ConvertToString( libffPublicKey->Y.c1 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->X.c0 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->X.c1 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c0 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c1 ) );
return std::make_shared< std::vector< std::string > >( pkey_str_vect );
}
diff --git a/bls/BLSPublicKeyShare.cpp b/bls/BLSPublicKeyShare.cpp
index 45ac2aaf..90f3a3d7 100644
--- a/bls/BLSPublicKeyShare.cpp
+++ b/bls/BLSPublicKeyShare.cpp
@@ -23,9 +23,8 @@
#include
#include
-#include
-#include
#include
+#include
BLSPublicKeyShare::BLSPublicKeyShare(
const std::shared_ptr< std::vector< std::string > > pkey_str_vect, size_t _requiredSigners,
@@ -33,9 +32,9 @@ BLSPublicKeyShare::BLSPublicKeyShare(
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
CHECK( pkey_str_vect );
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
- BLSutils::initBLS();
+ crypto::ThresholdUtils::initCurve();
publicKey = std::make_shared< libff::alt_bn128_G2 >();
@@ -47,20 +46,20 @@ BLSPublicKeyShare::BLSPublicKeyShare(
publicKey->Z.c1 = libff::alt_bn128_Fq::zero();
if ( publicKey->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Zero BLS public Key share" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key share" );
}
if ( !( publicKey->is_well_formed() ) ) {
- throw signatures::Bls::IsNotWellFormed( "Corrupt BLS public key share" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Corrupt BLS public key share" );
}
}
BLSPublicKeyShare::BLSPublicKeyShare(
const libff::alt_bn128_Fr& _skey, size_t _totalSigners, size_t _requiredSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
- BLSutils::initBLS();
+ crypto::ThresholdUtils::initCurve();
if ( _skey.is_zero() ) {
- throw signatures::Bls::ZeroSecretKey( "Zero BLS Secret Key" );
+ throw crypto::ThresholdUtils::ZeroSecretKey( "Zero BLS Secret Key" );
}
publicKey = std::make_shared< libff::alt_bn128_G2 >( _skey * libff::alt_bn128_G2::one() );
}
@@ -75,10 +74,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKeyShare::toString() {
publicKey->to_affine_coordinates();
- pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->X.c0 ) );
- pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->X.c1 ) );
- pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->Y.c0 ) );
- pkey_str_vect.push_back( BLSutils::ConvertToString( publicKey->Y.c1 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->X.c0 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->X.c1 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->Y.c0 ) );
+ pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( publicKey->Y.c1 ) );
return std::make_shared< std::vector< std::string > >( pkey_str_vect );
}
@@ -88,14 +87,14 @@ bool BLSPublicKeyShare::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > >
CHECK( hash_ptr );
CHECK( sign_ptr );
- std::shared_ptr< signatures::Bls > obj;
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ std::shared_ptr< crypto::Bls > obj;
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
if ( sign_ptr->getSigShare()->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Zero BLS Sig share" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Sig share" );
}
- obj = std::make_shared< signatures::Bls >( signatures::Bls( _requiredSigners, _totalSigners ) );
+ obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) );
bool res = obj->Verification( hash_ptr, *( sign_ptr->getSigShare() ), *publicKey );
return res;
@@ -105,20 +104,21 @@ bool BLSPublicKeyShare::VerifySigWithHelper( std::shared_ptr< std::array< uint8_
std::shared_ptr< BLSSigShare > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) {
CHECK( sign_ptr )
- std::shared_ptr< signatures::Bls > obj;
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ std::shared_ptr< crypto::Bls > obj;
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
if ( !hash_ptr ) {
- throw signatures::Bls::IncorrectInput( "hash is null" );
+ throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
}
if ( sign_ptr->getSigShare()->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Sig share is equal to zero" );
+ throw crypto::ThresholdUtils::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 );
+ std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x =
+ crypto::ThresholdUtils::ParseHint( hint );
- libff::alt_bn128_Fq x = BLSutils::HashToFq( hash_ptr );
+ libff::alt_bn128_Fq x = crypto::ThresholdUtils::HashToFq( hash_ptr );
x = x + y_shift_x.second;
diff --git a/bls/BLSSigShare.cpp b/bls/BLSSigShare.cpp
index b4499e7a..08c38da1 100644
--- a/bls/BLSSigShare.cpp
+++ b/bls/BLSSigShare.cpp
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file BLSSigShare.cpp
@author Stan Kladko, Sveta Rogova
@@ -23,7 +23,7 @@
#include
#include
-#include
+#include
#include
#include
@@ -52,35 +52,36 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign
: signerIndex( _signerIndex ),
requiredSigners( _requiredSigners ),
totalSigners( _totalSigners ) {
- BLSSignature::checkSigners( requiredSigners, totalSigners );
- BLSutils::initBLS();
+ crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners );
+ crypto::ThresholdUtils::initCurve();
if ( _signerIndex == 0 ) {
- throw signatures::Bls::IncorrectInput( "Zero signer index" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" );
}
if ( !_sigShare ) {
- throw signatures::Bls::IncorrectInput( "Null _sigShare" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null _sigShare" );
}
if ( _sigShare->size() < 10 ) {
- throw signatures::Bls::IsNotWellFormed(
+ throw crypto::ThresholdUtils::IsNotWellFormed(
"Signature too short:" + std::to_string( _sigShare->size() ) );
}
if ( _sigShare->size() > BLS_MAX_SIG_LEN ) {
- throw signatures::Bls::IsNotWellFormed(
+ throw crypto::ThresholdUtils::IsNotWellFormed(
"Signature too long:" + std::to_string( _sigShare->size() ) );
}
- std::shared_ptr< std::vector< std::string > > result = BLSutils::SplitString( _sigShare, ":" );
+ std::shared_ptr< std::vector< std::string > > result =
+ crypto::ThresholdUtils::SplitString( _sigShare, ":" );
if ( result->size() != 4 )
- throw signatures::Bls::IncorrectInput( "Misformatted signature" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Misformatted signature" );
for ( auto&& str : *result ) {
for ( char& c : str ) {
if ( !( c >= '0' && c <= '9' ) ) {
- throw signatures::Bls::IncorrectInput(
+ throw crypto::ThresholdUtils::IncorrectInput(
"Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str );
}
}
@@ -93,7 +94,7 @@ BLSSigShare::BLSSigShare( std::shared_ptr< std::string > _sigShare, size_t _sign
hint = result->at( 2 ) + ":" + result->at( 3 );
if ( !sigShare->is_well_formed() )
- throw signatures::Bls::IsNotWellFormed( "signature is not from G1" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "signature is not from G1" );
}
BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShare,
@@ -103,24 +104,24 @@ BLSSigShare::BLSSigShare( const std::shared_ptr< libff::alt_bn128_G1 >& _sigShar
signerIndex( _signerIndex ),
requiredSigners( _requiredSigners ),
totalSigners( _totalSigners ) {
- BLSutils::initBLS();
- BLSSignature::checkSigners( requiredSigners, totalSigners );
+ crypto::ThresholdUtils::initCurve();
+ crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners );
if ( !_sigShare ) {
- throw signatures::Bls::IncorrectInput( "Null _s" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null _s" );
}
if ( _sigShare->is_zero() ) {
- throw signatures::Bls::IsNotWellFormed( "Zero signature" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "Zero signature" );
}
if ( _signerIndex == 0 ) {
- throw signatures::Bls::IncorrectInput( "Zero signer index" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Zero signer index" );
}
if ( _hint.length() == 0 ) {
- throw signatures::Bls::IncorrectInput( "Empty or misformatted hint" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Empty or misformatted hint" );
}
if ( !_sigShare->is_well_formed() ) {
- throw signatures::Bls::IsNotWellFormed( "signature is not from G1" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "signature is not from G1" );
}
}
diff --git a/bls/BLSSigShareSet.cpp b/bls/BLSSigShareSet.cpp
index e792e8cd..4e349dd0 100644
--- a/bls/BLSSigShareSet.cpp
+++ b/bls/BLSSigShareSet.cpp
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file BLSSigShareSet.cpp
@author Stan Kladko, Sveta Rogova
@@ -27,19 +27,18 @@
#include
#include
#include
-#include
+#include
bool BLSSigShareSet::addSigShare( std::shared_ptr< BLSSigShare > _sigShare ) {
CHECK( _sigShare );
if ( was_merged ) {
- throw signatures::Bls::IncorrectInput( "Invalid state:was already merged" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Invalid state:was already merged" );
}
-
if ( sigShares.count( _sigShare->getSignerIndex() ) > 0 ) {
- throw signatures::Bls::IncorrectInput(
+ throw crypto::ThresholdUtils::IncorrectInput(
"Already have this index:" + std::to_string( _sigShare->getSignerIndex() ) );
return false;
}
@@ -53,10 +52,10 @@ size_t BLSSigShareSet::getTotalSigSharesCount() {
}
std::shared_ptr< BLSSigShare > BLSSigShareSet::getSigShareByIndex( size_t _index ) {
if ( _index == 0 ) {
- throw signatures::Bls::IncorrectInput( "Index out of range:" + std::to_string( _index ) );
+ throw crypto::ThresholdUtils::IncorrectInput(
+ "Index out of range:" + std::to_string( _index ) );
}
-
if ( sigShares.count( _index ) == 0 ) {
return nullptr;
}
@@ -65,22 +64,21 @@ 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 );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
- BLSutils::initBLS();
+ crypto::ThresholdUtils::initCurve();
}
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" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Not enough shares to create signature" );
was_merged = true;
- signatures::Bls obj = signatures::Bls( requiredSigners, totalSigners );
+ crypto::Bls obj = crypto::Bls( requiredSigners, totalSigners );
std::vector< size_t > participatingNodes;
std::vector< libff::alt_bn128_G1 > shares;
@@ -90,7 +88,8 @@ std::shared_ptr< BLSSignature > BLSSigShareSet::merge() {
shares.push_back( *item.second->getSigShare() );
}
- std::vector< libff::alt_bn128_Fr > lagrangeCoeffs = obj.LagrangeCoeffs( participatingNodes );
+ std::vector< libff::alt_bn128_Fr > lagrangeCoeffs =
+ crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners );
libff::alt_bn128_G1 signature = obj.SignatureRecover( shares, lagrangeCoeffs );
diff --git a/bls/BLSSignature.cpp b/bls/BLSSignature.cpp
index 0294ebe8..423deec1 100644
--- a/bls/BLSSignature.cpp
+++ b/bls/BLSSignature.cpp
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file BLSSignature.cpp
@author Stan Kladko, Sveta Rogova
@@ -22,7 +22,7 @@
*/
#include
-#include
+#include
std::shared_ptr< libff::alt_bn128_G1 > BLSSignature::getSig() const {
CHECK( sig );
@@ -34,18 +34,17 @@ BLSSignature::BLSSignature( const std::shared_ptr< libff::alt_bn128_G1 > sig, st
hint( _hint ),
requiredSigners( _requiredSigners ),
totalSigners( _totalSigners ) {
- checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
CHECK( sig );
- BLSutils::initBLS();
-
+ crypto::ThresholdUtils::initCurve();
if ( sig->is_zero() ) {
- throw signatures::Bls::IncorrectInput( "Zero BLS signature" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Zero BLS signature" );
}
if ( hint.length() == 0 ) {
- throw signatures::Bls::IncorrectInput( "Empty BLS hint" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Empty BLS hint" );
}
}
@@ -54,29 +53,30 @@ BLSSignature::BLSSignature(
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
CHECK( _sig );
- BLSSignature::checkSigners( requiredSigners, totalSigners );
+ crypto::ThresholdUtils::checkSigners( requiredSigners, totalSigners );
- BLSutils::initBLS();
+ crypto::ThresholdUtils::initCurve();
if ( _sig->size() < 10 ) {
- throw signatures::Bls::IsNotWellFormed(
+ throw crypto::ThresholdUtils::IsNotWellFormed(
"Signature too short:" + std::to_string( _sig->size() ) );
}
if ( _sig->size() > BLS_MAX_SIG_LEN ) {
- throw signatures::Bls::IsNotWellFormed(
+ throw crypto::ThresholdUtils::IsNotWellFormed(
"Signature too long:" + std::to_string( _sig->size() ) );
}
- std::shared_ptr< std::vector< std::string > > result = BLSutils::SplitString( _sig, ":" );
+ std::shared_ptr< std::vector< std::string > > result =
+ crypto::ThresholdUtils::SplitString( _sig, ":" );
if ( result->size() != 4 )
- throw signatures::Bls::IncorrectInput( "Misformatted signature" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Misformatted signature" );
for ( auto&& str : *result ) {
for ( char& c : str ) {
if ( !( c >= '0' && c <= '9' ) ) {
- throw signatures::Bls::IncorrectInput(
+ throw crypto::ThresholdUtils::IncorrectInput(
"Misformatted char:" + std::to_string( ( int ) c ) + " in component " + str );
}
}
@@ -87,7 +87,7 @@ BLSSignature::BLSSignature(
hint = result->at( 2 ) + ":" + result->at( 3 );
if ( !( sig->is_well_formed() ) ) {
- throw signatures::Bls::IsNotWellFormed( "signature is not from G1" );
+ throw crypto::ThresholdUtils::IsNotWellFormed( "signature is not from G1" );
}
}
@@ -101,18 +101,6 @@ 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" );
- }
-
-
- if ( _totalSigners == 0 ) {
- throw signatures::Bls::IncorrectInput( "_totalSigners == 0" );
- }
-}
std::string BLSSignature::getHint() const {
return hint;
diff --git a/bls/BLSSignature.h b/bls/BLSSignature.h
index 8f832c6c..e35ffc0e 100644
--- a/bls/BLSSignature.h
+++ b/bls/BLSSignature.h
@@ -41,7 +41,6 @@ class BLSSignature {
std::shared_ptr< libff::alt_bn128_G1 > getSig() const;
std::shared_ptr< std::string > toString();
- static void checkSigners( size_t _requiredSigners, size_t _totalSigners );
std::string getHint() const;
size_t getTotalSigners() const;
size_t getRequiredSigners() const;
diff --git a/bls/BLSutils.cpp b/bls/BLSutils.cpp
deleted file mode 100644
index 7c708907..00000000
--- a/bls/BLSutils.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
-Copyright (C) 2018-2019 SKALE Labs
-
-This file is part of libBLS.
-
-libBLS is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as published
-by the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-libBLS is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-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 .
-
-@file BLSUtils.cpp
-@author Sveta Rogova
-@date 2019
-*/
-
-#include "bls.h"
-#include
-#include
-#include
-
-#include
-#include
-
-std::atomic< bool > BLSutils::is_initialized = false;
-
-std::mutex initMutex;
-
-void BLSutils::initBLS() {
- std::lock_guard< std::mutex > lock( initMutex );
- if ( !is_initialized ) {
- libff::init_alt_bn128_params();
- is_initialized = true;
- }
-}
-
-std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > BLSutils::ParseHint( std::string& _hint ) {
- auto position = _hint.find( ":" );
-
- if ( position == std::string::npos ) {
- throw std::runtime_error( "Misformatted hint" );
- }
-
- libff::alt_bn128_Fq y( _hint.substr( 0, position ).c_str() );
- libff::alt_bn128_Fq shift_x( _hint.substr( position + 1 ).c_str() );
-
- return std::make_pair( y, shift_x );
-}
-
-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 );
- 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;
- }
- mpn_set_str( from_hex.data, hex.data(), 64, 16 );
-
- libff::alt_bn128_Fq ret_val( from_hex );
-
- return ret_val;
-}
-
-std::shared_ptr< std::vector< std::string > > BLSutils::SplitString(
- std::shared_ptr< std::string > str, const std::string& delim ) {
- CHECK( str );
-
- std::vector< std::string > tokens;
- size_t prev = 0, pos = 0;
- do {
- pos = str->find( delim, prev );
- if ( pos == std::string::npos )
- pos = str->length();
- std::string token = str->substr( prev, pos - prev );
- if ( !token.empty() )
- tokens.push_back( token );
- prev = pos + delim.length();
- } while ( pos < str->length() && prev < str->length() );
-
- return std::make_shared< std::vector< std::string > >( tokens );
-}
diff --git a/bls/BLSutils.h b/bls/BLSutils.h
deleted file mode 100644
index 695f895c..00000000
--- a/bls/BLSutils.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
-Copyright (C) 2018-2019 SKALE Labs
-
-This file is part of libBLS.
-
-libBLS is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as published
-by the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-libBLS is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-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 .
-
-@file BLSUtils.h
-@author Sveta Rogova
-@date 2019
-*/
-
-#include
-#include
-
-#include
-
-class BLSutils {
-public:
- template < class T >
- static std::string ConvertToString( const T& field_elem );
- static std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ParseHint( std::string& );
- static libff::alt_bn128_Fq HashToFq( std::shared_ptr< std::array< uint8_t, 32 > > );
- static std::shared_ptr< std::vector< std::string > > SplitString(
- const std::shared_ptr< std::string >, const std::string& delim );
- static void initBLS();
-
-
- static std::atomic< bool > is_initialized;
-};
-
-template < class T >
-std::string BLSutils::ConvertToString( const T& field_elem ) {
- mpz_t t;
- mpz_init( t );
-
- field_elem.as_bigint().to_mpz( t );
-
- char arr[mpz_sizeinbase( t, 10 ) + 2];
-
- char* tmp = mpz_get_str( arr, 10, t );
- mpz_clear( t );
-
- std::string output = tmp;
-
- return output;
-}
diff --git a/bls/bls.cpp b/bls/bls.cpp
index 8a230ed3..3d088a26 100644
--- a/bls/bls.cpp
+++ b/bls/bls.cpp
@@ -23,6 +23,7 @@ along with libBLS. If not, see .
#include
+#include
#include
#include
@@ -35,12 +36,10 @@ along with libBLS. If not, see .
#include
#include
-#include
-
-namespace signatures {
+namespace crypto {
Bls::Bls( const size_t t, const size_t n ) : t_( t ), n_( n ) {
- BLSutils::initBLS();
+ ThresholdUtils::initCurve();
}
std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeyGeneration() {
@@ -79,59 +78,13 @@ libff::alt_bn128_G1 Bls::Hashing(
return hash;
}
-libff::alt_bn128_G1 Bls::HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ) {
- CHECK( hash_byte_arr );
-
- libff::alt_bn128_Fq x1( BLSutils::HashToFq( hash_byte_arr ) );
-
- libff::alt_bn128_G1 result;
-
- while ( true ) {
- libff::alt_bn128_Fq y1_sqr = x1 ^ 3;
- y1_sqr = y1_sqr + libff::alt_bn128_coeff_b;
-
- libff::alt_bn128_Fq euler = y1_sqr ^ libff::alt_bn128_Fq::euler;
-
- if ( euler == libff::alt_bn128_Fq::one() ||
- euler == libff::alt_bn128_Fq::zero() ) { // if y1_sqr is a square
- result.X = x1;
- libff::alt_bn128_Fq temp_y = y1_sqr.sqrt();
-
- mpz_t pos_y;
- mpz_init( pos_y );
-
- temp_y.as_bigint().to_mpz( pos_y );
-
- mpz_t neg_y;
- mpz_init( neg_y );
-
- ( -temp_y ).as_bigint().to_mpz( neg_y );
-
- if ( mpz_cmp( pos_y, neg_y ) < 0 ) {
- temp_y = -temp_y;
- }
-
- mpz_clear( pos_y );
- mpz_clear( neg_y );
-
- result.Y = temp_y;
- break;
- } else {
- x1 = x1 + 1;
- }
- }
- result.Z = libff::alt_bn128_Fq::one();
-
- return result;
-}
-
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 );
libff::alt_bn128_G1 point;
libff::alt_bn128_Fq counter = libff::alt_bn128_Fq::zero();
- libff::alt_bn128_Fq x1( BLSutils::HashToFq( hash_byte_arr ) );
+ libff::alt_bn128_Fq x1( ThresholdUtils::HashToFq( hash_byte_arr ) );
while ( true ) {
@@ -171,7 +124,7 @@ std::pair< libff::alt_bn128_G1, std::string > Bls::HashtoG1withHint(
}
point.Z = libff::alt_bn128_Fq::one();
- return std::make_pair( point, BLSutils::ConvertToString( counter ) );
+ return std::make_pair( point, ThresholdUtils::fieldElementToString( counter ) );
}
libff::alt_bn128_G1 Bls::HashBytes(
@@ -183,7 +136,6 @@ libff::alt_bn128_G1 Bls::HashBytes(
std::string from_bytes( raw_bytes, length );
-
libff::alt_bn128_G1 hash = Hashing( from_bytes, *hash_func );
return hash;
@@ -195,7 +147,7 @@ libff::alt_bn128_G1 Bls::Signing(
// implemented constant time signing
if ( secret_key == libff::alt_bn128_Fr::zero() ) {
- throw ZeroSecretKey( "failed to sign a message hash" );
+ throw ThresholdUtils::ZeroSecretKey( "failed to sign a message hash" );
}
std::clock_t c_start = std::clock(); // hash
@@ -217,15 +169,16 @@ bool Bls::Verification( const std::string& to_be_hashed, const libff::alt_bn128_
libff::inhibit_profiling_info = true;
if ( !sign.is_well_formed() ) {
- throw IsNotWellFormed( "Error, signature does not lie on the alt_bn128 curve" );
+ throw ThresholdUtils::IsNotWellFormed(
+ "Error, signature does not lie on the alt_bn128 curve" );
}
if ( !public_key.is_well_formed() ) {
- throw IsNotWellFormed( "Error, public key is invalid" );
+ throw ThresholdUtils::IsNotWellFormed( "Error, public key is invalid" );
}
if ( libff::alt_bn128_modulus_r * sign != libff::alt_bn128_G1::zero() ) {
- throw IsNotWellFormed( "Error, signature is not member of G1" );
+ throw ThresholdUtils::IsNotWellFormed( "Error, signature is not member of G1" );
}
libff::alt_bn128_G1 hash = Hashing( to_be_hashed );
@@ -244,18 +197,19 @@ bool Bls::Verification( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_a
libff::inhibit_profiling_info = true;
if ( !sign.is_well_formed() ) {
- throw IsNotWellFormed( "Error, signature does not lie on the alt_bn128 curve" );
+ throw ThresholdUtils::IsNotWellFormed(
+ "Error, signature does not lie on the alt_bn128 curve" );
}
if ( !public_key.is_well_formed() ) {
- throw IsNotWellFormed( "Error, public key is invalid" );
+ throw ThresholdUtils::IsNotWellFormed( "Error, public key is invalid" );
}
if ( libff::alt_bn128_modulus_r * sign != libff::alt_bn128_G1::zero() ) {
- throw IsNotWellFormed( "Error, signature is not member of G1" );
+ throw ThresholdUtils::IsNotWellFormed( "Error, signature is not member of G1" );
}
- libff::alt_bn128_G1 hash = HashtoG1( hash_byte_arr );
+ libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_byte_arr );
return ( libff::alt_bn128_ate_reduced_pairing( sign, libff::alt_bn128_G2::one() ) ==
libff::alt_bn128_ate_reduced_pairing( hash, public_key ) );
@@ -266,14 +220,14 @@ std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeysRecover(
const std::vector< libff::alt_bn128_Fr >& coeffs,
const std::vector< libff::alt_bn128_Fr >& shares ) {
if ( shares.size() < this->t_ || coeffs.size() < this->t_ ) {
- throw IncorrectInput( "not enough participants in the threshold group" );
+ throw ThresholdUtils::IncorrectInput( "not enough participants in the threshold group" );
}
libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::zero();
for ( size_t i = 0; i < this->t_; ++i ) {
if ( shares[i] == libff::alt_bn128_Fr::zero() ) {
- throw ZeroSecretKey(
+ throw ThresholdUtils::ZeroSecretKey(
"at least one secret key share is equal to zero in KeysRecover group" );
}
secret_key += coeffs[i] * shares[i]; // secret key recovering using Lagrange Interpolation
@@ -288,14 +242,14 @@ std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeysRecover(
libff::alt_bn128_G1 Bls::SignatureRecover( const std::vector< libff::alt_bn128_G1 >& shares,
const std::vector< libff::alt_bn128_Fr >& coeffs ) {
if ( shares.size() < this->t_ || coeffs.size() < this->t_ ) {
- throw IncorrectInput( "not enough participants in the threshold group" );
+ throw ThresholdUtils::IncorrectInput( "not enough participants in the threshold group" );
}
libff::alt_bn128_G1 sign = libff::alt_bn128_G1::zero();
for ( size_t i = 0; i < this->t_; ++i ) {
if ( !shares[i].is_well_formed() ) {
- throw IsNotWellFormed( "incorrect input data to recover signature" );
+ throw ThresholdUtils::IsNotWellFormed( "incorrect input data to recover signature" );
}
sign = sign + coeffs[i] * shares[i]; // signature recovering using Lagrange Coefficients
}
@@ -303,38 +257,4 @@ libff::alt_bn128_G1 Bls::SignatureRecover( const std::vector< libff::alt_bn128_G
return sign; // first element is hash of a receiving message
}
-std::vector< libff::alt_bn128_Fr > Bls::LagrangeCoeffs( const std::vector< size_t >& idx ) {
- if ( idx.size() < this->t_ ) {
- throw IncorrectInput( "not enough participants in the threshold group" );
- }
-
- std::vector< libff::alt_bn128_Fr > res( this->t_ );
-
- libff::alt_bn128_Fr w = libff::alt_bn128_Fr::one();
-
- for ( size_t i = 0; i < this->t_; ++i ) {
- w *= libff::alt_bn128_Fr( idx[i] );
- }
-
- for ( size_t i = 0; i < this->t_; ++i ) {
- libff::alt_bn128_Fr v = libff::alt_bn128_Fr( idx[i] );
-
- for ( size_t j = 0; j < this->t_; ++j ) {
- if ( j != i ) {
- if ( libff::alt_bn128_Fr( idx[i] ) == libff::alt_bn128_Fr( idx[j] ) ) {
- throw IncorrectInput(
- "during the interpolation, have same indexes in list of indexes" );
- }
-
- v *= ( libff::alt_bn128_Fr( idx[j] ) -
- libff::alt_bn128_Fr( idx[i] ) ); // calculating Lagrange coefficients
- }
- }
-
- res[i] = w * v.invert();
- }
-
- return res;
-}
-
-} // namespace signatures
+} // namespace crypto
diff --git a/bls/bls.h b/bls/bls.h
index 19ad5fa9..697608e3 100644
--- a/bls/bls.h
+++ b/bls/bls.h
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file bls.h
@author Oleh Nikolaiev
@@ -39,43 +39,10 @@ static constexpr size_t BLS_MAX_COMPONENT_LEN = 80;
static constexpr size_t BLS_MAX_SIG_LEN = 240;
-namespace signatures {
+namespace crypto {
class Bls {
-private:
- class BlsException : public std::exception {
- protected:
- std::string what_str;
-
- public:
- BlsException( const std::string& err_str ) { what_str = err_str; }
-
- virtual const char* what() const noexcept override { return what_str.c_str(); }
- };
-
public:
- class IsNotWellFormed : public BlsException {
- public:
- IsNotWellFormed( const std::string& err_str ) : BlsException( err_str ) {
- what_str = "IsNotWellFormedData : " + err_str;
- }
- };
-
- class ZeroSecretKey : public BlsException {
- public:
- ZeroSecretKey( const std::string& err_str ) : BlsException( err_str ) {
- what_str = "Secret key is equal to zero : " + err_str;
- }
- };
-
- class IncorrectInput : public BlsException {
- public:
- IncorrectInput( const std::string& err_str ) : BlsException( err_str ) {
- what_str = "Failed to proceed data : " + err_str;
- }
- };
-
-
Bls( const size_t t, const size_t n );
std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > KeyGeneration();
@@ -86,8 +53,6 @@ class Bls {
static libff::alt_bn128_G1 HashBytes( const char* raw_bytes, size_t length,
std::string ( *hash_func )( const std::string& str ) = cryptlite::sha256::hash_hex );
- static libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > );
-
static std::pair< libff::alt_bn128_G1, std::string > HashtoG1withHint(
std::shared_ptr< std::array< uint8_t, 32 > > );
@@ -107,20 +72,18 @@ class Bls {
libff::alt_bn128_G1 SignatureRecover( const std::vector< libff::alt_bn128_G1 >& shares,
const std::vector< libff::alt_bn128_Fr >& coeffs );
- std::vector< libff::alt_bn128_Fr > LagrangeCoeffs( const std::vector< size_t >& idx );
-
private:
const size_t t_ = 0;
const size_t n_ = 0;
};
-} // namespace signatures
+} // namespace crypto
#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__ ); \
+ throw crypto::ThresholdUtils::IncorrectInput( __msg__ ); \
}
diff --git a/deps/build.sh b/deps/build.sh
index 56ee5f94..534c70f3 100755
--- a/deps/build.sh
+++ b/deps/build.sh
@@ -237,7 +237,6 @@ fi
WITH_FF="yes"
WITH_GMP="yes"
-WITH_PBC="yes"
if [ -z "${PARALLEL_COUNT}" ];
then
@@ -539,7 +538,6 @@ echo -e "${COLOR_VAR_NAME}WITH_JSONRPCCPP${COLOR_DOTS}........${COLOR_VAR_DESC}L
echo -e "${COLOR_VAR_NAME}WITH_CRYPTOPP${COLOR_DOTS}..........${COLOR_VAR_DESC}LibCrypto++${COLOR_DOTS}............................${COLOR_VAR_VAL}$WITH_CRYPTOPP${COLOR_RESET}"
echo -e "${COLOR_VAR_NAME}WITH_GMP${COLOR_DOTS}...............${COLOR_VAR_DESC}LibGMP${COLOR_DOTS}.................................${COLOR_VAR_VAL}$WITH_GMP${COLOR_RESET}"
echo -e "${COLOR_VAR_NAME}WITH_FF${COLOR_DOTS}................${COLOR_VAR_DESC}LibFF${COLOR_DOTS}..................................${COLOR_VAR_VAL}$WITH_FF${COLOR_RESET}"
-echo -e "${COLOR_VAR_NAME}WITH_PBC${COLOR_DOTS}...............${COLOR_VAR_DESC}LibPBC${COLOR_DOTS}.................................${COLOR_VAR_VAL}$WITH_PBC${COLOR_RESET}"
#
#
@@ -658,7 +656,7 @@ then
echo -e "${COLOR_SEPARATOR}==================== ${COLOR_PROJECT_NAME}GMP${COLOR_SEPARATOR} ==========================================${COLOR_RESET}"
if [ ! -f "$INSTALL_ROOT/lib/libgmp.a" ] || [ ! -f "$INSTALL_ROOT/lib/libgmpxx.a" ] || [ ! -f "$INSTALL_ROOT/lib/libgmp.la" ] || [ ! -f "$INSTALL_ROOT/lib/libgmpxx.la" ];
then
- # requiired for libff and pbc
+ # requiired for libff
env_restore
cd "$SOURCES_ROOT"
if [ ! -d "gmp-6.1.2" ];
@@ -712,39 +710,6 @@ then
fi
fi
-if [ "$WITH_PBC" = "yes" ];
-then
- echo -e "${COLOR_SEPARATOR}==================== ${COLOR_PROJECT_NAME}PBC${COLOR_SEPARATOR} ==========================================${COLOR_RESET}"
- if [ ! -f "$INSTALL_ROOT/lib/libpbc.a" ] || [ ! -f "$INSTALL_ROOT/lib/libpbc.la" ];
- then
- env_restore
- cd "$SOURCES_ROOT"
- if [ ! -d "pbc" ];
- then
- echo -e "${COLOR_INFO}getting it from git${COLOR_DOTS}...${COLOR_RESET}"
- git clone https://github.com/skalenetwork/pbc.git # pbc
- fi
- echo -e "${COLOR_INFO}configuring it${COLOR_DOTS}...${COLOR_RESET}"
- cd pbc
- export CFLAGS="$CFLAGS -I${INSTALL_ROOT}/include"
- export CXXFLAGS="$CXXFLAGS -I${INSTALL_ROOT}/include"
- export CPPFLAGS="$CPPFLAGS -I${INSTALL_ROOT}/include"
- export LDFLAGS="$LDFLAGS -L${INSTALL_ROOT}/lib"
- echo " CFLAGS = $CFLAGS"
- echo " CXXFLAGS = $CXXFLAGS"
- echo " CPPFLAGS = $CPPFLAGS"
- echo " LDFLAGS = $LDFLAGS"
- $LIBTOOLIZE --force && aclocal && autoheader && automake --force-missing --add-missing && autoconf
- ./configure ${CONF_CROSSCOMPILING_OPTS_GENERIC} ${CONF_DEBUG_OPTIONS} --with-pic --enable-static --disable-shared --prefix="$INSTALL_ROOT"
- echo -e "${COLOR_INFO}building it${COLOR_DOTS}...${COLOR_RESET}"
- $MAKE ${PARALLEL_MAKE_OPTIONS}
- $MAKE ${PARALLEL_MAKE_OPTIONS} install
- cd "$SOURCES_ROOT"
- else
- echo -e "${COLOR_SUCCESS}SKIPPED${COLOR_RESET}"
- fi
-fi
-
echo -e "${COLOR_SEPARATOR}===================================================================${COLOR_RESET}"
echo -e "${COLOR_YELLOW}BLS dependencies build actions...${COLOR_RESET}"
echo -e "${COLOR_SEPARATOR}==================== ${COLOR_PROJECT_NAME}FINISH${COLOR_SEPARATOR} =======================================${COLOR_RESET}"
diff --git a/deps/clean.sh b/deps/clean.sh
index f035b910..62a5370a 100755
--- a/deps/clean.sh
+++ b/deps/clean.sh
@@ -141,7 +141,6 @@ rm -rf ./libcryptopp
# rm -rf ./gtest
rm -rf ./gmp-6.1.2
rm -rf ./libff
-rm -rf ./pbc
echo "Done (all clean)."
#finish
diff --git a/dkg/DKGBLSSecret.cpp b/dkg/DKGBLSSecret.cpp
index ad4bb4f4..e7fd549e 100644
--- a/dkg/DKGBLSSecret.cpp
+++ b/dkg/DKGBLSSecret.cpp
@@ -21,33 +21,33 @@
@date 2019
*/
-#include "DKGBLSSecret.h"
+#include
-#include
#include
+#include
DKGBLSSecret::DKGBLSSecret( size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
- signatures::Dkg dkg( requiredSigners, totalSigners );
+ crypto::Dkg dkg( requiredSigners, totalSigners );
poly = dkg.GeneratePolynomial();
}
void DKGBLSSecret::setPoly( std::vector< libff::alt_bn128_Fr > _poly ) {
if ( _poly.size() != requiredSigners ) {
- throw std::runtime_error( "Wrong size of vector" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of vector" );
}
poly = _poly;
}
std::vector< libff::alt_bn128_Fr > DKGBLSSecret::getDKGBLSSecretShares() {
- signatures::Dkg dkg( requiredSigners, totalSigners );
+ crypto::Dkg dkg( requiredSigners, totalSigners );
return dkg.SecretKeyContribution( poly );
}
std::vector< libff::alt_bn128_G2 > DKGBLSSecret::getDKGBLSPublicShares() {
- signatures::Dkg dkg( requiredSigners, totalSigners );
+ crypto::Dkg dkg( requiredSigners, totalSigners );
return dkg.VerificationVector( poly );
}
diff --git a/dkg/DKGBLSSecret.h b/dkg/DKGBLSSecret.h
index 4179782b..ab2c5bcd 100644
--- a/dkg/DKGBLSSecret.h
+++ b/dkg/DKGBLSSecret.h
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
diff --git a/dkg/DKGBLSWrapper.cpp b/dkg/DKGBLSWrapper.cpp
index 3b984c78..c86b415d 100644
--- a/dkg/DKGBLSWrapper.cpp
+++ b/dkg/DKGBLSWrapper.cpp
@@ -14,43 +14,44 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
@date 2019
*/
-#include "DKGBLSWrapper.h"
+#include
-#include
#include
+#include
+
DKGBLSWrapper::DKGBLSWrapper( size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
- BLSSignature::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
DKGBLSSecret temp( _requiredSigners, _totalSigners );
dkg_secret_ptr = std::make_shared< DKGBLSSecret >( temp );
}
bool DKGBLSWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share,
- const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector ) {
+ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ) {
if ( _share.is_zero() )
- throw std::runtime_error( " Zero secret share" );
+ throw crypto::ThresholdUtils::ZeroSecretKey( " Zero secret share" );
if ( _verification_vector == nullptr ) {
- throw std::runtime_error( " Null verification vector" );
+ throw crypto::ThresholdUtils::IncorrectInput( " Null verification vector" );
}
if ( _verification_vector->size() != requiredSigners )
- throw std::runtime_error( "Wrong vector size" );
- signatures::Dkg dkg( requiredSigners, totalSigners );
+ throw crypto::ThresholdUtils::IncorrectInput( "Wrong vector size" );
+ crypto::Dkg dkg( requiredSigners, totalSigners );
return dkg.Verification( _signerIndex, _share, *_verification_vector );
}
void DKGBLSWrapper::setDKGSecret(
std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ) {
if ( _poly_ptr == nullptr )
- throw std::runtime_error( "Null polynomial ptr" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null polynomial ptr" );
dkg_secret_ptr->setPoly( *_poly_ptr );
}
@@ -67,12 +68,12 @@ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > DKGBLSWrapper::createDKGPu
BLSPrivateKeyShare DKGBLSWrapper::CreateBLSPrivateKeyShare(
std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) {
if ( secret_shares_ptr == nullptr )
- throw std::runtime_error( "Null secret_shares_ptr " );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null secret_shares_ptr " );
if ( secret_shares_ptr->size() != totalSigners )
- throw std::runtime_error( "Wrong number of secret key parts " );
+ throw crypto::ThresholdUtils::IncorrectInput( "Wrong number of secret key parts " );
- signatures::Dkg dkg( requiredSigners, totalSigners );
+ crypto::Dkg dkg( requiredSigners, totalSigners );
libff::alt_bn128_Fr skey_share = dkg.SecretKeyShareCreate( *secret_shares_ptr );
diff --git a/dkg/DKGBLSWrapper.h b/dkg/DKGBLSWrapper.h
index 42b59d63..0ec8b335 100644
--- a/dkg/DKGBLSWrapper.h
+++ b/dkg/DKGBLSWrapper.h
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
@@ -39,7 +39,7 @@ class DKGBLSWrapper {
DKGBLSWrapper( size_t _requiredSigners, size_t _totalSigners );
bool VerifyDKGShare( size_t signerIndex, const libff::alt_bn128_Fr& share,
- const std::shared_ptr< std::vector< libff::alt_bn128_G2 > >& _verification_vector );
+ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector );
void setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr );
@@ -51,6 +51,10 @@ class DKGBLSWrapper {
std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr );
libff::alt_bn128_Fr getValueAt0();
+
+ static BLSPublicKey CreateTEPublicKey(
+ std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all,
+ size_t _requiredSigners, size_t _totalSigners );
};
diff --git a/dkg/DKGTESecret.cpp b/dkg/DKGTESecret.cpp
index f77986ba..42c142c1 100644
--- a/dkg/DKGTESecret.cpp
+++ b/dkg/DKGTESecret.cpp
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
@@ -22,33 +22,35 @@
*/
-#include "DKGTESecret.h"
+#include
+#include
-#include
-#include
+#include
DKGTESecret::DKGTESecret( size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
- TEDataSingleton::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
- encryption::DkgTe dkg_te( requiredSigners, totalSigners );
+ libff::init_alt_bn128_params();
+
+ crypto::Dkg dkg_te( requiredSigners, totalSigners );
poly = dkg_te.GeneratePolynomial();
}
-void DKGTESecret::setPoly( std::vector< encryption::element_wrapper >& _poly ) {
+void DKGTESecret::setPoly( std::vector< libff::alt_bn128_Fr >& _poly ) {
if ( _poly.size() != requiredSigners ) {
- throw std::runtime_error( "Wrong size of vector" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of vector" );
}
poly = _poly;
}
-std::vector< encryption::element_wrapper > DKGTESecret::getDKGTESecretShares() {
- encryption::DkgTe dkg_te( requiredSigners, totalSigners );
- return dkg_te.CreateSecretKeyContribution( poly );
+std::vector< libff::alt_bn128_Fr > DKGTESecret::getDKGTESecretShares() {
+ crypto::Dkg dkg_te( requiredSigners, totalSigners );
+ return dkg_te.SecretKeyContribution( poly );
}
-std::vector< encryption::element_wrapper > DKGTESecret::getDKGTEPublicShares() {
- encryption::DkgTe dkg_te( requiredSigners, totalSigners );
- return dkg_te.CreateVerificationVector( poly );
+std::vector< libff::alt_bn128_G2 > DKGTESecret::getDKGTEPublicShares() {
+ crypto::Dkg dkg_te( requiredSigners, totalSigners );
+ return dkg_te.VerificationVector( poly );
}
diff --git a/dkg/DKGTESecret.h b/dkg/DKGTESecret.h
index bdfc128f..fffdf9fa 100644
--- a/dkg/DKGTESecret.h
+++ b/dkg/DKGTESecret.h
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
@@ -31,13 +31,13 @@ class DKGTESecret {
private:
size_t requiredSigners;
size_t totalSigners;
- std::vector< encryption::element_wrapper > poly;
+ std::vector< libff::alt_bn128_Fr > poly;
public:
DKGTESecret( size_t _requiredSigners, size_t _totalSigners );
- void setPoly( std::vector< encryption::element_wrapper >& _poly );
- std::vector< encryption::element_wrapper > getDKGTESecretShares();
- std::vector< encryption::element_wrapper > getDKGTEPublicShares();
+ void setPoly( std::vector< libff::alt_bn128_Fr >& _poly );
+ std::vector< libff::alt_bn128_Fr > getDKGTESecretShares();
+ std::vector< libff::alt_bn128_G2 > getDKGTEPublicShares();
};
diff --git a/dkg/DKGTEWrapper.cpp b/dkg/DKGTEWrapper.cpp
index 89ff8fe6..4ad5c507 100644
--- a/dkg/DKGTEWrapper.cpp
+++ b/dkg/DKGTEWrapper.cpp
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
@@ -22,95 +22,76 @@
*/
-#include "DKGTEWrapper.h"
-
-#include
+#include
+#include
DKGTEWrapper::DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
- TEDataSingleton::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
+
+ libff::init_alt_bn128_params();
DKGTESecret temp( _requiredSigners, _totalSigners );
dkg_secret_ptr = std::make_shared< DKGTESecret >( temp );
}
-bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const encryption::element_wrapper& _share,
- const std::shared_ptr< std::vector< encryption::element_wrapper > >& _verification_vector ) {
- if ( element_is0( const_cast< element_t& >( _share.el_ ) ) )
- throw std::runtime_error( "Zero secret share" );
+bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_Fr& _share,
+ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > _verification_vector ) {
+ if ( _share.is_zero() )
+ throw crypto::ThresholdUtils::ZeroSecretKey( "Zero secret share" );
if ( _verification_vector == nullptr )
- throw std::runtime_error( "Null verification vector" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null verification vector" );
if ( _verification_vector->size() != requiredSigners )
- throw std::runtime_error( "Wrong size of verification vector" );
- encryption::DkgTe dkg_te( requiredSigners, totalSigners );
- return dkg_te.Verify( _signerIndex, _share, *_verification_vector );
+ throw crypto::ThresholdUtils::IncorrectInput( "Wrong size of verification vector" );
+ crypto::Dkg dkg_te( requiredSigners, totalSigners );
+ return dkg_te.Verification( _signerIndex, _share, *_verification_vector );
}
-void DKGTEWrapper::setDKGSecret(
- std::shared_ptr< std::vector< encryption::element_wrapper > >& _poly_ptr ) {
+void DKGTEWrapper::setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ) {
if ( _poly_ptr == nullptr )
- throw std::runtime_error( "Null polynomial ptr" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null polynomial ptr" );
dkg_secret_ptr->setPoly( *_poly_ptr );
}
-std::shared_ptr< std::vector< encryption::element_wrapper > >
-DKGTEWrapper::createDKGSecretShares() {
- return std::make_shared< std::vector< encryption::element_wrapper > >(
+std::shared_ptr< std::vector< libff::alt_bn128_Fr > > DKGTEWrapper::createDKGSecretShares() {
+ return std::make_shared< std::vector< libff::alt_bn128_Fr > >(
dkg_secret_ptr->getDKGTESecretShares() );
}
-std::shared_ptr< std::vector< encryption::element_wrapper > >
-DKGTEWrapper::createDKGPublicShares() {
- return std::make_shared< std::vector< encryption::element_wrapper > >(
+std::shared_ptr< std::vector< libff::alt_bn128_G2 > > DKGTEWrapper::createDKGPublicShares() {
+ return std::make_shared< std::vector< libff::alt_bn128_G2 > >(
dkg_secret_ptr->getDKGTEPublicShares() );
}
-TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare( size_t signerIndex_,
- std::shared_ptr< std::vector< encryption::element_wrapper > > secret_shares_ptr ) {
+TEPrivateKeyShare DKGTEWrapper::CreateTEPrivateKeyShare(
+ size_t signerIndex_, std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr ) {
if ( secret_shares_ptr == nullptr )
- throw std::runtime_error( "Null secret_shares_ptr " );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null secret_shares_ptr " );
if ( secret_shares_ptr->size() != totalSigners )
- throw std::runtime_error( "Wrong number of secret key parts " );
+ throw crypto::ThresholdUtils::IncorrectInput( "Wrong number of secret key parts " );
- encryption::DkgTe dkg_te( requiredSigners, totalSigners );
+ crypto::Dkg dkg_te( requiredSigners, totalSigners );
- encryption::element_wrapper skey_share = dkg_te.CreateSecretKeyShare( *secret_shares_ptr );
+ libff::alt_bn128_Fr skey_share = dkg_te.SecretKeyShareCreate( *secret_shares_ptr );
return TEPrivateKeyShare( skey_share, signerIndex_, requiredSigners, totalSigners );
}
TEPublicKey DKGTEWrapper::CreateTEPublicKey(
- std::shared_ptr< std::vector< std::vector< encryption::element_wrapper > > > public_shares_all,
+ std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all,
size_t _requiredSigners, size_t _totalSigners ) {
- TEDataSingleton::checkSigners( _requiredSigners, _totalSigners );
+ crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
if ( public_shares_all == nullptr )
- throw std::runtime_error( "Null public shares all" );
+ throw crypto::ThresholdUtils::IncorrectInput( "Null public shares all" );
- element_t public_key;
- element_init_G1( public_key, TEDataSingleton::getData().pairing_ );
- element_set0( public_key );
+ libff::alt_bn128_G2 public_key = libff::alt_bn128_G2::zero();
for ( size_t i = 0; i < _totalSigners; i++ ) {
- element_t temp;
- element_init_G1( temp, TEDataSingleton::getData().pairing_ );
- element_set( temp, public_shares_all->at( i ).at( 0 ).el_ );
-
- element_t value;
- element_init_G1( value, TEDataSingleton::getData().pairing_ );
- element_add( value, public_key, temp );
-
- element_clear( temp );
- element_clear( public_key );
- element_init_G1( public_key, TEDataSingleton::getData().pairing_ );
-
- element_set( public_key, value );
-
- element_clear( value );
+ public_key = public_key + public_shares_all->at( i ).at( 0 );
}
- TEPublicKey common_public(
- encryption::element_wrapper( public_key ), _requiredSigners, _totalSigners );
- element_clear( public_key );
+ TEPublicKey common_public( public_key, _requiredSigners, _totalSigners );
+
return common_public;
}
diff --git a/dkg/DKGTEWrapper.h b/dkg/DKGTEWrapper.h
index b05c7fc1..e846edf3 100644
--- a/dkg/DKGTEWrapper.h
+++ b/dkg/DKGTEWrapper.h
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file TEPrivateKeyShare.h
@author Sveta Rogova
@@ -26,7 +26,7 @@
#define LIBBLS_DKGTEWRAPPER_H
#include
-#include
+#include
#include
class DKGTEWrapper {
@@ -39,21 +39,20 @@ class DKGTEWrapper {
public:
DKGTEWrapper( size_t _requiredSigners, size_t _totalSigners );
- bool VerifyDKGShare( size_t signerIndex, const encryption::element_wrapper& share,
- const std::shared_ptr< std::vector< encryption::element_wrapper > >& verification_vector );
+ bool VerifyDKGShare( size_t signerIndex, const libff::alt_bn128_Fr& share,
+ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > verification_vector );
- void setDKGSecret( std::shared_ptr< std::vector< encryption::element_wrapper > >& _poly_ptr );
+ void setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr );
- std::shared_ptr< std::vector< encryption::element_wrapper > > createDKGSecretShares();
+ std::shared_ptr< std::vector< libff::alt_bn128_Fr > > createDKGSecretShares();
- std::shared_ptr< std::vector< encryption::element_wrapper > > createDKGPublicShares();
+ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > createDKGPublicShares();
TEPrivateKeyShare CreateTEPrivateKeyShare( size_t signerIndex_,
- std::shared_ptr< std::vector< encryption::element_wrapper > > secret_shares_ptr );
+ std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr );
static TEPublicKey CreateTEPublicKey(
- std::shared_ptr< std::vector< std::vector< encryption::element_wrapper > > >
- public_shares_all,
+ std::shared_ptr< std::vector< std::vector< libff::alt_bn128_G2 > > > public_shares_all,
size_t _requiredSigners, size_t _totalSigners );
};
diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp
index 93701ccd..eabc6faf 100644
--- a/dkg/dkg.cpp
+++ b/dkg/dkg.cpp
@@ -21,19 +21,19 @@
@date 2018
*/
-#include "bls/BLSutils.h"
#include
+#include
#include
#include
#include
-namespace signatures {
+namespace crypto {
typedef std::vector< libff::alt_bn128_Fr > Polynomial;
Dkg::Dkg( const size_t t, const size_t n ) : t_( t ), n_( n ) {
- BLSutils::initBLS();
+ ThresholdUtils::initCurve();
}
Polynomial Dkg::GeneratePolynomial() {
@@ -139,4 +139,4 @@ bool Dkg::isG2( const libff::alt_bn128_G2& point ) {
libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero();
}
-} // namespace signatures
+} // namespace crypto
diff --git a/dkg/dkg.h b/dkg/dkg.h
index 23f324c4..509a8b65 100644
--- a/dkg/dkg.h
+++ b/dkg/dkg.h
@@ -29,7 +29,7 @@
#include
#include
-namespace signatures {
+namespace crypto {
class Dkg {
public:
@@ -66,4 +66,4 @@ class Dkg {
const size_t n_ = 0;
};
-} // namespace signatures
+} // namespace crypto
diff --git a/dkg/dkg_te.cpp b/dkg/dkg_te.cpp
deleted file mode 100644
index 7ebadafc..00000000
--- a/dkg/dkg_te.cpp
+++ /dev/null
@@ -1,224 +0,0 @@
-/*
-Copyright (C) 2018-2019 SKALE Labs
-
-This file is part of libBLS.
-
-libBLS is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as published
-by the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-libBLS is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-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 .
-
-@file dkg_te.cpp
-@author Oleh Nikolaiev
-@date 2019
-*/
-
-#include
-
-#include
-
-namespace encryption {
-
-typedef std::vector< element_wrapper > Polynomial;
-
-DkgTe::DkgTe( const size_t t, const size_t n ) : t_( t ), n_( n ) {}
-
-Polynomial DkgTe::GeneratePolynomial() {
- Polynomial pol( this->t_ );
-
- for ( size_t i = 0; i < this->t_; ++i ) {
- element_t g;
- element_init_Zr( g, TEDataSingleton::getData().pairing_ );
- element_random( g );
-
- while ( i == this->t_ - 1 && element_is0( g ) ) {
- element_random( g );
- }
-
- pol[i] = element_wrapper( g );
-
- element_clear( g );
- }
-
- return pol;
-}
-
-std::vector< element_wrapper > DkgTe::CreateVerificationVector(
- const std::vector< element_wrapper >& polynomial ) {
- std::vector< element_wrapper > verification_vector( this->t_ );
-
- for ( size_t i = 0; i < this->t_; ++i ) {
- element_t tmp;
- element_init_G1( tmp, TEDataSingleton::getData().pairing_ );
- element_mul_zn( tmp, TEDataSingleton::getData().generator_,
- const_cast< element_t& >( polynomial[i].el_ ) );
-
- verification_vector[i] = element_wrapper( tmp );
-
- element_clear( tmp );
- }
-
- return verification_vector;
-}
-
-element_wrapper DkgTe::ComputePolynomialValue(
- const std::vector< element_wrapper >& polynomial, const element_wrapper& point ) {
- element_t value;
- element_init_Zr( value, TEDataSingleton::getData().pairing_ );
- element_set0( value );
-
- element_t pow;
- element_init_Zr( pow, TEDataSingleton::getData().pairing_ );
- element_set1( pow );
-
- for ( size_t i = 0; i < this->t_; ++i ) {
- if ( i == this->t_ - 1 && element_is0( const_cast< element_t& >( polynomial[i].el_ ) ) ) {
- element_clear( value );
- element_clear( pow );
- throw std::runtime_error( "Error, incorrect degree of a polynomial" );
- }
-
- element_t tmp;
- element_init_Zr( tmp, TEDataSingleton::getData().pairing_ );
- element_mul( tmp, const_cast< element_t& >( polynomial[i].el_ ), pow );
-
- element_t tmp1;
- element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ );
- element_set( tmp1, value );
-
- element_clear( value );
-
- element_init_Zr( value, TEDataSingleton::getData().pairing_ );
-
- element_add( value, tmp1, tmp );
-
- element_clear( tmp1 );
-
- element_clear( tmp );
-
- element_init_Zr( tmp, TEDataSingleton::getData().pairing_ );
- element_set( tmp, pow );
-
- element_clear( pow );
- element_init_Zr( pow, TEDataSingleton::getData().pairing_ );
-
- element_mul( pow, tmp, const_cast< element_t& >( point.el_ ) );
-
- element_clear( tmp );
- }
-
- element_clear( pow );
-
- element_wrapper ret_val = element_wrapper( value );
- element_clear( value );
-
- return ret_val;
-}
-
-std::vector< element_wrapper > DkgTe::CreateSecretKeyContribution(
- const std::vector< element_wrapper >& polynomial ) {
- std::vector< element_wrapper > secret_key_contribution( this->n_ );
- for ( size_t i = 0; i < this->n_; ++i ) {
- element_t point;
- element_init_Zr( point, TEDataSingleton::getData().pairing_ );
- element_set_si( point, i + 1 );
-
- secret_key_contribution[i] = ComputePolynomialValue( polynomial, point );
-
- element_clear( point );
- }
-
- return secret_key_contribution;
-}
-
-element_wrapper DkgTe::CreateSecretKeyShare(
- const std::vector< element_wrapper >& secret_key_contribution ) {
- element_t secret_key_share;
- element_init_Zr( secret_key_share, TEDataSingleton::getData().pairing_ );
- element_set0( secret_key_share );
-
- for ( size_t i = 0; i < this->n_; ++i ) {
- element_t tmp;
- element_init_Zr( tmp, TEDataSingleton::getData().pairing_ );
-
- element_set( tmp, secret_key_share );
- element_add(
- secret_key_share, tmp, const_cast< element_t& >( secret_key_contribution[i].el_ ) );
-
- element_clear( tmp );
- }
-
- if ( element_is0( secret_key_share ) ) {
- throw std::runtime_error( "Error, at least one secret key share is equal to zero" );
- }
-
- element_wrapper skey_share( secret_key_share );
- element_clear( secret_key_share );
-
- return skey_share;
-}
-
-bool DkgTe::Verify( size_t idx, const element_wrapper& share,
- const std::vector< element_wrapper >& verification_vector ) {
- element_t value;
- element_init_G1( value, TEDataSingleton::getData().pairing_ );
-
- for ( size_t i = 0; i < this->t_; ++i ) {
- element_t tmp1;
- element_init_Zr( tmp1, TEDataSingleton::getData().pairing_ );
- element_set_si( tmp1, idx + 1 );
-
- element_t tmp2;
- element_init_Zr( tmp2, TEDataSingleton::getData().pairing_ );
- element_set_si( tmp2, i );
-
- element_t tmp3;
- element_init_Zr( tmp3, TEDataSingleton::getData().pairing_ );
- element_pow_zn( tmp3, tmp1, tmp2 );
-
- element_t tmp4;
- element_init_G1( tmp4, TEDataSingleton::getData().pairing_ );
- element_mul_zn( tmp4, const_cast< element_t& >( verification_vector[i].el_ ), tmp3 );
-
- if ( i == 0 ) {
- element_set( value, tmp4 );
- } else {
- element_t tmp;
- element_init_G1( tmp, TEDataSingleton::getData().pairing_ );
- element_set( tmp, value );
-
- element_clear( value );
- element_init_G1( value, TEDataSingleton::getData().pairing_ );
-
- element_add( value, tmp, tmp4 );
-
- element_clear( tmp );
- }
-
- element_clear( tmp1 );
- element_clear( tmp2 );
- element_clear( tmp3 );
- element_clear( tmp4 );
- }
-
- element_t mul;
- element_init_G1( mul, TEDataSingleton::getData().pairing_ );
- element_mul_zn(
- mul, TEDataSingleton::getData().generator_, const_cast< element_t& >( share.el_ ) );
-
- bool ret_val = ( element_cmp( value, mul ) == 0 );
-
- element_clear( value );
- element_clear( mul );
-
- return ret_val;
-}
-} // namespace encryption
diff --git a/dkg/dkg_te.h b/dkg/dkg_te.h
deleted file mode 100644
index 6af1dd60..00000000
--- a/dkg/dkg_te.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- Copyright (C) 2018-2019 SKALE Labs
-
- This file is part of libBLS.
-
- libBLS is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as published
- by the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- libBLS is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- 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 .
-
- @file dkg_te.h
- @author Oleh Nikolaiev
- @date 2019
- */
-
-#pragma once
-
-#include
-#include
-
-namespace encryption {
-
-class DkgTe {
-public:
- DkgTe( const size_t t, const size_t n );
-
- std::vector< element_wrapper > GeneratePolynomial();
-
- std::vector< element_wrapper > CreateVerificationVector(
- const std::vector< element_wrapper >& polynomial );
-
- element_wrapper ComputePolynomialValue(
- const std::vector< element_wrapper >& polynomial, const element_wrapper& point );
-
- std::vector< element_wrapper > CreateSecretKeyContribution(
- const std::vector< element_wrapper >& polynomial );
-
- element_wrapper CreateSecretKeyShare(
- const std::vector< element_wrapper >& secret_key_contribution );
-
- bool Verify( size_t idx, const element_wrapper& share,
- const std::vector< element_wrapper >& verification_vector );
-
-private:
- const size_t t_ = 0;
-
- const size_t n_ = 0;
-};
-
-} // namespace encryption
diff --git a/docs/using-distributed-key-generation.md b/docs/using-distributed-key-generation.md
index 70deb59d..bb8e5289 100644
--- a/docs/using-distributed-key-generation.md
+++ b/docs/using-distributed-key-generation.md
@@ -30,7 +30,7 @@ std::shared_ptr < std::vector > public_shares = dkg_obj.cr
For TE
```cpp
-std::shared_ptr > public_shares =
+std::shared_ptr > public_shares =
dkg_obj.createDKGPublicShares();
```
@@ -45,7 +45,7 @@ std::shared_ptr > private_shares = dkg_obj.cr
For TE
```cpp
-std::shared_ptr < std::vector > private_shares =
+std::shared_ptr < std::vector > private_shares =
dkg_obj.createDKGSecretShares();
```
@@ -77,15 +77,15 @@ Also in DKGTEWrapper there is a static function that creates common public key
TEPublicKey publicKey = DKGTEWrapper::CreateTEPublicKey( public_shares_all, t, n);
```
-where public_shares_all is shared_ptr to matrix of all public shares ( its type is std::shared_ptr<std::vector<std::vector<encryption::element_wrapper>>>).
+where public_shares_all is shared_ptr to matrix of all public shares ( its type is std::shared_ptr<std::vector<std::vector<crypto::element_wrapper>>>).
Here is an example of Threshold Encryption algorithm with DKG simulation for t = 3, n = 4.
```cpp
size_t num_signed = 3;
size_t num_all = 4;
-std::vector> secret_shares_all; // matrix of all secret shares
-std::vector> public_shares_all; //// matrix of all public shares
+std::vector> secret_shares_all; // matrix of all secret shares
+std::vector> public_shares_all; //// matrix of all public shares
std::vector dkgs; // instances of DKGTEWrapper for each participant
std::vector skeys; // private keys of participants
std::vector pkeys; // public keys of participants
@@ -95,11 +95,11 @@ for (size_t i = 0; i < num_all; i++) {
dkgs.push_back(dkg_wrap);
// create secret shares for each participant
- std::shared_ptr> secret_shares_ptr =
+ std::shared_ptr> secret_shares_ptr =
dkg_wrap.createDKGSecretShares();
// create public shares for each participant
- std::shared_ptr> public_shares_ptr =
+ std::shared_ptr> public_shares_ptr =
dkg_wrap.createDKGPublicShares();
secret_shares_all.push_back(*secret_shares_ptr);
@@ -109,12 +109,12 @@ for (size_t i = 0; i < num_all; i++) {
for (size_t i = 0; i < num_all; i++) // Verifying shares for each participant
for (size_t j = 0; j < num_all; j++) {
assert(dkgs.at(i).VerifyDKGShare(j, secret_shares_all.at(i).at(j),
- std::make_shared>(public_shares_all.at(i))));
+ std::make_shared>(public_shares_all.at(i))));
}
- std::vector> secret_key_shares;
+ std::vector> secret_key_shares;
for (size_t i = 0; i < num_all; i++) { // collect got secret shares in a vector
- std::vector secret_key_contribution;
+ std::vector secret_key_contribution;
for (size_t j = 0; j < num_all; j++) {
secret_key_contribution.push_back(secret_shares_all.at(j).at(i));
}
@@ -124,14 +124,14 @@ for (size_t i = 0; i < num_all; i++) // Verifying shares for each participa
for (size_t i = 0; i < num_all; i++) {
TEPrivateKeyShare pkey_share = dkgs.at(i).CreateTEPrivateKeyShare(
i + 1,
- std::make_shared>(
+ std::make_shared>(
secret_key_shares.at(i)));
skeys.push_back(pkey_share);
pkeys.push_back(TEPublicKeyShare(pkey_share, num_signed, num_all));
}
TEPublicKey common_public = DKGTEWrapper::CreateTEPublicKey(
- std::make_shared< std::vector>>(public_shares_all),
+ std::make_shared< std::vector>>(public_shares_all),
num_signed,
num_all);
@@ -142,7 +142,7 @@ for (size_t i = 0; i < num_all; i++) // Verifying shares for each participa
}
std::shared_ptr msg_ptr = std::make_shared(message);
- encryption::Ciphertext cypher = common_public.encrypt(msg_ptr);
+ crypto::Ciphertext cypher = common_public.encrypt(msg_ptr);
// removing 1 random participant ( because only 3 of 4 will participate)
size_t ind4del = rand_gen() % secret_shares_all.size();
@@ -155,9 +155,9 @@ for (size_t i = 0; i < num_all; i++) // Verifying shares for each participa
TEDecryptSet decr_set(num_signed, num_all);
for (size_t i = 0; i < num_signed; i++) {
- encryption::element_wrapper decrypt = skeys.at(i).decrypt(cypher);
+ crypto::element_wrapper decrypt = skeys.at(i).decrypt(cypher);
assert(pkeys.at(i).Verify(cypher, decrypt.el_));
- std::shared_ptr decr_ptr = std::make_shared(decrypt);
+ std::shared_ptr decr_ptr = std::make_shared(decrypt);
decr_set.addDecrypt(skeys.at(i).getSignerIndex(), decr_ptr);
}
diff --git a/docs/using-threshold-encryption.md b/docs/using-threshold-encryption.md
index ab6c0934..90ac6d38 100644
--- a/docs/using-threshold-encryption.md
+++ b/docs/using-threshold-encryption.md
@@ -44,7 +44,7 @@ where i is an index of a participant.
```cpp
TEPublic publicKey = *keys.second;
-encryption::Ciphertext cipher = publicKey.encrypt(message_ptr);
+crypto::Ciphertext cipher = publicKey.encrypt(message_ptr);
```
Where message_ptr is shared_ptr to string, cipher is encrypted message.
@@ -52,7 +52,7 @@ encryption::Ciphertext cipher = publicKey.encrypt(message_ptr);
5. Get pieces of decrypted message with private key of each participant and cipher got in step 4. Verify every piece with public key of corresponding participant.
```cpp
-encryption::element_wrapper piece = privateKey.decrypt(cipher);
+crypto::element_wrapper piece = privateKey.decrypt(cipher);
assert ( publicKeyShare.Verify(cipher, piece) ) ;
```
diff --git a/libBLS.h b/libBLS.h
index e5a3bba0..713531a4 100644
--- a/libBLS.h
+++ b/libBLS.h
@@ -14,7 +14,7 @@
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 .
+ along with libBLS. If not, see .
@file libBLS.h
@author Sveta Rogova
@@ -31,7 +31,6 @@
#include
#include
-#include
#include
#include
diff --git a/python/dkgpython.cpp b/python/dkgpython.cpp
index 4aa0e726..e950f9ac 100644
--- a/python/dkgpython.cpp
+++ b/python/dkgpython.cpp
@@ -68,7 +68,7 @@ static PyObject* MakePythonBool( bool value ) {
/////////////////////////
struct PyDkgObject {
- PyObject_HEAD signatures::Dkg* pDKG;
+ PyObject_HEAD crypto::Dkg* pDKG;
};
static int PyDkgObject_init( struct PyDkgObject* self, PyObject* args, PyObject* kwds ) {
@@ -77,7 +77,7 @@ static int PyDkgObject_init( struct PyDkgObject* self, PyObject* args, PyObject*
return -1;
}
- self->pDKG = new signatures::Dkg( t, n );
+ self->pDKG = new crypto::Dkg( t, n );
return 0;
}
diff --git a/test/dkg_attack.cpp b/test/dkg_attack.cpp
index 248120af..fb83f5c4 100644
--- a/test/dkg_attack.cpp
+++ b/test/dkg_attack.cpp
@@ -2,7 +2,6 @@
#include
#include
#include
-#include
#include
#include
#include
@@ -225,7 +224,7 @@ int main() {
// // Construct the final signature
// std::shared_ptr< BLSSignature > signature = signature_share_set.merge();
- // std::cout << "isG2:" << signatures::Dkg::isG2( *( common_public_key.getPublicKey() ) );
+ // std::cout << "isG2:" << crypto::Dkg::isG2( *( common_public_key.getPublicKey() ) );
// // This assertion will fail
// assert( common_public_key.VerifySig( hash_ptr, signature, num_signed, num_all ) );
diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp
index 87b9806e..c95cd43a 100644
--- a/test/test_TE_wrappers.cpp
+++ b/test/test_TE_wrappers.cpp
@@ -24,14 +24,14 @@ along with libBLS. If not, see .
#define BOOST_TEST_MODULE
-#include
+#include
#include
#include
#include
#include
#include
#include
-#include
+#include
#include
#include
@@ -54,73 +54,19 @@ std::string spoilMessage( std::string& message ) {
BOOST_AUTO_TEST_SUITE( ThresholdEncryptionWrappers )
-BOOST_AUTO_TEST_CASE( testSqrt ) {
- for ( size_t i = 0; i < 100; i++ ) {
- gmp_randstate_t state;
- gmp_randinit_default( state );
-
- mpz_t rand;
- mpz_init( rand );
-
- mpz_t num_limbs_mpz;
- mpz_init( num_limbs_mpz );
- mpz_set_si( num_limbs_mpz, num_limbs );
-
- mpz_urandomm( rand, state, num_limbs_mpz );
-
- mpz_clear( num_limbs_mpz );
-
- mpz_t modulus_q;
- mpz_init( modulus_q );
- mpz_set_str( modulus_q,
- "87807107996633125224377819847540498158068831994142082110286533992664756308802229570786"
- "25179422662221423155858769582317459277713367317481324925129998224791",
- 10 );
-
- mpz_t sqr_mod;
- mpz_init( sqr_mod );
- mpz_powm_ui( sqr_mod, rand, 2, modulus_q );
-
- mpz_t mpz_sqrt0;
- mpz_init( mpz_sqrt0 );
- mpz_mod( mpz_sqrt0, rand, modulus_q );
-
- mpz_clear( rand );
-
- mpz_t mpz_sqrt;
- mpz_init( mpz_sqrt );
-
- MpzSquareRoot( mpz_sqrt, sqr_mod );
-
- mpz_t sum;
- mpz_init( sum );
-
- mpz_add( sum, mpz_sqrt0, mpz_sqrt );
-
- BOOST_REQUIRE( mpz_cmp( mpz_sqrt0, mpz_sqrt ) == 0 || mpz_cmp( sum, modulus_q ) == 0 );
-
- mpz_clears( mpz_sqrt0, mpz_sqrt, sqr_mod, sum, modulus_q, 0 );
- gmp_randclear( state );
- }
-}
-
BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) {
for ( size_t i = 0; i < 10; i++ ) {
size_t num_all = rand_gen() % 16 + 1;
size_t num_signed = rand_gen() % num_all + 1;
- encryption::DkgTe dkg_te( num_signed, num_all );
+ crypto::Dkg dkg_te( num_signed, num_all );
- std::vector< encryption::element_wrapper > poly = dkg_te.GeneratePolynomial();
- element_t zero;
- element_init_Zr( zero, TEDataSingleton::getData().pairing_ );
- element_set0( zero );
- encryption::element_wrapper zero_el( zero );
+ std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial();
- element_clear( zero );
+ libff::alt_bn128_Fr zero_el = libff::alt_bn128_Fr::zero();
- encryption::element_wrapper common_skey = dkg_te.ComputePolynomialValue( poly, zero_el );
- BOOST_REQUIRE( element_cmp( common_skey.el_, poly.at( 0 ).el_ ) == 0 );
+ libff::alt_bn128_Fr common_skey = dkg_te.PolynomialValue( poly, zero_el );
+ BOOST_REQUIRE( common_skey == poly.at( 0 ) );
TEPrivateKey common_private( common_skey, num_signed, num_all );
@@ -131,16 +77,14 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) {
}
TEPublicKey common_public( common_private, num_signed, num_all );
- std::shared_ptr msg_ptr = std::make_shared< std::string >( message );
- encryption::Ciphertext cypher = common_public.encrypt( msg_ptr );
+ auto msg_ptr = std::make_shared< std::string >( message );
+ crypto::Ciphertext cypher = common_public.encrypt( msg_ptr );
- std::vector< encryption::element_wrapper > skeys =
- dkg_te.CreateSecretKeyContribution( poly );
+ std::vector< libff::alt_bn128_Fr > skeys = dkg_te.SecretKeyContribution( poly );
std::vector< TEPrivateKeyShare > skey_shares;
std::vector< TEPublicKeyShare > public_key_shares;
for ( size_t i = 0; i < num_all; i++ ) {
- skey_shares.emplace_back(
- TEPrivateKeyShare( skeys[i].el_, i + 1, num_signed, num_all ) );
+ skey_shares.emplace_back( TEPrivateKeyShare( skeys[i], i + 1, num_signed, num_all ) );
public_key_shares.emplace_back(
TEPublicKeyShare( skey_shares[i], num_signed, num_all ) );
}
@@ -157,92 +101,62 @@ BOOST_AUTO_TEST_CASE( TEProcessWithWrappers ) {
TEDecryptSet decr_set( num_signed, num_all );
for ( size_t i = 0; i < num_signed; i++ ) {
- encryption::element_wrapper decrypt = skey_shares[i].decrypt( cypher );
- BOOST_REQUIRE( public_key_shares[i].Verify( cypher, decrypt.el_ ) );
- std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt );
+ libff::alt_bn128_G2 decrypt = skey_shares[i].getDecryptionShare( cypher );
+ BOOST_REQUIRE( public_key_shares[i].Verify( cypher, decrypt ) );
+ auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt );
decr_set.addDecrypt( skey_shares[i].getSignerIndex(), decr_ptr );
}
std::string message_decrypted = decr_set.merge( cypher );
BOOST_REQUIRE( message == message_decrypted );
- encryption::Ciphertext bad_cypher = cypher; // corrupt V in cypher
+ crypto::Ciphertext bad_cypher = cypher; // corrupt V in cypher
std::get< 1 >( bad_cypher ) = spoilMessage( std::get< 1 >( cypher ) );
- bool is_exception_caught = false;
- try {
- decr_set.merge( bad_cypher );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
- // cannot add after merge
- decr_set.addDecrypt( num_signed, nullptr );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
+ BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput );
+
+ // cannot add after merge
+ BOOST_REQUIRE_THROW(
+ decr_set.addDecrypt( num_signed, nullptr ), crypto::ThresholdUtils::IncorrectInput );
bad_cypher = cypher; // corrupt U in cypher
- element_t rand_el;
- element_init_G1( rand_el, TEDataSingleton::getData().pairing_ );
+ libff::alt_bn128_G2 rand_el = libff::alt_bn128_G2::random_element();
std::get< 0 >( bad_cypher ) = rand_el;
- is_exception_caught = false;
- try {
- decr_set.merge( bad_cypher );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
+ BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput );
bad_cypher = cypher; // corrupt W in cypher
- element_t rand_el2;
- element_init_G1( rand_el2, TEDataSingleton::getData().pairing_ );
+ libff::alt_bn128_G1 rand_el2 = libff::alt_bn128_G1::random_element();
std::get< 2 >( bad_cypher ) = rand_el2;
- is_exception_caught = false;
- try {
- decr_set.merge( bad_cypher );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
+
+ BOOST_REQUIRE_THROW( decr_set.merge( bad_cypher ), crypto::ThresholdUtils::IncorrectInput );
size_t ind = rand_gen() % num_signed; // corrupt random private key share
- element_t bad_pkey;
- element_init_Zr( bad_pkey, TEDataSingleton::getData().pairing_ );
- element_random( bad_pkey );
- TEPrivateKeyShare bad_key( encryption::element_wrapper( bad_pkey ),
- skey_shares[ind].getSignerIndex(), num_signed, num_all );
+ libff::alt_bn128_Fr bad_pkey = libff::alt_bn128_Fr::random_element();
+ TEPrivateKeyShare bad_key(
+ bad_pkey, skey_shares[ind].getSignerIndex(), num_signed, num_all );
skey_shares[ind] = bad_key;
- element_clear( bad_pkey );
TEDecryptSet bad_decr_set( num_signed, num_all );
for ( size_t i = 0; i < num_signed; i++ ) {
- encryption::element_wrapper decrypt = skey_shares[i].decrypt( cypher );
+ libff::alt_bn128_G2 decrypt = skey_shares[i].getDecryptionShare( cypher );
if ( i == ind )
- BOOST_REQUIRE( !public_key_shares[i].Verify( cypher, decrypt.el_ ) );
- std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt );
+ BOOST_REQUIRE( !public_key_shares[i].Verify( cypher, decrypt ) );
+ auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt );
bad_decr_set.addDecrypt( skey_shares[i].getSignerIndex(), decr_ptr );
}
std::string bad_message_decrypted = bad_decr_set.merge( cypher );
BOOST_REQUIRE( message != bad_message_decrypted );
-
- element_clear( rand_el );
- element_clear( rand_el2 );
}
}
BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) {
- for ( size_t i = 0; i < 10; i++ ) {
+ for ( size_t i = 0; i < 10; ++i ) {
size_t num_all = rand_gen() % 16 + 1;
size_t num_signed = rand_gen() % num_all + 1;
- encryption::DkgTe dkg_te( num_signed, num_all );
-
+ crypto::Dkg dkg_te( num_signed, num_all );
std::string message;
size_t msg_length = 64;
@@ -255,8 +169,8 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) {
keys = TEPrivateKeyShare::generateSampleKeys( num_signed, num_all );
- std::shared_ptr msg_ptr = std::make_shared< std::string >( message );
- encryption::Ciphertext cypher = keys.second->encrypt( msg_ptr );
+ auto msg_ptr = std::make_shared< std::string >( message );
+ crypto::Ciphertext cypher = keys.second->encrypt( msg_ptr );
std::vector< TEPublicKeyShare > public_key_shares;
for ( size_t i = 0; i < num_all; i++ ) {
@@ -276,9 +190,9 @@ BOOST_AUTO_TEST_CASE( ShortTEProcessWithWrappers ) {
TEDecryptSet decr_set( num_signed, num_all );
for ( size_t i = 0; i < num_signed; i++ ) {
- encryption::element_wrapper decrypt = ( *keys.first->at( i ) ).decrypt( cypher );
- BOOST_REQUIRE( public_key_shares.at( i ).Verify( cypher, decrypt.el_ ) );
- std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt );
+ libff::alt_bn128_G2 decrypt = ( *keys.first->at( i ) ).getDecryptionShare( cypher );
+ BOOST_REQUIRE( public_key_shares.at( i ).Verify( cypher, decrypt ) );
+ auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt );
decr_set.addDecrypt( ( *keys.first->at( i ) ).getSignerIndex(), decr_ptr );
}
std::string message_decrypted = decr_set.merge( cypher );
@@ -291,49 +205,32 @@ BOOST_AUTO_TEST_CASE( WrappersFromString ) {
size_t num_all = rand_gen() % 16 + 1;
size_t num_signed = rand_gen() % num_all + 1;
- element_t test0;
- element_init_G1( test0, TEDataSingleton::getData().pairing_ );
- element_random( test0 );
- TEPublicKey common_pkey( encryption::element_wrapper( test0 ), num_signed, num_all );
-
- element_clear( test0 );
+ libff::alt_bn128_G2 test0 = libff::alt_bn128_G2::random_element();
+ TEPublicKey common_pkey( test0, num_signed, num_all );
TEPublicKey common_pkey_from_str( common_pkey.toString(), num_signed, num_all );
- BOOST_REQUIRE( element_cmp( common_pkey.getPublicKey().el_,
- common_pkey_from_str.getPublicKey().el_ ) == 0 );
+ BOOST_REQUIRE( common_pkey.getPublicKey() == common_pkey_from_str.getPublicKey() );
- element_t test;
- element_init_Zr( test, TEDataSingleton::getData().pairing_ );
- element_random( test );
- TEPrivateKey private_key( encryption::element_wrapper( test ), num_signed, num_all );
-
- element_clear( test );
+ libff::alt_bn128_Fr test = libff::alt_bn128_Fr::random_element();
+ TEPrivateKey private_key( test, num_signed, num_all );
TEPrivateKey private_key_from_str(
std::make_shared< std::string >( private_key.toString() ), num_signed, num_all );
- BOOST_REQUIRE( element_cmp( private_key.getPrivateKey().el_,
- private_key_from_str.getPrivateKey().el_ ) == 0 );
+ BOOST_REQUIRE( private_key.getPrivateKey() == private_key_from_str.getPrivateKey() );
- element_t test2;
- element_init_Zr( test2, TEDataSingleton::getData().pairing_ );
- element_random( test2 );
+ libff::alt_bn128_Fr test2 = libff::alt_bn128_Fr::random_element();
size_t signer = rand_gen() % num_all;
- TEPrivateKeyShare pr_key_share(
- encryption::element_wrapper( test2 ), signer, num_signed, num_all );
-
- element_clear( test2 );
+ TEPrivateKeyShare pr_key_share( test2, signer, num_signed, num_all );
TEPrivateKeyShare pr_key_share_from_str(
std::make_shared< std::string >( pr_key_share.toString() ), signer, num_signed,
num_all );
- BOOST_REQUIRE( element_cmp( pr_key_share.getPrivateKey().el_,
- pr_key_share_from_str.getPrivateKey().el_ ) == 0 );
+ BOOST_REQUIRE( pr_key_share.getPrivateKey() == pr_key_share_from_str.getPrivateKey() );
TEPublicKeyShare pkey( pr_key_share, num_signed, num_all );
TEPublicKeyShare pkey_from_str(
pkey.toString(), pr_key_share.getSignerIndex(), num_signed, num_all );
- BOOST_REQUIRE(
- element_cmp( pkey.getPublicKey().el_, pkey_from_str.getPublicKey().el_ ) == 0 );
+ BOOST_REQUIRE( pkey.getPublicKey() == pkey_from_str.getPublicKey() );
}
std::cerr << "TE wrappers tests finished" << std::endl;
}
@@ -342,8 +239,8 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
for ( size_t i = 0; i < 10; i++ ) {
size_t num_all = rand_gen() % 15 + 2;
size_t num_signed = rand_gen() % num_all + 1;
- std::vector< std::vector< encryption::element_wrapper > > secret_shares_all;
- std::vector< std::vector< encryption::element_wrapper > > public_shares_all;
+ std::vector< std::vector< libff::alt_bn128_Fr > > secret_shares_all;
+ std::vector< std::vector< libff::alt_bn128_G2 > > public_shares_all;
std::vector< DKGTEWrapper > dkgs;
std::vector< TEPrivateKeyShare > skeys;
std::vector< TEPublicKeyShare > pkeys;
@@ -351,16 +248,15 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
for ( size_t i = 0; i < num_all; i++ ) {
DKGTEWrapper dkg_wrap( num_signed, num_all );
- encryption::DkgTe dkg_te( num_signed, num_all );
- std::vector< encryption::element_wrapper > poly = dkg_te.GeneratePolynomial();
- auto shared_poly =
- std::make_shared< std::vector< encryption::element_wrapper > >( poly );
+ crypto::Dkg dkg_te( num_signed, num_all );
+ std::vector< libff::alt_bn128_Fr > poly = dkg_te.GeneratePolynomial();
+ auto shared_poly = std::make_shared< std::vector< libff::alt_bn128_Fr > >( poly );
dkg_wrap.setDKGSecret( shared_poly );
dkgs.push_back( dkg_wrap );
- std::shared_ptr< std::vector< encryption::element_wrapper > > secret_shares_ptr =
+ std::shared_ptr< std::vector< libff::alt_bn128_Fr > > secret_shares_ptr =
dkg_wrap.createDKGSecretShares();
- std::shared_ptr< std::vector< encryption::element_wrapper > > public_shares_ptr =
+ std::shared_ptr< std::vector< libff::alt_bn128_G2 > > public_shares_ptr =
dkg_wrap.createDKGPublicShares();
secret_shares_all.push_back( *secret_shares_ptr );
public_shares_all.push_back( *public_shares_ptr );
@@ -370,14 +266,14 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
for ( size_t i = 0; i < num_all; i++ )
for ( size_t j = 0; j < num_all; j++ ) {
BOOST_REQUIRE( dkgs.at( i ).VerifyDKGShare( j, secret_shares_all.at( i ).at( j ),
- std::make_shared< std::vector< encryption::element_wrapper > >(
+ std::make_shared< std::vector< libff::alt_bn128_G2 > >(
public_shares_all.at( i ) ) ) );
}
- std::vector< std::vector< encryption::element_wrapper > > secret_key_shares;
+ std::vector< std::vector< libff::alt_bn128_Fr > > secret_key_shares;
for ( size_t i = 0; i < num_all; i++ ) {
- std::vector< encryption::element_wrapper > secret_key_contribution;
+ std::vector< libff::alt_bn128_Fr > secret_key_contribution;
for ( size_t j = 0; j < num_all; j++ ) {
secret_key_contribution.push_back( secret_shares_all.at( j ).at( i ) );
}
@@ -386,7 +282,7 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
for ( size_t i = 0; i < num_all; i++ ) {
TEPrivateKeyShare pkey_share = dkgs.at( i ).CreateTEPrivateKeyShare(
- i + 1, std::make_shared< std::vector< encryption::element_wrapper > >(
+ i + 1, std::make_shared< std::vector< libff::alt_bn128_Fr > >(
secret_key_shares.at( i ) ) );
skeys.push_back( pkey_share );
pkeys.push_back( TEPublicKeyShare( pkey_share, num_signed, num_all ) );
@@ -415,11 +311,11 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
element_clear(value);
}
- TEPublicKey common_public(encryption::element_wrapper(public_key), num_signed, num_all);
+ TEPublicKey common_public(crypto::element_wrapper(public_key), num_signed, num_all);
element_clear(public_key);*/
TEPublicKey common_public = DKGTEWrapper::CreateTEPublicKey(
- std::make_shared< std::vector< std::vector< encryption::element_wrapper > > >(
+ std::make_shared< std::vector< std::vector< libff::alt_bn128_G2 > > >(
public_shares_all ),
num_signed, num_all );
@@ -429,8 +325,8 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
message += char( rand_gen() % 128 );
}
- std::shared_ptr msg_ptr = std::make_shared< std::string >( message );
- encryption::Ciphertext cypher = common_public.encrypt( msg_ptr );
+ auto msg_ptr = std::make_shared< std::string >( message );
+ crypto::Ciphertext cypher = common_public.encrypt( msg_ptr );
for ( size_t i = 0; i < num_all - num_signed; ++i ) {
size_t ind4del = rand_gen() % secret_shares_all.size();
@@ -444,9 +340,9 @@ BOOST_AUTO_TEST_CASE( ThresholdEncryptionWithDKG ) {
TEDecryptSet decr_set( num_signed, num_all );
for ( size_t i = 0; i < num_signed; i++ ) {
- encryption::element_wrapper decrypt = skeys[i].decrypt( cypher );
- BOOST_REQUIRE( pkeys[i].Verify( cypher, decrypt.el_ ) );
- std::shared_ptr decr_ptr = std::make_shared< encryption::element_wrapper >( decrypt );
+ libff::alt_bn128_G2 decrypt = skeys[i].getDecryptionShare( cypher );
+ BOOST_REQUIRE( pkeys[i].Verify( cypher, decrypt ) );
+ auto decr_ptr = std::make_shared< libff::alt_bn128_G2 >( decrypt );
decr_set.addDecrypt( skeys[i].getSignerIndex(), decr_ptr );
}
@@ -460,439 +356,283 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) {
size_t num_all = rand_gen() % 15 + 2;
size_t num_signed = rand_gen() % num_all + 1;
- bool is_exception_caught = false;
- try {
- TEDataSingleton::checkSigners( 0, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
+ BOOST_REQUIRE_THROW( crypto::ThresholdUtils::checkSigners( 0, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
- is_exception_caught = false;
- try {
- TEDataSingleton::checkSigners( 0, 0 );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
+ BOOST_REQUIRE_THROW(
+ crypto::ThresholdUtils::checkSigners( 0, 0 ), crypto::ThresholdUtils::IncorrectInput );
- is_exception_caught = false; // null public key share
- try {
- TEPublicKeyShare( nullptr, 1, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
+ // null public key share
+ BOOST_REQUIRE_THROW( TEPublicKeyShare( nullptr, 1, num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
- is_exception_caught = false; // 1 coord of public key share is not digit
- try {
+ {
+ // 1 coord of public key share is not digit
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& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW(
+ TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1,
+ num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // zero public key share
- try {
+ {
+ // zero public key share
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& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW(
+ TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1,
+ num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // one component public key share
- try {
+ {
+ // one component public key share
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& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false; // one zero component in cypher
- try {
- element_t el;
- element_init_Zr( el, TEDataSingleton::getData().pairing_ );
- element_random( el );
- encryption::element_wrapper el_wrap( el );
- element_clear( el );
- TEPublicKeyShare pkey(
- TEPrivateKeyShare( el_wrap, 1, num_signed, num_all ), num_signed, num_all );
+ BOOST_REQUIRE_THROW(
+ TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1,
+ num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
+ }
- element_t U;
- element_init_G1( U, TEDataSingleton::getData().pairing_ );
- element_set_str( U, "[0, 0]", 10 );
- encryption::element_wrapper U_wrap( U );
- element_clear( U );
+ {
+ // one zero component in cypher
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element();
+ TEPublicKeyShare pkey(
+ TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all );
+ libff::alt_bn128_G2 U = libff::alt_bn128_G2::zero();
- element_t W;
- element_init_G1( W, TEDataSingleton::getData().pairing_ );
- element_random( W );
- encryption::element_wrapper W_wrap( W );
- element_clear( W );
+ libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element();
- encryption::Ciphertext cypher;
- std::get< 0 >( cypher ) = U_wrap;
+ crypto::Ciphertext cypher;
+ std::get< 0 >( cypher ) = U;
std::get< 1 >( cypher ) = "tra-la-la";
- std::get< 2 >( cypher ) = W_wrap;
+ std::get< 2 >( cypher ) = W;
- pkey.Verify( cypher, el );
-
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false; // wrong string length in cypher
- try {
- element_t el;
- element_init_Zr( el, TEDataSingleton::getData().pairing_ );
- element_random( el );
- encryption::element_wrapper el_wrap( el );
- element_clear( el );
+ {
+ // wrong string length in cypher
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element();
TEPublicKeyShare pkey(
- TEPrivateKeyShare( el_wrap, 1, num_signed, num_all ), num_signed, num_all );
- element_t U;
- element_init_G1( U, TEDataSingleton::getData().pairing_ );
- element_random( U );
+ TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all );
+ libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element();
- element_t W;
- element_init_G1( W, TEDataSingleton::getData().pairing_ );
- element_random( W );
+ libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element();
- encryption::Ciphertext cypher;
- std::get< 0 >( cypher ) = encryption::element_wrapper( U );
+ crypto::Ciphertext cypher;
+ std::get< 0 >( cypher ) = U;
std::get< 1 >( cypher ) = "tra-la-la";
- std::get< 2 >( cypher ) = encryption::element_wrapper( W );
+ std::get< 2 >( cypher ) = W;
- element_clear( U );
- element_clear( W );
-
- pkey.Verify( cypher, el );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( pkey.Verify( cypher, U ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // zero decrypted
- try {
- element_t el;
- element_init_Zr( el, TEDataSingleton::getData().pairing_ );
- element_random( el );
+ {
+ // zero decrypted
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element();
TEPublicKeyShare pkey(
- TEPrivateKeyShare( encryption::element_wrapper( el ), 1, num_signed, num_all ),
- num_signed, num_all );
+ TEPrivateKeyShare( el, 1, num_signed, num_all ), num_signed, num_all );
- element_t U;
- element_init_G1( U, TEDataSingleton::getData().pairing_ );
- element_random( U );
+ libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element();
- element_t W;
- element_init_G1( W, TEDataSingleton::getData().pairing_ );
- element_random( W );
+ libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element();
- encryption::Ciphertext cypher;
- std::get< 0 >( cypher ) = encryption::element_wrapper( U );
+ crypto::Ciphertext cypher;
+ std::get< 0 >( cypher ) = U;
std::get< 1 >( cypher ) =
"Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!";
- std::get< 2 >( cypher ) = encryption::element_wrapper( W );
+ std::get< 2 >( cypher ) = W;
- element_t decr;
- element_init_G1( decr, TEDataSingleton::getData().pairing_ );
- element_set_str( decr, "[0, 0]", 10 );
- encryption::element_wrapper decrypt( decr );
- element_clear( decr );
+ libff::alt_bn128_G2 decrypt = libff::alt_bn128_G2::zero();
- element_clear( el );
- element_clear( U );
- element_clear( W );
+ BOOST_REQUIRE_THROW(
+ pkey.Verify( cypher, decrypt ), crypto::ThresholdUtils::IsNotWellFormed );
+ }
- pkey.Verify( cypher, decrypt.el_ );
+ {
+ // null private key share
+ BOOST_REQUIRE_THROW( TEPrivateKeyShare( nullptr, 1, num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
+ }
+ {
+ // zero private key share
+ std::string zero_str = "0";
+ BOOST_REQUIRE_THROW( TEPrivateKeyShare( std::make_shared< std::string >( zero_str ), 1,
+ num_signed, num_all ),
+ crypto::ThresholdUtils::ZeroSecretKey );
+ }
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ // zero private key share
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero();
+ BOOST_REQUIRE_THROW( TEPrivateKeyShare( el, 1, num_signed, num_all ),
+ crypto::ThresholdUtils::ZeroSecretKey );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // null private key share
- try {
- TEPrivateKeyShare( nullptr, 1, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ // wrong signer index
+ BOOST_REQUIRE_THROW( TEPrivateKeyShare( libff::alt_bn128_Fr::random_element(),
+ num_all + 1, num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // zero private key share
- try {
- std::string zero_str = "0";
- TEPrivateKeyShare(
- std::make_shared< std::string >( zero_str ), 1, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ // null public key
+ BOOST_REQUIRE_THROW( TEPublicKey( nullptr, num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // zero private key share
- try {
- element_t el;
- element_init_Zr( el, TEDataSingleton::getData().pairing_ );
- element_set0( el );
- encryption::element_wrapper el_wrap( el );
- element_clear( el );
- TEPrivateKeyShare( el_wrap, 1, num_signed, num_all );
+ {
+ // wrong formated public key
+ std::vector< std::string > pkey_str( {"0", "0", "0", "0"} );
+ BOOST_REQUIRE_THROW(
+ TEPublicKey( std::make_shared< std::vector< std::string > >( pkey_str ), num_signed,
+ num_all ),
+ crypto::ThresholdUtils::IsNotWellFormed );
+ }
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ // zero public key
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero();
+ BOOST_REQUIRE_THROW(
+ TEPublicKey pkey( TEPrivateKey( el, num_signed, num_all ), num_signed, num_all ),
+ crypto::ThresholdUtils::IsNotWellFormed );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // null public key
- try {
- TEPublicKey( nullptr, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ // zero public key
+ libff::alt_bn128_G2 el = libff::alt_bn128_G2::zero();
+ BOOST_REQUIRE_THROW( TEPublicKey pkey( el, num_signed, num_all ),
+ crypto::ThresholdUtils::IsNotWellFormed );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // zero public key
- try {
- 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& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false; // zero public key
- try {
- element_t el;
- element_init_Zr( el, TEDataSingleton::getData().pairing_ );
- element_set_si( el, 0 );
- encryption::element_wrapper el_wrap( el );
- element_clear( el );
- TEPublicKey pkey( TEPrivateKey( el_wrap, num_signed, num_all ), num_signed, num_all );
-
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false; // zero public key
- try {
- element_t el;
- element_init_G1( el, TEDataSingleton::getData().pairing_ );
- element_set_str( el, "[0, 0]", 10 );
- encryption::element_wrapper el_wrap( el );
- element_clear( el );
- TEPublicKey pkey( el_wrap, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false; // null message
- try {
- element_t el;
- element_init_G1( el, TEDataSingleton::getData().pairing_ );
- element_random( el );
+ {
+ // null message
+ libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element();
TEPublicKey pkey( el, num_signed, num_all );
- element_clear( el );
- pkey.encrypt( nullptr );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( pkey.encrypt( nullptr ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // message length is not 64
- try {
- element_t el;
- element_init_G1( el, TEDataSingleton::getData().pairing_ );
- element_random( el );
+ {
+ // message length is not 64
+ libff::alt_bn128_G2 el = libff::alt_bn128_G2::random_element();
TEPublicKey pkey( el, num_signed, num_all );
- element_clear( el );
- pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( pkey.encrypt( std::make_shared< std::string >( "tra-la-la" ) ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // null private key
- try {
- TEPrivateKey( nullptr, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ // null private key
+ BOOST_REQUIRE_THROW( TEPrivateKey( nullptr, num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false; // zero private key
- try {
+ {
+ // zero private key
std::string zero_str = "0";
- TEPrivateKey( std::make_shared< std::string >( zero_str ), num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false; // zero private key
- try {
- element_t el;
- element_init_Zr( el, TEDataSingleton::getData().pairing_ );
- element_set0( el );
- encryption::element_wrapper el_wrap( el );
- element_clear( el );
- TEPrivateKey( el_wrap, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false;
- try {
- TEDecryptSet decr_set( num_all + 1, num_signed ); //_requiredSigners > _totalSigners
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false;
- try {
- TEDecryptSet decr_set( num_signed, num_all ); // same indices in decrypt set
-
- element_t el1;
- element_init_G1( el1, TEDataSingleton::getData().pairing_ );
- element_random( el1 );
- std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el1 );
- element_clear( el1 );
-
- element_t el2;
- element_init_G1( el2, TEDataSingleton::getData().pairing_ );
- element_random( el2 );
- std::shared_ptr el_ptr2 = std::make_shared< encryption::element_wrapper >( el2 );
- element_clear( el2 );
+ BOOST_REQUIRE_THROW(
+ TEPrivateKey( std::make_shared< std::string >( zero_str ), num_signed, num_all ),
+ crypto::ThresholdUtils::IsNotWellFormed );
+ }
- decr_set.addDecrypt( 1, el_ptr1 );
- decr_set.addDecrypt( 1, el_ptr2 );
+ {
+ // zero private key
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero();
+ BOOST_REQUIRE_THROW(
+ TEPrivateKey( el, num_signed, num_all ), crypto::ThresholdUtils::IsNotWellFormed );
+ }
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ {
+ //_requiredSigners > _totalSigners
+ BOOST_REQUIRE_THROW( TEDecryptSet decr_set( num_all + 1, num_signed ),
+ crypto::ThresholdUtils::IsNotWellFormed );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
- TEDecryptSet decr_set( num_signed, num_all ); // zero element in decrypt set
+ {
+ // same indices in decrypt set
+ TEDecryptSet decr_set( num_signed, num_all );
- element_t el1;
- element_init_G1( el1, TEDataSingleton::getData().pairing_ );
- element_set_str( el1, "[0, 0]", 10 );
- std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el1 );
- element_clear( el1 );
+ libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element();
+ auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 );
+
+ libff::alt_bn128_G2 el2 = libff::alt_bn128_G2::random_element();
+ auto el_ptr2 = std::make_shared< libff::alt_bn128_G2 >( el2 );
decr_set.addDecrypt( 1, el_ptr1 );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false;
- try {
- TEDecryptSet decr_set( num_signed, num_all ); // null element in decrypt set
- element_t el1;
- element_init_G1( el1, TEDataSingleton::getData().pairing_ );
- element_set_str( el1, "[0, 0]", 10 );
- encryption::element_wrapper el_wrap( el1 );
- std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el_wrap );
- el_ptr1 = nullptr;
- element_clear( el1 );
- decr_set.addDecrypt( 1, el_ptr1 );
+ BOOST_REQUIRE_THROW(
+ decr_set.addDecrypt( 1, el_ptr2 ), crypto::ThresholdUtils::IncorrectInput );
+ }
+
+ {
+ // zero element in decrypt set
+ TEDecryptSet decr_set( num_signed, num_all );
+
+ libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero();
+ auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 );
+
+ BOOST_REQUIRE_THROW(
+ decr_set.addDecrypt( 1, el_ptr1 ), crypto::ThresholdUtils::IsNotWellFormed );
+ }
+
+ {
+ // null element in decrypt set
+ TEDecryptSet decr_set( num_signed, num_all );
+ libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::zero();
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 );
+ el_ptr1 = nullptr;
+ BOOST_REQUIRE_THROW(
+ decr_set.addDecrypt( 1, el_ptr1 ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
- TEDecryptSet decr_set( num_signed, num_all ); // not enough elements in decrypt set
- element_t el1;
- element_init_G1( el1, TEDataSingleton::getData().pairing_ );
- element_random( el1 );
- encryption::element_wrapper el_wrap( el1 );
- element_clear( el1 );
- std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el_wrap );
+ {
+ // not enough elements in decrypt set
+ TEDecryptSet decr_set( num_signed, num_all );
+ libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element();
+
+ auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 );
decr_set.addDecrypt( 1, el_ptr1 );
- element_t U;
- element_init_G1( U, TEDataSingleton::getData().pairing_ );
- element_random( U );
- encryption::element_wrapper U_wrap( U );
- element_clear( U );
+ libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element();
- element_t W;
- element_init_G1( W, TEDataSingleton::getData().pairing_ );
- element_random( W );
- encryption::element_wrapper W_wrap( W );
- element_clear( W );
+ libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element();
- encryption::Ciphertext cypher;
- std::get< 0 >( cypher ) = U_wrap;
+ crypto::Ciphertext cypher;
+ std::get< 0 >( cypher ) = U;
std::get< 1 >( cypher ) =
"Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!";
- std::get< 2 >( cypher ) = W_wrap;
-
- decr_set.merge( cypher );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
- }
- BOOST_REQUIRE( is_exception_caught );
-
- is_exception_caught = false;
- try {
- TEDecryptSet decr_set( 1, 1 ); // cannot combine shares
- element_t el1;
- element_init_G1( el1, TEDataSingleton::getData().pairing_ );
- element_random( el1 );
- std::shared_ptr el_ptr1 = std::make_shared< encryption::element_wrapper >( el1 );
- element_clear( el1 );
+ std::get< 2 >( cypher ) = W;
+
+ BOOST_REQUIRE_THROW(
+ decr_set.merge( cypher ), crypto::ThresholdUtils::IsNotWellFormed );
+ }
+
+ {
+ // cannot combine shares
+ TEDecryptSet decr_set( 1, 1 );
+ libff::alt_bn128_G2 el1 = libff::alt_bn128_G2::random_element();
+ auto el_ptr1 = std::make_shared< libff::alt_bn128_G2 >( el1 );
decr_set.addDecrypt( 1, el_ptr1 );
- element_t U;
- element_init_G1( U, TEDataSingleton::getData().pairing_ );
- element_random( U );
- encryption::element_wrapper U_wrap( U );
- element_clear( U );
+ libff::alt_bn128_G2 U = libff::alt_bn128_G2::random_element();
- element_t W;
- element_init_G1( W, TEDataSingleton::getData().pairing_ );
- element_random( W );
- encryption::element_wrapper W_wrap( W );
- element_clear( W );
+ libff::alt_bn128_G1 W = libff::alt_bn128_G1::random_element();
- encryption::Ciphertext cypher;
- std::get< 0 >( cypher ) = U_wrap;
+ crypto::Ciphertext cypher;
+ std::get< 0 >( cypher ) = U;
std::get< 1 >( cypher ) =
"Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!";
- std::get< 2 >( cypher ) = W_wrap;
+ std::get< 2 >( cypher ) = W;
- decr_set.merge( cypher );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( decr_set.merge( cypher ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
}
}
@@ -900,123 +640,84 @@ BOOST_AUTO_TEST_CASE( ExceptionsDKGWrappersTest ) {
size_t num_all = rand_gen() % 15 + 2;
size_t num_signed = rand_gen() % num_all + 1;
- bool is_exception_caught = false;
- try {
+ {
// zero share
DKGTEWrapper dkg_te( num_signed, num_all );
- element_t el1;
- element_init_Zr( el1, TEDataSingleton::getData().pairing_ );
- element_set0( el1 );
- encryption::element_wrapper el_wrap( el1 );
- element_clear( el1 );
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::zero();
- dkg_te.VerifyDKGShare( 1, el_wrap, dkg_te.createDKGPublicShares() );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( dkg_te.VerifyDKGShare( 1, el, dkg_te.createDKGPublicShares() ),
+ crypto::ThresholdUtils::ZeroSecretKey );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
// null verification vector
DKGTEWrapper dkg_te( num_signed, num_all );
- element_t el1;
- element_init_Zr( el1, TEDataSingleton::getData().pairing_ );
- element_random( el1 );
- encryption::element_wrapper el_wrap( el1 );
- element_clear( el1 );
- dkg_te.VerifyDKGShare( 1, el_wrap, nullptr );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element();
+ BOOST_REQUIRE_THROW(
+ dkg_te.VerifyDKGShare( 1, el, nullptr ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- element_t el1;
- element_init_Zr( el1, TEDataSingleton::getData().pairing_ );
- element_random( el1 );
- encryption::element_wrapper el_wrap( el1 );
- element_clear( el1 );
+ libff::alt_bn128_Fr el = libff::alt_bn128_Fr::random_element();
- std::vector< encryption::element_wrapper > pub_shares = *dkg_te.createDKGPublicShares();
+ std::vector< libff::alt_bn128_G2 > pub_shares = *dkg_te.createDKGPublicShares();
pub_shares.erase( pub_shares.begin() );
- dkg_te.VerifyDKGShare( 1, el_wrap,
- std::make_shared< std::vector< encryption::element_wrapper > >( pub_shares ) );
-
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW(
+ dkg_te.VerifyDKGShare(
+ 1, el, std::make_shared< std::vector< libff::alt_bn128_G2 > >( pub_shares ) ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- std::shared_ptr< std::vector< encryption::element_wrapper > > shares =
+ std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares =
dkg_te.createDKGSecretShares();
shares = nullptr;
- dkg_te.setDKGSecret( shares );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW(
+ dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- std::shared_ptr< std::vector< encryption::element_wrapper > > shares =
+ std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares =
dkg_te.createDKGSecretShares();
shares->erase( shares->begin() + shares->size() - 2 );
shares->shrink_to_fit();
- dkg_te.setDKGSecret( shares );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW(
+ dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- dkg_te.CreateTEPrivateKeyShare( 1, nullptr );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW(
+ dkg_te.CreateTEPrivateKeyShare( 1, nullptr ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- auto wrong_size_vector = std::make_shared< std::vector< encryption::element_wrapper > >();
+ auto wrong_size_vector = std::make_shared< std::vector< libff::alt_bn128_Fr > >();
wrong_size_vector->resize( num_signed - 1 );
- dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( dkg_te.CreateTEPrivateKeyShare( 1, wrong_size_vector ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- std::shared_ptr< std::vector< encryption::element_wrapper > > shares;
- dkg_te.setDKGSecret( shares );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ std::shared_ptr< std::vector< libff::alt_bn128_Fr > > shares;
+ BOOST_REQUIRE_THROW(
+ dkg_te.setDKGSecret( shares ), crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
- is_exception_caught = false;
- try {
+ {
DKGTEWrapper dkg_te( num_signed, num_all );
- dkg_te.CreateTEPublicKey( nullptr, num_signed, num_all );
- } catch ( std::runtime_error& ) {
- is_exception_caught = true;
+ BOOST_REQUIRE_THROW( dkg_te.CreateTEPublicKey( nullptr, num_signed, num_all ),
+ crypto::ThresholdUtils::IncorrectInput );
}
- BOOST_REQUIRE( is_exception_caught );
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/test_bls.cpp b/test/test_bls.cpp
index e3695d4d..b713ef83 100644
--- a/test/test_bls.cpp
+++ b/test/test_bls.cpp
@@ -32,7 +32,7 @@
#include
#include
#include
-#include
+#include
#include