Skip to main content

amaru_kernel/cardano/
hash.rs

1// Copyright 2026 PRAGMA
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub use pallas_crypto::hash::{Hash, Hasher};
16
17// -----------------------------------------------------------------------------
18// Hash sizes
19// -----------------------------------------------------------------------------
20
21pub mod size {
22    pub const BLOCK_BODY: usize = 32;
23
24    pub const CREDENTIAL: usize = 28;
25
26    pub const DATUM: usize = 32;
27
28    pub const HEADER: usize = 32;
29
30    pub const KEY: usize = CREDENTIAL;
31
32    pub const NONCE: usize = 32;
33
34    pub const POOL_COLD_KEY: usize = 28;
35
36    pub const SCRIPT: usize = CREDENTIAL;
37
38    pub const TRANSACTION_BODY: usize = 32;
39
40    pub const VRF_KEY: usize = 32;
41}
42
43// -----------------------------------------------------------------------------
44// Aliases
45// -----------------------------------------------------------------------------
46
47pub type HeaderHash = Hash<{ size::HEADER }>;
48
49pub type PoolId = Hash<{ size::POOL_COLD_KEY }>;
50
51pub type TransactionId = Hash<{ size::TRANSACTION_BODY }>;
52
53// -----------------------------------------------------------------------------
54// Constants
55// -----------------------------------------------------------------------------
56
57pub const NULL_HASH28: Hash<28> = Hash::new([0; 28]);
58
59pub const NULL_HASH32: Hash<32> = Hash::new([0; 32]);
60
61pub const ORIGIN_HASH: Hash<{ size::HEADER }> = NULL_HASH32;
62
63#[cfg(any(test, feature = "test-utils"))]
64pub use tests::*;
65
66#[cfg(any(test, feature = "test-utils"))]
67mod tests {
68    use proptest::prelude::*;
69
70    use super::*;
71
72    pub fn any_hash28() -> impl Strategy<Value = Hash<28>> {
73        any::<[u8; 28]>().prop_map(Hash::from)
74    }
75
76    pub fn any_hash32() -> impl Strategy<Value = Hash<32>> {
77        any::<[u8; 32]>().prop_map(Hash::from)
78    }
79}