Skip to content

Commit

Permalink
chore: move domain hash names to separate crate (#6076)
Browse files Browse the repository at this point in the history
Description
---
Refactor out the common hash domain names between `tari` and `tari-dan`
repos.

Motivation and Context
---
Constants should always be defined at one place.

How Has This Been Tested?
---

What process can a PR reviewer use to test or verify this change?
---

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->

---------

Co-authored-by: SW van Heerden <swvheerden@gmail.com>
  • Loading branch information
Cifko and SWvheerden authored Jan 19, 2024
1 parent 7a54cf2 commit 4677610
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 46 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ members = [
"applications/minotari_app_utilities",
"applications/minotari_merge_mining_proxy",
"applications/minotari_miner",
"integration_tests"
"integration_tests",
"hash_domains",
]

# Add here until we move to edition=2021
Expand Down
23 changes: 19 additions & 4 deletions applications/minotari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ tari_p2p = { path = "../../base_layer/p2p", features = ["auto-update"] }
tari_script = { path = "../../infrastructure/tari_script" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_utilities = { version = "0.7" }
minotari_wallet = { path = "../../base_layer/wallet", features = ["bundled_sqlite"] }
minotari_wallet = { path = "../../base_layer/wallet", features = [
"bundled_sqlite",
] }
tari_hash_domains = { path = "../../hash_domains" }

# Uncomment for tokio tracing via tokio-console (needs "tracing" featurs)
console-subscriber = "0.1.8"
Expand All @@ -33,8 +36,20 @@ clap = { version = "3.2", features = ["derive", "env"] }
config = "0.13.0"
crossterm = { version = "0.25.0" }
digest = "0.10"
futures = { version = "^0.3.16", default-features = false, features = ["alloc"] }
log4rs = { git = "https://github.com/tari-project/log4rs.git", default_features = false, features = ["config_parsing", "threshold_filter", "yaml_format", "console_appender", "rolling_file_appender", "compound_policy", "size_trigger", "fixed_window_roller", "delete_roller"] }
futures = { version = "^0.3.16", default-features = false, features = [
"alloc",
] }
log4rs = { git = "https://github.com/tari-project/log4rs.git", default_features = false, features = [
"config_parsing",
"threshold_filter",
"yaml_format",
"console_appender",
"rolling_file_appender",
"compound_policy",
"size_trigger",
"fixed_window_roller",
"delete_roller",
] }
log = { version = "0.4.8", features = ["std"] }
qrcode = { version = "0.12" }
rand = "0.8"
Expand Down Expand Up @@ -67,7 +82,7 @@ default-features = false
features = ["crossterm"]

[build-dependencies]
tari_features = { path = "../../common/tari_features"}
tari_features = { path = "../../common/tari_features" }

[features]
default = ["libtor"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use minotari_wallet::output_manager_service::UtxoSelectionCriteria;
use regex::Regex;
use reqwest::StatusCode;
use tari_core::transactions::{tari_amount::MicroMinotari, transaction_components::TemplateType};
use tari_crypto::{hash_domain, hashing::DomainSeparation};
use tari_crypto::hashing::DomainSeparation;
use tari_hash_domains::TariEngineHashDomain;
use tari_utilities::hex::Hex;
use tokio::{
runtime::{Handle, Runtime},
Expand Down Expand Up @@ -537,7 +538,6 @@ impl RegisterTemplateTab {
StatusCode::OK => match data.bytes().await {
Ok(bytes) => {
let mut hasher = Blake2b::<U32>::default();
hash_domain!(TariEngineHashDomain, "com.tari.dan.engine", 0);
TariEngineHashDomain::add_domain_separation_tag(&mut hasher, "Template");
let hash: [u8; 32] = hasher.chain_update(bytes).finalize().into();
hex_string = hash.to_hex();
Expand Down
48 changes: 30 additions & 18 deletions base_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,41 @@ edition = "2018"
default = ["base_node"]
transactions = []
mempool_proto = []
base_node = ["tari_mmr", "transactions", "mempool_proto", "base_node_proto", "monero", "randomx-rs"]
base_node = [
"tari_mmr",
"transactions",
"mempool_proto",
"base_node_proto",
"monero",
"randomx-rs",
]
base_node_proto = []
benches = ["base_node"]
metrics = ["tari_metrics"]

[dependencies]
tari_common = { path = "../../common" }
tari_common_types = { path = "../../base_layer/common_types" }
tari_comms = { path = "../../comms/core" }
tari_comms_dht = { path = "../../comms/dht" }
tari_comms_rpc_macros = { path = "../../comms/rpc_macros" }
tari_common = { path = "../../common" }
tari_common_types = { path = "../../base_layer/common_types" }
tari_comms = { path = "../../comms/core" }
tari_comms_dht = { path = "../../comms/dht" }
tari_comms_rpc_macros = { path = "../../comms/rpc_macros" }
tari_crypto = { version = "0.20", features = ["borsh"] }
tari_metrics = { path = "../../infrastructure/metrics", optional = true }
tari_mmr = { path = "../../base_layer/mmr", optional = true}
tari_p2p = { path = "../../base_layer/p2p" }
tari_mmr = { path = "../../base_layer/mmr", optional = true }
tari_p2p = { path = "../../base_layer/p2p" }
tari_script = { path = "../../infrastructure/tari_script" }
tari_service_framework = { path = "../service_framework" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_storage = { path = "../../infrastructure/storage" }
tari_test_utils = { path = "../../infrastructure/test_utils" }
tari_service_framework = { path = "../service_framework" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_storage = { path = "../../infrastructure/storage" }
tari_test_utils = { path = "../../infrastructure/test_utils" }
tari_utilities = { version = "0.7", features = ["borsh"] }
tari_key_manager = { path = "../key_manager", features = ["key_manager_service"] }
tari_key_manager = { path = "../key_manager", features = [
"key_manager_service",
] }
tari_common_sqlite = { path = "../../common_sqlite" }
tari_hash_domains = { path = "../../hash_domains" }

async-trait = {version = "0.1.50"}
async-trait = { version = "0.1.50" }
bincode = "1.1.4"
bitflags = { version = "2.4", features = ["serde"] }
blake2 = "0.10"
Expand Down Expand Up @@ -76,12 +86,14 @@ tokio = { version = "1.23", features = ["time", "sync", "macros"] }
tracing = "0.1.26"
zeroize = "1"
primitive-types = { version = "0.12", features = ["serde"] }
tiny-keccak = { git = "https://github.com/tari-project/tiny-keccak", rev = "bcddc65530d8646de7282cd8d18d891dc434b643",features = ["keccak"] }
tiny-keccak = { git = "https://github.com/tari-project/tiny-keccak", rev = "bcddc65530d8646de7282cd8d18d891dc434b643", features = [
"keccak",
] }

[dev-dependencies]
criterion = { version = "0.4.0" }
tari_p2p = { path = "../../base_layer/p2p", features = ["test-mocks"] }
tari_test_utils = { path = "../../infrastructure/test_utils" }
tari_p2p = { path = "../../base_layer/p2p", features = ["test-mocks"] }
tari_test_utils = { path = "../../infrastructure/test_utils" }
curve25519-dalek = { package = "tari-curve25519-dalek", version = "4.0.3" }
# SQLite required for the integration tests
libsqlite3-sys = { version = "0.25.1", features = ["bundled"] }
Expand All @@ -92,7 +104,7 @@ toml = { version = "0.5" }
quickcheck = "1.0"

[build-dependencies]
tari_common = { path = "../../common", features = ["build"] }
tari_common = { path = "../../common", features = ["build"] }

[[bench]]
name = "mempool"
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use blake2::Blake2b;
use digest::consts::U64;
use tari_crypto::hash_domain;
use tari_hash_domains::ConfidentialOutputHashDomain;

use crate::consensus::DomainSeparatedConsensusHasher;

Expand All @@ -35,7 +35,6 @@ pub mod rolling_avg;
#[cfg(feature = "base_node")]
pub mod rolling_vec;

hash_domain!(ConfidentialOutputHashDomain, "com.tari.dan.confidential_output", 1);
/// Hasher used in the DAN to derive masks and encrypted value keys
pub type ConfidentialOutputHasher = DomainSeparatedConsensusHasher<ConfidentialOutputHashDomain, Blake2b<U64>>;

Expand Down
7 changes: 1 addition & 6 deletions base_layer/core/src/common/one_sided.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tari_crypto::{
hashing::{DomainSeparatedHash, DomainSeparatedHasher},
keys::{PublicKey as PKtrait, SecretKey as SKtrait},
};
use tari_hash_domains::WalletOutputEncryptionKeysDomain;
use tari_utilities::byte_array::ByteArrayError;

hash_domain!(
Expand All @@ -39,12 +40,6 @@ hash_domain!(
1
);

hash_domain!(
WalletOutputEncryptionKeysDomain,
"com.tari.base_layer.wallet.output_encryption_keys",
1
);

hash_domain!(
WalletOutputSpendingKeysDomain,
"com.tari.base_layer.wallet.output_spending_keys",
Expand Down
6 changes: 1 addition & 5 deletions base_layer/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod domain_hashing {
use blake2::Blake2b;
use digest::consts::U32;
use tari_crypto::{hash_domain, hashing::DomainSeparatedHasher};
use tari_hash_domains::ValidatorNodeBmtHashDomain;
use tari_mmr::{
pruned_hashset::PrunedHashSet,
sparse_merkle_tree::SparseMerkleTree,
Expand All @@ -80,11 +81,6 @@ mod domain_hashing {

pub type OutputSmt = SparseMerkleTree<OutputSmtHasherBlake256>;

hash_domain!(
ValidatorNodeBmtHashDomain,
"com.tari.base_layer.core.validator_node_mmr",
1
);
pub type ValidatorNodeBmtHasherBlake256 = DomainSeparatedHasher<Blake2b<U32>, ValidatorNodeBmtHashDomain>;
pub type ValidatorNodeBMT = BalancedBinaryMerkleTree<ValidatorNodeBmtHasherBlake256>;
}
Expand Down
7 changes: 0 additions & 7 deletions base_layer/core/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,3 @@ pub mod test_helpers;
// Hash domain for all transaction-related hashes, including the script signature challenge, transaction hash and kernel
// signature challenge
hash_domain!(TransactionHashDomain, "com.tari.base_layer.core.transactions", 0);

// Hash domain used to derive the final AEAD encryption key for encrypted data in UTXOs
hash_domain!(
TransactionSecureNonceKdfDomain,
"com.tari.base_layer.core.transactions.secure_nonce_kdf",
0
);
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use digest::{consts::U32, generic_array::GenericArray, FixedOutput};
use serde::{Deserialize, Serialize};
use tari_common_types::types::{Commitment, PrivateKey};
use tari_crypto::{hashing::DomainSeparatedHasher, keys::SecretKey};
use tari_hash_domains::TransactionSecureNonceKdfDomain;
use tari_utilities::{
hex::{from_hex, to_hex, Hex, HexError},
safe_array::SafeArray,
Expand All @@ -50,7 +51,7 @@ use thiserror::Error;
use zeroize::{Zeroize, Zeroizing};

use super::EncryptedDataKey;
use crate::transactions::{tari_amount::MicroMinotari, TransactionSecureNonceKdfDomain};
use crate::transactions::tari_amount::MicroMinotari;

// Useful size constants, each in bytes
const SIZE_NONCE: usize = size_of::<XNonce>();
Expand Down
9 changes: 9 additions & 0 deletions hash_domains/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "tari_hash_domains"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tari_crypto = "0.20.0"
45 changes: 45 additions & 0 deletions hash_domains/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023. 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::hash_domain;

// These are the hash domains that are also used in tari-dan.

hash_domain!(ConfidentialOutputHashDomain, "com.tari.dan.confidential_output", 1);
hash_domain!(TariEngineHashDomain, "com.tari.dan.engine", 0);

// Hash domain used to derive the final AEAD encryption key for encrypted data in UTXOs
hash_domain!(
TransactionSecureNonceKdfDomain,
"com.tari.base_layer.core.transactions.secure_nonce_kdf",
0
);
hash_domain!(
ValidatorNodeBmtHashDomain,
"com.tari.base_layer.core.validator_node_mmr",
1
);
hash_domain!(
WalletOutputEncryptionKeysDomain,
"com.tari.base_layer.wallet.output_encryption_keys",
1
);

0 comments on commit 4677610

Please sign in to comment.