derec-library 0.0.1-alpha.8

Rust SDK for the DeRec protocol, including native and WebAssembly bindings.
Documentation
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

use crate::primitives::recovery::RecoveryError;
use crate::utils::verify_timestamps;
use crate::{
    derec_message::{DeRecMessageBuilder, current_timestamp, extract_inner_message},
    types::{ChannelId, SharedKey},
};
use derec_cryptography::vss;
use derec_proto::{
    CommittedDeRecShare, DeRecMessage, DeRecResult, DeRecShare, GetShareRequestMessage,
    GetShareResponseMessage, MessageBody, StatusEnum, StoreShareRequestMessage,
};
use prost::Message;

const SHARE_ALGORITHM_VSS: i32 = 0;

pub struct ProduceResult {
    /// Serialized outer [`derec_proto::DeRecMessage`] envelope with the encrypted response payload.
    pub envelope: Vec<u8>,
}

pub struct ExtractResult {
    pub response: GetShareResponseMessage,
}

pub struct RecoverResult {
    pub secret_data: Vec<u8>,
}

/// Produces a recovery response envelope containing the requested committed share.
///
/// This function is typically executed by a Helper after receiving a recovery request and
/// locating the corresponding stored share from the earlier sharing flow.
///
/// The Helper:
///
/// 1. Validates that `stored_share_request` contains non-empty committed share bytes
/// 2. Decodes the embedded [`derec_proto::CommittedDeRecShare`] and inner [`derec_proto::DeRecShare`]
/// 3. Validates that the stored share matches the requested `secret_id` and `version`
/// 4. Builds and encrypts a [`derec_proto::GetShareResponseMessage`] carrying the committed share
///
/// # Arguments
///
/// * `channel_id` - Identifier of the previously paired Helper channel.
/// * `request` - The decoded [`derec_proto::GetShareRequestMessage`] previously extracted
///   from the recovery request envelope.
/// * `stored_share_request` - The decoded [`derec_proto::StoreShareRequestMessage`] previously
///   stored by this Helper during the sharing flow.
/// * `shared_key` - Previously established 32-byte symmetric channel key used to encrypt
///   the response.
///
/// # Returns
///
/// On success returns [`ProduceResult`] containing:
///
/// - `envelope`: serialized outer [`derec_proto::DeRecMessage`] bytes carrying an encrypted
///   inner [`derec_proto::GetShareResponseMessage`]
///
/// # Errors
///
/// Returns [`crate::Error`] (specifically `Error::Recovery(...)`) in the following cases:
///
/// - [`RecoveryError::EmptyCommittedDeRecShare`] if the stored share has no committed share bytes
/// - [`RecoveryError::DecodeCommittedDeRecShare`] if the committed share cannot be decoded
/// - [`RecoveryError::DecodeDeRecShare`] if the inner [`derec_proto::DeRecShare`] cannot be decoded
/// - [`RecoveryError::SecretIdMismatch`] if the stored share does not match the requested `secret_id`
/// - [`RecoveryError::VersionMismatch`] if the stored share does not match the requested `version`
/// - outer response envelope construction or encryption fails
///
/// # Security Notes
///
/// - The response contains share material and must be treated as sensitive.
///
/// # Example
///
/// ```
/// use derec_library::primitives::{recovery, sharing};
/// use derec_library::types::ChannelId;
///
/// let channels = [ChannelId(1), ChannelId(2), ChannelId(3)];
/// let shared_key = [42u8; 32];
/// let channel_id = ChannelId(1);
///
/// // Sharing flow: produce a stored share that the Helper retains for recovery.
/// let sharing::request::SplitResult { shares } =
///     sharing::request::split(&channels, 1, 1, b"super_secret_value", 2)
///         .expect("split failed");
/// let committed_share = shares.get(&channel_id).expect("missing share");
/// let sharing::request::ProduceResult { envelope: share_envelope } =
///     sharing::request::produce(channel_id, 1, 1, committed_share, &[], "", &shared_key, None, None)
///         .expect("share produce failed");
/// let sharing::request::ExtractResult { request: stored_share_request } =
///     sharing::request::extract(&share_envelope, &shared_key).expect("share extract failed");
///
/// // Recovery flow: Owner asks for the share back, Helper answers.
/// let recovery::request::ProduceResult { envelope: req_envelope } =
///     recovery::request::produce(channel_id, 1, 1, &shared_key, None).expect("recovery request failed");
/// let recovery::request::ExtractResult { request: get_share_request } =
///     recovery::request::extract(&req_envelope, &shared_key)
///         .expect("recovery request extract failed");
///
/// let recovery::response::ProduceResult { envelope } =
///     recovery::response::produce(channel_id, &get_share_request, &stored_share_request, &shared_key)
///         .expect("recovery response failed");
///
/// assert!(!envelope.is_empty());
/// ```
#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(channel_id = channel_id.0, version = request.version))
)]
pub fn produce(
    channel_id: ChannelId,
    request: &GetShareRequestMessage,
    stored_share_request: &StoreShareRequestMessage,
    shared_key: &SharedKey,
) -> Result<ProduceResult, crate::Error> {
    if stored_share_request.share.is_empty() {
        #[cfg(feature = "logging")]
        tracing::warn!("stored share is empty");

        return Err(RecoveryError::EmptyCommittedDeRecShare.into());
    }

    let committed_derec_share = CommittedDeRecShare::decode(stored_share_request.share.as_slice())
        .map_err(|source| RecoveryError::DecodeCommittedDeRecShare { source })?;

    let derec_share = DeRecShare::decode(committed_derec_share.de_rec_share.as_slice())
        .map_err(|source| RecoveryError::DecodeDeRecShare { source })?;

    if derec_share.secret_id != request.secret_id {
        #[cfg(feature = "logging")]
        tracing::warn!("secret_id mismatch between request and stored share");
        return Err(RecoveryError::SecretIdMismatch.into());
    }

    if derec_share.version != request.version {
        #[cfg(feature = "logging")]
        tracing::warn!(
            expected = request.version,
            got = derec_share.version,
            "version mismatch between request and stored share"
        );

        return Err(RecoveryError::VersionMismatch {
            expected: request.version,
            got: derec_share.version,
        }
        .into());
    }

    let timestamp = current_timestamp();

    let message = GetShareResponseMessage {
        share_algorithm: SHARE_ALGORITHM_VSS,
        committed_de_rec_share: stored_share_request.share.clone(),
        result: Some(DeRecResult {
            status: StatusEnum::Ok as i32,
            memo: String::new(),
        }),
        timestamp: Some(timestamp),
        secret_id: request.secret_id,
        version: request.version,
    };

    let envelope = DeRecMessageBuilder::channel()
        .channel_id(channel_id)
        .timestamp(timestamp)
        .message_body(MessageBody::GetShareResponse(message))
        .encrypt(shared_key)?
        .build()?
        .encode_to_vec();

    #[cfg(feature = "logging")]
    tracing::info!("recovery response envelope produced");

    Ok(ProduceResult { envelope })
}

/// Decrypts and decodes an incoming [`derec_proto::GetShareResponseMessage`] from an outer
/// [`derec_proto::DeRecMessage`] envelope.
///
/// This function:
///
/// 1. Decodes the outer [`derec_proto::DeRecMessage`] envelope from `envelope_bytes`
/// 2. Decrypts and decodes the inner [`derec_proto::GetShareResponseMessage`] using `shared_key`
/// 3. Validates the invariant `envelope.timestamp == response.timestamp`
///
/// Call this on the **Owner** side after receiving a recovery response from a Helper.
/// The decrypted response can then be passed to [`recover`] as part of the `responses` slice.
///
/// # Arguments
///
/// * `envelope_bytes` - Serialized outer [`derec_proto::DeRecMessage`] bytes carrying an
///   encrypted inner [`derec_proto::GetShareResponseMessage`], as produced by [`produce`].
/// * `shared_key` - Previously established 32-byte symmetric channel key used to decrypt
///   the inner message.
///
/// # Returns
///
/// On success returns [`ExtractResult`] containing:
///
/// - `response`: the decrypted inner [`derec_proto::GetShareResponseMessage`]
///
/// # Errors
///
/// Returns [`crate::Error`] if:
///
/// - `envelope_bytes` cannot be decoded as a valid [`derec_proto::DeRecMessage`]
/// - decryption or inner-message decoding fails
/// - `envelope.timestamp != response.timestamp`
/// - the inner message is not a [`derec_proto::GetShareResponseMessage`]
///
/// # Security Notes
///
/// - The decrypted response carries the Helper's committed share material and must be
///   treated as sensitive.
/// - **No freshness or replay protection.** The timestamp check
///   enforced here only binds the envelope to the inner body
///   (`envelope.timestamp == body.timestamp`). It does NOT enforce
///   a freshness window against the receiver's clock and does NOT
///   detect replays of a previously-captured ciphertext. Because
///   the channel key is long-lived, a recorded envelope stays
///   decryptable indefinitely. Callers MUST add a freshness window
///   and per-channel anti-replay (monotonic counter or nonce log)
///   on top before driving any side-effecting state off the parsed
///   body.
///
/// # Example
///
/// ```
/// use derec_library::primitives::{recovery, sharing};
/// use derec_library::types::ChannelId;
///
/// let channels = [ChannelId(1), ChannelId(2), ChannelId(3)];
/// let shared_key = [42u8; 32];
/// let channel_id = ChannelId(1);
///
/// // Sharing flow: produce a stored share that the Helper retains for recovery.
/// let sharing::request::SplitResult { shares } =
///     sharing::request::split(&channels, 1, 1, b"super_secret_value", 2)
///         .expect("split failed");
/// let committed_share = shares.get(&channel_id).expect("missing share");
/// let sharing::request::ProduceResult { envelope: share_envelope } =
///     sharing::request::produce(channel_id, 1, 1, committed_share, &[], "", &shared_key, None, None)
///         .expect("share produce failed");
/// let sharing::request::ExtractResult { request: stored_share_request } =
///     sharing::request::extract(&share_envelope, &shared_key).expect("share extract failed");
///
/// // Recovery flow: Owner asks for the share back, Helper answers, Owner extracts.
/// let recovery::request::ProduceResult { envelope: req_envelope } =
///     recovery::request::produce(channel_id, 1, 1, &shared_key, None).expect("recovery request failed");
/// let recovery::request::ExtractResult { request: get_share_request } =
///     recovery::request::extract(&req_envelope, &shared_key)
///         .expect("recovery request extract failed");
/// let recovery::response::ProduceResult { envelope: resp_envelope } =
///     recovery::response::produce(channel_id, &get_share_request, &stored_share_request, &shared_key)
///         .expect("recovery response failed");
///
/// let recovery::response::ExtractResult { response } =
///     recovery::response::extract(&resp_envelope, &shared_key)
///         .expect("recovery response extract failed");
///
/// assert!(response.result.is_some());
/// ```
#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(envelope_len = envelope_bytes.len()))
)]
pub fn extract(
    envelope_bytes: &[u8],
    shared_key: &SharedKey,
) -> Result<ExtractResult, crate::Error> {
    let envelope = DeRecMessage::decode(envelope_bytes).map_err(crate::Error::ProtobufDecode)?;

    let response = match extract_inner_message(&envelope.message, shared_key)? {
        MessageBody::GetShareResponse(message) => message,
        _ => {
            #[cfg(feature = "logging")]
            tracing::warn!("unexpected message type; expected GetShareResponseMessage");
            return Err(crate::Error::Invariant(
                "Invalid message. Expected: GetShareResponseMessage",
            ));
        }
    };

    verify_timestamps(envelope.timestamp, response.timestamp)?;

    #[cfg(feature = "logging")]
    tracing::info!("recovery response extracted and validated");

    Ok(ExtractResult { response })
}

/// Reconstructs the original secret from Helper recovery responses.
///
/// This function performs the recovery pipeline:
///
/// 1. Validates inputs (`responses`, `secret_id`, `version`)
/// 2. Validates that each response reports `result.status == Ok`
/// 3. Decodes each embedded [`derec_proto::CommittedDeRecShare`]
/// 4. Decodes each embedded [`derec_proto::DeRecShare`]
/// 5. Validates that each share matches the requested `secret_id` and `version`
/// 6. Maps validated shares into [`derec_cryptography::vss::VSSShare`] values
/// 7. Reconstructs the original secret using the VSS recovery algorithm
///
/// # Arguments
///
/// * `secret_id` - The secret identifier being recovered.
/// * `version` - The version of the secret being recovered.
/// * `responses` - Collection of Helper responses. Each entry must contain:
///   - `share_response`: decrypted [`derec_proto::GetShareResponseMessage`] previously
///     returned by [`extract`]
///
/// # Returns
///
/// On success returns [`RecoverResult`] containing:
///
/// - `secret_data`: the reconstructed secret bytes
///
/// # Errors
///
/// Returns [`crate::Error`] (specifically `Error::Recovery(...)`) in the following cases:
///
/// - [`RecoveryError::EmptyResponses`] if `responses` is empty
/// - any response is missing the `result` field (returned as `crate::Error::Invariant`)
/// - [`RecoveryError::NonOkStatus`] if any response indicates a non-OK status, carrying the
///   Helper's status code and memo string
/// - [`RecoveryError::EmptyCommittedDeRecShare`] if any response contains empty committed share bytes
/// - [`RecoveryError::DecodeCommittedDeRecShare`] if committed share decoding fails
/// - [`RecoveryError::DecodeDeRecShare`] if inner share decoding fails
/// - [`RecoveryError::SecretIdMismatch`] if any decoded share does not match `secret_id`
/// - [`RecoveryError::VersionMismatch`] if any decoded share does not match `version`
/// - [`RecoveryError::ReconstructionFailed`] if VSS reconstruction fails
///
/// # Security Notes
///
/// - The reconstructed secret is highly sensitive. Callers should minimize exposure in memory,
///   avoid logging, and store it securely if persistence is required.
/// - Responses are treated as untrusted input; this function validates protocol status,
///   secret identity, and version binding before attempting reconstruction.
///
/// # Example
///
/// ```
/// use derec_library::primitives::{recovery, sharing};
/// use derec_library::primitives::recovery::response::RecoverResult;
/// use derec_library::types::ChannelId;
///
/// let channels = [ChannelId(1), ChannelId(2), ChannelId(3)];
/// let shared_key = [42u8; 32];
/// let secret_data = b"super_secret_value";
///
/// // Sharing flow: store one share per Helper for later recovery.
/// let sharing::request::SplitResult { shares } =
///     sharing::request::split(&channels, 1, 1, secret_data, 2).expect("split failed");
///
/// let mut stored_shares = Vec::new();
/// for &channel_id in &channels[..2] {
///     let committed_share = shares.get(&channel_id).expect("missing share");
///     let sharing::request::ProduceResult { envelope } =
///         sharing::request::produce(channel_id, 1, 1, committed_share, &[], "", &shared_key, None, None)
///             .expect("share produce failed");
///     let sharing::request::ExtractResult { request } =
///         sharing::request::extract(&envelope, &shared_key).expect("share extract failed");
///     stored_shares.push((channel_id, request));
/// }
///
/// // Recovery flow: collect threshold-many recovered shares.
/// let mut responses = Vec::new();
/// for (channel_id, stored_share_request) in &stored_shares {
///     let recovery::request::ProduceResult { envelope: req_env } =
///         recovery::request::produce(*channel_id, 1, 1, &shared_key, None)
///             .expect("recovery request failed");
///     let recovery::request::ExtractResult { request: get_share_req } =
///         recovery::request::extract(&req_env, &shared_key)
///             .expect("recovery request extract failed");
///     let recovery::response::ProduceResult { envelope: resp_env } =
///         recovery::response::produce(*channel_id, &get_share_req, stored_share_request, &shared_key)
///             .expect("recovery response failed");
///     let recovery::response::ExtractResult { response } =
///         recovery::response::extract(&resp_env, &shared_key)
///             .expect("recovery response extract failed");
///     responses.push(response);
/// }
///
/// let inputs: Vec<&_> = responses.iter().collect();
///
/// let RecoverResult { secret_data: recovered } =
///     recovery::response::recover(1, 1, &inputs).expect("recover failed");
///
/// assert_eq!(recovered, secret_data);
/// ```
#[cfg_attr(
    feature = "logging",
    tracing::instrument(skip_all, fields(version = version, responses_count = responses.len()))
)]
pub fn recover(
    secret_id: u64,
    version: u32,
    responses: &[&GetShareResponseMessage],
) -> Result<RecoverResult, crate::Error> {
    if responses.is_empty() {
        #[cfg(feature = "logging")]
        tracing::warn!("responses list is empty");
        return Err(RecoveryError::EmptyResponses.into());
    }

    let mut shares = Vec::with_capacity(responses.len());

    for response in responses {
        shares.push(extract_share_from_response(response, secret_id, version)?);
    }

    let secret_data =
        vss::recover(&shares).map_err(|source| RecoveryError::ReconstructionFailed { source })?;

    #[cfg(feature = "logging")]
    tracing::info!("secret reconstructed from shares");

    Ok(RecoverResult { secret_data })
}

fn extract_share_from_response(
    response: &GetShareResponseMessage,
    secret_id: u64,
    version: u32,
) -> Result<vss::VSSShare, crate::Error> {
    let result = response.result.as_ref().ok_or(crate::Error::Invariant(
        "GetShareResponseMessage is missing result field",
    ))?;

    if result.status != StatusEnum::Ok as i32 {
        #[cfg(feature = "logging")]
        tracing::warn!(status = result.status, memo = %result.memo, "recovery share response status is not Ok");
        return Err(RecoveryError::NonOkStatus {
            status: result.status,
            memo: result.memo.to_owned(),
        }
        .into());
    }

    if response.committed_de_rec_share.is_empty() {
        return Err(RecoveryError::EmptyCommittedDeRecShare.into());
    }

    let committed_derec_share =
        CommittedDeRecShare::decode(response.committed_de_rec_share.as_slice())
            .map_err(|source| RecoveryError::DecodeCommittedDeRecShare { source })?;

    let derec_share = DeRecShare::decode(committed_derec_share.de_rec_share.as_slice())
        .map_err(|source| RecoveryError::DecodeDeRecShare { source })?;

    if derec_share.secret_id != secret_id {
        return Err(RecoveryError::SecretIdMismatch.into());
    }

    if derec_share.version != version {
        return Err(RecoveryError::VersionMismatch {
            expected: version,
            got: derec_share.version,
        }
        .into());
    }

    Ok(vss::VSSShare {
        x: derec_share.x,
        y: derec_share.y,
        encrypted_secret: derec_share.encrypted_secret,
        commitment: committed_derec_share.commitment,
        merkle_path: committed_derec_share
            .merkle_path
            .into_iter()
            .map(|h| (h.is_left, h.hash))
            .collect(),
    })
}