-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tari_crypto hashing api support (#4328)
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
1 parent
f20468e
commit dba167b
Showing
29 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.