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
use std::collections::HashSet;

use bc_components::{DigestProvider, Digest};
#[cfg(feature = "encrypt")]
use bc_components::{SymmetricKey, Nonce};
#[cfg(feature = "encrypt")]
use dcbor::prelude::*;

use crate::{Assertion, Envelope, EnvelopeEncodable, EnvelopeError};

use super::envelope::EnvelopeCase;

/// An action to perform on a target set in an envelope.
pub enum ObscureAction {
    /// Elide the target, leaving only its digest.
    Elide,

    /// Encrypt the target using the specified key.
    #[cfg(feature = "encrypt")]
    Encrypt(SymmetricKey),

    /// Compress the target.
    #[cfg(feature = "compress")]
    Compress,
}

/// Support for eliding elements from envelopes.
///
/// This includes eliding, encrypting and compressing (obscuring) elements.
impl Envelope {
    /// Returns the elided variant of this envelope.
    ///
    /// Returns the same envelope if it is already elided.
    pub fn elide(&self) -> Self {
        match self.case() {
            EnvelopeCase::Elided(_) => self.clone(),
            _ => Self::new_elided(self.digest().into_owned())
        }
    }

    /// Returns a version of this envelope with elements in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: The target set of digests.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_removing_set_with_action(&self, target: &HashSet<Digest>, action: &ObscureAction) -> Self {
        self.elide_set_with_action(target, false, action)
    }

    /// Returns a version of this envelope with elements in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: The target set of digests.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_removing_set(&self, target: &HashSet<Digest>) -> Self {
        self.elide_set(target, false)
    }

    /// Returns a version of this envelope with elements in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: An array of `DigestProvider`s.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_removing_array_with_action(&self, target: &[&dyn DigestProvider], action: &ObscureAction) -> Self {
        self.elide_array_with_action(target, false, action)
    }

    /// Returns a version of this envelope with elements in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: An array of `DigestProvider`s.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_removing_array(&self, target: &[&dyn DigestProvider]) -> Self {
        self.elide_array(target, false)
    }

    /// Returns a version of this envelope with the target element elided.
    ///
    /// - Parameters:
    ///   - target: A `DigestProvider`.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_removing_target_with_action(&self, target: &dyn DigestProvider, action: &ObscureAction) -> Self {
        self.elide_target_with_action(target, false, action)
    }

    /// Returns a version of this envelope with the target element elided.
    ///
    /// - Parameters:
    ///   - target: A `DigestProvider`.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_removing_target(&self, target: &dyn DigestProvider) -> Self {
        self.elide_target(target, false)
    }

    /// Returns a version of this envelope with elements *not* in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: The target set of digests.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_revealing_set_with_action(&self, target: &HashSet<Digest>, action: &ObscureAction) -> Self {
        self.elide_set_with_action(target, true, action)
    }

    /// Returns a version of this envelope with elements *not* in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: The target set of digests.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_revealing_set(&self, target: &HashSet<Digest>) -> Self {
        self.elide_set(target, true)
    }

    /// Returns a version of this envelope with elements *not* in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: An array of `DigestProvider`s.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_revealing_array_with_action(&self, target: &[&dyn DigestProvider], action: &ObscureAction) -> Self {
        self.elide_array_with_action(target, true, action)
    }

    /// Returns a version of this envelope with elements *not* in the `target` set elided.
    ///
    /// - Parameters:
    ///   - target: An array of `DigestProvider`s.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_revealing_array(&self, target: &[&dyn DigestProvider]) -> Self {
        self.elide_array(target, true)
    }

    /// Returns a version of this envelope with all elements *except* the target element elided.
    ///
    /// - Parameters:
    ///   - target: A `DigestProvider`.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_revealing_target_with_action(&self, target: &dyn DigestProvider, action: &ObscureAction) -> Self {
        self.elide_target_with_action(target, true, action)
    }

    /// Returns a version of this envelope with all elements *except* the target element elided.
    ///
    /// - Parameters:
    ///   - target: A `DigestProvider`.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_revealing_target(&self, target: &dyn DigestProvider) -> Self {
        self.elide_target(target, true)
    }

    // Target Matches   isRevealing     elide
    // ----------------------------------------
    //     false           false        false
    //     false           true         true
    //     true            false        true
    //     true            true         false

    /// Returns an elided version of this envelope.
    ///
    /// - Parameters:
    ///   - target: The target set of digests.
    ///   - isRevealing: If `true`, the target set contains the digests of the elements to
    ///   leave revealed. If it is `false`, the target set contains the digests of the
    ///   elements to elide.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_set_with_action(&self, target: &HashSet<Digest>, is_revealing: bool, action: &ObscureAction) -> Self {
        let self_digest = self.digest().into_owned();
        if target.contains(&self_digest) != is_revealing {
            match action {
                ObscureAction::Elide => self.elide(),
                #[cfg(feature = "encrypt")]
                ObscureAction::Encrypt(key) => {
                    let message = key.encrypt(self.tagged_cbor().cbor_data(), Some(self_digest), None::<Nonce>);
                    Self::new_with_encrypted(message).unwrap()
                },
                #[cfg(feature = "compress")]
                ObscureAction::Compress => self.compress().unwrap(),
            }
        } else if let EnvelopeCase::Assertion(assertion) = self.case() {
            let predicate = assertion.predicate().elide_set_with_action(target, is_revealing, action);
            let object = assertion.object().elide_set_with_action(target, is_revealing, action);
            let elided_assertion = Assertion::new(predicate, object);
            assert!(&elided_assertion == assertion);
            Self::new_with_assertion(elided_assertion)
        } else if let EnvelopeCase::Node { subject, assertions, ..} = self.case() {
            let elided_subject = subject.elide_set_with_action(target, is_revealing, action);
            assert!(elided_subject.digest() == subject.digest());
            let elided_assertions = assertions.iter().map(|assertion| {
                let elided_assertion = assertion.elide_set_with_action(target, is_revealing, action);
                assert!(elided_assertion.digest() == assertion.digest());
                elided_assertion
            }).collect();
            Self::new_with_unchecked_assertions(elided_subject, elided_assertions)
        } else if let EnvelopeCase::Wrapped { envelope, .. } = self.case() {
            let elided_envelope = envelope.elide_set_with_action(target, is_revealing, action);
            assert!(elided_envelope.digest() == envelope.digest());
            Self::new_wrapped(elided_envelope)
        } else {
            self.clone()
        }
    }

    /// Returns an elided version of this envelope.
    ///
    /// - Parameters:
    ///   - target: The target set of digests.
    ///   - isRevealing: If `true`, the target set contains the digests of the elements to
    ///   leave revealed. If it is `false`, the target set contains the digests of the
    ///   elements to elide.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_set(&self, target: &HashSet<Digest>, is_revealing: bool) -> Self {
        self.elide_set_with_action(target, is_revealing, &ObscureAction::Elide)
    }

    /// Returns an elided version of this envelope.
    ///
    /// - Parameters:
    ///   - target: An array of `DigestProvider`s.
    ///   - isRevealing: If `true`, the target set contains the digests of the elements to
    ///   leave revealed. If it is `false`, the target set contains the digests of the
    ///   elements to elide.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_array_with_action(&self, target: &[&dyn DigestProvider], is_revealing: bool, action: &ObscureAction) -> Self {
        self.elide_set_with_action(&target.iter().map(|provider| provider.digest().into_owned()).collect(), is_revealing, action)
    }

    /// Returns an elided version of this envelope.
    ///
    /// - Parameters:
    ///   - target: An array of `DigestProvider`s.
    ///   - isRevealing: If `true`, the target set contains the digests of the elements to
    ///   leave revealed. If it is `false`, the target set contains the digests of the
    ///   elements to elide.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_array(&self, target: &[&dyn DigestProvider], is_revealing: bool) -> Self {
        self.elide_array_with_action(target, is_revealing, &ObscureAction::Elide)
    }

    /// Returns an elided version of this envelope.
    ///
    /// - Parameters:
    ///   - target: A `DigestProvider`.
    ///   - isRevealing: If `true`, the target is the element to leave revealed, eliding
    ///   all others. If it is `false`, the target is the element to elide, leaving all
    ///   others revealed.
    ///   - action: Perform the specified action (elision, encryption or compression).
    ///
    /// - Returns: The elided envelope.
    pub fn elide_target_with_action(&self, target: &dyn DigestProvider, is_revealing: bool, action: &ObscureAction) -> Self {
        self.elide_array_with_action(&[target], is_revealing, action)
    }

    /// Returns an elided version of this envelope.
    ///
    /// - Parameters:
    ///   - target: A `DigestProvider`.
    ///   - isRevealing: If `true`, the target is the element to leave revealed, eliding
    ///   all others. If it is `false`, the target is the element to elide, leaving all
    ///   others revealed.
    ///
    /// - Returns: The elided envelope.
    pub fn elide_target(&self, target: &dyn DigestProvider, is_revealing: bool) -> Self {
        self.elide_target_with_action(target, is_revealing, &ObscureAction::Elide)
    }

    /// Returns the unelided variant of this envelope.
    ///
    /// Returns the same envelope if it is already unelided.
    pub fn unelide(&self, envelope: impl EnvelopeEncodable) -> Result<Self, EnvelopeError> {
        let envelope = envelope.envelope();
        if self.digest() == envelope.digest() {
            Ok(envelope)
        } else {
            Err(EnvelopeError::InvalidDigest)
        }
    }
}