1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Domain separation for ClockHash-256
//!
//! Domain separation ensures that hashes computed for different use cases
//! (block headers, transactions, Merkle trees, etc.) produce different outputs
//! even when the input data is identical. This prevents cross-domain collision
//! attacks and maintains the security properties of the hash function.
//!
//! ## Security Importance
//!
//! Without domain separation, an attacker who finds a collision in one domain
//! could potentially use it to attack a different domain. Domain separation
//! ensures that collisions in one domain have no impact on others.
//!
//! ## Implementation
//!
//! Domain separation is implemented by prepending the domain identifier
//! followed by a null byte separator before the actual data:
//!
//! `hash(domain || 0x00 || data)`
//!
//! This approach is simple, efficient, and provides strong separation guarantees.
use crateClockHasher;
/// Predefined domain tag constants for ClockinChain use cases
///
/// These constants define the standard domain separators used throughout
/// the ClockinChain ecosystem. Each domain serves a specific purpose and
/// ensures isolation between different hash applications.
/// Type-safe domain tag enumeration for ClockinChain use cases
///
/// `DomainTag` provides compile-time type safety for domain separation.
/// Each variant corresponds to a specific use case and maps to the
/// appropriate domain tag bytes. This prevents typos and ensures
/// consistent domain usage across the codebase.
///
/// # Examples
///
/// Using with typed domains:
/// ```rust
/// use clock_hash::{clockhash256_with_domain, DomainTag};
///
/// let block_data = b"block header data";
/// let tx_data = b"transaction data";
///
/// let block_hash = clockhash256_with_domain(DomainTag::Block, block_data);
/// let tx_hash = clockhash256_with_domain(DomainTag::Transaction, tx_data);
///
/// assert_ne!(block_hash, tx_hash); // Guaranteed to be different
/// ```
///
/// Converting to bytes:
/// ```rust
/// # use clock_hash::DomainTag;
/// let tag = DomainTag::Merkle;
/// let bytes = tag.as_bytes();
/// assert_eq!(bytes, b"CLK-MERKLE");
/// ```
/// Compute ClockHash-256 with custom domain separation.
///
/// This function provides domain separation by prepending the domain identifier
/// followed by a null byte separator before hashing the actual data. This ensures
/// that identical data produces different hashes when used in different domains,
/// preventing cross-domain collision attacks.
///
/// # Arguments
///
/// * `domain` - Custom domain identifier bytes (must be consistent for the same domain)
/// * `data` - The data to hash
///
/// # Returns
///
/// A 32-byte array containing the domain-separated ClockHash-256 hash
///
/// # Examples
///
/// Using custom domain tags:
/// ```rust
/// # use clock_hash::clockhash256_domain;
/// let data = b"some data";
///
/// let domain1 = b"MY-APP-V1";
/// let domain2 = b"MY-APP-V2";
///
/// let hash1 = clockhash256_domain(domain1, data);
/// let hash2 = clockhash256_domain(domain2, data);
///
/// assert_ne!(hash1, hash2); // Different domains = different hashes
/// ```
///
/// Custom domain vs no domain:
/// ```rust
/// # use clock_hash::{clockhash256_domain, clockhash256};
/// let data = b"test";
/// let custom_hash = clockhash256_domain(b"CUSTOM", data);
/// let plain_hash = clockhash256(data);
///
/// assert_ne!(custom_hash, plain_hash); // Domain separation works
/// ```
///
/// # Security Notes
///
/// - Domain identifiers should be unique and consistent within your application
/// - Never use the same domain for different purposes
/// - Domain separation is critical for maintaining hash function security properties
/// Compute ClockHash-256 with a typed domain tag.
///
/// This function provides type-safe domain separation using the `DomainTag` enum.
/// It automatically converts the enum variant to the appropriate domain bytes
/// and performs domain-separated hashing.
///
/// # Arguments
///
/// * `domain` - The domain tag enum variant specifying the use case
/// * `data` - The data to hash
///
/// # Returns
///
/// A 32-byte array containing the domain-separated ClockHash-256 hash
///
/// # Examples
///
/// Type-safe domain hashing:
/// ```rust
/// use clock_hash::{clockhash256_with_domain, DomainTag};
///
/// let data = b"important data";
///
/// // Type-safe domain specification
/// let block_hash = clockhash256_with_domain(DomainTag::Block, data);
/// let merkle_hash = clockhash256_with_domain(DomainTag::Merkle, data);
///
/// assert_ne!(block_hash, merkle_hash);
/// ```
///
/// All domain types:
/// ```rust
/// # use clock_hash::{clockhash256_with_domain, DomainTag};
/// # let data = b"test";
/// let block_hash = clockhash256_with_domain(DomainTag::Block, data);
/// let tx_hash = clockhash256_with_domain(DomainTag::Transaction, data);
/// let merkle_hash = clockhash256_with_domain(DomainTag::Merkle, data);
/// let nonce_hash = clockhash256_with_domain(DomainTag::Nonce, data);
/// let rng_hash = clockhash256_with_domain(DomainTag::Rng, data);
///
/// // All hashes are guaranteed to be different
/// let hashes = vec![block_hash, tx_hash, merkle_hash, nonce_hash, rng_hash];
/// for i in 0..hashes.len() {
/// for j in (i+1)..hashes.len() {
/// assert_ne!(hashes[i], hashes[j]);
/// }
/// }
/// ```
///
/// # Performance
///
/// This function has the same performance characteristics as `clockhash256_domain()`
/// since it simply delegates to that function after converting the enum to bytes.