From bb3f106f54e3a33ef245ecc571ca5a62f13ed4ed Mon Sep 17 00:00:00 2001 From: Oleh Date: Mon, 19 Dec 2022 15:45:15 +0200 Subject: [PATCH 01/16] add new api for aggregated signatures --- bls/bls.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++---- bls/bls.h | 6 ++++++ dkg/dkg.cpp | 7 +------ dkg/dkg.h | 2 -- tools/utils.cpp | 5 ----- tools/utils.h | 8 +++++++- 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index b9fcf230..9c433053 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -43,9 +43,8 @@ Bls::Bls( const size_t t, const size_t n ) : t_( t ), n_( n ) { } std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeyGeneration() { - // generate secret and public KeysRecover - libff::alt_bn128_Fr secret_key = - libff::alt_bn128_Fr::random_element(); // secret key generation + // generate sample secret and public keys + libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); // secret key generation while ( secret_key == libff::alt_bn128_Fr::zero() ) { secret_key = libff::alt_bn128_Fr::random_element(); @@ -162,6 +161,50 @@ libff::alt_bn128_G1 Bls::Signing( return sign; } +libff::alt_bn128_G1 Bls::CoreSignAggregated( const std::string& message, const libff::alt_bn128_Fr secret_key ) { + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); + + uint64_t bin_len; + if ( !ThresholdUtils::hex2carray( message.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + throw std::runtime_error( "Invalid hash" ); + } + + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); + + return secret_key * hash; +} + +libff::alt_bn128_G1 Bls::Aggregate( const std::vector< libff::alt_bn128_G1 >& signatures ) { + libff::alt_bn128_G1 res = libff::alt_bn128_G1::zero(); + + for (const auto& signature: signatures) { + if ( !ThresholdUtils::ValidateKey( signature ) ) { + throw ThresholdUtils::IsNotWellFormed("One of the signatures to be aggregated is malicious"); + } + + res = res + signature; + } + + return res; +} + +bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, const libff::alt_bn128_G1& signature ) { + if ( !ThresholdUtils::ValidateKey( public_key ) || !ThresholdUtils::ValidateKey( signature ) ) { + throw ThresholdUtils::IsNotWellFormed("Either signature or public key is malicious"); + } + + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); + + uint64_t bin_len; + if ( !ThresholdUtils::hex2carray( message.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + throw std::runtime_error( "Invalid hash" ); + } + + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); + + return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == libff::alt_bn128_ate_reduced_pairing( signature, libff::alt_bn128_G2::one() ); +} + bool Bls::Verification( const std::string& to_be_hashed, const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key ) { // verifies that a given signature corresponds to given public key @@ -239,7 +282,7 @@ bool Bls::AggregatedVerification( throw ThresholdUtils::IsNotWellFormed( "Error, public key is invalid" ); } - if ( !ThresholdUtils::isG2( public_key ) ) { + if ( !ThresholdUtils::ValidateKey( public_key ) ) { throw ThresholdUtils::IsNotWellFormed( "Error, public key is not member of G2" ); } diff --git a/bls/bls.h b/bls/bls.h index c9c1cbe7..3030fea8 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -56,6 +56,12 @@ class Bls { static libff::alt_bn128_G1 Signing( const libff::alt_bn128_G1 hash, const libff::alt_bn128_Fr secret_key ); + + static libff::alt_bn128_G1 CoreSignAggregated( const std::string& message, const libff::alt_bn128_Fr secret_key ); + + static libff::alt_bn128_G1 Aggregate( const std::vector< libff::alt_bn128_G1 >& signatures ); + + static bool CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, const libff::alt_bn128_G1& signature ); static bool Verification( const std::string& to_be_hashed, const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key ); diff --git a/dkg/dkg.cpp b/dkg/dkg.cpp index 3945df2b..fc2eb37a 100644 --- a/dkg/dkg.cpp +++ b/dkg/dkg.cpp @@ -110,7 +110,7 @@ bool Dkg::Verification( size_t idx, libff::alt_bn128_Fr share, // idx-th node verifies that share corresponds to the verification vector libff::alt_bn128_G2 value = libff::alt_bn128_G2::zero(); for ( size_t i = 0; i < this->t_; ++i ) { - if ( !this->isG2( verification_vector[i] ) ) { + if ( !ThresholdUtils::ValidateKey( verification_vector[i] ) ) { return false; } value = value + power( libff::alt_bn128_Fr( idx + 1 ), i ) * verification_vector[i]; @@ -134,9 +134,4 @@ size_t Dkg::GetN() const { return this->n_; } -bool Dkg::isG2( const libff::alt_bn128_G2& point ) { - return point.is_well_formed() && - libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); -} - } // namespace libBLS diff --git a/dkg/dkg.h b/dkg/dkg.h index c3dfac23..43de8ee8 100644 --- a/dkg/dkg.h +++ b/dkg/dkg.h @@ -58,8 +58,6 @@ class Dkg { size_t GetN() const; - static bool isG2( const libff::alt_bn128_G2& point ); - private: const size_t t_ = 0; diff --git a/tools/utils.cpp b/tools/utils.cpp index 6547ef50..e8cb554c 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -298,11 +298,6 @@ bool ThresholdUtils::checkHex( const std::string& hex ) { return true; } -bool ThresholdUtils::isG2( const libff::alt_bn128_G2& point ) { - return point.is_well_formed() && - libff::alt_bn128_G2::order() * point == libff::alt_bn128_G2::zero(); -} - std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > ThresholdUtils::ParseHint( const std::string& _hint ) { auto position = _hint.find( ":" ); diff --git a/tools/utils.h b/tools/utils.h index 69af4ee2..f7ae2f66 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -119,7 +119,8 @@ class ThresholdUtils { static bool checkHex( const std::string& hex ); - static bool isG2( const libff::alt_bn128_G2& point ); + template + static bool ValidateKey( const T& point ); }; template < class T > @@ -139,6 +140,11 @@ std::string ThresholdUtils::fieldElementToString( const T& field_elem, int base return output; } +template < class T > +bool ThresholdUtils::ValidateKey( const T& point ) { + return point.is_well_formed() && T::order() * point == T::zero(); +} + } // namespace libBLS #endif // LIBBLS_UTILS_H From fd388830634e86608ae56b5563dc59fd0733b908 Mon Sep 17 00:00:00 2001 From: Oleh Date: Wed, 21 Dec 2022 13:57:36 +0200 Subject: [PATCH 02/16] add proof of possession approach for aggregated signatures --- bls/bls.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ bls/bls.h | 8 ++++++++ 2 files changed, 49 insertions(+) diff --git a/bls/bls.cpp b/bls/bls.cpp index 9c433053..f8cddd57 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -140,6 +140,21 @@ libff::alt_bn128_G1 Bls::HashBytes( return hash; } +libff::alt_bn128_G1 Bls::HashPublicKeyToG1( const libff::alt_bn128_G2& elem ) { + auto serialized_elem_vector = ThresholdUtils::G2ToString( elem, 16 ); + + std::string serialized_elem = std::accumulate( serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); + + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); + + uint64_t bin_len; + if ( !ThresholdUtils::hex2carray( serialized_elem.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + throw std::runtime_error( "Invalid hash" ); + } + + return ThresholdUtils::HashtoG1( hash_bytes_arr ); +} + libff::alt_bn128_G1 Bls::Signing( const libff::alt_bn128_G1 hash, const libff::alt_bn128_Fr secret_key ) { // sign a message with its hash and secret key @@ -205,6 +220,12 @@ bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == libff::alt_bn128_ate_reduced_pairing( signature, libff::alt_bn128_G2::one() ); } +bool Bls::FastAggregateVerify( const std::vector< libff::alt_bn128_G2 >& public_keys, const std::string& message, const libff::alt_bn128_G1& signature ) { + libff::alt_bn128_G2 sum = std::accumulate( public_keys.begin(), public_keys.end(), libff::alt_bn128_G2::zero() ); + + return CoreVerify( sum, message, signature ); +} + bool Bls::Verification( const std::string& to_be_hashed, const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key ) { // verifies that a given signature corresponds to given public key @@ -341,4 +362,24 @@ libff::alt_bn128_G1 Bls::SignatureRecover( const std::vector< libff::alt_bn128_G return sign; // first element is hash of a receiving message } +libff::alt_bn128_G1 Bls::PopProve( const libff::alt_bn128_Fr& secret_key ) { + libff::alt_bn128_G2 public_key = secret_key * libff::alt_bn128_G2::one(); + + libff::alt_bn128_G1 hash = HashPublicKeyToG1( public_key ); + + libff::alt_bn128_G1 ret = secret_key * hash; + + return ret; +} + +bool Bls::PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ) { + if ( !ThresholdUtils::ValidateKey( proof ) || !ThresholdUtils::ValidateKey( public_key ) ) { + throw ThresholdUtils::IsNotWellFormed( "incorrect input data to verify proof of possession" ); + } + + libff::alt_bn128_G1 hash = HashPublicKeyToG1( public_key ); + + return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == libff::alt_bn128_ate_reduced_pairing( proof, libff::alt_bn128_G2::one() ); +} + } // namespace libBLS diff --git a/bls/bls.h b/bls/bls.h index 3030fea8..c1867b61 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -54,6 +54,8 @@ class Bls { static std::pair< libff::alt_bn128_G1, std::string > HashtoG1withHint( std::shared_ptr< std::array< uint8_t, 32 > > ); + static libff::alt_bn128_G1 HashPublicKeyToG1( const libff::alt_bn128_G2& elem ); + static libff::alt_bn128_G1 Signing( const libff::alt_bn128_G1 hash, const libff::alt_bn128_Fr secret_key ); @@ -63,6 +65,8 @@ class Bls { static bool CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, const libff::alt_bn128_G1& signature ); + static bool FastAggregateVerify( const std::vector< libff::alt_bn128_G2 >& public_keys, const std::string& message, const libff::alt_bn128_G1& signature ); + static bool Verification( const std::string& to_be_hashed, const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key ); @@ -80,6 +84,10 @@ class Bls { libff::alt_bn128_G1 SignatureRecover( const std::vector< libff::alt_bn128_G1 >& shares, const std::vector< libff::alt_bn128_Fr >& coeffs ); + static libff::alt_bn128_G1 PopProve( const libff::alt_bn128_Fr& secret_key ); + + static bool PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ); + private: const size_t t_ = 0; From 94abbcccaed40c6f358f2d2299fbd429947dd7ab Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 21 Dec 2022 14:38:26 +0200 Subject: [PATCH 03/16] format code --- bls/BLSPrivateKeyShare.cpp | 7 +- bls/BLSPublicKey.cpp | 6 +- bls/bls.cpp | 37 ++- bls/bls.h | 14 +- test/test_TE_wrappers.cpp | 8 +- test/test_bls.cpp | 8 +- test/unit_tests_bls.cpp | 18 +- test/unit_tests_dkg.cpp | 4 +- third_party/cryptlite/base64.cpp | 4 +- third_party/cryptlite/sha1.h | 10 +- third_party/cryptlite/sha256.h | 12 +- third_party/json.hpp | 298 +++++++++--------- threshold_encryption/threshold_encryption.cpp | 6 +- tools/utils.cpp | 2 +- tools/utils.h | 2 +- 15 files changed, 224 insertions(+), 212 deletions(-) diff --git a/bls/BLSPrivateKeyShare.cpp b/bls/BLSPrivateKeyShare.cpp index ee4b6297..4ea7a85a 100644 --- a/bls/BLSPrivateKeyShare.cpp +++ b/bls/BLSPrivateKeyShare.cpp @@ -128,11 +128,8 @@ BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSi std::vector< libff::alt_bn128_Fr > skeys = dkg_obj.SecretKeyContribution( pol ); libff::alt_bn128_Fr common_skey = pol.at( 0 ); - std::shared_ptr< BLSPublicKey > pkey_ptr = std::make_shared< BLSPublicKey >( - common_skey, - _requiredSigners, - _totalSigners - ); + std::shared_ptr< BLSPublicKey > pkey_ptr = + std::make_shared< BLSPublicKey >( common_skey, _requiredSigners, _totalSigners ); for ( size_t i = 0; i < _totalSigners; ++i ) { std::string key_str = libBLS::ThresholdUtils::fieldElementToString( skeys.at( i ) ); diff --git a/bls/BLSPublicKey.cpp b/bls/BLSPublicKey.cpp index 1c078de5..9878e30a 100644 --- a/bls/BLSPublicKey.cpp +++ b/bls/BLSPublicKey.cpp @@ -50,8 +50,7 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > } } -BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey, size_t t, size_t n ) - : t( t ), n( n ) { +BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey, size_t t, size_t n ) : t( t ), n( n ) { libBLS::ThresholdUtils::initCurve(); // do not check signers for compatibility @@ -63,8 +62,7 @@ BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey, size_t t, size_t n } } -BLSPublicKey::BLSPublicKey(const libff::alt_bn128_Fr& skey, size_t t, size_t n ) - : t( t ), n( n ) { +BLSPublicKey::BLSPublicKey( const libff::alt_bn128_Fr& skey, size_t t, size_t n ) : t( t ), n( n ) { // do not check signers for compatibility // libBLS::ThresholdUtils::checkSigners( t, n ); diff --git a/bls/bls.cpp b/bls/bls.cpp index f8cddd57..30c5fb57 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -44,7 +44,8 @@ Bls::Bls( const size_t t, const size_t n ) : t_( t ), n_( n ) { std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeyGeneration() { // generate sample secret and public keys - libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); // secret key generation + libff::alt_bn128_Fr secret_key = + libff::alt_bn128_Fr::random_element(); // secret key generation while ( secret_key == libff::alt_bn128_Fr::zero() ) { secret_key = libff::alt_bn128_Fr::random_element(); @@ -143,12 +144,14 @@ libff::alt_bn128_G1 Bls::HashBytes( libff::alt_bn128_G1 Bls::HashPublicKeyToG1( const libff::alt_bn128_G2& elem ) { auto serialized_elem_vector = ThresholdUtils::G2ToString( elem, 16 ); - std::string serialized_elem = std::accumulate( serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); + std::string serialized_elem = std::accumulate( + serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); uint64_t bin_len; - if ( !ThresholdUtils::hex2carray( serialized_elem.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + if ( !ThresholdUtils::hex2carray( + serialized_elem.c_str(), &bin_len, hash_bytes_arr->data() ) ) { throw std::runtime_error( "Invalid hash" ); } @@ -176,7 +179,8 @@ libff::alt_bn128_G1 Bls::Signing( return sign; } -libff::alt_bn128_G1 Bls::CoreSignAggregated( const std::string& message, const libff::alt_bn128_Fr secret_key ) { +libff::alt_bn128_G1 Bls::CoreSignAggregated( + const std::string& message, const libff::alt_bn128_Fr secret_key ) { auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); uint64_t bin_len; @@ -192,9 +196,10 @@ libff::alt_bn128_G1 Bls::CoreSignAggregated( const std::string& message, const l libff::alt_bn128_G1 Bls::Aggregate( const std::vector< libff::alt_bn128_G1 >& signatures ) { libff::alt_bn128_G1 res = libff::alt_bn128_G1::zero(); - for (const auto& signature: signatures) { + for ( const auto& signature : signatures ) { if ( !ThresholdUtils::ValidateKey( signature ) ) { - throw ThresholdUtils::IsNotWellFormed("One of the signatures to be aggregated is malicious"); + throw ThresholdUtils::IsNotWellFormed( + "One of the signatures to be aggregated is malicious" ); } res = res + signature; @@ -203,9 +208,10 @@ libff::alt_bn128_G1 Bls::Aggregate( const std::vector< libff::alt_bn128_G1 >& si return res; } -bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, const libff::alt_bn128_G1& signature ) { +bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, + const libff::alt_bn128_G1& signature ) { if ( !ThresholdUtils::ValidateKey( public_key ) || !ThresholdUtils::ValidateKey( signature ) ) { - throw ThresholdUtils::IsNotWellFormed("Either signature or public key is malicious"); + throw ThresholdUtils::IsNotWellFormed( "Either signature or public key is malicious" ); } auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); @@ -217,11 +223,14 @@ bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); - return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == libff::alt_bn128_ate_reduced_pairing( signature, libff::alt_bn128_G2::one() ); + return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == + libff::alt_bn128_ate_reduced_pairing( signature, libff::alt_bn128_G2::one() ); } -bool Bls::FastAggregateVerify( const std::vector< libff::alt_bn128_G2 >& public_keys, const std::string& message, const libff::alt_bn128_G1& signature ) { - libff::alt_bn128_G2 sum = std::accumulate( public_keys.begin(), public_keys.end(), libff::alt_bn128_G2::zero() ); +bool Bls::FastAggregateVerify( const std::vector< libff::alt_bn128_G2 >& public_keys, + const std::string& message, const libff::alt_bn128_G1& signature ) { + libff::alt_bn128_G2 sum = + std::accumulate( public_keys.begin(), public_keys.end(), libff::alt_bn128_G2::zero() ); return CoreVerify( sum, message, signature ); } @@ -374,12 +383,14 @@ libff::alt_bn128_G1 Bls::PopProve( const libff::alt_bn128_Fr& secret_key ) { bool Bls::PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ) { if ( !ThresholdUtils::ValidateKey( proof ) || !ThresholdUtils::ValidateKey( public_key ) ) { - throw ThresholdUtils::IsNotWellFormed( "incorrect input data to verify proof of possession" ); + throw ThresholdUtils::IsNotWellFormed( + "incorrect input data to verify proof of possession" ); } libff::alt_bn128_G1 hash = HashPublicKeyToG1( public_key ); - return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == libff::alt_bn128_ate_reduced_pairing( proof, libff::alt_bn128_G2::one() ); + return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == + libff::alt_bn128_ate_reduced_pairing( proof, libff::alt_bn128_G2::one() ); } } // namespace libBLS diff --git a/bls/bls.h b/bls/bls.h index c1867b61..527c271d 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -58,14 +58,17 @@ class Bls { static libff::alt_bn128_G1 Signing( const libff::alt_bn128_G1 hash, const libff::alt_bn128_Fr secret_key ); - - static libff::alt_bn128_G1 CoreSignAggregated( const std::string& message, const libff::alt_bn128_Fr secret_key ); + + static libff::alt_bn128_G1 CoreSignAggregated( + const std::string& message, const libff::alt_bn128_Fr secret_key ); static libff::alt_bn128_G1 Aggregate( const std::vector< libff::alt_bn128_G1 >& signatures ); - static bool CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, const libff::alt_bn128_G1& signature ); + static bool CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& message, + const libff::alt_bn128_G1& signature ); - static bool FastAggregateVerify( const std::vector< libff::alt_bn128_G2 >& public_keys, const std::string& message, const libff::alt_bn128_G1& signature ); + static bool FastAggregateVerify( const std::vector< libff::alt_bn128_G2 >& public_keys, + const std::string& message, const libff::alt_bn128_G1& signature ); static bool Verification( const std::string& to_be_hashed, const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key ); @@ -86,7 +89,8 @@ class Bls { static libff::alt_bn128_G1 PopProve( const libff::alt_bn128_Fr& secret_key ); - static bool PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ); + static bool PopVerify( + const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ); private: const size_t t_ = 0; diff --git a/test/test_TE_wrappers.cpp b/test/test_TE_wrappers.cpp index da3c9401..fea433f0 100644 --- a/test/test_TE_wrappers.cpp +++ b/test/test_TE_wrappers.cpp @@ -346,7 +346,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // 1 coord of public key share is not a number - std::vector< std::string > pkey_str( {"123", "abc"} ); + std::vector< std::string > pkey_str( { "123", "abc" } ); BOOST_REQUIRE_THROW( TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, num_signed, num_all ), @@ -355,7 +355,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // wrong formated public key share - std::vector< std::string > pkey_str( {"0", "0", "0", "0"} ); + std::vector< std::string > pkey_str( { "0", "0", "0", "0" } ); BOOST_REQUIRE_THROW( TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, num_signed, num_all ), @@ -364,7 +364,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // one component public key share - std::vector< std::string > pkey_str( {"1232450"} ); + std::vector< std::string > pkey_str( { "1232450" } ); BOOST_REQUIRE_THROW( TEPublicKeyShare( std::make_shared< std::vector< std::string > >( pkey_str ), 1, num_signed, num_all ), @@ -465,7 +465,7 @@ BOOST_AUTO_TEST_CASE( ExceptionsTest ) { { // wrong formated public key - std::vector< std::string > pkey_str( {"0", "0", "0", "0"} ); + 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 ), diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 6c43e60f..ce2bd986 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -850,7 +850,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } { - std::vector< std::string > coords = {"0", "0", "0", "0"}; + std::vector< std::string > coords = { "0", "0", "0", "0" }; auto vector_ptr_str = std::make_shared< std::vector< std::string > >( coords ); BOOST_REQUIRE_THROW( BLSPublicKey pkey( vector_ptr_str ), libBLS::ThresholdUtils::IsNotWellFormed ); @@ -960,7 +960,7 @@ BOOST_AUTO_TEST_CASE( Exceptions ) { } { - std::vector< std::string > coords = {"0", "0", "0", "0"}; + std::vector< std::string > coords = { "0", "0", "0", "0" }; auto vector_str_ptr = std::make_shared< std::vector< std::string > >( coords ); BOOST_REQUIRE_THROW( BLSPublicKeyShare pkey( vector_str_ptr, num_signed, num_all ), libBLS::ThresholdUtils::IsNotWellFormed ); @@ -1233,7 +1233,7 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { { DKGBLSWrapper dkg_wrap( num_signed, num_all ); - std::vector< libff::alt_bn128_G2 > vect = {libff::alt_bn128_G2::random_element()}; + std::vector< libff::alt_bn128_G2 > vect = { libff::alt_bn128_G2::random_element() }; BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::zero(), std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ), libBLS::ThresholdUtils::ZeroSecretKey ); @@ -1254,7 +1254,7 @@ BOOST_AUTO_TEST_CASE( DKGWrappersExceptions ) { { DKGBLSWrapper dkg_wrap( num_signed + 1, num_all + 1 ); - std::vector< libff::alt_bn128_G2 > vect = {libff::alt_bn128_G2::random_element()}; + std::vector< libff::alt_bn128_G2 > vect = { libff::alt_bn128_G2::random_element() }; BOOST_REQUIRE_THROW( dkg_wrap.VerifyDKGShare( 1, libff::alt_bn128_Fr::random_element(), std::make_shared< std::vector< libff::alt_bn128_G2 > >( vect ) ), libBLS::ThresholdUtils::IncorrectInput ); diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 0703652b..6fce90d8 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE( SimillarHashes ) { libBLS::Bls obj = libBLS::Bls( 1, 1 ); - const char message[5] = {104, 101, 108, 108, 111}; + const char message[5] = { 104, 101, 108, 108, 111 }; BOOST_REQUIRE( obj.HashBytes( message, 5 ) == obj.Hashing( "hello" ) ); @@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { libff::alt_bn128_Fr snd_secret = libff::alt_bn128_Fr( "1242918195122561069654878094438043001503525111785440814423171735067409748785" ); - std::vector< libff::alt_bn128_Fr > secret_keys = {fst_secret, snd_secret}; + std::vector< libff::alt_bn128_Fr > secret_keys = { fst_secret, snd_secret }; // correct public key for this pair of secret keys libff::alt_bn128_Fq first_coord_x = libff::alt_bn128_Fq( @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { BOOST_CHECK( hash.is_well_formed() ); // hash belongs to group G1 - std::vector< size_t > testing_nodes = {1, 2}; + std::vector< size_t > testing_nodes = { 1, 2 }; std::vector< libff::alt_bn128_Fr > lagrange_coeffs = libBLS::ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); @@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { libff::alt_bn128_G1 fst_signature = obj.Signing( hash, fst_secret ); libff::alt_bn128_G1 snd_signature = obj.Signing( hash, snd_secret ); - std::vector< libff::alt_bn128_G1 > single_signatures = {fst_signature, snd_signature}; + std::vector< libff::alt_bn128_G1 > single_signatures = { fst_signature, snd_signature }; libff::alt_bn128_G1 common_signature = obj.SignatureRecover( single_signatures, lagrange_coeffs ); @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { libff::alt_bn128_Fr snd_secret = libff::alt_bn128_Fr( "1242918195122561069654878094438043001503525111785440814423171735067409748785" ); - std::vector< libff::alt_bn128_Fr > secret_keys = {fst_secret, snd_secret}; + std::vector< libff::alt_bn128_Fr > secret_keys = { fst_secret, snd_secret }; // correct public key for secret keys from previous test libff::alt_bn128_Fq first_coord_x = libff::alt_bn128_Fq( @@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { BOOST_CHECK( hash.is_well_formed() ); // hash belongs to group G1 - std::vector< size_t > testing_nodes = {1, 2}; + std::vector< size_t > testing_nodes = { 1, 2 }; std::vector< libff::alt_bn128_Fr > lagrange_coeffs = libBLS::ThresholdUtils::LagrangeCoeffs( testing_nodes, 2 ); @@ -198,7 +198,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { libff::alt_bn128_G1 fst_signature = obj.Signing( hash, fst_secret ); libff::alt_bn128_G1 snd_signature = obj.Signing( hash, snd_secret ); - std::vector< libff::alt_bn128_G1 > single_signatures = {fst_signature, snd_signature}; + std::vector< libff::alt_bn128_G1 > single_signatures = { fst_signature, snd_signature }; libff::alt_bn128_G1 common_signature = obj.SignatureRecover( single_signatures, lagrange_coeffs ); @@ -513,13 +513,13 @@ BOOST_AUTO_TEST_CASE( SignVerification ) { BOOST_REQUIRE_THROW( obj_2_2.SignatureRecover( sig_shares, coeffs ), libBLS::ThresholdUtils::IsNotWellFormed ); - std::vector< size_t > idx = {1}; + std::vector< size_t > idx = { 1 }; BOOST_REQUIRE_THROW( libBLS::ThresholdUtils::LagrangeCoeffs( idx, num_signed ), libBLS::ThresholdUtils::IncorrectInput ); idx.clear(); - idx = {1, 1}; + idx = { 1, 1 }; BOOST_REQUIRE_THROW( libBLS::ThresholdUtils::LagrangeCoeffs( idx, 2 ), libBLS::ThresholdUtils::IncorrectInput ); diff --git a/test/unit_tests_dkg.cpp b/test/unit_tests_dkg.cpp index 5137265d..968af06f 100644 --- a/test/unit_tests_dkg.cpp +++ b/test/unit_tests_dkg.cpp @@ -49,7 +49,7 @@ BOOST_AUTO_TEST_SUITE( DkgAlgorithm ) BOOST_AUTO_TEST_CASE( PolynomialValue ) { libBLS::Dkg obj = libBLS::Dkg( 3, 4 ); std::vector< libff::alt_bn128_Fr > polynomial = { - libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" )}; + libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ) }; libff::alt_bn128_Fr value = obj.PolynomialValue( polynomial, 5 ); @@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE( PolynomialValue ) { polynomial.clear(); polynomial = { - libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" )}; + libff::alt_bn128_Fr( "0" ), libff::alt_bn128_Fr( "1" ), libff::alt_bn128_Fr( "0" ) }; BOOST_REQUIRE_THROW( value = obj.PolynomialValue( polynomial, 5 ), std::logic_error ); } diff --git a/third_party/cryptlite/base64.cpp b/third_party/cryptlite/base64.cpp index bf2c9057..3d740226 100644 --- a/third_party/cryptlite/base64.cpp +++ b/third_party/cryptlite/base64.cpp @@ -3,10 +3,10 @@ namespace cryptlite { const char base64::enctable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -const char base64::dectable[128] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, +const char base64::dectable[128] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, - 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1}; + 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 }; } // namespace cryptlite diff --git a/third_party/cryptlite/sha1.h b/third_party/cryptlite/sha1.h index f25562a7..7b42e8ce 100644 --- a/third_party/cryptlite/sha1.h +++ b/third_party/cryptlite/sha1.h @@ -125,14 +125,14 @@ class sha1 { } void final_bits( const boost::uint8_t message_bits, unsigned int length ) { - boost::uint8_t masks[8] = {/* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, + boost::uint8_t masks[8] = { /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0, /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8, - /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE}; - boost::uint8_t markbit[8] = {/* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, + /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE }; + boost::uint8_t markbit[8] = { /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10, /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04, - /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01}; + /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01 }; if ( !length ) return; @@ -222,7 +222,7 @@ class sha1 { } void process_message_block() { - const boost::uint32_t K[4] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6}; + const boost::uint32_t K[4] = { 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 }; boost::uint32_t temp; boost::uint32_t W[80]; boost::uint32_t A, B, C, D, E; diff --git a/third_party/cryptlite/sha256.h b/third_party/cryptlite/sha256.h index 38ce20d6..8298a52c 100644 --- a/third_party/cryptlite/sha256.h +++ b/third_party/cryptlite/sha256.h @@ -140,15 +140,15 @@ class sha256 { } void final_bits( const boost::uint8_t message_bits, unsigned int length ) { - boost::uint8_t masks[8] = {/* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, + boost::uint8_t masks[8] = { /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0, /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8, - /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE}; + /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE }; - boost::uint8_t markbit[8] = {/* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, + boost::uint8_t markbit[8] = { /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10, /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04, - /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01}; + /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01 }; if ( !length ) return; @@ -250,7 +250,7 @@ class sha256 { } void process_message_block() { - static const boost::uint32_t K[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + static const boost::uint32_t K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, @@ -259,7 +259,7 @@ class sha256 { 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; boost::uint32_t temp1, temp2; boost::uint32_t W[64]; boost::uint32_t A, B, C, D, E, F, G, H; diff --git a/third_party/json.hpp b/third_party/json.hpp index 1a5da0ea..9a372bd7 100644 --- a/third_party/json.hpp +++ b/third_party/json.hpp @@ -1151,10 +1151,10 @@ Returns an ordering that is similar to Python: @since version 1.0.0 */ inline bool operator<( const value_t lhs, const value_t rhs ) noexcept { - static constexpr std::array< std::uint8_t, 8 > order = {{ + static constexpr std::array< std::uint8_t, 8 > order = { { 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */ - }}; + } }; const auto l_index = static_cast< std::size_t >( lhs ); const auto r_index = static_cast< std::size_t >( rhs ); @@ -1432,7 +1432,7 @@ void from_json( const BasicJsonType& j, ArithmeticType& val ) { template < typename BasicJsonType, typename A1, typename A2 > void from_json( const BasicJsonType& j, std::pair< A1, A2 >& p ) { - p = {j.at( 0 ).template get< A1 >(), j.at( 1 ).template get< A2 >()}; + p = { j.at( 0 ).template get< A1 >(), j.at( 1 ).template get< A2 >() }; } template < typename BasicJsonType, typename Tuple, std::size_t... Idx > @@ -1890,7 +1890,7 @@ void to_json( BasicJsonType& j, const T ( &arr )[N] ) { template < typename BasicJsonType, typename... Args > void to_json( BasicJsonType& j, const std::pair< Args... >& p ) { - j = {p.first, p.second}; + j = { p.first, p.second }; } // for https://github.com/nlohmann/json/pull/1134 @@ -1899,12 +1899,12 @@ template < typename BasicJsonType, typename T, iteration_proxy_internal >::value, int > = 0 > void to_json( BasicJsonType& j, const T& b ) { - j = {{b.key(), b.value()}}; + j = { { b.key(), b.value() } }; } template < typename BasicJsonType, typename Tuple, std::size_t... Idx > void to_json_tuple_impl( BasicJsonType& j, const Tuple& t, index_sequence< Idx... > /*unused*/ ) { - j = {std::get< Idx >( t )...}; + j = { std::get< Idx >( t )... }; } template < typename BasicJsonType, typename... Args > @@ -2163,7 +2163,7 @@ class wide_string_input_adapter : public input_adapter_protocol { std::size_t current_wchar = 0; /// a buffer for UTF-8 bytes - std::array< std::char_traits< char >::int_type, 4 > utf8_bytes = {{0, 0, 0, 0}}; + std::array< std::char_traits< char >::int_type, 4 > utf8_bytes = { { 0, 0, 0, 0 } }; /// index to the utf8_codes array for the next valid byte std::size_t utf8_bytes_index = 0; @@ -2228,8 +2228,7 @@ class input_adapter { [&first]( std::pair< bool, int > res, decltype( *first ) val ) { res.first &= ( val == *( std::next( std::addressof( *first ), res.second++ ) ) ); return res; - } ) - .first; + } ).first; assert( is_contiguous ); #endif @@ -2418,7 +2417,7 @@ class lexer { assert( current == 'u' ); int codepoint = 0; - const auto factors = {12, 8, 4, 0}; + const auto factors = { 12, 8, 4, 0 }; for ( const auto factor : factors ) { get(); @@ -2960,7 +2959,7 @@ class lexer { case 0xDD: case 0xDE: case 0xDF: { - if ( JSON_UNLIKELY( not next_byte_in_range( {0x80, 0xBF} ) ) ) { + if ( JSON_UNLIKELY( not next_byte_in_range( { 0x80, 0xBF } ) ) ) { return token_type::parse_error; } break; @@ -2968,7 +2967,7 @@ class lexer { // U+0800..U+0FFF: bytes E0 A0..BF 80..BF case 0xE0: { - if ( JSON_UNLIKELY( not( next_byte_in_range( {0xA0, 0xBF, 0x80, 0xBF} ) ) ) ) { + if ( JSON_UNLIKELY( not( next_byte_in_range( { 0xA0, 0xBF, 0x80, 0xBF } ) ) ) ) { return token_type::parse_error; } break; @@ -2990,7 +2989,7 @@ class lexer { case 0xEC: case 0xEE: case 0xEF: { - if ( JSON_UNLIKELY( not( next_byte_in_range( {0x80, 0xBF, 0x80, 0xBF} ) ) ) ) { + if ( JSON_UNLIKELY( not( next_byte_in_range( { 0x80, 0xBF, 0x80, 0xBF } ) ) ) ) { return token_type::parse_error; } break; @@ -2998,7 +2997,7 @@ class lexer { // U+D000..U+D7FF: bytes ED 80..9F 80..BF case 0xED: { - if ( JSON_UNLIKELY( not( next_byte_in_range( {0x80, 0x9F, 0x80, 0xBF} ) ) ) ) { + if ( JSON_UNLIKELY( not( next_byte_in_range( { 0x80, 0x9F, 0x80, 0xBF } ) ) ) ) { return token_type::parse_error; } break; @@ -3007,7 +3006,7 @@ class lexer { // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF case 0xF0: { if ( JSON_UNLIKELY( - not( next_byte_in_range( {0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF} ) ) ) ) { + not( next_byte_in_range( { 0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF } ) ) ) ) { return token_type::parse_error; } break; @@ -3018,7 +3017,7 @@ class lexer { case 0xF2: case 0xF3: { if ( JSON_UNLIKELY( - not( next_byte_in_range( {0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF} ) ) ) ) { + not( next_byte_in_range( { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF } ) ) ) ) { return token_type::parse_error; } break; @@ -3027,7 +3026,7 @@ class lexer { // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF case 0xF4: { if ( JSON_UNLIKELY( - not( next_byte_in_range( {0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF} ) ) ) ) { + not( next_byte_in_range( { 0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF } ) ) ) ) { return token_type::parse_error; } break; @@ -4287,7 +4286,7 @@ class json_sax_dom_callback_parser { // do not handle this value if we know it would be added to a discarded // container if ( not keep_stack.back() ) { - return {false, nullptr}; + return { false, nullptr }; } // create value @@ -4299,18 +4298,18 @@ class json_sax_dom_callback_parser { // do not handle this value if we just learnt it shall be discarded if ( not keep ) { - return {false, nullptr}; + return { false, nullptr }; } if ( ref_stack.empty() ) { root = std::move( value ); - return {true, &root}; + return { true, &root }; } // skip this value if we already decided to skip the parent // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360) if ( not ref_stack.back() ) { - return {false, nullptr}; + return { false, nullptr }; } // we now only expect arrays and objects @@ -4318,7 +4317,7 @@ class json_sax_dom_callback_parser { if ( ref_stack.back()->is_array() ) { ref_stack.back()->m_value.array->push_back( std::move( value ) ); - return {true, &( ref_stack.back()->m_value.array->back() )}; + return { true, &( ref_stack.back()->m_value.array->back() ) }; } else { // check if we should store an element for the current key assert( not key_keep_stack.empty() ); @@ -4326,12 +4325,12 @@ class json_sax_dom_callback_parser { key_keep_stack.pop_back(); if ( not store_element ) { - return {false, nullptr}; + return { false, nullptr }; } assert( object_element ); *object_element = std::move( value ); - return {true, object_element}; + return { true, object_element }; } } @@ -7508,7 +7507,7 @@ class binary_reader { std::string get_token_string() const { char cr[3]; snprintf( cr, 3, "%.2hhX", static_cast< unsigned char >( current ) ); - return std::string{cr}; + return std::string{ cr }; } /*! @@ -8743,7 +8742,7 @@ struct diyfp // f * 2^e assert( x.e == y.e ); assert( x.f >= y.f ); - return {x.f - y.f, x.e}; + return { x.f - y.f, x.e }; } /*! @@ -8804,11 +8803,11 @@ struct diyfp // f * 2^e // Effectively we only need to add the highest bit in p_lo to p_hi (and // Q_hi + 1 does not overflow). - Q += uint64_t{1} << ( 64 - 32 - 1 ); // round, ties up + Q += uint64_t{ 1 } << ( 64 - 32 - 1 ); // round, ties up const uint64_t h = p3 + p2_hi + p1_hi + ( Q >> 32 ); - return {h, x.e + y.e + 64}; + return { h, x.e + y.e + 64 }; } /*! @@ -8836,7 +8835,7 @@ struct diyfp // f * 2^e assert( delta >= 0 ); assert( ( ( x.f << delta ) >> delta ) == x.f ); - return {x.f << delta, target_exponent}; + return { x.f << delta, target_exponent }; } }; @@ -8871,7 +8870,7 @@ boundaries compute_boundaries( FloatType value ) { std::numeric_limits< FloatType >::digits; // = p (includes the hidden bit) constexpr int kBias = std::numeric_limits< FloatType >::max_exponent - 1 + ( kPrecision - 1 ); constexpr int kMinExp = 1 - kBias; - constexpr uint64_t kHiddenBit = uint64_t{1} << ( kPrecision - 1 ); // = 2^(p-1) + constexpr uint64_t kHiddenBit = uint64_t{ 1 } << ( kPrecision - 1 ); // = 2^(p-1) using bits_type = typename std::conditional< kPrecision == 24, uint32_t, uint64_t >::type; @@ -8916,7 +8915,7 @@ boundaries compute_boundaries( FloatType value ) { // Determine w- = m- such that e_(w-) = e_(w+). const diyfp w_minus = diyfp::normalize_to( m_minus, w_plus.e ); - return {diyfp::normalize( v ), w_minus, w_plus}; + return { diyfp::normalize( v ), w_minus, w_plus }; } // Given normalized diyfp w, Grisu needs to find a (normalized) cached @@ -9047,85 +9046,85 @@ inline cached_power get_cached_power_for_binary_exponent( int e ) { constexpr int kCachedPowersDecStep = 8; static constexpr cached_power kCachedPowers[] = { - {0xAB70FE17C79AC6CA, -1060, -300}, - {0xFF77B1FCBEBCDC4F, -1034, -292}, - {0xBE5691EF416BD60C, -1007, -284}, - {0x8DD01FAD907FFC3C, -980, -276}, - {0xD3515C2831559A83, -954, -268}, - {0x9D71AC8FADA6C9B5, -927, -260}, - {0xEA9C227723EE8BCB, -901, -252}, - {0xAECC49914078536D, -874, -244}, - {0x823C12795DB6CE57, -847, -236}, - {0xC21094364DFB5637, -821, -228}, - {0x9096EA6F3848984F, -794, -220}, - {0xD77485CB25823AC7, -768, -212}, - {0xA086CFCD97BF97F4, -741, -204}, - {0xEF340A98172AACE5, -715, -196}, - {0xB23867FB2A35B28E, -688, -188}, - {0x84C8D4DFD2C63F3B, -661, -180}, - {0xC5DD44271AD3CDBA, -635, -172}, - {0x936B9FCEBB25C996, -608, -164}, - {0xDBAC6C247D62A584, -582, -156}, - {0xA3AB66580D5FDAF6, -555, -148}, - {0xF3E2F893DEC3F126, -529, -140}, - {0xB5B5ADA8AAFF80B8, -502, -132}, - {0x87625F056C7C4A8B, -475, -124}, - {0xC9BCFF6034C13053, -449, -116}, - {0x964E858C91BA2655, -422, -108}, - {0xDFF9772470297EBD, -396, -100}, - {0xA6DFBD9FB8E5B88F, -369, -92}, - {0xF8A95FCF88747D94, -343, -84}, - {0xB94470938FA89BCF, -316, -76}, - {0x8A08F0F8BF0F156B, -289, -68}, - {0xCDB02555653131B6, -263, -60}, - {0x993FE2C6D07B7FAC, -236, -52}, - {0xE45C10C42A2B3B06, -210, -44}, - {0xAA242499697392D3, -183, -36}, - {0xFD87B5F28300CA0E, -157, -28}, - {0xBCE5086492111AEB, -130, -20}, - {0x8CBCCC096F5088CC, -103, -12}, - {0xD1B71758E219652C, -77, -4}, - {0x9C40000000000000, -50, 4}, - {0xE8D4A51000000000, -24, 12}, - {0xAD78EBC5AC620000, 3, 20}, - {0x813F3978F8940984, 30, 28}, - {0xC097CE7BC90715B3, 56, 36}, - {0x8F7E32CE7BEA5C70, 83, 44}, - {0xD5D238A4ABE98068, 109, 52}, - {0x9F4F2726179A2245, 136, 60}, - {0xED63A231D4C4FB27, 162, 68}, - {0xB0DE65388CC8ADA8, 189, 76}, - {0x83C7088E1AAB65DB, 216, 84}, - {0xC45D1DF942711D9A, 242, 92}, - {0x924D692CA61BE758, 269, 100}, - {0xDA01EE641A708DEA, 295, 108}, - {0xA26DA3999AEF774A, 322, 116}, - {0xF209787BB47D6B85, 348, 124}, - {0xB454E4A179DD1877, 375, 132}, - {0x865B86925B9BC5C2, 402, 140}, - {0xC83553C5C8965D3D, 428, 148}, - {0x952AB45CFA97A0B3, 455, 156}, - {0xDE469FBD99A05FE3, 481, 164}, - {0xA59BC234DB398C25, 508, 172}, - {0xF6C69A72A3989F5C, 534, 180}, - {0xB7DCBF5354E9BECE, 561, 188}, - {0x88FCF317F22241E2, 588, 196}, - {0xCC20CE9BD35C78A5, 614, 204}, - {0x98165AF37B2153DF, 641, 212}, - {0xE2A0B5DC971F303A, 667, 220}, - {0xA8D9D1535CE3B396, 694, 228}, - {0xFB9B7CD9A4A7443C, 720, 236}, - {0xBB764C4CA7A44410, 747, 244}, - {0x8BAB8EEFB6409C1A, 774, 252}, - {0xD01FEF10A657842C, 800, 260}, - {0x9B10A4E5E9913129, 827, 268}, - {0xE7109BFBA19C0C9D, 853, 276}, - {0xAC2820D9623BF429, 880, 284}, - {0x80444B5E7AA7CF85, 907, 292}, - {0xBF21E44003ACDD2D, 933, 300}, - {0x8E679C2F5E44FF8F, 960, 308}, - {0xD433179D9C8CB841, 986, 316}, - {0x9E19DB92B4E31BA9, 1013, 324}, + { 0xAB70FE17C79AC6CA, -1060, -300 }, + { 0xFF77B1FCBEBCDC4F, -1034, -292 }, + { 0xBE5691EF416BD60C, -1007, -284 }, + { 0x8DD01FAD907FFC3C, -980, -276 }, + { 0xD3515C2831559A83, -954, -268 }, + { 0x9D71AC8FADA6C9B5, -927, -260 }, + { 0xEA9C227723EE8BCB, -901, -252 }, + { 0xAECC49914078536D, -874, -244 }, + { 0x823C12795DB6CE57, -847, -236 }, + { 0xC21094364DFB5637, -821, -228 }, + { 0x9096EA6F3848984F, -794, -220 }, + { 0xD77485CB25823AC7, -768, -212 }, + { 0xA086CFCD97BF97F4, -741, -204 }, + { 0xEF340A98172AACE5, -715, -196 }, + { 0xB23867FB2A35B28E, -688, -188 }, + { 0x84C8D4DFD2C63F3B, -661, -180 }, + { 0xC5DD44271AD3CDBA, -635, -172 }, + { 0x936B9FCEBB25C996, -608, -164 }, + { 0xDBAC6C247D62A584, -582, -156 }, + { 0xA3AB66580D5FDAF6, -555, -148 }, + { 0xF3E2F893DEC3F126, -529, -140 }, + { 0xB5B5ADA8AAFF80B8, -502, -132 }, + { 0x87625F056C7C4A8B, -475, -124 }, + { 0xC9BCFF6034C13053, -449, -116 }, + { 0x964E858C91BA2655, -422, -108 }, + { 0xDFF9772470297EBD, -396, -100 }, + { 0xA6DFBD9FB8E5B88F, -369, -92 }, + { 0xF8A95FCF88747D94, -343, -84 }, + { 0xB94470938FA89BCF, -316, -76 }, + { 0x8A08F0F8BF0F156B, -289, -68 }, + { 0xCDB02555653131B6, -263, -60 }, + { 0x993FE2C6D07B7FAC, -236, -52 }, + { 0xE45C10C42A2B3B06, -210, -44 }, + { 0xAA242499697392D3, -183, -36 }, + { 0xFD87B5F28300CA0E, -157, -28 }, + { 0xBCE5086492111AEB, -130, -20 }, + { 0x8CBCCC096F5088CC, -103, -12 }, + { 0xD1B71758E219652C, -77, -4 }, + { 0x9C40000000000000, -50, 4 }, + { 0xE8D4A51000000000, -24, 12 }, + { 0xAD78EBC5AC620000, 3, 20 }, + { 0x813F3978F8940984, 30, 28 }, + { 0xC097CE7BC90715B3, 56, 36 }, + { 0x8F7E32CE7BEA5C70, 83, 44 }, + { 0xD5D238A4ABE98068, 109, 52 }, + { 0x9F4F2726179A2245, 136, 60 }, + { 0xED63A231D4C4FB27, 162, 68 }, + { 0xB0DE65388CC8ADA8, 189, 76 }, + { 0x83C7088E1AAB65DB, 216, 84 }, + { 0xC45D1DF942711D9A, 242, 92 }, + { 0x924D692CA61BE758, 269, 100 }, + { 0xDA01EE641A708DEA, 295, 108 }, + { 0xA26DA3999AEF774A, 322, 116 }, + { 0xF209787BB47D6B85, 348, 124 }, + { 0xB454E4A179DD1877, 375, 132 }, + { 0x865B86925B9BC5C2, 402, 140 }, + { 0xC83553C5C8965D3D, 428, 148 }, + { 0x952AB45CFA97A0B3, 455, 156 }, + { 0xDE469FBD99A05FE3, 481, 164 }, + { 0xA59BC234DB398C25, 508, 172 }, + { 0xF6C69A72A3989F5C, 534, 180 }, + { 0xB7DCBF5354E9BECE, 561, 188 }, + { 0x88FCF317F22241E2, 588, 196 }, + { 0xCC20CE9BD35C78A5, 614, 204 }, + { 0x98165AF37B2153DF, 641, 212 }, + { 0xE2A0B5DC971F303A, 667, 220 }, + { 0xA8D9D1535CE3B396, 694, 228 }, + { 0xFB9B7CD9A4A7443C, 720, 236 }, + { 0xBB764C4CA7A44410, 747, 244 }, + { 0x8BAB8EEFB6409C1A, 774, 252 }, + { 0xD01FEF10A657842C, 800, 260 }, + { 0x9B10A4E5E9913129, 827, 268 }, + { 0xE7109BFBA19C0C9D, 853, 276 }, + { 0xAC2820D9623BF429, 880, 284 }, + { 0x80444B5E7AA7CF85, 907, 292 }, + { 0xBF21E44003ACDD2D, 933, 300 }, + { 0x8E679C2F5E44FF8F, 960, 308 }, + { 0xD433179D9C8CB841, 986, 316 }, + { 0x9E19DB92B4E31BA9, 1013, 324 }, }; // This computation gives exactly the same results for k as @@ -9261,7 +9260,7 @@ inline void grisu2_digit_gen( // = ((p1 ) * 2^-e + (p2 )) * 2^e // = p1 + p2 * 2^e - const diyfp one( uint64_t{1} << -M_plus.e, M_plus.e ); + const diyfp one( uint64_t{ 1 } << -M_plus.e, M_plus.e ); auto p1 = static_cast< uint32_t >( M_plus.f >> -one.e ); // p1 = f div 2^-e (Since -e >= 32, p1 // fits into a 32-bit int.) @@ -9326,7 +9325,7 @@ inline void grisu2_digit_gen( // Note: // Since rest and delta share the same exponent e, it suffices to // compare the significands. - const uint64_t rest = ( uint64_t{p1} << -one.e ) + p2; + const uint64_t rest = ( uint64_t{ p1 } << -one.e ) + p2; if ( rest <= delta ) { // V = buffer * 10^n, with M- <= V <= M+. @@ -9341,7 +9340,7 @@ inline void grisu2_digit_gen( // // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e // - const uint64_t ten_n = uint64_t{pow10} << -one.e; + const uint64_t ten_n = uint64_t{ pow10 } << -one.e; grisu2_round( buffer, length, dist, delta, rest, ten_n ); return; @@ -10327,7 +10326,7 @@ class serializer { @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ */ static uint8_t decode( uint8_t& state, uint32_t& codep, const uint8_t byte ) noexcept { - static const std::array< uint8_t, 400 > utf8d = {{ + static const std::array< uint8_t, 400 > utf8d = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1F 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -10356,7 +10355,7 @@ class serializer { 1, 1, 1, // s5..s6 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8 - }}; + } }; const uint8_t type = utf8d[byte]; @@ -10372,7 +10371,7 @@ class serializer { output_adapter_t< char > o = nullptr; /// a (hopefully) large enough character buffer - std::array< char, 64 > number_buffer{{}}; + std::array< char, 64 > number_buffer{ {} }; /// the locale const std::lconv* loc = nullptr; @@ -10382,7 +10381,7 @@ class serializer { const char decimal_point = '\0'; /// string buffer - std::array< char, 512 > string_buffer{{}}; + std::array< char, 512 > string_buffer{ {} }; /// the indentation character const char indent_char; @@ -10566,7 +10565,7 @@ class json_pointer { } json_pointer result = *this; - result.reference_tokens = {reference_tokens[0]}; + result.reference_tokens = { reference_tokens[0] }; return result; } @@ -11080,8 +11079,8 @@ struct adl_serializer { */ template < typename BasicJsonType, typename ValueType > static auto from_json( BasicJsonType&& j, ValueType& val ) noexcept( - noexcept(::nlohmann::from_json( std::forward< BasicJsonType >( j ), val ) ) ) - -> decltype(::nlohmann::from_json( std::forward< BasicJsonType >( j ), val ), void() ) { + noexcept( ::nlohmann::from_json( std::forward< BasicJsonType >( j ), val ) ) ) + -> decltype( ::nlohmann::from_json( std::forward< BasicJsonType >( j ), val ), void() ) { ::nlohmann::from_json( std::forward< BasicJsonType >( j ), val ); } @@ -11096,8 +11095,8 @@ struct adl_serializer { */ template < typename BasicJsonType, typename ValueType > static auto to_json( BasicJsonType& j, ValueType&& val ) noexcept( - noexcept(::nlohmann::to_json( j, std::forward< ValueType >( val ) ) ) ) - -> decltype(::nlohmann::to_json( j, std::forward< ValueType >( val ) ), void() ) { + noexcept( ::nlohmann::to_json( j, std::forward< ValueType >( val ) ) ) ) + -> decltype( ::nlohmann::to_json( j, std::forward< ValueType >( val ) ), void() ) { ::nlohmann::to_json( j, std::forward< ValueType >( val ) ); } }; @@ -11379,25 +11378,25 @@ class basic_json { #endif #if defined( __ICC ) || defined( __INTEL_COMPILER ) - result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}}; + result["compiler"] = { { "family", "icc" }, { "version", __INTEL_COMPILER } }; #elif defined( __clang__ ) - result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}}; + result["compiler"] = { { "family", "clang" }, { "version", __clang_version__ } }; #elif defined( __GNUC__ ) || defined( __GNUG__ ) - result["compiler"] = {{"family", "gcc"}, - {"version", std::to_string( __GNUC__ ) + "." + std::to_string( __GNUC_MINOR__ ) + "." + - std::to_string( __GNUC_PATCHLEVEL__ )}}; + result["compiler"] = { { "family", "gcc" }, + { "version", std::to_string( __GNUC__ ) + "." + std::to_string( __GNUC_MINOR__ ) + "." + + std::to_string( __GNUC_PATCHLEVEL__ ) } }; #elif defined( __HP_cc ) || defined( __HP_aCC ) result["compiler"] = "hp" #elif defined( __IBMCPP__ ) - result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}}; + result["compiler"] = { { "family", "ilecpp" }, { "version", __IBMCPP__ } }; #elif defined( _MSC_VER ) - result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}}; + result["compiler"] = { { "family", "msvc" }, { "version", _MSC_VER } }; #elif defined( __PGI ) - result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}}; + result["compiler"] = { { "family", "pgcpp" }, { "version", __PGI } }; #elif defined( __SUNPRO_CC ) - result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}}; + result["compiler"] = { { "family", "sunpro" }, { "version", __SUNPRO_CC } }; #else - result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}}; + result["compiler"] = { { "family", "unknown" }, { "version", "unknown" } }; #endif #ifdef __cplusplus @@ -12014,7 +12013,9 @@ class basic_json { break; } - default: { break; } + default: { + break; + } } } }; @@ -13328,8 +13329,8 @@ class basic_json { } /// get a pointer to the value (integer number) - constexpr const number_integer_t* get_impl_ptr( const number_integer_t* /*unused*/ ) const - noexcept { + constexpr const number_integer_t* get_impl_ptr( + const number_integer_t* /*unused*/ ) const noexcept { return is_number_integer() ? &m_value.number_integer : nullptr; } @@ -13339,8 +13340,8 @@ class basic_json { } /// get a pointer to the value (unsigned number) - constexpr const number_unsigned_t* get_impl_ptr( const number_unsigned_t* /*unused*/ ) const - noexcept { + constexpr const number_unsigned_t* get_impl_ptr( + const number_unsigned_t* /*unused*/ ) const noexcept { return is_number_unsigned() ? &m_value.number_unsigned : nullptr; } @@ -13350,8 +13351,8 @@ class basic_json { } /// get a pointer to the value (floating-point number) - constexpr const number_float_t* get_impl_ptr( const number_float_t* /*unused*/ ) const - noexcept { + constexpr const number_float_t* get_impl_ptr( + const number_float_t* /*unused*/ ) const noexcept { return is_number_float() ? &m_value.number_float : nullptr; } @@ -15717,7 +15718,7 @@ class basic_json { it.m_it.object_iterator = res.first; // return pair of iterator and boolean - return {it, res.second}; + return { it, res.second }; } /// Helper for insertion of an iterator @@ -18298,7 +18299,7 @@ class basic_json { if ( source.type() != target.type() ) { // different types: replace value - result.push_back( {{"op", "replace"}, {"path", path}, {"value", target}} ); + result.push_back( { { "op", "replace" }, { "path", path }, { "value", target } } ); } else { switch ( source.type() ) { case value_t::array: { @@ -18320,14 +18321,15 @@ class basic_json { // add operations in reverse order to avoid invalid // indices result.insert( result.begin() + end_index, - object( {{"op", "remove"}, {"path", path + "/" + std::to_string( i )}} ) ); + object( { { "op", "remove" }, + { "path", path + "/" + std::to_string( i ) } } ) ); ++i; } // add other remaining elements while ( i < target.size() ) { - result.push_back( {{"op", "add"}, {"path", path + "/" + std::to_string( i )}, - {"value", target[i]}} ); + result.push_back( { { "op", "add" }, + { "path", path + "/" + std::to_string( i ) }, { "value", target[i] } } ); ++i; } @@ -18347,7 +18349,7 @@ class basic_json { } else { // found a key that is not in o -> remove it result.push_back( - object( {{"op", "remove"}, {"path", path + "/" + key}} ) ); + object( { { "op", "remove" }, { "path", path + "/" + key } } ) ); } } @@ -18356,8 +18358,8 @@ class basic_json { if ( source.find( it.key() ) == source.end() ) { // found a key that is not in this -> add it const auto key = json_pointer::escape( it.key() ); - result.push_back( - {{"op", "add"}, {"path", path + "/" + key}, {"value", it.value()}} ); + result.push_back( { { "op", "add" }, { "path", path + "/" + key }, + { "value", it.value() } } ); } } @@ -18366,7 +18368,7 @@ class basic_json { default: { // both primitive type: replace value - result.push_back( {{"op", "replace"}, {"path", path}, {"value", target}} ); + result.push_back( { { "op", "replace" }, { "path", path }, { "value", target } } ); break; } } diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 24ac5b4d..f4bd9e26 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -141,7 +141,7 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::encryptWithAES( auto V = std::get< 1 >( ciphertext ); auto W = std::get< 2 >( ciphertext ); - return {{U, V, W}, encrypted_message}; + return { { U, V, W }, encrypted_message }; } std::string TE::encryptMessage( const std::string& message, const std::string& common_public_str ) { @@ -359,7 +359,7 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::aesCiphertextFromString( throw ThresholdUtils::IncorrectInput( "Bad encrypted aes key provided" ); } - return {{U, V, W}, aes_cipher}; + return { { U, V, W }, aes_cipher }; } Ciphertext TE::ciphertextFromString( const std::string& ciphertext ) { @@ -390,7 +390,7 @@ Ciphertext TE::ciphertextFromString( const std::string& ciphertext ) { throw ThresholdUtils::IncorrectInput( "Bad encrypted aes key provided" ); } - return {U, V, W}; + return { U, V, W }; } } // namespace libBLS diff --git a/tools/utils.cpp b/tools/utils.cpp index e8cb554c..7b697bc7 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -247,7 +247,7 @@ std::string ThresholdUtils::carray2Hex( const unsigned char* d, uint64_t len ) { _hexArray.resize( 2 * len ); char hexval[16] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; for ( uint64_t j = 0; j < len; j++ ) { _hexArray[j * 2] = hexval[( ( d[j] >> 4 ) & 0xF )]; diff --git a/tools/utils.h b/tools/utils.h index f7ae2f66..5409d495 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -119,7 +119,7 @@ class ThresholdUtils { static bool checkHex( const std::string& hex ); - template + template < class T > static bool ValidateKey( const T& point ); }; From 1d397f0eded009fed7d79ee3194d013cc60b797a Mon Sep 17 00:00:00 2001 From: Oleh Date: Wed, 21 Dec 2022 17:38:30 +0200 Subject: [PATCH 04/16] small refactoring bls aggregated signatures --- bls/bls.cpp | 20 +++----------------- tools/utils.cpp | 11 +++++++++++ tools/utils.h | 2 ++ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index 30c5fb57..089e0b37 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -181,14 +181,7 @@ libff::alt_bn128_G1 Bls::Signing( libff::alt_bn128_G1 Bls::CoreSignAggregated( const std::string& message, const libff::alt_bn128_Fr secret_key ) { - auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); - - uint64_t bin_len; - if ( !ThresholdUtils::hex2carray( message.c_str(), &bin_len, hash_bytes_arr->data() ) ) { - throw std::runtime_error( "Invalid hash" ); - } - - libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( message ); return secret_key * hash; } @@ -213,15 +206,8 @@ bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& if ( !ThresholdUtils::ValidateKey( public_key ) || !ThresholdUtils::ValidateKey( signature ) ) { throw ThresholdUtils::IsNotWellFormed( "Either signature or public key is malicious" ); } - - auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); - - uint64_t bin_len; - if ( !ThresholdUtils::hex2carray( message.c_str(), &bin_len, hash_bytes_arr->data() ) ) { - throw std::runtime_error( "Invalid hash" ); - } - - libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( hash_bytes_arr ); + + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( message ); return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == libff::alt_bn128_ate_reduced_pairing( signature, libff::alt_bn128_G2::one() ); diff --git a/tools/utils.cpp b/tools/utils.cpp index 7b697bc7..b922910b 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -231,6 +231,17 @@ libff::alt_bn128_G1 ThresholdUtils::HashtoG1( return result; } +libff::alt_bn128_G1 ThresholdUtils::HashtoG1( const std::string& message ) { + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); + + uint64_t bin_len; + if ( !ThresholdUtils::hex2carray( message.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + throw std::runtime_error( "Invalid hash" ); + } + + return ThresholdUtils::HashtoG1( hash_bytes_arr ); +} + bool ThresholdUtils::isStringNumber( const std::string& str ) { if ( str.at( 0 ) == '0' && str.length() > 1 ) return false; diff --git a/tools/utils.h b/tools/utils.h index 5409d495..ebed594d 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -86,6 +86,8 @@ class ThresholdUtils { static libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); + + static libff::alt_bn128_G1 HashtoG1( const std::string& message ); static std::vector< uint8_t > aesEncrypt( const std::string& message, const std::string& key ); From e1f1eb38edafabe4d4e216538d301ce671dbc8d4 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 21 Dec 2022 17:44:07 +0200 Subject: [PATCH 05/16] format code --- bls/bls.cpp | 2 +- tools/utils.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index 089e0b37..699e347b 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -206,7 +206,7 @@ bool Bls::CoreVerify( const libff::alt_bn128_G2& public_key, const std::string& if ( !ThresholdUtils::ValidateKey( public_key ) || !ThresholdUtils::ValidateKey( signature ) ) { throw ThresholdUtils::IsNotWellFormed( "Either signature or public key is malicious" ); } - + libff::alt_bn128_G1 hash = ThresholdUtils::HashtoG1( message ); return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == diff --git a/tools/utils.h b/tools/utils.h index ebed594d..7109533f 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -86,7 +86,7 @@ class ThresholdUtils { static libff::alt_bn128_G1 HashtoG1( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_arr ); - + static libff::alt_bn128_G1 HashtoG1( const std::string& message ); static std::vector< uint8_t > aesEncrypt( const std::string& message, const std::string& key ); From 6fd572148d5a927f4d5d81b58b7ef4313e25d219 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Wed, 28 Dec 2022 13:15:09 +0200 Subject: [PATCH 06/16] add hashtog1withhint --- bls/bls.cpp | 18 ++++++++++++++++++ bls/bls.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/bls/bls.cpp b/bls/bls.cpp index 699e347b..04285888 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -158,6 +158,24 @@ libff::alt_bn128_G1 Bls::HashPublicKeyToG1( const libff::alt_bn128_G2& elem ) { return ThresholdUtils::HashtoG1( hash_bytes_arr ); } +std::pair< libff::alt_bn128_G1, std::string > Bls::HashPublicKeyToG1WithHint( + const libff::alt_bn128_G2& elem ) { + auto serialized_elem_vector = ThresholdUtils::G2ToString( elem, 16 ); + + std::string serialized_elem = std::accumulate( + serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); + + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); + + uint64_t bin_len; + if ( !ThresholdUtils::hex2carray( + serialized_elem.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + throw std::runtime_error( "Invalid hash" ); + } + + return Bls::HashtoG1withHint( hash_bytes_arr ); +} + libff::alt_bn128_G1 Bls::Signing( const libff::alt_bn128_G1 hash, const libff::alt_bn128_Fr secret_key ) { // sign a message with its hash and secret key diff --git a/bls/bls.h b/bls/bls.h index 527c271d..8b4c91c8 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -56,6 +56,9 @@ class Bls { static libff::alt_bn128_G1 HashPublicKeyToG1( const libff::alt_bn128_G2& elem ); + static std::pair< libff::alt_bn128_G1, std::string > HashPublicKeyToG1WithHint( + const libff::alt_bn128_G2& elem ); + static libff::alt_bn128_G1 Signing( const libff::alt_bn128_G1 hash, const libff::alt_bn128_Fr secret_key ); From e7be567f07d20673ca390698ed649da1debdbe93 Mon Sep 17 00:00:00 2001 From: Oleh Date: Thu, 5 Jan 2023 13:49:40 +0100 Subject: [PATCH 07/16] fix hashPublicKeyToPoint and add tests --- bls/bls.cpp | 8 +++-- bls/bls.h | 2 +- test/unit_tests_bls.cpp | 68 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index 04285888..c6a74838 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -145,13 +145,15 @@ libff::alt_bn128_G1 Bls::HashPublicKeyToG1( const libff::alt_bn128_G2& elem ) { auto serialized_elem_vector = ThresholdUtils::G2ToString( elem, 16 ); std::string serialized_elem = std::accumulate( - serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); + serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) );\ +// std::cout << serialized_elem.length() << '\n'; + std::string hashed_pubkey = cryptlite::sha256::hash_hex( serialized_elem ); + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); uint64_t bin_len; - if ( !ThresholdUtils::hex2carray( - serialized_elem.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + if ( !ThresholdUtils::hex2carray( hashed_pubkey.c_str(), &bin_len, hash_bytes_arr->data() ) ) { throw std::runtime_error( "Invalid hash" ); } diff --git a/bls/bls.h b/bls/bls.h index 8b4c91c8..a20ace1a 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -43,7 +43,7 @@ class Bls { public: Bls( const size_t t, const size_t n ); - std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > KeyGeneration(); + static std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > KeyGeneration(); static libff::alt_bn128_G1 Hashing( const std::string& message, std::string ( *hash_func )( const std::string& str ) = cryptlite::sha256::hash_hex ); diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 6fce90d8..e206d1c4 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -41,6 +41,29 @@ #include +std::default_random_engine rand_gen( ( unsigned int ) time( 0 ) ); + +std::array< uint8_t, 32 > GenerateRandHash() { + // generates random hexadermical hash + std::array< uint8_t, 32 > hash_byte_arr; + for ( size_t i = 0; i < 32; i++ ) { + hash_byte_arr.at( i ) = rand_gen() % 256; + } + + return hash_byte_arr; +} + +std::string rand32HexStr() { + std::array s = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + + std::string res = ""; + for (size_t i = 0; i < 32; ++i) { + res.push_back( *(s.begin() + rand_gen() % 16) ); + } + + return res; +} + BOOST_AUTO_TEST_SUITE( libBls ) BOOST_AUTO_TEST_CASE( zeroSecretKey ) { @@ -50,7 +73,7 @@ BOOST_AUTO_TEST_CASE( zeroSecretKey ) { libBLS::Bls obj = libBLS::Bls( 1, 1 ); - std::string message = "Waiting for exception"; + std::string message = rand32HexStr(); libff::alt_bn128_G1 hash = obj.Hashing( message ); @@ -69,7 +92,7 @@ BOOST_AUTO_TEST_CASE( singleBlsrun ) { libff::alt_bn128_Fr secret_key = keys.first; libff::alt_bn128_G2 public_key = keys.second; - std::string message = "testingSKALE"; + std::string message = rand32HexStr(); libff::alt_bn128_G1 hash = obj.Hashing( message ); @@ -124,7 +147,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignatures ) { libff::alt_bn128_G2 public_key = libff::alt_bn128_G2( first_coord, second_coord, libff::alt_bn128_Fq2::one() ); - std::string message = "testingSKALE"; + std::string message = rand32HexStr(); libff::alt_bn128_G1 hash = obj.Hashing( message ); @@ -184,7 +207,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesFalse ) { libff::alt_bn128_G2 public_key = libff::alt_bn128_G2( first_coord, second_coord, libff::alt_bn128_Fq2::one() ); - std::string message = "testingSKALE"; + std::string message = rand32HexStr(); libff::alt_bn128_G1 hash = obj.Hashing( message ); @@ -236,7 +259,7 @@ BOOST_AUTO_TEST_CASE( BlsThresholdSignaturesReal ) { } } - std::string message = "testingSKALE"; + std::string message = rand32HexStr(); libff::alt_bn128_G1 hash = obj.Hashing( message ); @@ -302,7 +325,7 @@ BOOST_AUTO_TEST_CASE( simillarSignatures ) { } } - std::string message = "testingSKALE"; + std::string message = rand32HexStr(); libff::alt_bn128_G1 hash = obj.Hashing( message ); @@ -441,6 +464,39 @@ BOOST_AUTO_TEST_CASE(differentMessages) { } }*/ +BOOST_AUTO_TEST_CASE( blsAggregatedSignatures ) { + std::cout << "Testing blsAggregatedSignatures\n"; + + libBLS::ThresholdUtils::initCurve(); + + auto first_key = libBLS::Bls::KeyGeneration(); + auto second_key = libBLS::Bls::KeyGeneration(); + + std::string hex_message = rand32HexStr(); // random hex + + libff::alt_bn128_G1 first_signature = libBLS::Bls::CoreSignAggregated(hex_message, first_key.first); + libff::alt_bn128_G1 second_signature = libBLS::Bls::CoreSignAggregated(hex_message, second_key.first); + + BOOST_REQUIRE( libBLS::Bls::CoreVerify( first_key.second, hex_message, first_signature ) ); + BOOST_REQUIRE( libBLS::Bls::CoreVerify( second_key.second, hex_message, second_signature ) ); + + libff::alt_bn128_G1 aggregated_signature = libBLS::Bls::Aggregate( { first_signature, second_signature } ); + + BOOST_REQUIRE( libBLS::Bls::FastAggregateVerify( { first_key.second, second_key.second }, hex_message, aggregated_signature ) ); +} + +BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { + std::cout << "Testing blsAggregatedSignaturesPopProveVerify\n"; + + libBLS::ThresholdUtils::initCurve(); + + auto key_pair = libBLS::Bls::KeyGeneration(); + + auto pop_prove = libBLS::Bls::PopProve( key_pair.first ); + + BOOST_REQUIRE( libBLS::Bls::PopVerify( key_pair.second, pop_prove ) ); +} + BOOST_AUTO_TEST_SUITE_END() From 8940ad426eeea89db35bbf71a853f8fd6eeeb83f Mon Sep 17 00:00:00 2001 From: Oleh Date: Thu, 5 Jan 2023 15:54:53 +0100 Subject: [PATCH 08/16] add more tests --- bls/bls.cpp | 7 +++---- bls/bls.h | 2 +- test/unit_tests_bls.cpp | 12 ++++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index c6a74838..c660d127 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -147,7 +147,6 @@ libff::alt_bn128_G1 Bls::HashPublicKeyToG1( const libff::alt_bn128_G2& elem ) { std::string serialized_elem = std::accumulate( serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) );\ -// std::cout << serialized_elem.length() << '\n'; std::string hashed_pubkey = cryptlite::sha256::hash_hex( serialized_elem ); auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); @@ -387,8 +386,8 @@ libff::alt_bn128_G1 Bls::PopProve( const libff::alt_bn128_Fr& secret_key ) { return ret; } -bool Bls::PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ) { - if ( !ThresholdUtils::ValidateKey( proof ) || !ThresholdUtils::ValidateKey( public_key ) ) { +bool Bls::PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& prove ) { + if ( !ThresholdUtils::ValidateKey( prove ) || !ThresholdUtils::ValidateKey( public_key ) ) { throw ThresholdUtils::IsNotWellFormed( "incorrect input data to verify proof of possession" ); } @@ -396,7 +395,7 @@ bool Bls::PopVerify( const libff::alt_bn128_G2& public_key, const libff::alt_bn1 libff::alt_bn128_G1 hash = HashPublicKeyToG1( public_key ); return libff::alt_bn128_ate_reduced_pairing( hash, public_key ) == - libff::alt_bn128_ate_reduced_pairing( proof, libff::alt_bn128_G2::one() ); + libff::alt_bn128_ate_reduced_pairing( prove, libff::alt_bn128_G2::one() ); } } // namespace libBLS diff --git a/bls/bls.h b/bls/bls.h index a20ace1a..95ef87eb 100644 --- a/bls/bls.h +++ b/bls/bls.h @@ -93,7 +93,7 @@ class Bls { static libff::alt_bn128_G1 PopProve( const libff::alt_bn128_Fr& secret_key ); static bool PopVerify( - const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& proof ); + const libff::alt_bn128_G2& public_key, const libff::alt_bn128_G1& prove ); private: const size_t t_ = 0; diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index e206d1c4..003ca15e 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -483,6 +483,14 @@ BOOST_AUTO_TEST_CASE( blsAggregatedSignatures ) { libff::alt_bn128_G1 aggregated_signature = libBLS::Bls::Aggregate( { first_signature, second_signature } ); BOOST_REQUIRE( libBLS::Bls::FastAggregateVerify( { first_key.second, second_key.second }, hex_message, aggregated_signature ) ); + + auto malicious_key = libBLS::Bls::KeyGeneration(); + + libff::alt_bn128_G1 malicious_signature = libBLS::Bls::CoreSignAggregated( hex_message, malicious_key.first ); + + auto malicious_aggregated_signature = libBLS::Bls::Aggregate( { first_signature, malicious_signature } ); + + BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( { first_key.second, second_key.second }, hex_message, malicious_aggregated_signature ) ); } BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { @@ -495,6 +503,10 @@ BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { auto pop_prove = libBLS::Bls::PopProve( key_pair.first ); BOOST_REQUIRE( libBLS::Bls::PopVerify( key_pair.second, pop_prove ) ); + + auto random_prove = libff::alt_bn128_G1::random_element(); + + BOOST_REQUIRE( !libBLS::Bls::PopVerify( key_pair.second, random_prove ) ); } BOOST_AUTO_TEST_SUITE_END() From dd146163c88200ccbdd43bc1249748a3c6dfd065 Mon Sep 17 00:00:00 2001 From: Oleh Date: Thu, 5 Jan 2023 18:36:28 +0100 Subject: [PATCH 09/16] add more tests --- test/test_bls.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index ce2bd986..2356601c 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -97,6 +97,34 @@ libff::alt_bn128_G1 SpoilSignature( libff::alt_bn128_G1& sign ) { return bad_sign; } +libff::alt_bn128_G2 SpoilPublicKey( libff::alt_bn128_G2& sign ) { + libff::alt_bn128_G2 bad_sign = sign; + while ( bad_sign.is_well_formed() ) { + size_t bad_coord_num = rand_gen() % 6; + switch ( bad_coord_num ) { + case 0: + bad_sign.X.c0 = SpoilSignCoord( sign.X.c0 ); + break; + case 1: + bad_sign.X.c1 = SpoilSignCoord( sign.X.c1 ); + break; + case 2: + bad_sign.Y.c0 = SpoilSignCoord( sign.Y.c0 ); + break; + case 3: + bad_sign.Y.c1 = SpoilSignCoord( sign.Y.c1 ); + break; + case 4: + bad_sign.Z.c0 = SpoilSignCoord( sign.Z.c0 ); + break; + case 5: + bad_sign.Z.c1 = SpoilSignCoord( sign.Z.c1 ); + break; + } + } + return bad_sign; +} + std::array< uint8_t, 32 > GenerateRandHash() { // generates random hexadermical hash std::array< uint8_t, 32 > hash_byte_arr; @@ -107,6 +135,17 @@ std::array< uint8_t, 32 > GenerateRandHash() { return hash_byte_arr; } +std::string rand32HexStr() { + std::array s = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + + std::string res = ""; + for (size_t i = 0; i < 32; ++i) { + res.push_back( *(s.begin() + rand_gen() % 16) ); + } + + return res; +} + BOOST_AUTO_TEST_CASE( libBls ) { libff::inhibit_profiling_info = true; std::cerr << "STARTING LIBBLS TESTS" << std::endl; @@ -587,7 +626,7 @@ BOOST_AUTO_TEST_CASE( BLSWITHDKG ) { std::cerr << "BLS WITH DKG TEST FINISHED" << std::endl; } -BOOST_AUTO_TEST_CASE( BLSAGGREGATEDVERIFICATION ) { +BOOST_AUTO_TEST_CASE( BLSAGGREGATEDVERIFICATIONONLY ) { 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 ) + 1; @@ -720,6 +759,57 @@ BOOST_AUTO_TEST_CASE( BLSAGGREGATEDVERIFICATION ) { std::cerr << "BLS AGGREGATED VERIFICATION TEST FINISHED" << std::endl; } +BOOST_AUTO_TEST_CASE( BLSAGGREGATEDSIGNATURESSCHEME ) { + size_t num_all = rand_gen() % 100 + 2; + + std::vector< libff::alt_bn128_Fr > private_keys( num_all ); + std::vector< libff::alt_bn128_G2 > public_keys( num_all ); + for ( size_t i = 0; i < num_all; ++i ) { + auto key_pair = libBLS::Bls::KeyGeneration(); + private_keys[i] = key_pair.first; + public_keys[i] = key_pair.second; + } + + std::string hex_message = rand32HexStr(); + + std::vector< libff::alt_bn128_G1 > signatures( num_all ); + for ( size_t i = 0; i < num_all; ++i ) { + signatures[i] = libBLS::Bls::CoreSignAggregated( hex_message, private_keys[i] ); + BOOST_REQUIRE( libBLS::Bls::CoreVerify( public_keys[i], hex_message, signatures[i] ) ); + BOOST_REQUIRE_THROW( libBLS::Bls::CoreVerify( public_keys[i], hex_message, SpoilSignature( signatures[i] ) ), libBLS::ThresholdUtils::IsNotWellFormed ); + } + + for ( size_t i = 0; i < num_all; ++i ) { + auto malicious_signatures = signatures; + size_t rand_idx = rand_gen() % num_all; + malicious_signatures[ rand_idx ] = SpoilSignature( signatures[rand_idx] ); + + BOOST_REQUIRE_THROW( libBLS::Bls::Aggregate( malicious_signatures ), libBLS::ThresholdUtils::IsNotWellFormed ); + } + + auto aggregated_signature = libBLS::Bls::Aggregate( signatures ); + + BOOST_REQUIRE( libBLS::Bls::FastAggregateVerify( public_keys, hex_message, aggregated_signature ) ); + + for ( size_t i = 0; i < num_all; ++i ) { + auto malicious_public_keys = public_keys; + size_t rand_idx = rand_gen() % num_all; + malicious_public_keys[ rand_idx ] = SpoilPublicKey( public_keys[rand_idx] ); + + BOOST_REQUIRE_THROW( libBLS::Bls::FastAggregateVerify( malicious_public_keys, hex_message, aggregated_signature ), libBLS::ThresholdUtils::IsNotWellFormed ); + } + + for ( size_t i = 0; i < num_all; ++i ) { + auto malicious_public_keys = public_keys; + size_t rand_idx = rand_gen() % num_all; + malicious_public_keys[ rand_idx ] = libff::alt_bn128_G2::random_element(); + + BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( malicious_public_keys, hex_message, aggregated_signature ) ); + } + + std::cerr << "BLS AGGREGATED SIGNATURES SCHEME TEST FINISHED" << std::endl; +} + BOOST_AUTO_TEST_CASE( Exceptions ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 1; From 37eb7cd0a019a5c7692ee0e6aaec2a9e4e467ef1 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 5 Jan 2023 18:38:03 +0100 Subject: [PATCH 10/16] make format --- bls/bls.cpp | 4 +-- test/test_bls.cpp | 58 ++++++++++++++++++++---------------- test/unit_tests_bls.cpp | 66 +++++++++++++++++++++++------------------ 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index c660d127..d3c381fc 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -145,10 +145,10 @@ libff::alt_bn128_G1 Bls::HashPublicKeyToG1( const libff::alt_bn128_G2& elem ) { auto serialized_elem_vector = ThresholdUtils::G2ToString( elem, 16 ); std::string serialized_elem = std::accumulate( - serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) );\ + serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); std::string hashed_pubkey = cryptlite::sha256::hash_hex( serialized_elem ); - + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); uint64_t bin_len; diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 2356601c..68bff25d 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -136,13 +136,14 @@ std::array< uint8_t, 32 > GenerateRandHash() { } std::string rand32HexStr() { - std::array s = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - + std::array< char, 16 > s = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + std::string res = ""; - for (size_t i = 0; i < 32; ++i) { - res.push_back( *(s.begin() + rand_gen() % 16) ); + for ( size_t i = 0; i < 32; ++i ) { + res.push_back( *( s.begin() + rand_gen() % 16 ) ); } - + return res; } @@ -761,7 +762,7 @@ BOOST_AUTO_TEST_CASE( BLSAGGREGATEDVERIFICATIONONLY ) { BOOST_AUTO_TEST_CASE( BLSAGGREGATEDSIGNATURESSCHEME ) { size_t num_all = rand_gen() % 100 + 2; - + std::vector< libff::alt_bn128_Fr > private_keys( num_all ); std::vector< libff::alt_bn128_G2 > public_keys( num_all ); for ( size_t i = 0; i < num_all; ++i ) { @@ -769,44 +770,51 @@ BOOST_AUTO_TEST_CASE( BLSAGGREGATEDSIGNATURESSCHEME ) { private_keys[i] = key_pair.first; public_keys[i] = key_pair.second; } - + std::string hex_message = rand32HexStr(); - + std::vector< libff::alt_bn128_G1 > signatures( num_all ); for ( size_t i = 0; i < num_all; ++i ) { signatures[i] = libBLS::Bls::CoreSignAggregated( hex_message, private_keys[i] ); BOOST_REQUIRE( libBLS::Bls::CoreVerify( public_keys[i], hex_message, signatures[i] ) ); - BOOST_REQUIRE_THROW( libBLS::Bls::CoreVerify( public_keys[i], hex_message, SpoilSignature( signatures[i] ) ), libBLS::ThresholdUtils::IsNotWellFormed ); + BOOST_REQUIRE_THROW( + libBLS::Bls::CoreVerify( public_keys[i], hex_message, SpoilSignature( signatures[i] ) ), + libBLS::ThresholdUtils::IsNotWellFormed ); } - + for ( size_t i = 0; i < num_all; ++i ) { auto malicious_signatures = signatures; size_t rand_idx = rand_gen() % num_all; - malicious_signatures[ rand_idx ] = SpoilSignature( signatures[rand_idx] ); - - BOOST_REQUIRE_THROW( libBLS::Bls::Aggregate( malicious_signatures ), libBLS::ThresholdUtils::IsNotWellFormed ); + malicious_signatures[rand_idx] = SpoilSignature( signatures[rand_idx] ); + + BOOST_REQUIRE_THROW( libBLS::Bls::Aggregate( malicious_signatures ), + libBLS::ThresholdUtils::IsNotWellFormed ); } - + auto aggregated_signature = libBLS::Bls::Aggregate( signatures ); - - BOOST_REQUIRE( libBLS::Bls::FastAggregateVerify( public_keys, hex_message, aggregated_signature ) ); - + + BOOST_REQUIRE( + libBLS::Bls::FastAggregateVerify( public_keys, hex_message, aggregated_signature ) ); + for ( size_t i = 0; i < num_all; ++i ) { auto malicious_public_keys = public_keys; size_t rand_idx = rand_gen() % num_all; - malicious_public_keys[ rand_idx ] = SpoilPublicKey( public_keys[rand_idx] ); - - BOOST_REQUIRE_THROW( libBLS::Bls::FastAggregateVerify( malicious_public_keys, hex_message, aggregated_signature ), libBLS::ThresholdUtils::IsNotWellFormed ); + malicious_public_keys[rand_idx] = SpoilPublicKey( public_keys[rand_idx] ); + + BOOST_REQUIRE_THROW( libBLS::Bls::FastAggregateVerify( + malicious_public_keys, hex_message, aggregated_signature ), + libBLS::ThresholdUtils::IsNotWellFormed ); } - + for ( size_t i = 0; i < num_all; ++i ) { auto malicious_public_keys = public_keys; size_t rand_idx = rand_gen() % num_all; - malicious_public_keys[ rand_idx ] = libff::alt_bn128_G2::random_element(); - - BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( malicious_public_keys, hex_message, aggregated_signature ) ); + malicious_public_keys[rand_idx] = libff::alt_bn128_G2::random_element(); + + BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( + malicious_public_keys, hex_message, aggregated_signature ) ); } - + std::cerr << "BLS AGGREGATED SIGNATURES SCHEME TEST FINISHED" << std::endl; } diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 003ca15e..96c3df28 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -54,13 +54,14 @@ std::array< uint8_t, 32 > GenerateRandHash() { } std::string rand32HexStr() { - std::array s = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - + std::array< char, 16 > s = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + std::string res = ""; - for (size_t i = 0; i < 32; ++i) { - res.push_back( *(s.begin() + rand_gen() % 16) ); + for ( size_t i = 0; i < 32; ++i ) { + res.push_back( *( s.begin() + rand_gen() % 16 ) ); } - + return res; } @@ -466,46 +467,53 @@ BOOST_AUTO_TEST_CASE(differentMessages) { BOOST_AUTO_TEST_CASE( blsAggregatedSignatures ) { std::cout << "Testing blsAggregatedSignatures\n"; - + libBLS::ThresholdUtils::initCurve(); - + auto first_key = libBLS::Bls::KeyGeneration(); auto second_key = libBLS::Bls::KeyGeneration(); - - std::string hex_message = rand32HexStr(); // random hex - libff::alt_bn128_G1 first_signature = libBLS::Bls::CoreSignAggregated(hex_message, first_key.first); - libff::alt_bn128_G1 second_signature = libBLS::Bls::CoreSignAggregated(hex_message, second_key.first); - + std::string hex_message = rand32HexStr(); // random hex + + libff::alt_bn128_G1 first_signature = + libBLS::Bls::CoreSignAggregated( hex_message, first_key.first ); + libff::alt_bn128_G1 second_signature = + libBLS::Bls::CoreSignAggregated( hex_message, second_key.first ); + BOOST_REQUIRE( libBLS::Bls::CoreVerify( first_key.second, hex_message, first_signature ) ); BOOST_REQUIRE( libBLS::Bls::CoreVerify( second_key.second, hex_message, second_signature ) ); - - libff::alt_bn128_G1 aggregated_signature = libBLS::Bls::Aggregate( { first_signature, second_signature } ); - - BOOST_REQUIRE( libBLS::Bls::FastAggregateVerify( { first_key.second, second_key.second }, hex_message, aggregated_signature ) ); - + + libff::alt_bn128_G1 aggregated_signature = + libBLS::Bls::Aggregate( { first_signature, second_signature } ); + + BOOST_REQUIRE( libBLS::Bls::FastAggregateVerify( + { first_key.second, second_key.second }, hex_message, aggregated_signature ) ); + auto malicious_key = libBLS::Bls::KeyGeneration(); - - libff::alt_bn128_G1 malicious_signature = libBLS::Bls::CoreSignAggregated( hex_message, malicious_key.first ); - - auto malicious_aggregated_signature = libBLS::Bls::Aggregate( { first_signature, malicious_signature } ); - - BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( { first_key.second, second_key.second }, hex_message, malicious_aggregated_signature ) ); + + libff::alt_bn128_G1 malicious_signature = + libBLS::Bls::CoreSignAggregated( hex_message, malicious_key.first ); + + auto malicious_aggregated_signature = + libBLS::Bls::Aggregate( { first_signature, malicious_signature } ); + + BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( + { first_key.second, second_key.second }, hex_message, malicious_aggregated_signature ) ); } BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { std::cout << "Testing blsAggregatedSignaturesPopProveVerify\n"; - + libBLS::ThresholdUtils::initCurve(); - + auto key_pair = libBLS::Bls::KeyGeneration(); - + auto pop_prove = libBLS::Bls::PopProve( key_pair.first ); - + BOOST_REQUIRE( libBLS::Bls::PopVerify( key_pair.second, pop_prove ) ); - + auto random_prove = libff::alt_bn128_G1::random_element(); - + BOOST_REQUIRE( !libBLS::Bls::PopVerify( key_pair.second, random_prove ) ); } From 36b2616ea5e907b7ffddccc7133654fe10be3fdb Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Mon, 16 Jan 2023 14:27:42 +0100 Subject: [PATCH 11/16] format code --- test/unit_tests_bls.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 96c3df28..92d4e3e6 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -499,6 +499,8 @@ BOOST_AUTO_TEST_CASE( blsAggregatedSignatures ) { BOOST_REQUIRE( !libBLS::Bls::FastAggregateVerify( { first_key.second, second_key.second }, hex_message, malicious_aggregated_signature ) ); + + std::cout << "DONE\n"; } BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { @@ -515,6 +517,8 @@ BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { auto random_prove = libff::alt_bn128_G1::random_element(); BOOST_REQUIRE( !libBLS::Bls::PopVerify( key_pair.second, random_prove ) ); + + std::cout << "DONE\n"; } BOOST_AUTO_TEST_SUITE_END() From b63529bed05eb73e60a104e2460acdde0909f57f Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Mon, 16 Jan 2023 14:29:15 +0100 Subject: [PATCH 12/16] fix HashPublicKeyToG1WithHint --- bls/bls.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bls/bls.cpp b/bls/bls.cpp index d3c381fc..53eb41da 100644 --- a/bls/bls.cpp +++ b/bls/bls.cpp @@ -166,11 +166,12 @@ std::pair< libff::alt_bn128_G1, std::string > Bls::HashPublicKeyToG1WithHint( std::string serialized_elem = std::accumulate( serialized_elem_vector.begin(), serialized_elem_vector.end(), std::string( "" ) ); + std::string hashed_pubkey = cryptlite::sha256::hash_hex( serialized_elem ); + auto hash_bytes_arr = std::make_shared< std::array< uint8_t, 32 > >(); uint64_t bin_len; - if ( !ThresholdUtils::hex2carray( - serialized_elem.c_str(), &bin_len, hash_bytes_arr->data() ) ) { + if ( !ThresholdUtils::hex2carray( hashed_pubkey.c_str(), &bin_len, hash_bytes_arr->data() ) ) { throw std::runtime_error( "Invalid hash" ); } From 902783e1bcf416673a69fe3e7fe270d0df781c5e Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 19 Jan 2023 13:56:04 +0000 Subject: [PATCH 13/16] add more tests --- test/test_bls.cpp | 49 ++++++++++++++++++++++++++++++++--------- test/unit_tests_bls.cpp | 18 --------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 68bff25d..71e73214 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -97,32 +97,32 @@ libff::alt_bn128_G1 SpoilSignature( libff::alt_bn128_G1& sign ) { return bad_sign; } -libff::alt_bn128_G2 SpoilPublicKey( libff::alt_bn128_G2& sign ) { - libff::alt_bn128_G2 bad_sign = sign; - while ( bad_sign.is_well_formed() ) { +libff::alt_bn128_G2 SpoilPublicKey( libff::alt_bn128_G2& elem ) { + libff::alt_bn128_G2 bad_elem = elem; + while ( bad_elem.is_well_formed() ) { size_t bad_coord_num = rand_gen() % 6; switch ( bad_coord_num ) { case 0: - bad_sign.X.c0 = SpoilSignCoord( sign.X.c0 ); + bad_elem.X.c0 = SpoilSignCoord( elem.X.c0 ); break; case 1: - bad_sign.X.c1 = SpoilSignCoord( sign.X.c1 ); + bad_elem.X.c1 = SpoilSignCoord( elem.X.c1 ); break; case 2: - bad_sign.Y.c0 = SpoilSignCoord( sign.Y.c0 ); + bad_elem.Y.c0 = SpoilSignCoord( elem.Y.c0 ); break; case 3: - bad_sign.Y.c1 = SpoilSignCoord( sign.Y.c1 ); + bad_elem.Y.c1 = SpoilSignCoord( elem.Y.c1 ); break; case 4: - bad_sign.Z.c0 = SpoilSignCoord( sign.Z.c0 ); + bad_elem.Z.c0 = SpoilSignCoord( elem.Z.c0 ); break; case 5: - bad_sign.Z.c1 = SpoilSignCoord( sign.Z.c1 ); + bad_elem.Z.c1 = SpoilSignCoord( elem.Z.c1 ); break; } } - return bad_sign; + return bad_elem; } std::array< uint8_t, 32 > GenerateRandHash() { @@ -818,6 +818,35 @@ BOOST_AUTO_TEST_CASE( BLSAGGREGATEDSIGNATURESSCHEME ) { std::cerr << "BLS AGGREGATED SIGNATURES SCHEME TEST FINISHED" << std::endl; } +BOOST_AUTO_TEST_CASE( BLSAGGREGATEDPOPPROVEVERIFY ) { + std::cout << "Testing blsAggregatedSignaturesPopProveVerify\n"; + + libBLS::ThresholdUtils::initCurve(); + + auto key_pair = libBLS::Bls::KeyGeneration(); + + auto pop_prove = libBLS::Bls::PopProve( key_pair.first ); + + BOOST_REQUIRE( libBLS::Bls::PopVerify( key_pair.second, pop_prove ) ); + + auto random_prove = libff::alt_bn128_G1::random_element(); + + BOOST_REQUIRE( !libBLS::Bls::PopVerify( key_pair.second, random_prove ) ); + + BOOST_REQUIRE( libBLS::Bls::HashPublicKeyToG1WithHint( key_pair.second ).first == + libBLS::Bls::HashPublicKeyToG1( key_pair.second ) ); + + auto spoiled_public_key = SpoilPublicKey( key_pair.second ); + auto spoiled_pop_prove = SpoilSignature( pop_prove ); + + BOOST_REQUIRE_THROW( libBLS::Bls::PopVerify( spoiled_public_key, pop_prove ), + libBLS::ThresholdUtils::IsNotWellFormed ); + BOOST_REQUIRE_THROW( libBLS::Bls::PopVerify( key_pair.second, spoiled_pop_prove ), + libBLS::ThresholdUtils::IsNotWellFormed ); + + std::cout << "BLS AGGREGATED POP PROVE VERIFY TEST PASSED\n"; +} + BOOST_AUTO_TEST_CASE( Exceptions ) { size_t num_all = rand_gen() % 15 + 2; size_t num_signed = rand_gen() % ( num_all - 1 ) + 1; diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 92d4e3e6..32bbc0ba 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -503,24 +503,6 @@ BOOST_AUTO_TEST_CASE( blsAggregatedSignatures ) { std::cout << "DONE\n"; } -BOOST_AUTO_TEST_CASE( blsAggregatedSignaturesPopProveVerify ) { - std::cout << "Testing blsAggregatedSignaturesPopProveVerify\n"; - - libBLS::ThresholdUtils::initCurve(); - - auto key_pair = libBLS::Bls::KeyGeneration(); - - auto pop_prove = libBLS::Bls::PopProve( key_pair.first ); - - BOOST_REQUIRE( libBLS::Bls::PopVerify( key_pair.second, pop_prove ) ); - - auto random_prove = libff::alt_bn128_G1::random_element(); - - BOOST_REQUIRE( !libBLS::Bls::PopVerify( key_pair.second, random_prove ) ); - - std::cout << "DONE\n"; -} - BOOST_AUTO_TEST_SUITE_END() From 63e6aead534e736e2d0a4f650afc799a2e257bab Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 19 Jan 2023 15:27:33 +0000 Subject: [PATCH 14/16] add more tests --- test/unit_tests_te.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 64725a1d..cbfd2346 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -264,6 +264,8 @@ BOOST_AUTO_TEST_CASE( EncryptionCipherToString ) { auto ciphertext_with_aes = te_instance.aesCiphertextFromString( ciphertext_string ); auto ciphertext = ciphertext_with_aes.first; + BOOST_REQUIRE( ciphertext == te_instance.ciphertextFromString( ciphertext_string ) ); + auto encrypted_message = ciphertext_with_aes.second; libff::alt_bn128_G2 decryption_share = te_instance.getDecryptionShare( ciphertext, secret_key ); From 2796c118d9be3f0ce7692f0a0d9c019dae912093 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 19 Jan 2023 15:43:55 +0000 Subject: [PATCH 15/16] remove extra code --- test/unit_tests_bls.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/unit_tests_bls.cpp b/test/unit_tests_bls.cpp index 32bbc0ba..44a980d6 100644 --- a/test/unit_tests_bls.cpp +++ b/test/unit_tests_bls.cpp @@ -43,16 +43,6 @@ std::default_random_engine rand_gen( ( unsigned int ) time( 0 ) ); -std::array< uint8_t, 32 > GenerateRandHash() { - // generates random hexadermical hash - std::array< uint8_t, 32 > hash_byte_arr; - for ( size_t i = 0; i < 32; i++ ) { - hash_byte_arr.at( i ) = rand_gen() % 256; - } - - return hash_byte_arr; -} - std::string rand32HexStr() { std::array< char, 16 > s = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; From 502b5c32d472dda8997350c3e622849303328c93 Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Thu, 19 Jan 2023 16:01:44 +0000 Subject: [PATCH 16/16] remove extra code --- test/test_bls.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/test_bls.cpp b/test/test_bls.cpp index 71e73214..325cb610 100644 --- a/test/test_bls.cpp +++ b/test/test_bls.cpp @@ -757,7 +757,7 @@ BOOST_AUTO_TEST_CASE( BLSAGGREGATEDVERIFICATIONONLY ) { BOOST_REQUIRE( common_pkey.AggregatedVerifySig( hash_ptrs, common_sig_ptrs ) ); } } - std::cerr << "BLS AGGREGATED VERIFICATION TEST FINISHED" << std::endl; + std::cerr << "BLS AGGREGATED VERIFICATION ONLY TEST FINISHED" << std::endl; } BOOST_AUTO_TEST_CASE( BLSAGGREGATEDSIGNATURESSCHEME ) { @@ -819,8 +819,6 @@ BOOST_AUTO_TEST_CASE( BLSAGGREGATEDSIGNATURESSCHEME ) { } BOOST_AUTO_TEST_CASE( BLSAGGREGATEDPOPPROVEVERIFY ) { - std::cout << "Testing blsAggregatedSignaturesPopProveVerify\n"; - libBLS::ThresholdUtils::initCurve(); auto key_pair = libBLS::Bls::KeyGeneration();