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
//! Chain verification: replay records and prove the chain is untampered.
//!
//! A [`Verifier`] feeds records back through the same canonical hashing
//! protocol used by [`crate::Chain::append`] and checks three invariants
//! per record:
//!
//! 1. **Id linkage.** The record's id is the expected next id in the chain.
//! 2. **Prev-hash linkage.** The record's `prev_hash` equals the previous
//! record's `hash` (or [`Digest::ZERO`] for the genesis record).
//! 3. **Hash integrity.** The record's stored `hash` equals the digest
//! recomputed from `(id, timestamp, actor, action, target, outcome,
//! prev_hash)` using the same field separator the chain uses.
//!
//! Optionally, a [`Verifier`] also enforces strict timestamp monotonicity
//! (each record's timestamp must be strictly greater than the previous).
//!
//! The verifier is **stateful and sequential**: callers feed records in
//! chain order. A failure leaves the verifier's state at the last record
//! it accepted, so the caller can inspect [`Verifier::next_id`] /
//! [`Verifier::last_hash`] to learn how far verification got.
use cratecanonical;
use crateTimestamp;
use crate;
use crate;
use crate;
/// Replays a chain of records and proves their hash linkage is intact.
///
/// The verifier is generic over its [`Hasher`]: callers must supply an
/// implementation that produces the same digests the original [`Chain`]
/// produced. Two different hash algorithms will not interoperate.
///
/// [`Chain`]: crate::Chain
///
/// # Example
///
/// ```
/// use audit_trail::{
/// Action, Actor, Chain, Clock, Digest, Hasher, Outcome, Record, RecordId, Sink,
/// SinkError, Target, Timestamp, Verifier, HASH_LEN,
/// };
///
/// // XOR-fold hasher (insecure — for demonstration only).
/// struct XorHasher([u8; HASH_LEN], usize);
/// impl Hasher for XorHasher {
/// fn reset(&mut self) { self.0 = [0u8; HASH_LEN]; self.1 = 0; }
/// fn update(&mut self, bytes: &[u8]) {
/// for b in bytes { self.0[self.1 % HASH_LEN] ^= *b; self.1 = self.1.wrapping_add(1); }
/// }
/// fn finalize(&mut self, out: &mut Digest) { *out = Digest::from_bytes(self.0); }
/// }
///
/// // Capture every record the chain emits.
/// #[derive(Default)]
/// struct CaptureSink { records: Vec<(RecordId, Timestamp, Digest, Digest, Outcome)> }
/// impl Sink for CaptureSink {
/// fn write(&mut self, r: &Record<'_>) -> Result<(), SinkError> {
/// self.records.push((r.id(), r.timestamp(), r.prev_hash(), r.hash(), r.outcome()));
/// Ok(())
/// }
/// }
///
/// struct Tick(core::cell::Cell<u64>);
/// impl Clock for Tick {
/// fn now(&self) -> Timestamp {
/// let v = self.0.get(); self.0.set(v + 1); Timestamp::from_nanos(v)
/// }
/// }
///
/// // Build a 3-record chain.
/// let mut chain = Chain::new(
/// XorHasher([0u8; HASH_LEN], 0),
/// CaptureSink::default(),
/// Tick(core::cell::Cell::new(1)),
/// );
/// chain.append(Actor::new("a"), Action::new("x"), Target::new("t"), Outcome::Success).unwrap();
/// chain.append(Actor::new("a"), Action::new("y"), Target::new("u"), Outcome::Success).unwrap();
/// chain.append(Actor::new("a"), Action::new("z"), Target::new("v"), Outcome::Failure).unwrap();
/// let (_h, sink, _c) = chain.into_parts();
///
/// // Replay every captured record through the verifier.
/// let actors = ["a", "a", "a"];
/// let actions = ["x", "y", "z"];
/// let targets = ["t", "u", "v"];
/// let mut verifier = Verifier::new(XorHasher([0u8; HASH_LEN], 0));
/// for (i, (id, ts, prev, hash, outcome)) in sink.records.iter().enumerate() {
/// let record = Record::new(
/// *id, *ts,
/// Actor::new(actors[i]), Action::new(actions[i]), Target::new(targets[i]),
/// *outcome, *prev, *hash,
/// );
/// verifier.verify(&record).unwrap();
/// }
/// assert_eq!(verifier.next_id(), RecordId::from_u64(3));
/// ```