Skip to content

Commit

Permalink
feat: add tari_crypto hashing api support (#4328)
Browse files Browse the repository at this point in the history
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:
``` rust
        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._)
``` rust
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_).
  • Loading branch information
hansieodendaal committed Jul 22, 2022
1 parent f20468e commit dba167b
Show file tree
Hide file tree
Showing 29 changed files with 426 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions base_layer/common_types/src/types/default_hash_domain.rs
@@ -0,0 +1,36 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use tari_crypto::hashing::DomainSeparation;

/// The default domain separation marker for use in the tari project.
pub struct DefaultHashDomain;

impl DomainSeparation for DefaultHashDomain {
fn version() -> u8 {
1
}

fn domain() -> &'static str {
"com.tari.tari_project.hash_domain"
}
}
36 changes: 36 additions & 0 deletions base_layer/common_types/src/types/mac_hash_domain.rs
@@ -0,0 +1,36 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use tari_crypto::hashing::DomainSeparation;

/// A domain separation marker for use in MAC derivation algorithms.
pub struct MacHashDomain;

impl DomainSeparation for MacHashDomain {
fn version() -> u8 {
1
}

fn domain() -> &'static str {
"com.tari.tari_project.mac_domain"
}
}
11 changes: 11 additions & 0 deletions base_layer/common_types/src/types/mod.rs
Expand Up @@ -21,11 +21,14 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

mod bullet_rangeproofs;
mod default_hash_domain;
mod fixed_hash;
mod mac_hash_domain;

pub use bullet_rangeproofs::BulletRangeProof;
use tari_crypto::{
hash::blake2::Blake256,
hashing::DomainSeparatedHasher,
ristretto::{
bulletproofs_plus::BulletproofsPlusService,
pedersen::{extended_commitment_factory::ExtendedPedersenCommitmentFactory, PedersenCommitment},
Expand All @@ -41,6 +44,8 @@ pub type BlockHash = Vec<u8>;

pub use fixed_hash::{FixedHash, FixedHashSizeError};

use crate::types::{default_hash_domain::DefaultHashDomain, mac_hash_domain::MacHashDomain};

/// Define the explicit Signature implementation for the Tari base layer. A different signature scheme can be
/// employed by redefining this type.
pub type Signature = RistrettoSchnorr;
Expand Down Expand Up @@ -81,3 +86,9 @@ pub type RangeProofService = BulletproofsPlusService;

/// Specify the range proof
pub type RangeProof = BulletRangeProof;

/// Generic domain separated hasher
pub type DefaultDomainHasher<D> = DomainSeparatedHasher<D, DefaultHashDomain>;

/// MAC domain separated hasher
pub type MacDomainHasher<D> = DomainSeparatedHasher<D, MacHashDomain>;
9 changes: 9 additions & 0 deletions base_layer/core/src/blocks/mod.rs
Expand Up @@ -61,3 +61,12 @@ pub use new_block_template::NewBlockTemplate;
mod new_blockheader_template;
#[cfg(feature = "base_node")]
pub use new_blockheader_template::NewBlockHeaderTemplate;
use tari_common::hashing_domain::HashingDomain;

/// The base layer core blocks domain separated hashing domain
/// Usage:
/// let hash = core_blocks_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn core_blocks_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.core.blocks")
}
9 changes: 9 additions & 0 deletions base_layer/core/src/consensus/mod.rs
Expand Up @@ -44,5 +44,14 @@ pub use consensus_encoding::{

mod network;
pub use network::NetworkConsensus;
use tari_common::hashing_domain::HashingDomain;

pub mod emission;

/// The base layer core consensus domain separated hashing domain
/// Usage:
/// let hash = core_consensus_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn core_consensus_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.core.consensus")
}
9 changes: 9 additions & 0 deletions base_layer/core/src/covenants/mod.rs
Expand Up @@ -44,10 +44,19 @@ pub use error::CovenantError;
// Used in macro
#[allow(unused_imports)]
pub(crate) use fields::OutputField;
use tari_common::hashing_domain::HashingDomain;
pub use token::CovenantToken;

#[macro_use]
mod macros;

#[cfg(test)]
mod test;

/// The base layer core covenants domain separated hashing domain
/// Usage:
/// let hash = core_covenants_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn core_covenants_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.core.covenants")
}
9 changes: 9 additions & 0 deletions base_layer/core/src/lib.rs
Expand Up @@ -67,3 +67,12 @@ pub mod large_ints {
}
}
pub use large_ints::{U256, U512};
use tari_common::hashing_domain::HashingDomain;

/// The base layer core domain separated hashing domain
/// Usage:
/// let hash = core_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn core_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.core")
}
10 changes: 10 additions & 0 deletions base_layer/core/src/proof_of_work/mod.rs
Expand Up @@ -65,3 +65,13 @@ pub mod lwma_diff;

#[cfg(feature = "base_node")]
pub mod randomx_factory;

use tari_common::hashing_domain::HashingDomain;

/// The base layer core proof-of-work domain separated hashing domain
/// Usage:
/// let hash = core_proof_of_work_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn core_proof_of_work_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.core.proof_of_work")
}
9 changes: 9 additions & 0 deletions base_layer/core/src/transactions/mod.rs
Expand Up @@ -15,6 +15,7 @@ pub mod transaction_components;

mod format_currency;
pub use format_currency::format_currency;
use tari_common::hashing_domain::HashingDomain;

pub mod transaction_protocol;
pub use transaction_protocol::{recipient::ReceiverTransactionProtocol, sender::SenderTransactionProtocol};
Expand All @@ -24,3 +25,11 @@ pub mod weight;

#[macro_use]
pub mod test_helpers;

/// The base layer core transactions domain separated hashing domain
/// Usage:
/// let hash = core_transactions_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn core_transactions_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.core.transactions")
}
Expand Up @@ -95,6 +95,7 @@ pub struct UnblindedOutput {
impl UnblindedOutput {
/// Creates a new un-blinded output

#[allow(clippy::too_many_arguments)]
pub fn new(
version: TransactionOutputVersion,
value: MicroTari,
Expand Down
1 change: 1 addition & 0 deletions base_layer/key_manager/Cargo.toml
Expand Up @@ -12,6 +12,7 @@ crate-type = ["lib", "cdylib"]

[dependencies]
tari_common_types = { version = "^0.34", path = "../../base_layer/common_types" }
tari_common = {path = "../../common"}
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.0" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag = "v0.4.4" }

Expand Down
10 changes: 10 additions & 0 deletions base_layer/key_manager/src/lib.rs
@@ -1,6 +1,8 @@
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use tari_common::hashing_domain::HashingDomain;

pub mod cipher_seed;
pub mod diacritics;
pub mod error;
Expand All @@ -11,3 +13,11 @@ pub mod mnemonic_wordlists;
#[allow(clippy::unused_unit)]
#[cfg(feature = "wasm")]
pub mod wasm;

/// The base layer key manager domain separated hashing domain
/// Usage:
/// let hash = key_manager_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn key_manager_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.key_manager")
}
1 change: 1 addition & 0 deletions base_layer/mmr/Cargo.toml
Expand Up @@ -14,6 +14,7 @@ benches = ["criterion"]

[dependencies]
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag = "v0.4.4" }
tari_common = {path = "../../common"}
thiserror = "1.0.26"
digest = "0.9.0"
log = "0.4"
Expand Down
9 changes: 9 additions & 0 deletions base_layer/mmr/src/lib.rs
Expand Up @@ -156,6 +156,15 @@ pub use mem_backend_vec::MemBackendVec;
pub use merkle_mountain_range::MerkleMountainRange;
/// A data structure for proving a hash inclusion in an MMR
pub use merkle_proof::{MerkleProof, MerkleProofError};
use tari_common::hashing_domain::HashingDomain;

/// The base layer MMR domain separated hashing domain
/// Usage:
/// let hash = mmr_hash_domain().digest::<Blake256>(b"my secret");
/// etc.
pub fn mmr_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.mmr")
}

macro_rules! if_native_bitmap {
($($item:item)*) => {
Expand Down
9 changes: 9 additions & 0 deletions base_layer/p2p/src/lib.rs
Expand Up @@ -44,6 +44,7 @@ mod dns;
// Re-export
pub use socks_authentication::SocksAuthentication;
pub use tari_common::configuration::Network;
use tari_common::hashing_domain::HashingDomain;
pub use tor_authentication::TorControlAuthentication;
pub use transport::{Socks5TransportConfig, TcpTransportConfig, TorTransportConfig, TransportConfig, TransportType};

Expand All @@ -57,3 +58,11 @@ pub const MAJOR_NETWORK_VERSION: u8 = 0;
/// Minor network version. This should change with each time the network protocol has changed in a backward-compatible
/// way.
pub const MINOR_NETWORK_VERSION: u8 = 0;

/// The base layer p2p domain separated hashing domain
/// Usage:
/// let hash = p2p_hash_domain(.digest::<Blake256>(b"my secret");
/// etc.
pub fn p2p_hash_domain() -> HashingDomain {
HashingDomain::new("base_layer.p2p")
}
2 changes: 2 additions & 0 deletions common/Cargo.toml
Expand Up @@ -14,6 +14,8 @@ build = ["toml", "prost-build"]
static-application-info = ["git2"]

[dependencies]
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.0" }
tari_common_types = { path = "../base_layer/common_types" }
anyhow = "1.0.53"
config = { version = "0.13.0", default_features = false, features = ["toml"] }
derivative = "2.2.0"
Expand Down

0 comments on commit dba167b

Please sign in to comment.