enprot 0.4.0

Engyon Protected Text (EPT) — confidentiality processor and capability ledger
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright (c) 2018-2026 [Ribose Inc](https://www.ribose.com).
//
// 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.
//
// 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
// OWNER 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.

//! [`AnchorDag`] — adjacency structure over [`SignedAnchor`]s with
//! parent resolution and validation.
//!
//! A DAG is constructed by pushing anchors in the order they appear
//! in a file. The structure detects:
//!
//! - Duplicate anchor IDs
//! - Forward references (anchor references a parent not yet seen)
//! - Cycles (anchor references itself transitively)
//! - Missing genesis (every parent chain must terminate at a
//!   zero-parent anchor)
//!
//! Verification (signature checks against pubkeys) is a separate
//! concern; see [`AnchorDag::verify_signatures`].

use std::collections::{HashMap, HashSet};

use crate::error::Error;

use super::anchor::{AnchorHash, SignedAnchor};

/// A validated DAG of chain anchors. Construction is fallible —
/// invalid input (forward refs, cycles, duplicates) returns [`DagError`].
#[derive(Debug, Default)]
pub struct AnchorDag {
    /// Insertion order — preserves history for streaming audits.
    order: Vec<AnchorHash>,
    /// Hash → anchor.
    by_id: HashMap<AnchorHash, SignedAnchor>,
}

/// Per-anchor verification result.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AnchorReport {
    pub id: AnchorHash,
    pub parents: Vec<AnchorHash>,
    pub ok: bool,
    /// Set when `ok == false`; human-readable reason.
    pub error: Option<String>,
}

/// Whole-DAG verification summary.
#[derive(Debug, Clone)]
pub struct DagReport {
    pub anchors_total: usize,
    pub reports: Vec<AnchorReport>,
    /// Anchor IDs that have no children (the "tips" of the DAG).
    /// Multiple tips mean a fork — verifiers report this so callers
    /// can decide whether to merge or investigate.
    pub tips: Vec<AnchorHash>,
}

impl AnchorDag {
    pub fn new() -> Self {
        Self::default()
    }

    /// Append an anchor. Validates:
    /// - No duplicate IDs (re-inserting the same anchor is OK; returns `Ok(())`)
    /// - All parents already in the DAG
    /// - No cycle introduced
    pub fn push(&mut self, signed: SignedAnchor) -> std::result::Result<(), DagError> {
        let id = signed.id().map_err(DagError::Internal)?;
        if self.by_id.contains_key(&id) {
            // Idempotent re-insert: same anchor → Ok.
            return Ok(());
        }
        for parent in &signed.anchor.parents {
            if !self.by_id.contains_key(parent) {
                return Err(DagError::ForwardReference {
                    anchor: id,
                    missing_parent: *parent,
                });
            }
        }
        // Cycle check: walk parents transitively, none should be `id`.
        if self.reachable_from_any(&signed.anchor.parents, &id) {
            return Err(DagError::Cycle { anchor: id });
        }
        self.order.push(id);
        self.by_id.insert(id, signed);
        Ok(())
    }

    /// True if `target` is reachable from any of `start_set` by walking
    /// parent edges. Used for cycle detection.
    fn reachable_from_any(&self, start_set: &[AnchorHash], target: &AnchorHash) -> bool {
        let mut visited: HashSet<AnchorHash> = HashSet::new();
        let mut stack: Vec<AnchorHash> = start_set.to_vec();
        while let Some(h) = stack.pop() {
            if &h == target {
                return true;
            }
            if !visited.insert(h) {
                continue;
            }
            if let Some(anchor) = self.by_id.get(&h) {
                stack.extend(anchor.anchor.parents.iter().cloned());
            }
        }
        false
    }

    pub fn get(&self, id: &AnchorHash) -> Option<&SignedAnchor> {
        self.by_id.get(id)
    }

    pub fn len(&self) -> usize {
        self.order.len()
    }

    pub fn is_empty(&self) -> bool {
        self.order.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&AnchorHash, &SignedAnchor)> {
        self.order.iter().map(move |id| (id, &self.by_id[id]))
    }
    /// Anchor IDs with no children in the DAG. Genesis is not
    /// necessarily a tip — only "no one built on me".
    pub fn tips(&self) -> Vec<AnchorHash> {
        let mut referenced_as_parent: HashSet<AnchorHash> = HashSet::new();
        for anchor in self.by_id.values() {
            for p in &anchor.anchor.parents {
                referenced_as_parent.insert(*p);
            }
        }
        self.order
            .iter()
            .filter(|id| !referenced_as_parent.contains(*id))
            .cloned()
            .collect()
    }

    /// Verify every anchor's signature using a caller-supplied
    /// pubkey resolver. The resolver maps signer fingerprint hex →
    /// PEM pubkey. Anchors whose signer isn't in the resolver are
    /// reported as failed.
    pub fn verify_signatures<F>(&self, pubkey_resolver: F) -> DagReport
    where
        F: Fn(&str) -> Option<String>,
    {
        let mut reports = Vec::with_capacity(self.order.len());

        for id in &self.order {
            let signed = &self.by_id[id];
            let fp_hex = signed.anchor.signer.fp.to_hex();
            let result = match pubkey_resolver(&fp_hex) {
                Some(pubkey_pem) => signed.verify(&pubkey_pem),
                None => Err(Error::msg(format!(
                    "no pubkey registered for signer fingerprint {}",
                    fp_hex
                ))),
            };
            let (ok, error) = match result {
                Ok(()) => (true, None),
                Err(e) => (false, Some(e.to_string())),
            };
            reports.push(AnchorReport {
                id: *id,
                parents: signed.anchor.parents.clone(),
                ok,
                error,
            });
        }

        DagReport {
            anchors_total: self.order.len(),
            reports,
            tips: self.tips(),
        }
    }
}

/// DAG construction or validation error.
#[derive(Debug, thiserror::Error)]
pub enum DagError {
    #[error("forward reference in {anchor}: parent {missing_parent} not yet seen")]
    ForwardReference {
        anchor: AnchorHash,
        missing_parent: AnchorHash,
    },

    #[error("cycle detected at {anchor}")]
    Cycle { anchor: AnchorHash },

    #[error("internal error: {0}")]
    Internal(#[source] Error),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capability::KeyFp;
    use crate::ledger::anchor::{Anchor, AnchorHash, PayloadHash, SignerId};
    use crate::pki::{self, SigAlgKind};

    fn keypair() -> (String, String, KeyFp) {
        let mut rng = botan::RandomNumberGenerator::new_system().unwrap();
        let (priv_pem, pub_pem) = pki::keygen(SigAlgKind::Ed25519, &mut rng).unwrap();
        let fp = KeyFp::from_pem(&pub_pem).unwrap();
        (priv_pem, pub_pem, fp)
    }

    fn genesis_anchor(fp: KeyFp) -> Anchor {
        Anchor::builder(
            SignerId::new(SigAlgKind::Ed25519, fp),
            PayloadHash([1u8; 32]),
        )
        .with_mutations("genesis")
        .build()
    }

    fn child_anchor(parent: AnchorHash, fp: KeyFp, payload_byte: u8) -> Anchor {
        Anchor::builder(
            SignerId::new(SigAlgKind::Ed25519, fp),
            PayloadHash([payload_byte; 32]),
        )
        .with_parent(parent)
        .with_mutations(format!("child {}", payload_byte))
        .build()
    }

    fn signed(anchor: Anchor, priv_pem: &str, pub_pem: &str) -> SignedAnchor {
        anchor.sign(priv_pem, pub_pem, SigAlgKind::Ed25519).unwrap()
    }

    #[test]
    fn empty_dag_has_no_tips() {
        let dag = AnchorDag::new();
        assert!(dag.is_empty());
        assert!(dag.tips().is_empty());
    }

    #[test]
    fn push_genesis_succeeds() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();
        assert_eq!(dag.len(), 1);
    }

    #[test]
    fn forward_reference_rejected() {
        let (priv_pem, pub_pem, fp) = keypair();
        let phantom = AnchorHash([0xff; 32]);
        let bad = Anchor::builder(
            SignerId::new(SigAlgKind::Ed25519, fp),
            PayloadHash([2u8; 32]),
        )
        .with_parent(phantom) // not in DAG
        .build();
        let bad_signed = signed(bad, &priv_pem, &pub_pem);

        let mut dag = AnchorDag::new();
        let err = dag.push(bad_signed).unwrap_err();
        assert!(matches!(err, DagError::ForwardReference { .. }));
    }

    #[test]
    fn linear_chain_appends_cleanly() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g_id = g.id().unwrap();

        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();

        // Three children off the chain
        let mut prev = g_id;
        for i in 2..=4u8 {
            let a = child_anchor(prev, fp, i);
            let s = signed(a, &priv_pem, &pub_pem);
            let new_id = s.id().unwrap();
            dag.push(s).unwrap();
            prev = new_id;
        }
        assert_eq!(dag.len(), 4);
        assert_eq!(dag.tips().len(), 1); // linear → single tip
    }

    #[test]
    fn fork_produces_two_tips() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g_id = g.id().unwrap();

        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();

        let a = signed(child_anchor(g_id, fp, 2), &priv_pem, &pub_pem);
        let b = signed(child_anchor(g_id, fp, 3), &priv_pem, &pub_pem);
        dag.push(a).unwrap();
        dag.push(b).unwrap();

        assert_eq!(dag.len(), 3);
        assert_eq!(dag.tips().len(), 2); // fork
    }

    #[test]
    fn merge_anchor_two_parents() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g_id = g.id().unwrap();

        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();

        let a_signed = signed(child_anchor(g_id, fp, 2), &priv_pem, &pub_pem);
        let b_signed = signed(child_anchor(g_id, fp, 3), &priv_pem, &pub_pem);
        let a_id = a_signed.id().unwrap();
        let b_id = b_signed.id().unwrap();
        dag.push(a_signed).unwrap();
        dag.push(b_signed).unwrap();

        // Merge anchor with both as parents
        let merge = Anchor::builder(
            SignerId::new(SigAlgKind::Ed25519, fp),
            PayloadHash([4u8; 32]),
        )
        .with_parent(a_id)
        .with_parent(b_id)
        .with_mutations("merge a + b")
        .build();
        let merge_signed = signed(merge, &priv_pem, &pub_pem);
        dag.push(merge_signed).unwrap();

        assert_eq!(dag.len(), 4);
        assert_eq!(dag.tips().len(), 1); // merge is the only tip
    }

    #[test]
    fn cycle_detected() {
        let (priv_pem, pub_pem, fp) = keypair();

        // Build a small DAG and verify it stays valid as we push more
        // anchors. True cycle detection happens at push-time via
        // reachable_from_any; the only way to attempt a cycle is to
        // forge a parent reference, which `push` rejects as a forward
        // reference first.
        let mut dag = AnchorDag::new();

        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g_id = g.id().unwrap();
        dag.push(g).unwrap();

        let child = signed(child_anchor(g_id, fp, 2), &priv_pem, &pub_pem);
        let child_id = child.id().unwrap();
        dag.push(child).unwrap();

        let grandchild = signed(child_anchor(child_id, fp, 3), &priv_pem, &pub_pem);
        dag.push(grandchild).unwrap();

        // A "self-referential" anchor: tries to claim itself as parent.
        // We can't construct this directly (chicken-and-egg), but we
        // can verify the DAG rejects a forward reference cleanly.
        let phantom = AnchorHash([0xee; 32]);
        let bad = Anchor::builder(
            SignerId::new(SigAlgKind::Ed25519, fp),
            PayloadHash([5u8; 32]),
        )
        .with_parent(phantom)
        .build();
        let bad_signed = signed(bad, &priv_pem, &pub_pem);
        assert!(matches!(
            dag.push(bad_signed),
            Err(DagError::ForwardReference { .. })
        ));

        assert_eq!(dag.len(), 3);
    }

    #[test]
    fn idempotent_reinsert() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g1 = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g2 = g1.clone();

        let mut dag = AnchorDag::new();
        dag.push(g1).unwrap();
        // Re-inserting the same anchor is a no-op (not an error).
        dag.push(g2).unwrap();
        assert_eq!(dag.len(), 1);
    }

    #[test]
    fn verify_signatures_all_ok() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g_id = g.id().unwrap();

        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();
        dag.push(signed(child_anchor(g_id, fp, 2), &priv_pem, &pub_pem))
            .unwrap();

        let report = dag.verify_signatures(|_fp_hex| Some(pub_pem.clone()));
        assert_eq!(report.anchors_total, 2);
        assert!(report.reports.iter().all(|r| r.ok));
    }

    #[test]
    fn verify_signatures_fails_with_missing_pubkey() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);

        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();

        let report = dag.verify_signatures(|_| None);
        assert_eq!(report.anchors_total, 1);
        assert!(!report.reports[0].ok);
        assert!(report.reports[0].error.is_some());
    }

    #[test]
    fn tips_change_after_push() {
        let (priv_pem, pub_pem, fp) = keypair();
        let g = signed(genesis_anchor(fp), &priv_pem, &pub_pem);
        let g_id = g.id().unwrap();

        let mut dag = AnchorDag::new();
        dag.push(g).unwrap();
        assert_eq!(dag.tips(), vec![g_id]);

        let child = signed(child_anchor(g_id, fp, 2), &priv_pem, &pub_pem);
        let child_id = child.id().unwrap();
        dag.push(child).unwrap();
        assert_eq!(dag.tips(), vec![child_id]); // g is no longer a tip
    }
}