Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tari_crypto hashing api support #4328

Merged
merged 2 commits into from Jul 22, 2022

Conversation

hansieodendaal
Copy link
Contributor

@hansieodendaal hansieodendaal commented Jul 21, 2022

Description

Added a hashing domain API for general use based on the new domain separated hashing API from tari_crypto; this is a proposal to implement the hashing domain API system wide. A new struct HashingDomain was added to provide a standardized way of implementing the new tari_crypto hashing api, which provides standardized domain separated hashing, with two tari project specific hash domain structs, DefaultHashDomain and MacHashDomain.

USe of HashingDomain allows differentiated domain declarations per "hash originating generation" crate level (see Suggested hashing domains below). The domain labels were chosen to complement the tari project specific hash domain label prefixes provided by DefaultHashDomain and MacHashDomain as "com.tari.tari_project.hash_domain.v1" and "com.tari.tari_project.mac_domain.v1". As an example, resulting domain labels for COMMON_HASH_DOMAIN that specifies its own label "common" will then be "com.tari.tari_project.hash_domain.v1.common" and "com.tari.tari_project.mac_domain.v1.common".

An example use case is:

        let mut hasher = common_hash_domain().hasher::<Blake256>();
        hasher.update(b"my 1st secret");
        hasher.update(b"my 2nd secret");
        let hash = hasher.finalize();

The general idea is to use the lowest level hashing domain for the crate where an original hash needs to be generated, and where no higher level hash domain is defined for a specific crate, common_hash_domain() needs to be used. Code that replays a specific hash needs to use the same hash domain where the hash originated, for example the mempool.

This standardized domain declarations should make code maintenance where domain hashing is used simpler and more efficient. The next PR will demonstrate use of tari_script_hash_domain() and mmr_hash_domain().

Suggested hashing domains (This can easily be modified as needed.)

pub fn common_hash_domain() -> HashingDomain {HashingDomain::new("common")}
pub fn comms_core_hash_domain() -> HashingDomain {HashingDomain::new("comms.core")}
pub fn comms_dht_hash_domain() -> HashingDomain {HashingDomain::new("comms.dht")}
pub fn core_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.core")}
pub fn core_blocks_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.core.blocks")}
pub fn core_consensus_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.core.consensus")}
pub fn core_covenants_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.core.covenants")}
pub fn core_proof_of_work_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.core.proof_of_work")}
pub fn core_transactions_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.core.transactions")}
pub fn dan_layer_core_hash_domain() -> HashingDomain {HashingDomain::new("dan_layer.core")}
pub fn dan_layer_engine_hash_domain() -> HashingDomain {HashingDomain::new("dan_layer.engine")}
pub fn dan_layer_hash_domain() -> HashingDomain {HashingDomain::new("dan_layer")}
pub fn key_manager_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.key_manager")}
pub fn mmr_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.mmr")}
pub fn p2p_hash_domain() -> HashingDomain {HashingDomain::new("base_layer.p2p")}
pub fn tari_script_hash_domain() -> HashingDomain {HashingDomain::new("infrastructure.tari_script")}

Motivation and Context

Needed to implement the tari_crypto standardized domain separated hashing.

How Has This Been Tested?

Unit tests (no system level impact with this PR yet).

Added a hashing domain API for general use based on the new domain separated
hashing API from tari_crypto; this is a proposal to implement the hashing
domain API system wide. A new struct HashingDomain was added to provide a
standardized way of implementing the new tari_crypto hashing api, which
provides standardized domain separated hashing. This currently supports
GenericHashDomain and MacDomain from tari_crypto.
Copy link
Collaborator

@stringhandler stringhandler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is exactly what @CjS77 intended, but let's see what it looks like when you have replaced more functionality with the hashers

common/src/hashing_domain.rs Show resolved Hide resolved
common/src/lib.rs Show resolved Hide resolved
@stringhandler stringhandler merged commit dba167b into tari-project:development Jul 22, 2022
@hansieodendaal hansieodendaal deleted the ho_hashing_api branch July 22, 2022 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants