diff --git a/AgentDocs/Architecture.md b/AgentDocs/Architecture.md index a86037ba9..b3e7bd044 100644 --- a/AgentDocs/Architecture.md +++ b/AgentDocs/Architecture.md @@ -66,7 +66,7 @@ SuperGenius/ │ ├── blockchain/ # BlockTree, BlockStorage, BlockHeaderRepository │ ├── coinprices/ # CoinGecko price retriever │ ├── crdt/ # CRDT datastore, GlobalDB, PubSub broadcaster -│ ├── crypto/ # ED25519, SR25519, Secp256k1, BIP39, VRF, Hasher +│ ├── crypto/ # ED25519, SR25519, Secp256k1, BIP39, VRF, hash helpers │ ├── local_secure_storage/ # Platform-specific encrypted key storage │ ├── macro/ # Utility macros │ ├── outcome/ # outcome::result adapter @@ -497,8 +497,7 @@ TransferProofProto { BaseProofData proof_data; TransferProofPublicInputs public_ | `VRFProviderImpl` | class | Concrete VRF (Ristretto-VRF). | | `VRFOutput` | struct | VRF output bytes + proof bytes. | | `VRFVerifyOutput` | struct | VRF verification result + output. | -| `Hasher` | interface | Multi-algorithm hash (blake2b-256/512, keccak, sha2, twox). | -| `HasherImpl` | class | Concrete hasher. | +| Hash helpers | free functions | Multi-algorithm hashing (blake2b, keccak, sha2, twox). | | `CryptoStore` | interface | Key store: list, generate, get keypairs by key type. | | `CryptoStoreImpl` | class | Concrete in-memory + file-backed key store. | | `Bip39Provider` | interface | BIP39 mnemonic generation and seed derivation. | diff --git a/src/account/AccountMessenger.cpp b/src/account/AccountMessenger.cpp index c8aeeab3a..4427bf9d7 100644 --- a/src/account/AccountMessenger.cpp +++ b/src/account/AccountMessenger.cpp @@ -12,7 +12,7 @@ #include #include "AccountMessenger.hpp" #include "base/sgns_version.hpp" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include "primitives/cid/cid.hpp" OUTCOME_CPP_DEFINE_CATEGORY_3( sgns, AccountMessenger::Error, e ) @@ -1023,8 +1023,7 @@ namespace sgns uint64_t random_value = gen(); std::string to_hash = address_ + std::to_string( random_value ); - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( to_hash.data(), to_hash.size() ); + auto hash = sgns::crypto::sha2_256( to_hash.data(), to_hash.size() ); uint64_t req_id = 0; std::memcpy( &req_id, hash.data(), sizeof( req_id ) ); @@ -1139,8 +1138,7 @@ namespace sgns uint64_t random_value = gen(); std::string to_hash = address_ + std::to_string( random_value ); - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( to_hash.data(), to_hash.size() ); + auto hash = sgns::crypto::sha2_256( to_hash.data(), to_hash.size() ); uint64_t req_id = 0; std::memcpy( &req_id, hash.data(), sizeof( req_id ) ); @@ -1266,8 +1264,7 @@ namespace sgns uint64_t random_value = gen(); std::string to_hash = address_ + std::to_string( random_value ); - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( to_hash.data(), to_hash.size() ); + auto hash = sgns::crypto::sha2_256( to_hash.data(), to_hash.size() ); uint64_t req_id = 0; std::memcpy( &req_id, hash.data(), sizeof( req_id ) ); diff --git a/src/account/EscrowTransaction.cpp b/src/account/EscrowTransaction.cpp index a06ec0ed0..ce3ba56d7 100644 --- a/src/account/EscrowTransaction.cpp +++ b/src/account/EscrowTransaction.cpp @@ -8,7 +8,6 @@ #include -#include "crypto/hasher/hasher_impl.hpp" #include "base/blob.hpp" namespace sgns diff --git a/src/account/GeniusNode.cpp b/src/account/GeniusNode.cpp index 496b61ecb..1ff68a506 100644 --- a/src/account/GeniusNode.cpp +++ b/src/account/GeniusNode.cpp @@ -641,7 +641,6 @@ namespace sgns transaction_manager_ = TransactionManager::New( tx_globaldb_, io_, account_, - std::make_shared(), blockchain_, is_full_node_, subnet_id_ ); diff --git a/src/account/GeniusNode.hpp b/src/account/GeniusNode.hpp index 01afe4226..088f8108b 100644 --- a/src/account/GeniusNode.hpp +++ b/src/account/GeniusNode.hpp @@ -32,7 +32,6 @@ #include "account/ChainRpcEndpointProvider.hpp" #include "eth/eth_watch_service.hpp" #include -#include "crypto/hasher/hasher_impl.hpp" #include "processing/impl/processing_core_impl.hpp" #include "processing/impl/processing_subtask_result_storage_impl.hpp" #include "processing/processing_service.hpp" diff --git a/src/account/GeniusTransaction.cpp b/src/account/GeniusTransaction.cpp index ff65818c4..f3164349d 100644 --- a/src/account/GeniusTransaction.cpp +++ b/src/account/GeniusTransaction.cpp @@ -1,6 +1,6 @@ #include "GeniusTransaction.hpp" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" namespace sgns { @@ -37,8 +37,7 @@ namespace sgns dag_st.clear_signature(); dag_st.clear_data_hash(); - auto hasher_ = std::make_shared(); - auto hash = hasher_->blake2b_256( SerializeByteVector() ); + auto hash = crypto::blake2b_256( SerializeByteVector() ); dag_st.set_data_hash( hash.toReadableString() ); dag_st.set_signature( std::move( signature ) ); @@ -52,8 +51,7 @@ namespace sgns dag_copy.clear_signature(); dag_copy.clear_data_hash(); - auto hasher_ = std::make_shared(); - auto calculated_hash = hasher_->blake2b_256( SerializeByteVector( dag_copy ) ); + auto calculated_hash = crypto::blake2b_256( SerializeByteVector( dag_copy ) ); return hash == calculated_hash.toReadableString(); } diff --git a/src/account/Migration3_6_0To3_7_0.cpp b/src/account/Migration3_6_0To3_7_0.cpp index 91ba652d5..ea7a1a262 100644 --- a/src/account/Migration3_6_0To3_7_0.cpp +++ b/src/account/Migration3_6_0To3_7_0.cpp @@ -7,7 +7,6 @@ #include "account/proto/SGTransaction.pb.h" #include "base/sgns_version.hpp" #include "blockchain/Blockchain.hpp" -#include "crypto/hasher/hasher_impl.hpp" #include "storage/database_error.hpp" #include @@ -288,7 +287,6 @@ namespace sgns transaction_manager_ = TransactionManager::New( db_3_7_0_, ioContext_, account_, - std::make_shared(), blockchain_, is_full_node_ ); } diff --git a/src/account/MigrationTransaction.cpp b/src/account/MigrationTransaction.cpp index 8d6db4c69..36f792bf9 100644 --- a/src/account/MigrationTransaction.cpp +++ b/src/account/MigrationTransaction.cpp @@ -8,7 +8,7 @@ #include #include "base/blob.hpp" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" namespace sgns { @@ -201,8 +201,7 @@ namespace sgns const TokenID &token_id ) { const auto payload = fmt::format( "migration:{}:{}:{}", from_version, source_address, token_id.ToHex() ); - auto hasher = std::make_shared(); - return hasher->blake2b_256( std::vector( payload.begin(), payload.end() ) ).toReadableString(); + return crypto::blake2b_256( std::vector( payload.begin(), payload.end() ) ).toReadableString(); } MigrationTransaction MigrationTransaction::New( uint64_t amount, diff --git a/src/account/MintTransactionV2.cpp b/src/account/MintTransactionV2.cpp index 1294435fe..db10aa6ef 100644 --- a/src/account/MintTransactionV2.cpp +++ b/src/account/MintTransactionV2.cpp @@ -6,7 +6,6 @@ #include "account/MintTransactionV2.hpp" #include "base/blob.hpp" -#include "crypto/hasher/hasher_impl.hpp" namespace sgns { diff --git a/src/account/ProcessingTransaction.cpp b/src/account/ProcessingTransaction.cpp index ef0a0babd..403f45a48 100644 --- a/src/account/ProcessingTransaction.cpp +++ b/src/account/ProcessingTransaction.cpp @@ -8,7 +8,7 @@ #include "account/ProcessingTransaction.hpp" #include -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" namespace sgns { @@ -22,10 +22,9 @@ namespace sgns subtask_ids_( std::move( subtask_ids ) ), node_addresses_( std::move( node_addresses ) ) { - auto hasher_ = std::make_shared(); - auto hash_data = hasher_->blake2b_256( SerializeByteVector() ); + auto hash_data = crypto::blake2b_256( SerializeByteVector() ); dag_st.set_data_hash( hash_data.toReadableString() ); - hash_data = hasher_->blake2b_256( std::vector{ job_id.begin(), job_id.end() } ); + hash_data = crypto::blake2b_256( std::vector{ job_id.begin(), job_id.end() } ); job_hash_ = uint256_t{ "0x" + hash_data.toReadableString() }; } diff --git a/src/account/TransactionManager.cpp b/src/account/TransactionManager.cpp index 4b7823a17..2c03ea8e3 100644 --- a/src/account/TransactionManager.cpp +++ b/src/account/TransactionManager.cpp @@ -28,6 +28,7 @@ #include "account/proto/SGTransaction.pb.h" #include "crdt/proto/delta.pb.h" #include "base/sgns_version.hpp" +#include "crypto/hasher.hpp" #include "outcome/outcome.hpp" #include "proof/ProcessingProof.hpp" @@ -107,7 +108,6 @@ namespace sgns std::shared_ptr TransactionManager::New( std::shared_ptr processing_db, std::shared_ptr ctx, std::shared_ptr account, - std::shared_ptr hasher, std::shared_ptr blockchain, bool full_node, uint16_t subnet_id, @@ -117,7 +117,6 @@ namespace sgns auto instance = std::shared_ptr( new TransactionManager( std::move( processing_db ), std::move( ctx ), std::move( account ), - std::move( hasher ), std::move( blockchain ), full_node, subnet_id, @@ -251,7 +250,6 @@ namespace sgns TransactionManager::TransactionManager( std::shared_ptr processing_db, std::shared_ptr ctx, std::shared_ptr account, - std::shared_ptr hasher, std::shared_ptr blockchain, bool full_node, uint16_t subnet_id, @@ -260,7 +258,6 @@ namespace sgns globaldb_m( std::move( processing_db ) ), ctx_m( std::move( ctx ) ), account_m( std::move( account ) ), - hasher_m( std::move( hasher ) ), blockchain_( std::move( blockchain ) ), full_node_m( full_node ), subnet_id_( subnet_id ), @@ -784,7 +781,7 @@ namespace sgns { return outcome::failure( boost::system::error_code{} ); } - auto hash_data = hasher_m->blake2b_256( std::vector{ job_id.begin(), job_id.end() } ); + auto hash_data = crypto::blake2b_256( std::vector{ job_id.begin(), job_id.end() } ); const std::string lock_id = "0x" + hash_data.toReadableString(); BOOST_OUTCOME_TRY( @@ -3874,7 +3871,7 @@ namespace sgns const uint64_t registry_epoch = validator_registry->GetRegistryEpoch(); const auto registry_cid = validator_registry->GetRegistryCid(); - auto registry_hash = hasher_m->sha2_256( registry_cid.data(), registry_cid.size() ); + auto registry_hash = crypto::sha2_256( registry_cid.data(), registry_cid.size() ); if ( auto checkpoint_res = account_m->GetUTXOManager().CreateCheckpoint( registry_epoch, tx_hash_bin.value(), diff --git a/src/account/TransactionManager.hpp b/src/account/TransactionManager.hpp index 6c9ccfe8c..94a7fa701 100644 --- a/src/account/TransactionManager.hpp +++ b/src/account/TransactionManager.hpp @@ -27,7 +27,6 @@ #include "account/PublicChainInputValidator.hpp" #include "base/logger.hpp" #include "base/buffer.hpp" -#include "crypto/hasher.hpp" #include "blockchain/Blockchain.hpp" #include "processing/proto/SGProcessing.pb.h" @@ -85,7 +84,6 @@ namespace sgns * @param[in] processing_db Database of the CRDT * @param[in] ctx The io context used to run its inner methods * @param[in] account Genius account to be used - * @param[in] hasher Hasher to be used * @param[in] full_node Parameter to indicate if the account is a full node * @param[in] timestamp_tolerance Time to analyze a transaction with the same nonce/key * @param[in] mutability_window Window of time where a transaction can be modified @@ -98,7 +96,6 @@ namespace sgns std::shared_ptr processing_db, std::shared_ptr ctx, std::shared_ptr account, - std::shared_ptr hasher, std::shared_ptr blockchain, bool full_node = false, uint16_t subnet_id = 0, @@ -317,7 +314,6 @@ namespace sgns TransactionManager( std::shared_ptr processing_db, std::shared_ptr ctx, std::shared_ptr account, - std::shared_ptr hasher, std::shared_ptr blockchain, bool full_node, std::chrono::milliseconds timestamp_tolerance, @@ -326,7 +322,6 @@ namespace sgns TransactionManager( std::shared_ptr processing_db, std::shared_ptr ctx, std::shared_ptr account, - std::shared_ptr hasher, std::shared_ptr blockchain, bool full_node, uint16_t subnet_id, @@ -495,7 +490,6 @@ namespace sgns std::shared_ptr ctx_m; std::shared_ptr account_m; - std::shared_ptr hasher_m; std::shared_ptr blockchain_; bool full_node_m; uint16_t subnet_id_ = 0; ///< Subnet ID from config (reserved). diff --git a/src/blockchain/Consensus.cpp b/src/blockchain/Consensus.cpp index f4fe65543..b70da0fdb 100644 --- a/src/blockchain/Consensus.cpp +++ b/src/blockchain/Consensus.cpp @@ -16,7 +16,7 @@ #include "base/hexutil.hpp" #include "base/sgns_version.hpp" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include "account/GeniusAccount.hpp" #include "blockchain/ConsensusAuth.hpp" @@ -525,8 +525,7 @@ namespace sgns return AggregatorRole::NotInRegistry; } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( proposal.proposal_id().data(), proposal.proposal_id().size() ); + auto hash = sgns::crypto::sha2_256( proposal.proposal_id().data(), proposal.proposal_id().size() ); uint64_t base_index = 0; for ( size_t i = 0; i < sizeof( uint64_t ) && i < hash.size(); ++i ) { @@ -2585,8 +2584,7 @@ namespace sgns return outcome::failure( std::errc::invalid_argument ); } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( serialized.data(), serialized.size() ); + auto hash = sgns::crypto::sha2_256( serialized.data(), serialized.size() ); ConsensusManagerLogger()->debug( "{}: success", __func__ ); return base::hex_lower( gsl::span( hash.data(), hash.size() ) ); } @@ -2601,8 +2599,7 @@ namespace sgns { return outcome::failure( std::errc::invalid_argument ); } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( payload.data(), payload.size() ); + auto hash = sgns::crypto::sha2_256( payload.data(), payload.size() ); return std::string( reinterpret_cast( hash.data() ), hash.size() ); } @@ -2652,8 +2649,7 @@ namespace sgns return outcome::failure( std::errc::invalid_argument ); } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( subject_type.data(), subject_type.size() ); + auto hash = sgns::crypto::sha2_256( subject_type.data(), subject_type.size() ); return std::string( reinterpret_cast( hash.data() ), hash.size() ); } @@ -2852,8 +2848,7 @@ namespace sgns return {}; } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( signing_bytes.value().data(), signing_bytes.value().size() ); + auto hash = sgns::crypto::sha2_256( signing_bytes.value().data(), signing_bytes.value().size() ); auto proposal_id = base::hex_lower( gsl::span( hash.data(), hash.size() ) ); ConsensusManagerLogger()->debug( "{}: Proposal ID {} created", __func__, proposal_id.substr( 0, 8 ) ); return proposal_id; diff --git a/src/blockchain/ConsensusAuth.hpp b/src/blockchain/ConsensusAuth.hpp index 9824a0ad5..e75887f13 100644 --- a/src/blockchain/ConsensusAuth.hpp +++ b/src/blockchain/ConsensusAuth.hpp @@ -13,7 +13,7 @@ #include "account/GeniusAccount.hpp" #include "base/hexutil.hpp" #include "blockchain/impl/proto/Consensus.pb.h" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include #include "outcome/outcome.hpp" @@ -105,8 +105,7 @@ namespace sgns return outcome::failure( signing_bytes.error() ); } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( signing_bytes.value().data(), signing_bytes.value().size() ); + auto hash = sgns::crypto::sha2_256( signing_bytes.value().data(), signing_bytes.value().size() ); return base::hex_lower( gsl::span( hash.data(), hash.size() ) ); } diff --git a/src/blockchain/ValidatorRegistry.cpp b/src/blockchain/ValidatorRegistry.cpp index a182a0d02..c59fa8437 100644 --- a/src/blockchain/ValidatorRegistry.cpp +++ b/src/blockchain/ValidatorRegistry.cpp @@ -23,7 +23,7 @@ #include "blockchain/Consensus.hpp" #include "blockchain/ConsensusAuth.hpp" #include "blockchain/impl/proto/ValidatorRegistry.pb.h" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include "crdt/graphsync_dagsyncer.hpp" namespace sgns @@ -705,8 +705,7 @@ namespace sgns payload.push_back( '\n' ); payload += subject_hashes[i]; } - sgns::crypto::HasherImpl hasher; - auto hash = hasher.sha2_256( payload.data(), payload.size() ); + auto hash = sgns::crypto::sha2_256( payload.data(), payload.size() ); return base::hex_lower( gsl::span( hash.data(), hash.size() ) ); } diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index 209d319a5..fce1943a6 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -1,5 +1,5 @@ add_library(hasher - hasher/hasher_impl.cpp + hasher.cpp ) target_link_libraries(hasher PRIVATE diff --git a/src/crypto/hasher.cpp b/src/crypto/hasher.cpp new file mode 100644 index 000000000..897fa1dd9 --- /dev/null +++ b/src/crypto/hasher.cpp @@ -0,0 +1,59 @@ +#include "crypto/hasher.hpp" + +#include +#include + +#include "crypto/keccak/keccak.h" +#include "crypto/sha/sha256.hpp" +#include "crypto/twox/twox.hpp" + +namespace sgns::crypto +{ + base::Hash64 twox_64( gsl::span buffer ) + { + return make_twox64( buffer ); + } + + base::Hash128 twox_128( gsl::span buffer ) + { + return make_twox128( buffer ); + } + + base::Hash128 blake2b_128( gsl::span buffer ) + { + base::Hash128 out; + sgns_blake2b( out.data(), out.size(), nullptr, 0, buffer.data(), buffer.size() ); + return out; + } + + base::Hash256 twox_256( gsl::span buffer ) + { + return make_twox256( buffer ); + } + + base::Hash256 blake2b_256( gsl::span buffer ) + { + base::Hash256 out; + sgns_blake2b( out.data(), out.size(), nullptr, 0, buffer.data(), buffer.size() ); + return out; + } + + base::Hash256 keccak_256( gsl::span buffer ) + { + base::Hash256 out; + sha3_HashBuffer( 256, SHA3_FLAGS::SHA3_FLAGS_KECCAK, buffer.data(), buffer.size(), out.data(), out.size() ); + return out; + } + + base::Hash256 blake2s_256( gsl::span buffer ) + { + base::Hash256 out; + blake2s( out.data(), out.size(), nullptr, 0, buffer.data(), buffer.size() ); + return out; + } + + base::Hash256 sha2_256( gsl::span buffer ) + { + return sha256( buffer ); + } +} // namespace sgns::crypto diff --git a/src/crypto/hasher.hpp b/src/crypto/hasher.hpp index 9ce98e7d3..8bf65cb98 100644 --- a/src/crypto/hasher.hpp +++ b/src/crypto/hasher.hpp @@ -6,89 +6,22 @@ #include #include "base/blob.hpp" -#include "singleton/IComponent.hpp" -namespace sgns::crypto { - class Hasher : public IComponent { - protected: - using Hash64 = base::Hash64; - using Hash128 = base::Hash128; - using Hash256 = base::Hash256; - - public: - ~Hasher() override = default; - - /** - * @brief twox_128 calculates 16-byte twox hash - * @param buffer source buffer - * @return 128-bit hash value - */ - //------------ by ruymaster ----// - [[nodiscard]] virtual Hash64 twox_64( gsl::span buffer ) const = 0; - - /** - * @brief twox_128 calculates 16-byte twox hash - * @param buffer source buffer - * @return 128-bit hash value - */ - //------------ by ruymaster ----// - [[nodiscard]] virtual Hash128 twox_128( gsl::span buffer ) const = 0; - - /** - * @brief blake2b_128 function calculates 16-byte blake2b hash - * @param buffer source value - * @return 128-bit hash value - */ - [[nodiscard]] virtual Hash128 blake2b_128( gsl::span buffer ) const = 0; - - /** - * @brief twox_256 calculates 32-byte twox hash - * @param buffer source buffer - * @return 256-bit hash value - */ - //--------------------- - [[nodiscard]] virtual Hash256 twox_256( gsl::span buffer ) const = 0; - - /** - * @brief blake2b_256 function calculates 32-byte blake2b hash - * @param buffer source value - * @return 256-bit hash value - */ - [[nodiscard]] virtual Hash256 blake2b_256( gsl::span buffer ) const = 0; - - /** - * @brief keccak_256 function calculates 32-byte keccak hash - * @param buffer source value - * @return 256-bit hash value - */ - [[nodiscard]] virtual Hash256 keccak_256( gsl::span buffer ) const = 0; - - /** - * @brief blake2s_256 function calculates 32-byte blake2s hash - * @param buffer source value - * @return 256-bit hash value - */ - [[nodiscard]] virtual Hash256 blake2s_256( gsl::span buffer ) const = 0; - - /** - * @brief sha2_256 function calculates 32-byte sha2-256 hash - * @param buffer source value - * @return 256-bit hash value - */ - [[nodiscard]] virtual Hash256 sha2_256( gsl::span buffer ) const = 0; - - /** - * @brief sha2_256 helper for raw memory buffers - * @param data pointer to source bytes - * @param size source byte size - * @return 256-bit hash value - */ - [[nodiscard]] Hash256 sha2_256( const void *data, size_t size ) const - { - return sha2_256( - gsl::span( reinterpret_cast( data ), size ) ); - } - }; +namespace sgns::crypto +{ + [[nodiscard]] base::Hash64 twox_64( gsl::span buffer ); + [[nodiscard]] base::Hash128 twox_128( gsl::span buffer ); + [[nodiscard]] base::Hash128 blake2b_128( gsl::span buffer ); + [[nodiscard]] base::Hash256 twox_256( gsl::span buffer ); + [[nodiscard]] base::Hash256 blake2b_256( gsl::span buffer ); + [[nodiscard]] base::Hash256 keccak_256( gsl::span buffer ); + [[nodiscard]] base::Hash256 blake2s_256( gsl::span buffer ); + [[nodiscard]] base::Hash256 sha2_256( gsl::span buffer ); + + [[nodiscard]] inline base::Hash256 sha2_256( const void *data, size_t size ) + { + return sha2_256( gsl::span( reinterpret_cast( data ), size ) ); + } } #endif diff --git a/src/crypto/hasher/hasher_impl.cpp b/src/crypto/hasher/hasher_impl.cpp deleted file mode 100644 index 1e15a3200..000000000 --- a/src/crypto/hasher/hasher_impl.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "crypto/hasher/hasher_impl.hpp" - -#include - -#include -#include -#include "crypto/keccak/keccak.h" -#include "crypto/sha/sha256.hpp" -//------------------- -#include "crypto/twox/twox.hpp" - -namespace sgns::crypto { - using base::Hash128; - using base::Hash256; - using base::Hash64; - -//----------------------------- - Hash64 HasherImpl::twox_64(gsl::span buffer) const { - return make_twox64(buffer); - } - - Hash128 HasherImpl::twox_128(gsl::span buffer) const { - return make_twox128(buffer); - } - - Hash128 HasherImpl::blake2b_128(gsl::span buffer) const { - Hash128 out; - sgns_blake2b(out.data(), 16, nullptr, 0, buffer.data(), buffer.size()); - return out; - } - -//---------------------------- - Hash256 HasherImpl::twox_256(gsl::span buffer) const { - return make_twox256(buffer); - } - - Hash256 HasherImpl::blake2b_256(gsl::span buffer) const { - Hash256 out; - sgns_blake2b(out.data(), 32, nullptr, 0, buffer.data(), buffer.size()); - return out; - } - - Hash256 HasherImpl::keccak_256(gsl::span buffer) const { - Hash256 out; - sha3_HashBuffer(256, - SHA3_FLAGS::SHA3_FLAGS_KECCAK, - buffer.data(), - buffer.size(), - out.data(), - 32); - return out; - } - - Hash256 HasherImpl::blake2s_256(gsl::span buffer) const { - Hash256 out; - blake2s(out.data(), 32, nullptr, 0, buffer.data(), buffer.size()); - return out; - } - - Hash256 HasherImpl::sha2_256(gsl::span buffer) const { - return crypto::sha256(buffer); - } -} // namespace sgns::crypto diff --git a/src/crypto/hasher/hasher_impl.hpp b/src/crypto/hasher/hasher_impl.hpp deleted file mode 100644 index 02f25391c..000000000 --- a/src/crypto/hasher/hasher_impl.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef SUPERGENIUS_SRC_CRYPTO_HASHER_HASHER_IMPL_HPP_ -#define SUPERGENIUS_SRC_CRYPTO_HASHER_HASHER_IMPL_HPP_ - -#include "crypto/hasher.hpp" - -namespace sgns::crypto -{ - class HasherImpl : public Hasher - { - public: - using Hasher::sha2_256; - - ~HasherImpl() override = default; - - [[nodiscard]] Hash64 twox_64( gsl::span buffer ) const override; - - [[nodiscard]] Hash128 twox_128( gsl::span buffer ) const override; - - [[nodiscard]] Hash128 blake2b_128( gsl::span buffer ) const override; - - [[nodiscard]] Hash256 twox_256( gsl::span buffer ) const override; - - [[nodiscard]] Hash256 blake2b_256( gsl::span buffer ) const override; - - [[nodiscard]] Hash256 keccak_256( gsl::span buffer ) const override; - - [[nodiscard]] Hash256 blake2s_256( gsl::span buffer ) const override; - - [[nodiscard]] Hash256 sha2_256( gsl::span buffer ) const override; - - std::string GetName() override - { - return "HasherImpl"; - } - }; -} - -#endif diff --git a/src/local_secure_storage/impl/Linux.cpp b/src/local_secure_storage/impl/Linux.cpp index 4f109d04a..b06c08010 100644 --- a/src/local_secure_storage/impl/Linux.cpp +++ b/src/local_secure_storage/impl/Linux.cpp @@ -1,7 +1,5 @@ #include "Linux.hpp" -#include "crypto/hasher/hasher_impl.hpp" - #include #include diff --git a/test/src/account/transaction_manager_certificate_fallback_test.cpp b/test/src/account/transaction_manager_certificate_fallback_test.cpp index fcfe04708..1f7d992c6 100644 --- a/test/src/account/transaction_manager_certificate_fallback_test.cpp +++ b/test/src/account/transaction_manager_certificate_fallback_test.cpp @@ -21,7 +21,7 @@ #include "blockchain/Consensus.hpp" #include "blockchain/impl/proto/Consensus.pb.h" #include "account/proto/SGTransaction.pb.h" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include #include "testutil/storage/base_crdt_test.hpp" @@ -133,8 +133,7 @@ namespace dag_copy.clear_signature(); dag_copy.clear_data_hash(); auto serialized_bytes = tx_obj->SerializeByteVector( dag_copy ); - sgns::crypto::HasherImpl hasher; - auto hash = hasher.blake2b_256( + auto hash = sgns::crypto::blake2b_256( gsl::span( serialized_bytes.data(), serialized_bytes.size() ) ); // Step 4: Rebuild the TransferTx proto with the correct data_hash @@ -212,9 +211,9 @@ class CertificateFallbackTest : public test::CRDTFixture tm_ = TransactionManager::New( db_, io_, account_, - std::make_shared(), blockchain_, false, // full_node + 0, // subnet_id kTimestampTolerance, kMutabilityWindow ); assert( tm_ != nullptr ); diff --git a/test/src/account/utxo_manager_test.cpp b/test/src/account/utxo_manager_test.cpp index 932251e0c..1efd98f35 100644 --- a/test/src/account/utxo_manager_test.cpp +++ b/test/src/account/utxo_manager_test.cpp @@ -6,7 +6,7 @@ #include "account/UTXOManager.hpp" #include "account/GeniusUTXO.hpp" #include "account/TokenID.hpp" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include "testutil/storage/base_rocksdb_test.hpp" using namespace sgns; @@ -16,7 +16,6 @@ using namespace sgns::base; static constexpr std::string_view PRIV_KEY = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; static const Hash256 DUMMY_HASH{}; static const TokenID TOKEN_1 = TokenID::FromBytes( { 0x01 } ); -static crypto::HasherImpl HASHER; std::vector BalanceUTXOs() { @@ -40,12 +39,12 @@ class UTXOManagerTest : public test::RocksDBFixture std::string( PRIV_KEY ), []( const std::vector &data ) { - auto hashed = HASHER.sha2_256( data ); + auto hashed = crypto::sha2_256( data ); return std::vector( hashed.begin(), hashed.end() ); }, []( const std::string &_, const std::vector &signature, const std::vector &data ) { - auto hashed = HASHER.sha2_256( data ); + auto hashed = crypto::sha2_256( data ); return signature == std::vector( hashed.begin(), hashed.end() ); } ); @@ -159,7 +158,7 @@ TEST_F( UTXOManagerTest, RefreshAllUTXOsRemovesAll ) TEST_F( UTXOManagerTest, VerifyParameters ) { - ASSERT_TRUE( utxo_manager->SetUTXOs( { GeniusUTXO( HASHER.sha2_256( {} ), 0, 420, TOKEN_1 ) } ).has_value() ); + ASSERT_TRUE( utxo_manager->SetUTXOs( { GeniusUTXO( crypto::sha2_256( {} ), 0, 420, TOKEN_1 ) } ).has_value() ); auto tx = utxo_manager->CreateTxParameter( 69, "foobar", TOKEN_1 ); EXPECT_TRUE( tx.has_value() ); EXPECT_TRUE( utxo_manager->VerifyParameters( tx.value() ) ); @@ -195,8 +194,8 @@ TEST_F( UTXOManagerTest, MerkleRootDeterministicAcrossInsertionOrder ) { const std::array seed_a{ 0xA1 }; const std::array seed_b{ 0xB2 }; - const auto hash_a = HASHER.sha2_256( gsl::span( seed_a ) ); - const auto hash_b = HASHER.sha2_256( gsl::span( seed_b ) ); + const auto hash_a = crypto::sha2_256( gsl::span( seed_a ) ); + const auto hash_b = crypto::sha2_256( gsl::span( seed_b ) ); std::vector ordered_a{ GeniusUTXO( hash_a, 0, 100, TOKEN_1 ), @@ -221,8 +220,8 @@ TEST_F( UTXOManagerTest, MerkleRootChangesWhenUTXOSetChanges ) { const std::array seed_a{ 0xC3 }; const std::array seed_b{ 0xD4 }; - const auto hash_a = HASHER.sha2_256( gsl::span( seed_a ) ); - const auto hash_b = HASHER.sha2_256( gsl::span( seed_b ) ); + const auto hash_a = crypto::sha2_256( gsl::span( seed_a ) ); + const auto hash_b = crypto::sha2_256( gsl::span( seed_b ) ); ASSERT_TRUE( utxo_manager ->SetUTXOs( { @@ -246,8 +245,8 @@ TEST_F( UTXOManagerTest, CheckpointRoundtrip ) { const std::array seed_tx{ 0x11 }; const std::array seed_registry{ 0x22 }; - const auto tx_hash = HASHER.sha2_256( gsl::span( seed_tx ) ); - const auto registry_hash = HASHER.sha2_256( gsl::span( seed_registry ) ); + const auto tx_hash = crypto::sha2_256( gsl::span( seed_tx ) ); + const auto registry_hash = crypto::sha2_256( gsl::span( seed_registry ) ); ASSERT_TRUE( utxo_manager->SetUTXOs( { GeniusUTXO( tx_hash, 0, 123, TOKEN_1 ) } ).has_value() ); diff --git a/test/src/blockchain/consensus_subject_test.cpp b/test/src/blockchain/consensus_subject_test.cpp index b53c3d0b2..14a9d392b 100644 --- a/test/src/blockchain/consensus_subject_test.cpp +++ b/test/src/blockchain/consensus_subject_test.cpp @@ -4,7 +4,7 @@ #include "blockchain/Consensus.hpp" #include "account/proto/SGTransaction.pb.h" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" namespace sgns { @@ -56,8 +56,7 @@ namespace void RefreshPayloadHash( sgns::ConsensusSubject &subject ) { - sgns::crypto::HasherImpl hasher; - auto payload_hash = hasher.sha2_256( + auto payload_hash = sgns::crypto::sha2_256( gsl::span( reinterpret_cast( subject.payload().data() ), subject.payload().size() ) ); @@ -311,10 +310,8 @@ TEST( ConsensusSubjectTest, Sanitization_Blake2bHashOfKnownDataMatches ) { // Given: Known input bytes and their expected blake2b_256 hash const std::vector input = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; - sgns::crypto::HasherImpl hasher; - // When: Computing blake2b_256 of the known bytes - const auto hash = hasher.blake2b_256( + const auto hash = sgns::crypto::blake2b_256( gsl::span( input.data(), input.size() ) ); // Then: Hash has correct size (32 bytes) @@ -373,8 +370,7 @@ TEST( ConsensusSubjectTest, Sanitization_HashMismatch_RejectsBeforeParse ) { // Given: An embedded transaction and a mismatched tx_hash (simulating tampering) const auto embedded = MakeTestEmbeddedTransfer(); - sgns::crypto::HasherImpl hasher; - const auto hash_of_different_data = hasher.blake2b_256( + const auto hash_of_different_data = sgns::crypto::blake2b_256( gsl::span( reinterpret_cast( "wrong data" ), 10 ) ); diff --git a/test/src/crypto/hasher/hasher_test.cpp b/test/src/crypto/hasher/hasher_test.cpp index bd07f9f7e..e845cc100 100644 --- a/test/src/crypto/hasher/hasher_test.cpp +++ b/test/src/crypto/hasher/hasher_test.cpp @@ -1,22 +1,16 @@ #include #include "base/blob.hpp" -#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/hasher.hpp" #include "testutil/literals.hpp" using sgns::base::Buffer; /** - * Hasher fixture + * Hash helper fixture */ class HasherFixture : public testing::Test { public: - ~HasherFixture() override = default; - - HasherFixture() { - hasher = std::make_shared(); - } - /** * useful function for convenience */ @@ -33,17 +27,15 @@ class HasherFixture : public testing::Test { return out; } - protected: - std::shared_ptr hasher; }; /** * @given pre-known source value - * @when Hasher::twox_64 method is applied + * @when crypto::twox_64 is applied * @then expected result obtained */ TEST_F(HasherFixture, twox_64) { - auto hash = hasher->twox_64(Buffer().put("foo")); + auto hash = sgns::crypto::twox_64(Buffer().put("foo")); // match is output obtained from substrate sgns::base::Blob<8> match; @@ -61,11 +53,11 @@ TEST_F(HasherFixture, twox_64) { // /** // * @given some common source value -// * @when Hasher::twox_128 method is applied +// * @when crypto::twox_128 is applied // * @then expected result obtained // */ TEST_F(HasherFixture, twox_128) { - auto hash = hasher->twox_128(Buffer{"414243444546"_unhex}); + auto hash = sgns::crypto::twox_128(Buffer{"414243444546"_unhex}); std::vector match = { 184, 65, 176, 250, 243, 129, 181, 3, 77, 82, 63, 150, 129, 221, 191, 251}; ASSERT_EQ(blob2buffer<16>(hash).toVector(), match); @@ -73,13 +65,13 @@ TEST_F(HasherFixture, twox_128) { // /** // * @given some common source value -// * @when Hasher::twox_256 method is applied +// * @when crypto::twox_256 is applied // * @then expected result obtained // */ TEST_F(HasherFixture, twox_256) { // some value auto v = Buffer{0x41, 0x42, 0x43, 0x44, 0x45, 0x46}; - auto hash = hasher->twox_256(v); + auto hash = sgns::crypto::twox_256(v); std::vector match = {184, 65, 176, 250, 243, 129, 181, 3, 77, 82, 63, 150, 129, 221, 191, 251, 33, 226, 149, 136, 6, 232, 81, 118, @@ -89,11 +81,11 @@ TEST_F(HasherFixture, twox_256) { /** * @given some common source value - * @when Hasher::sha2_256 method is applied + * @when crypto::sha2_256 is applied * @then expected result obtained */ TEST_F(HasherFixture, sha2_256) { - auto hash = hasher->sha2_256(string2buffer( + auto hash = sgns::crypto::sha2_256(string2buffer( "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); std::string_view match = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"; @@ -102,7 +94,7 @@ TEST_F(HasherFixture, sha2_256) { /** * @given some common source value - * @when Hasher::blake2_256 method is applied + * @when crypto::blake2b_256 is applied * @then expected result obtained */ TEST_F(HasherFixture, blake2_256) { @@ -110,19 +102,19 @@ TEST_F(HasherFixture, blake2_256) { std::vector match = "ba67336efd6a3df3a70eeb757860763036785c182ff4cf587541a0068d09f5b2"_unhex; - auto hash = hasher->blake2b_256(buffer); + auto hash = sgns::crypto::blake2b_256(buffer); ASSERT_EQ(blob2buffer<32>(hash).toVector(), match); } /** * @given some common source value - * @when Hasher::blake2_128 method is applied + * @when crypto::blake2b_128 is applied * @then expected result obtained */ TEST_F(HasherFixture, blake2_128) { Buffer buffer{"6920616d2064617461"_unhex}; std::vector match = "de944c5c12e55ee9a07cf5bf4b674995"_unhex; - auto hash = hasher->blake2b_128(buffer); + auto hash = sgns::crypto::blake2b_128(buffer); ASSERT_EQ(blob2buffer<16>(hash).toVector(), match); }