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
//! `Seal<D>` — the adapter through which a reveal can be *verified*.
//!
//! Lives in `adapter.rs` (not `seal.rs`) to avoid `seal::seal`.
use crateCard;
use crateDeckedBase;
use crateSlotId;
use Rng;
/// A card-sealing scheme: the caller's lock and key.
///
/// cardpack defines the shape; the *caller* provides the implementation, the
/// keys, and the tokens. cardpack never constructs an `S`, never stores one,
/// and **no cardpack type is generic over one** — the only kernel caller is
/// [`Revealed::reveal_with`](crate::seal::revealed::Revealed::reveal_with),
/// which is generic at the method.
///
/// Five items: three associated types and two methods. The slot is passed
/// to both methods so a backend *may* bind payload to slot (an AEAD does; an
/// `ElGamal` scheme ignores it); `seal` takes an RNG because every real
/// backend is randomized.
///
/// The round-trip law every implementation must satisfy —
/// `unseal(seal(card, slot, rng), slot, token) == card` — is one generic
/// test, `seal_roundtrip` (behind `seal-test-double`), exported
/// under the `seal-test-double` feature so backends in other crates can run it.
///
/// # Implementing one
///
/// ```
/// use cardpack::prelude::*;
///
/// // A toy scheme: `Sealed` is the card itself, so the "secret" is a lie.
/// // Real backends do real crypto — see `HolderKeySeal` (`seal-aead`).
/// struct Toy;
///
/// impl Seal<Standard52> for Toy {
/// type Sealed = Card<Standard52>;
/// type Token = u16;
/// type Error = CardError;
///
/// fn seal(&self, card: Card<Standard52>, _slot: SlotId, _rng: &mut dyn rand::Rng)
/// -> Result<Card<Standard52>, CardError> { Ok(card) }
///
/// fn unseal(&self, sealed: &Card<Standard52>, _slot: SlotId, token: &u16)
/// -> Result<Card<Standard52>, CardError>
/// {
/// if *token == 42 { Ok(*sealed) } else { Err(CardError::Fubar) }
/// }
/// }
///
/// let ace = Standard52::deck().cards()[0];
/// use rand::SeedableRng;
/// let mut rng = rand::rngs::StdRng::seed_from_u64(1);
///
/// let sealed = Toy.seal(ace, SlotId::new(3), &mut rng)?;
///
/// assert_eq!(Toy.unseal(&sealed, SlotId::new(3), &42)?, ace);
/// assert!(Toy.unseal(&sealed, SlotId::new(3), &0).is_err());
/// # Ok::<(), CardError>(())
/// ```