Skip to content

Commit

Permalink
Merge pull request #153 from lxndrHss/bls-aggregated-verification
Browse files Browse the repository at this point in the history
Bls aggregated verification
  • Loading branch information
olehnikolaiev authored Aug 30, 2021
2 parents 7e865a8 + c6bee7a commit e9b70e2
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 88 deletions.
3 changes: 1 addition & 2 deletions bls/BLSPrivateKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ 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 );

for ( size_t i = 0; i < _totalSigners; ++i ) {
std::string key_str = crypto::ThresholdUtils::fieldElementToString( skeys.at( i ) );
Expand Down
75 changes: 40 additions & 35 deletions bls/BLSPublicKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@
#include <tools/utils.h>


BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > pkey_str_vect,
size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > pkey_str_vect ) {
crypto::ThresholdUtils::initCurve();

CHECK( pkey_str_vect )

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

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

libffPublicKey->X.c0 = libff::alt_bn128_Fq( pkey_str_vect->at( 0 ).c_str() );
Expand All @@ -54,44 +50,26 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > >
}
}

BLSPublicKey::BLSPublicKey(
const libff::alt_bn128_G2& pkey, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey ) {
crypto::ThresholdUtils::initCurve();

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

libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( pkey );
if ( libffPublicKey->is_zero() ) {
throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS Public Key" );
}
}

BLSPublicKey::BLSPublicKey(
const libff::alt_bn128_Fr& skey, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
BLSPublicKey::BLSPublicKey( const libff::alt_bn128_Fr& skey ) {
libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( skey * libff::alt_bn128_G2::one() );
if ( libffPublicKey->is_zero() ) {
throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
}
}

size_t BLSPublicKey::getTotalSigners() const {
return totalSigners;
}

size_t BLSPublicKey::getRequiredSigners() const {
return requiredSigners;
}

bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr,
std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) {
std::shared_ptr< BLSSignature > sign_ptr ) {
crypto::ThresholdUtils::initCurve();

std::shared_ptr< crypto::Bls > obj;
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );

if ( !hash_ptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
}
Expand All @@ -100,16 +78,12 @@ bool BLSPublicKey::VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_
throw crypto::ThresholdUtils::IsNotWellFormed( "Sig share is equal to zero or corrupt" );
}

obj = std::make_shared< crypto::Bls >( crypto::Bls( _requiredSigners, _totalSigners ) );

bool res = obj->Verification( hash_ptr, *( sign_ptr->getSig() ), *libffPublicKey );
bool res = crypto::Bls::Verification( hash_ptr, *( sign_ptr->getSig() ), *libffPublicKey );
return res;
}

bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr,
std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners ) {
std::shared_ptr< crypto::Bls > obj;
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
std::shared_ptr< BLSSignature > sign_ptr ) {
if ( !hash_ptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
}
Expand Down Expand Up @@ -139,10 +113,41 @@ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32
libff::alt_bn128_ate_reduced_pairing( hash, *libffPublicKey ) );
}

bool BLSPublicKey::AggregatedVerifySig(
std::vector< std::shared_ptr< std::array< uint8_t, 32 > > >& hash_ptr_vec,
std::vector< std::shared_ptr< BLSSignature > >& sign_ptr_vec ) {
crypto::ThresholdUtils::initCurve();

if ( hash_ptr_vec.size() != sign_ptr_vec.size() ) {
throw crypto::ThresholdUtils::IncorrectInput(
"Number of signatures and hashes do not match" );
}

for ( auto& hash_ptr : hash_ptr_vec ) {
if ( !hash_ptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
}
}

std::vector< libff::alt_bn128_G1 > libff_sig_vec;
libff_sig_vec.reserve( sign_ptr_vec.size() );

for ( auto& sign_ptr : sign_ptr_vec ) {
if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) {
throw crypto::ThresholdUtils::IsNotWellFormed(
"Sig share is equal to zero or corrupt" );
}

libff_sig_vec.push_back( *( sign_ptr->getSig() ) );
}

bool res = crypto::Bls::AggregatedVerification( hash_ptr_vec, libff_sig_vec, *libffPublicKey );
return res;
}

BLSPublicKey::BLSPublicKey(
std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > koefs_pkeys_map,
size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
size_t _requiredSigners, size_t _totalSigners ) {
crypto::ThresholdUtils::initCurve();

crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
Expand All @@ -159,7 +164,7 @@ BLSPublicKey::BLSPublicKey(
}

std::vector< libff::alt_bn128_Fr > lagrangeCoeffs =
crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, requiredSigners );
crypto::ThresholdUtils::LagrangeCoeffs( participatingNodes, _requiredSigners );

libff::alt_bn128_G2 key = libff::alt_bn128_G2::zero();
size_t i = 0;
Expand Down
21 changes: 9 additions & 12 deletions bls/BLSPublicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,24 @@ class BLSPublicKey {
private:
std::shared_ptr< libff::alt_bn128_G2 > libffPublicKey;

size_t requiredSigners;
size_t totalSigners;

public:
BLSPublicKey( const std::shared_ptr< std::vector< std::string > >, size_t _requiredSigners,
size_t _totalSigners );
BLSPublicKey( const libff::alt_bn128_Fr& skey, size_t _requiredSigners, size_t _totalSigners );
BLSPublicKey( const libff::alt_bn128_G2& skey, size_t _requiredSigners, size_t _totalSigners );
BLSPublicKey( const std::shared_ptr< std::vector< std::string > > );
BLSPublicKey( const libff::alt_bn128_Fr& skey );
BLSPublicKey( const libff::alt_bn128_G2& skey );

BLSPublicKey(
std::shared_ptr< std::map< size_t, std::shared_ptr< BLSPublicKeyShare > > > map_pkeys_koefs,
size_t _requiredSigners, size_t _totalSigners );

size_t getTotalSigners() const;
size_t getRequiredSigners() const;

bool VerifySig( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr,
std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners );
std::shared_ptr< BLSSignature > sign_ptr );

bool VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32 > > hash_ptr,
std::shared_ptr< BLSSignature > sign_ptr, size_t _requiredSigners, size_t _totalSigners );
std::shared_ptr< BLSSignature > sign_ptr );

bool AggregatedVerifySig(
std::vector< std::shared_ptr< std::array< uint8_t, 32 > > >& hash_ptr_vec,
std::vector< std::shared_ptr< BLSSignature > >& sign_ptr_vec );

std::shared_ptr< std::vector< std::string > > toString();

Expand Down
41 changes: 41 additions & 0 deletions bls/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,47 @@ bool Bls::Verification( std::shared_ptr< std::array< uint8_t, 32 > > hash_byte_a
// there are several types of pairing, it does not matter which one is chosen for verification
}

bool Bls::AggregatedVerification(
std::vector< std::shared_ptr< std::array< uint8_t, 32 > > > hash_byte_arr,
const std::vector< libff::alt_bn128_G1 > sign, const libff::alt_bn128_G2 public_key ) {
for ( auto& hash : hash_byte_arr ) {
CHECK( hash );
}

libff::inhibit_profiling_info = true;

for ( auto& sig : sign ) {
if ( !sig.is_well_formed() ) {
throw ThresholdUtils::IsNotWellFormed(
"Error, signature does not lie on the alt_bn128 curve" );
}
if ( libff::alt_bn128_modulus_r * sig != libff::alt_bn128_G1::zero() ) {
throw ThresholdUtils::IsNotWellFormed( "Error, signature is not member of G1" );
}
}

if ( !public_key.is_well_formed() ) {
throw ThresholdUtils::IsNotWellFormed( "Error, public key is invalid" );
}

if ( !ThresholdUtils::isG2( public_key ) ) {
throw ThresholdUtils::IsNotWellFormed( "Error, public key is not member of G2" );
}

libff::alt_bn128_G1 aggregated_hash = libff::alt_bn128_G1::zero();
for ( std::shared_ptr< std::array< uint8_t, 32 > >& hash : hash_byte_arr ) {
aggregated_hash = aggregated_hash + ThresholdUtils::HashtoG1( hash );
}

libff::alt_bn128_G1 aggregated_sig = libff::alt_bn128_G1::zero();
for ( libff::alt_bn128_G1 sig : sign ) {
aggregated_sig = aggregated_sig + sig;
}

return ( libff::alt_bn128_ate_reduced_pairing( aggregated_sig, libff::alt_bn128_G2::one() ) ==
libff::alt_bn128_ate_reduced_pairing( aggregated_hash, public_key ) );
}

std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > Bls::KeysRecover(
const std::vector< libff::alt_bn128_Fr >& coeffs,
const std::vector< libff::alt_bn128_Fr >& shares ) {
Expand Down
4 changes: 4 additions & 0 deletions bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class Bls {
static bool Verification( std::shared_ptr< std::array< uint8_t, 32 > >,
const libff::alt_bn128_G1 sign, const libff::alt_bn128_G2 public_key );

static bool AggregatedVerification(
std::vector< std::shared_ptr< std::array< uint8_t, 32 > > > hash_byte_arr,
const std::vector< libff::alt_bn128_G1 > sign, const libff::alt_bn128_G2 public_key );

std::pair< libff::alt_bn128_Fr, libff::alt_bn128_G2 > KeysRecover(
const std::vector< libff::alt_bn128_Fr >& coeffs,
const std::vector< libff::alt_bn128_Fr >& shares );
Expand Down
1 change: 1 addition & 0 deletions dkg/DKGTEWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ bool DKGTEWrapper::VerifyDKGShare( size_t _signerIndex, const libff::alt_bn128_F
void DKGTEWrapper::setDKGSecret( std::shared_ptr< std::vector< libff::alt_bn128_Fr > > _poly_ptr ) {
if ( _poly_ptr == nullptr )
throw crypto::ThresholdUtils::IncorrectInput( "Null polynomial ptr" );

dkg_secret_ptr->setPoly( *_poly_ptr );
}

Expand Down
Loading

0 comments on commit e9b70e2

Please sign in to comment.