Skip to content

Commit

Permalink
Merge pull request #161 from skalenetwork/bug/remove-namespace-crypto
Browse files Browse the repository at this point in the history
rename namespace crypto
  • Loading branch information
olehnikolaiev authored Oct 21, 2021
2 parents 237e51b + 2255f25 commit dc7eb83
Show file tree
Hide file tree
Showing 50 changed files with 500 additions and 500 deletions.
24 changes: 12 additions & 12 deletions bls/BLSPrivateKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
BLSPrivateKey::BLSPrivateKey(
const std::shared_ptr< std::string >& _key, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
crypto::ThresholdUtils::initCurve();
libBLS::ThresholdUtils::initCurve();

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

privateKey = std::make_shared< libff::alt_bn128_Fr >( _key->c_str() );
if ( *privateKey == libff::alt_bn128_Fr::zero() ) {
throw crypto::ThresholdUtils::ZeroSecretKey(
throw libBLS::ThresholdUtils::ZeroSecretKey(
"Secret key share is equal to zero or corrupt" );
}
}
Expand All @@ -51,23 +51,23 @@ BLSPrivateKey::BLSPrivateKey(
std::shared_ptr< std::vector< size_t > > koefs, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
if ( skeys == nullptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "Secret keys ptr is null" );
throw libBLS::ThresholdUtils::IncorrectInput( "Secret keys ptr is null" );
}
if ( koefs == nullptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "Signers indices ptr is null" );
throw libBLS::ThresholdUtils::IncorrectInput( "Signers indices ptr is null" );
}

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

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

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

Expand All @@ -80,10 +80,10 @@ std::shared_ptr< libff::alt_bn128_Fr > BLSPrivateKey::getPrivateKey() const {

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

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

return key_str;
}
48 changes: 24 additions & 24 deletions bls/BLSPrivateKeyShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,53 @@
BLSPrivateKeyShare::BLSPrivateKeyShare(
const std::string& _key, size_t _requiredSigners, size_t _totalSigners )
: requiredSigners( _requiredSigners ), totalSigners( _totalSigners ) {
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
crypto::ThresholdUtils::initCurve();
libBLS::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
libBLS::ThresholdUtils::initCurve();
if ( _key.empty() ) {
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" );
throw libBLS::ThresholdUtils::IncorrectInput( "Secret key share string is empty" );
}
privateKey = std::make_shared< libff::alt_bn128_Fr >( _key.c_str() );

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

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

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

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

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

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

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

libff::alt_bn128_G1 hash = crypto::ThresholdUtils::HashtoG1( hash_byte_arr );
libff::alt_bn128_G1 hash = libBLS::ThresholdUtils::HashtoG1( hash_byte_arr );

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

ss->to_affine_coordinates();

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

auto s =
Expand All @@ -88,16 +88,16 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::sign(

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

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

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

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

ss->to_affine_coordinates();

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

auto s =
Expand All @@ -119,19 +119,19 @@ std::shared_ptr< BLSSigShare > BLSPrivateKeyShare::signWithHelper(
std::shared_ptr< std::pair< std::shared_ptr< std::vector< std::shared_ptr< BLSPrivateKeyShare > > >,
std::shared_ptr< BLSPublicKey > > >
BLSPrivateKeyShare::generateSampleKeys( size_t _requiredSigners, size_t _totalSigners ) {
crypto::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );
libBLS::ThresholdUtils::checkSigners( _requiredSigners, _totalSigners );

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

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

libff::alt_bn128_Fr common_skey = pol.at( 0 );
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 ) );
std::string key_str = libBLS::ThresholdUtils::fieldElementToString( skeys.at( i ) );

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

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

if ( key_str->empty() )
throw crypto::ThresholdUtils::IncorrectInput( "Secret key share string is empty" );
throw libBLS::ThresholdUtils::IncorrectInput( "Secret key share string is empty" );
return key_str;
}
56 changes: 28 additions & 28 deletions bls/BLSPublicKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > > pkey_str_vect ) {
crypto::ThresholdUtils::initCurve();
libBLS::ThresholdUtils::initCurve();

CHECK( pkey_str_vect )

Expand All @@ -42,61 +42,61 @@ BLSPublicKey::BLSPublicKey( const std::shared_ptr< std::vector< std::string > >
libffPublicKey->Z.c1 = libff::alt_bn128_Fq::zero();

if ( libffPublicKey->is_zero() ) {
throw crypto::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key " );
throw libBLS::ThresholdUtils::IsNotWellFormed( "Zero BLS public Key " );
}

if ( !( libffPublicKey->is_well_formed() ) ) {
throw crypto::ThresholdUtils::IsNotWellFormed( "BLS public Key is corrupt" );
throw libBLS::ThresholdUtils::IsNotWellFormed( "BLS public Key is corrupt" );
}
}

BLSPublicKey::BLSPublicKey( const libff::alt_bn128_G2& pkey ) {
crypto::ThresholdUtils::initCurve();
libBLS::ThresholdUtils::initCurve();

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

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" );
throw libBLS::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
}
}

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

if ( !hash_ptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
throw libBLS::ThresholdUtils::IncorrectInput( "hash is null" );
}

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

bool res = crypto::Bls::Verification( hash_ptr, *( sign_ptr->getSig() ), *libffPublicKey );
bool res = libBLS::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 ) {
if ( !hash_ptr ) {
throw crypto::ThresholdUtils::IncorrectInput( "hash is null" );
throw libBLS::ThresholdUtils::IncorrectInput( "hash is null" );
}
if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) {
throw crypto::ThresholdUtils::IncorrectInput( "Sig share is equal to zero or corrupt" );
throw libBLS::ThresholdUtils::IncorrectInput( "Sig share is equal to zero or corrupt" );
}

std::string hint = sign_ptr->getHint();

std::pair< libff::alt_bn128_Fq, libff::alt_bn128_Fq > y_shift_x =
crypto::ThresholdUtils::ParseHint( hint );
libBLS::ThresholdUtils::ParseHint( hint );

libff::alt_bn128_Fq x = crypto::ThresholdUtils::HashToFq( hash_ptr );
libff::alt_bn128_Fq x = libBLS::ThresholdUtils::HashToFq( hash_ptr );
x = x + y_shift_x.second;

libff::alt_bn128_Fq y_sqr = y_shift_x.first ^ 2;
Expand All @@ -116,16 +116,16 @@ bool BLSPublicKey::VerifySigWithHelper( std::shared_ptr< std::array< uint8_t, 32
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();
libBLS::ThresholdUtils::initCurve();

if ( hash_ptr_vec.size() != sign_ptr_vec.size() ) {
throw crypto::ThresholdUtils::IncorrectInput(
throw libBLS::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" );
throw libBLS::ThresholdUtils::IncorrectInput( "hash is null" );
}
}

Expand All @@ -134,27 +134,27 @@ bool BLSPublicKey::AggregatedVerifySig(

for ( auto& sign_ptr : sign_ptr_vec ) {
if ( !sign_ptr || sign_ptr->getSig()->is_zero() ) {
throw crypto::ThresholdUtils::IsNotWellFormed(
throw libBLS::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 );
bool res = libBLS::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 )
: t( _requiredSigners ), n( _totalSigners ) {
crypto::ThresholdUtils::initCurve();
libBLS::ThresholdUtils::initCurve();

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

if ( !koefs_pkeys_map ) {
throw crypto::ThresholdUtils::IncorrectInput( "map is null" );
throw libBLS::ThresholdUtils::IncorrectInput( "map is null" );
}

std::vector< size_t > participatingNodes;
Expand All @@ -165,7 +165,7 @@ BLSPublicKey::BLSPublicKey(
}

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

libff::alt_bn128_G2 key = libff::alt_bn128_G2::zero();
size_t i = 0;
Expand All @@ -180,7 +180,7 @@ BLSPublicKey::BLSPublicKey(

libffPublicKey = std::make_shared< libff::alt_bn128_G2 >( key );
if ( libffPublicKey->is_zero() ) {
throw crypto::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
throw libBLS::ThresholdUtils::IsNotWellFormed( "Public Key is equal to zero or corrupt" );
}
}

Expand All @@ -189,10 +189,10 @@ std::shared_ptr< std::vector< std::string > > BLSPublicKey::toString() {

libffPublicKey->to_affine_coordinates();

pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->X.c0 ) );
pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->X.c1 ) );
pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c0 ) );
pkey_str_vect.push_back( crypto::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c1 ) );
pkey_str_vect.push_back( libBLS::ThresholdUtils::fieldElementToString( libffPublicKey->X.c0 ) );
pkey_str_vect.push_back( libBLS::ThresholdUtils::fieldElementToString( libffPublicKey->X.c1 ) );
pkey_str_vect.push_back( libBLS::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c0 ) );
pkey_str_vect.push_back( libBLS::ThresholdUtils::fieldElementToString( libffPublicKey->Y.c1 ) );

return std::make_shared< std::vector< std::string > >( pkey_str_vect );
}
Expand Down
Loading

0 comments on commit dc7eb83

Please sign in to comment.