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 hashing API use to base layer (see issue #4394) #4447

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
000d939
clear pending coinbase transactions now rely on utxo hashes
jorgeantonio21 Aug 3, 2022
3a93730
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 3, 2022
af7843d
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 4, 2022
d9a9d1c
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 5, 2022
468aff6
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 8, 2022
aa225a4
sync with dev
jorgeantonio21 Aug 8, 2022
68ec0b2
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 9, 2022
6edf4fb
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 9, 2022
bfcc7ed
add hashing api domain separation for base_layer/common_types
jorgeantonio21 Aug 10, 2022
303cf64
add hashing API to base layer
jorgeantonio21 Aug 11, 2022
1ec6c9d
remove unused code
jorgeantonio21 Aug 11, 2022
265821d
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 11, 2022
39d3309
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 11, 2022
c77aeb3
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 11, 2022
7d4b991
cleaning
jorgeantonio21 Aug 11, 2022
1ce46e9
remove unused hashing.rs
jorgeantonio21 Aug 11, 2022
73bb978
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 11, 2022
37326f0
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 11, 2022
a80f0f3
add suggestions on PR
jorgeantonio21 Aug 12, 2022
8c9fd22
run cargo fmt
jorgeantonio21 Aug 12, 2022
8bcbfa9
add blak256 import
jorgeantonio21 Aug 12, 2022
8301c74
cargo fmt
jorgeantonio21 Aug 12, 2022
597861b
remove unused imports
jorgeantonio21 Aug 12, 2022
ff7f661
add further simplifications
jorgeantonio21 Aug 12, 2022
9d9c5ad
add necesary dependency on tests
jorgeantonio21 Aug 12, 2022
aeb9184
changes
jorgeantonio21 Aug 12, 2022
84d5a3b
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 12, 2022
e181364
change getTransactionOutputHash js test and remove generic type from …
jorgeantonio21 Aug 12, 2022
8d4f495
run cargo fmt
jorgeantonio21 Aug 12, 2022
11b6da1
refactor incorrect add of path
jorgeantonio21 Aug 12, 2022
1b53ac7
run cargo fmt
jorgeantonio21 Aug 12, 2022
a273740
refactor some code
jorgeantonio21 Aug 12, 2022
a98a461
run cargo fmt
jorgeantonio21 Aug 12, 2022
47839c5
remove unused imports
jorgeantonio21 Aug 12, 2022
d5a894f
refactor cucumber tests
jorgeantonio21 Aug 12, 2022
a3bc299
add changes
jorgeantonio21 Aug 12, 2022
2047081
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 15, 2022
839673d
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 16, 2022
352e5f2
merge development
jorgeantonio21 Aug 16, 2022
edc0c88
removed unused hash domain
jorgeantonio21 Aug 16, 2022
053f3a7
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 16, 2022
2a4bdde
resolve conflicts with merged development
jorgeantonio21 Aug 16, 2022
f3c39af
resolve further issues
jorgeantonio21 Aug 16, 2022
573c075
resolve further issues
jorgeantonio21 Aug 16, 2022
2004f58
run cargo fmt
jorgeantonio21 Aug 16, 2022
102fc34
remove unused dependencies
jorgeantonio21 Aug 16, 2022
c318dc1
resolve `it_validates_that_the_covenant_is_honoured`
jorgeantonio21 Aug 16, 2022
3c738c5
run cargo fmt
jorgeantonio21 Aug 16, 2022
7c0e3cb
refactor cucumber tests
jorgeantonio21 Aug 16, 2022
d3cb3ee
remove files that were already eliminated
jorgeantonio21 Aug 16, 2022
ddb6b51
refactor transactionOutputHashing.js
jorgeantonio21 Aug 16, 2022
00bd69e
add changes to transactionOuptutHashing.js
jorgeantonio21 Aug 16, 2022
2e66f24
refactor feature tests
jorgeantonio21 Aug 16, 2022
0b75544
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 17, 2022
6d034d9
merge development
jorgeantonio21 Aug 17, 2022
49f20df
remove unnecessary allocation
jorgeantonio21 Aug 17, 2022
01269a9
refactor finalize
jorgeantonio21 Aug 17, 2022
6316cb7
remove cucumber tests with @dan
jorgeantonio21 Aug 17, 2022
686719b
Merge branch 'development' of github.com:jorgeantonio21/tari into dev…
jorgeantonio21 Aug 17, 2022
069f4cc
merge development
jorgeantonio21 Aug 17, 2022
e832b29
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 18, 2022
ea65f9f
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 18, 2022
47baefa
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 18, 2022
7ed11c6
Merge branch 'development' into ja-hashing-base-layer
jorgeantonio21 Aug 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions base_layer/common_types/src/types/bullet_rangeproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use std::fmt;

use digest::Digest;
use serde::{
de::{self, Visitor},
Deserialize,
Expand All @@ -32,14 +31,18 @@ use serde::{
};
use tari_utilities::{hex::*, ByteArray, ByteArrayError, Hashable};

use crate::types::Blake256;
use super::{base_layer_common_types_domain_hasher, BULLET_RANGEPROOFS_HASHER_LABEL};
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BulletRangeProof(pub Vec<u8>);
/// Implement the hashing function for RangeProof for use in the MMR
impl Hashable for BulletRangeProof {
fn hash(&self) -> Vec<u8> {
Blake256::new().chain(&self.0).finalize().to_vec()
base_layer_common_types_domain_hasher(BULLET_RANGEPROOFS_HASHER_LABEL)
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
.chain(&self.0)
.finalize()
.as_ref()
.to_vec()
}
}

Expand Down
15 changes: 15 additions & 0 deletions base_layer/common_types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,18 @@ pub type RangeProofService = BulletproofsPlusService;

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

use tari_crypto::{hash_domain, hashing::DomainSeparatedHasher};

pub(crate) const BULLET_RANGEPROOFS_HASHER_LABEL: &str = "bullet_rangeproofs.hasher";

hash_domain!(
BaseLayerCommonTypesDomain,
"com.tari.tari-project.base_layer.common_types"
);

pub(crate) fn base_layer_common_types_domain_hasher(
label: &'static str,
) -> DomainSeparatedHasher<Blake256, BaseLayerCommonTypesDomain> {
DomainSeparatedHasher::<Blake256, BaseLayerCommonTypesDomain>::new_with_label(label)
}
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 5 additions & 3 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use std::{
time::Instant,
};

use blake2::Digest;
use croaring::Bitmap;
use fs2::FileExt;
use lmdb_zero::{open, ConstTransaction, Database, Environment, ReadTransaction, WriteTransaction};
Expand All @@ -57,6 +56,7 @@ use tari_utilities::{
ByteArray,
};

use super::{base_layer_core_chain_storage_lmdb_hasher, LMDB_STORAGE_HASH_LABEL};
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
use crate::{
blocks::{
Block,
Expand Down Expand Up @@ -2736,10 +2736,12 @@ impl UniqueIdIndexKey {
/// `parent_public_key` - the parent asset public key to which the token is assigned
/// `unique_id` - a series of bytes representing the token uniquely for the asset
pub fn new(parent_public_key: Option<&PublicKey>, unique_id: &[u8]) -> Self {
let unique_id_hash = Blake256::default().chain(unique_id).finalize();
let unique_id_hash = base_layer_core_chain_storage_lmdb_hasher::<Blake256>(LMDB_STORAGE_HASH_LABEL)
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
.chain(unique_id)
.finalize();
Self::from_raw_parts(
parent_public_key.map(|p| p.as_bytes()).unwrap_or(&[0; 32][..]),
&unique_id_hash,
unique_id_hash.as_ref(),
// u64::MAX
&[0xff; 8][..],
)
Expand Down
15 changes: 15 additions & 0 deletions base_layer/core/src/chain_storage/lmdb_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
// 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 digest::Digest;
pub use lmdb_db::{create_lmdb_database, create_recovery_lmdb_database, LMDBDatabase};
use serde::{Deserialize, Serialize};
use tari_common_types::types::HashOutput;
use tari_crypto::{
hash_domain,
hashing::{DomainSeparatedHasher, LengthExtensionAttackResistant},
};

use crate::transactions::transaction_components::{TransactionInput, TransactionKernel, TransactionOutput};

Expand Down Expand Up @@ -72,3 +77,13 @@ pub(crate) struct TransactionKernelRowData {
pub mmr_position: u32,
pub hash: HashOutput,
}

hash_domain!(BaseLayerCoreDomain, "com.tari.tari-project.base_layer.core");
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) const LMDB_STORAGE_HASH_LABEL: &str = "lmdb_db";

pub(crate) fn base_layer_core_chain_storage_lmdb_hasher<D: Digest + LengthExtensionAttackResistant>(
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
label: &'static str,
) -> DomainSeparatedHasher<D, BaseLayerCoreDomain> {
DomainSeparatedHasher::<D, BaseLayerCoreDomain>::new_with_label(label)
}
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 17 additions & 9 deletions base_layer/core/src/covenants/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ use std::{

use digest::Digest;
use integer_encoding::VarIntWriter;
use tari_crypto::hash::blake2::Blake256;
use tari_crypto::{hash::blake2::Blake256, hashing::DomainSeparation};

use super::{BaseLayerCovenantsDomain, COVENANTS_FIELD_HASHER_LABEL};
use crate::{
consensus::ToConsensusBytes,
covenants::{
Expand Down Expand Up @@ -369,8 +370,9 @@ impl OutputFields {

pub fn construct_challenge_from(&self, output: &TransactionOutput) -> Blake256 {
let mut challenge = Blake256::new();
BaseLayerCovenantsDomain::add_domain_separation_tag(&mut challenge, COVENANTS_FIELD_HASHER_LABEL);
for field in &self.fields {
challenge.update(field.get_field_value_bytes(output));
challenge.update(&field.get_field_value_bytes(output).as_slice());
}
challenge
}
Expand Down Expand Up @@ -402,7 +404,6 @@ mod test {

use super::*;
use crate::{
consensus::ConsensusEncoding,
covenant,
covenants::test::{create_input, create_outputs},
transactions::{
Expand Down Expand Up @@ -570,6 +571,9 @@ mod test {
use super::*;

mod construct_challenge_from {
use blake2::Digest;
use tari_crypto::hashing::DomainSeparation;

use super::*;

#[test]
Expand All @@ -591,12 +595,16 @@ mod test {
fields.push(OutputField::Commitment);
fields.push(OutputField::Script);
let hash = fields.construct_challenge_from(&output).finalize();

let mut challenge = Vec::new();
output.features.consensus_encode(&mut challenge).unwrap();
output.commitment.consensus_encode(&mut challenge).unwrap();
output.script.consensus_encode(&mut challenge).unwrap();
let expected_hash = Blake256::new().chain(&challenge).finalize();
let hash = hash.to_vec();

let mut hasher = Blake256::new();
BaseLayerCovenantsDomain::add_domain_separation_tag(&mut hasher, COVENANTS_FIELD_HASHER_LABEL);
let expected_hash = hasher
.chain(output.features.to_consensus_bytes())
.chain(output.commitment.to_consensus_bytes())
.chain(output.script.to_consensus_bytes())
.finalize()
.to_vec();
assert_eq!(hash, expected_hash);
}
}
Expand Down
22 changes: 14 additions & 8 deletions base_layer/core/src/covenants/filters/fields_hashed_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use digest::Digest;

use crate::covenants::{context::CovenantContext, error::CovenantError, filters::Filter, output_set::OutputSet};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldsHashedEqFilter;

Expand All @@ -32,22 +31,29 @@ impl Filter for FieldsHashedEqFilter {
let fields = context.next_arg()?.require_outputfields()?;
let hash = context.next_arg()?.require_hash()?;
output_set.retain(|output| {
let challenge = fields.construct_challenge_from(output);
Ok(challenge.finalize()[..] == *hash)
let challenge = fields.construct_challenge_from(output).finalize();
let challenge = challenge.to_vec();
Ok(challenge[..] == *hash)
jorgeantonio21 marked this conversation as resolved.
Show resolved Hide resolved
})?;
Ok(())
}
}

#[cfg(test)]
mod test {
use tari_common_types::types::{Challenge, FixedHash};
use tari_common_types::types::FixedHash;
use tari_crypto::{hash::blake2::Blake256, hashing::DomainSeparation};

use super::*;
use crate::{
consensus::ToConsensusBytes,
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
covenants::{
filters::test::setup_filter_test,
test::create_input,
BaseLayerCovenantsDomain,
COVENANTS_FIELD_HASHER_LABEL,
},
transactions::transaction_components::{OutputFeatures, SideChainFeatures},
};

Expand All @@ -58,9 +64,9 @@ mod test {
sidechain_features: Some(Box::new(SideChainFeatures::new(FixedHash::hash_bytes("A")))),
..Default::default()
};
let hashed = Challenge::new().chain(features.to_consensus_bytes()).finalize();
let mut hash = [0u8; 32];
hash.copy_from_slice(hashed.as_slice());
let mut hasher = Blake256::new();
BaseLayerCovenantsDomain::add_domain_separation_tag(&mut hasher, COVENANTS_FIELD_HASHER_LABEL);
let hash = hasher.chain(&features.to_consensus_bytes()).finalize();
let covenant = covenant!(fields_hashed_eq(@fields(@field::features), @hash(hash.into())));
let input = create_input();
let (mut context, outputs) = setup_filter_test(&covenant, &input, 0, |outputs| {
Expand Down
10 changes: 10 additions & 0 deletions base_layer/core/src/covenants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ mod macros;

#[cfg(test)]
mod test;

use tari_crypto::hash_domain;

hash_domain!(
BaseLayerCovenantsDomain,
"com.tari.tari-project.base_layer.covenants",
1
);

pub(crate) const COVENANTS_FIELD_HASHER_LABEL: &str = "fields";
19 changes: 19 additions & 0 deletions base_layer/core/src/mempool/unconfirmed_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,24 @@ mod error;
mod unconfirmed_pool;

// Public re-exports
use digest::Digest;
pub use error::UnconfirmedPoolError;
use tari_crypto::{
hash_domain,
hashing::{DomainSeparatedHasher, LengthExtensionAttackResistant},
};
pub use unconfirmed_pool::{UnconfirmedPool, UnconfirmedPoolConfig};

hash_domain!(
BaseLayerCoreMemPoolDomain,
"com.tari.tari-project.base_layer.core.mempool",
1
);

pub(crate) const UNCONFIRMED_POOL_HASH_DOMAIN_LABEL: &str = "uncorfimed_pool_output_token_id";

pub(crate) fn base_layer_core_mempool_hash_domain<D: Digest + LengthExtensionAttackResistant>(
label: &'static str,
) -> DomainSeparatedHasher<D, BaseLayerCoreMemPoolDomain> {
DomainSeparatedHasher::<D, BaseLayerCoreMemPoolDomain>::new_with_label(label)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use std::{
sync::Arc,
};

use digest::Digest;
use log::*;
use serde::{Deserialize, Serialize};
use tari_common_types::types::{HashOutput, PrivateKey, PublicKey, Signature};
use tari_crypto::hash::blake2::Blake256;
use tari_utilities::{hex::Hex, ByteArray, Hashable};

use super::{base_layer_core_mempool_hash_domain, UNCONFIRMED_POOL_HASH_DOMAIN_LABEL};
use crate::{
blocks::Block,
mempool::{
Expand All @@ -46,7 +46,6 @@ use crate::{
weight::TransactionWeight,
},
};

pub const LOG_TARGET: &str = "c::mp::unconfirmed_pool::unconfirmed_pool_storage";

type TransactionKey = usize;
Expand Down Expand Up @@ -660,11 +659,14 @@ fn get_output_token_id(output: &TransactionOutput) -> Option<[u8; 32]> {
.as_ref()
.map(|pk| pk.as_bytes())
.unwrap_or_else(|| root_pk.as_bytes());
Blake256::new()
let hash = base_layer_core_mempool_hash_domain::<Blake256>(UNCONFIRMED_POOL_HASH_DOMAIN_LABEL)
.chain(parent_pk_bytes)
.chain(unique_id)
.finalize()
.into()
.finalize();

let mut output = [0u8; 32];
output.copy_from_slice(hash.as_ref());
output
})
}

Expand Down Expand Up @@ -1027,11 +1029,14 @@ mod test {
unconfirmed_pool
.insert_many(vec![tx1.clone(), tx2.clone(), tx3.clone(), tx4.clone()], &tx_weight)
.unwrap();
let expected_hash: [u8; 32] = Blake256::new()

let domain_separated_hash = base_layer_core_mempool_hash_domain::<Blake256>(UNCONFIRMED_POOL_HASH_DOMAIN_LABEL)
.chain(parent_pk.as_bytes())
.chain(&unique_id)
.finalize()
.into();
.finalize();

let mut expected_hash: [u8; 32] = [0u8; 32];
expected_hash.copy_from_slice(domain_separated_hash.as_ref());
let entry = unconfirmed_pool.txs_by_unique_id.get(&expected_hash).unwrap();
let tx_id1 = unconfirmed_pool
.txs_by_signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@
// 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 digest::Digest;
use tari_common_types::types::{Commitment, FixedHash};
use tari_crypto::hash::blake2::Blake256;
use tari_utilities::ByteArray;

use super::{base_layer_core_transactions_side_chain_domain, CONTRACT_ACCEPTANCE_CHALLENGE_LABEL};
#[derive(Debug, Clone, Copy)]
pub struct ContractAcceptanceChallenge(FixedHash);

impl ContractAcceptanceChallenge {
pub fn new(constiution_commitment: &Commitment, contract_id: &FixedHash) -> Self {
// TODO: Use new tari_crypto domain-separated hashing
let hash = Blake256::new()
let hash = base_layer_core_transactions_side_chain_domain(CONTRACT_ACCEPTANCE_CHALLENGE_LABEL)
.chain(constiution_commitment.as_bytes())
.chain(contract_id.as_slice())
.finalize()
.into();
.finalize();

let mut slice = [0u8; FixedHash::byte_size()];
slice.copy_from_slice(hash.as_ref());

let hash = FixedHash::from(slice);

Self(hash)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,20 @@ pub type FixedString = [u8; FIXED_STR_LEN];
pub fn bytes_into_fixed_string<T: AsRef<[u8]>>(value: T) -> FixedString {
tari_common_types::array::copy_into_fixed_array_lossy::<_, FIXED_STR_LEN>(value.as_ref())
}

use tari_crypto::{hash::blake2::Blake256, hash_domain, hashing::DomainSeparatedHasher};

hash_domain!(
BaseLayerCoreTransactionsSideChainDomain,
"com.tari.tari-project.base_layer.core.transactions.side_chain",
1
);

pub(crate) const CONTRACT_ACCEPTANCE_CHALLENGE_LABEL: &str = "contract_acceptance_challenge";
pub(crate) const SIGNER_SIGNATURE_LABEL: &str = "signer_signature";

pub(crate) fn base_layer_core_transactions_side_chain_domain(
label: &'static str,
) -> DomainSeparatedHasher<Blake256, BaseLayerCoreTransactionsSideChainDomain> {
DomainSeparatedHasher::<Blake256, BaseLayerCoreTransactionsSideChainDomain>::new_with_label(label)
}
Loading