arkhe-forge-platform 0.13.0

L2 services for ArkheForge Runtime: projection observer, manifest loader, policy, rate limiter, audit receipts, crypto-erasure coordinator, process-protection shim. Builds on L0 arkhe-kernel + L1 arkhe-forge-core.
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
//! Threshold HSM pre-sign library — byte-level GF(256) Shamir secret sharing.
//!
//! Distributes the auto_promote
//! authorization token via `t-of-n` Shamir secret sharing; promotion consumes
//! `t` shares to reconstruct the token and appends an entry to the consumed
//! journal.
//!
//! The implementation keeps the cryptographic surface inside this crate
//! instead of pulling an external dep. For each secret byte we draw a random
//! polynomial of degree `t-1` over `GF(2^8)` with the irreducible polynomial
//! `x^8 + x^4 + x^3 + x + 1` (AES / NIST) and evaluate it at points `1..=n`.
//! Reconstruction interpolates the polynomial at `x = 0` via Lagrange basis.
//! Share index `0` is reserved for the secret and never appears on-wire.
//!
//! # Security notes
//!
//! * Per-byte independent polynomials — catastrophic failure of a single
//!   share reveals at most `t-1` bytes of leakage per coefficient.
//! * RNG: `getrandom::getrandom` via the OS CSPRNG. Absent entropy (e.g. a
//!   stripped-down embedded target with no `getrandom` backend) surfaces
//!   [`ThresholdError::EntropyFailure`] — callers must resolve upstream.
//! * The reconstruction path verifies every share index lies in `1..=255`,
//!   rejects duplicates, and bails if the caller submits fewer than `t` of
//!   them. Tampered shares are detected only insofar as the reconstructed
//!   secret disagrees with the expected value; integrity is layered on at
//!   the caller via `ConsumedTokenJournal` (token_hash match).

use getrandom::getrandom as os_getrandom;

/// Shamir threshold configuration — default `t=2, n=3`.
#[derive(Debug, Clone, Copy)]
pub struct ThresholdConfig {
    /// Minimum share count required for reconstruction.
    pub t: u8,
    /// Total share count.
    pub n: u8,
}

impl ThresholdConfig {
    /// Default config — 2-of-3.
    pub const DEFAULT: Self = Self { t: 2, n: 3 };

    /// `2 <= t <= n <= 255`. `n == 255` is allowed because index 0 is reserved (secret).
    pub fn is_valid(&self) -> bool {
        self.t >= 2 && self.t <= self.n
    }
}

/// Single Shamir share — index (`1..=n`) + per-byte y-coordinates of the
/// secret polynomial evaluated at `index`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Share {
    /// Share index — `1..=n`. `0` is reserved for the secret itself.
    pub index: u8,
    /// Share bytes — polynomial y-coordinates, one per secret byte.
    pub bytes: Vec<u8>,
}

/// Threshold computation error.
#[non_exhaustive]
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum ThresholdError {
    /// `is_valid()` failed — `t < 2` or `t > n`.
    #[error("invalid threshold config: t={t} n={n}")]
    InvalidConfig {
        /// threshold.
        t: u8,
        /// total.
        n: u8,
    },
    /// `combine_shares` received fewer than `t` shares.
    #[error("insufficient shares: need {need}, got {got}")]
    InsufficientShares {
        /// Required count.
        need: usize,
        /// Actual count.
        got: usize,
    },
    /// A share carries index `0` (reserved) or index `> 255` (impossible since
    /// the field is `u8`, but checked defensively).
    #[error("invalid share index: {index}")]
    InvalidShareIndex {
        /// Offending share index.
        index: u8,
    },
    /// Two or more shares presented the same index — ambiguous interpolation.
    #[error("duplicate share indices in combine input")]
    DuplicateShares,
    /// Shares carry mismatched byte-lengths — the caller has mixed sets from
    /// different splits.
    #[error("share byte length mismatch: expected {expected}, got {got}")]
    ShareLengthMismatch {
        /// Length of the first share.
        expected: usize,
        /// Length of the offending share.
        got: usize,
    },
    /// `getrandom` failed to deliver entropy on the target platform.
    #[error("entropy source failure: {0}")]
    EntropyFailure(String),
}

// ───────────────────── GF(2^8) arithmetic ─────────────────────

/// AES irreducible polynomial: `x^8 + x^4 + x^3 + x + 1`.
const GF_REDUCER: u16 = 0x11B;

#[inline]
fn gf_add(a: u8, b: u8) -> u8 {
    a ^ b
}

/// Russian-peasant multiplication in `GF(2^8)` reduced modulo `GF_REDUCER`.
fn gf_mul(mut a: u8, mut b: u8) -> u8 {
    let mut r: u16 = 0;
    while a != 0 && b != 0 {
        if b & 1 == 1 {
            r ^= a as u16;
        }
        let hi_bit = a & 0x80;
        a <<= 1;
        if hi_bit != 0 {
            a ^= (GF_REDUCER & 0xFF) as u8;
        }
        b >>= 1;
    }
    r as u8
}

/// Multiplicative inverse in `GF(2^8)` — extended Euclidean not needed; we use
/// `a^254 = a^{-1}` via exponentiation-by-squaring (Fermat's little theorem
/// in the field of 256 elements). Panics-free: for `a = 0` returns `0`, which
/// the caller must guard against (denominators never zero by construction).
fn gf_inv(a: u8) -> u8 {
    if a == 0 {
        return 0;
    }
    let mut result: u8 = 1;
    let mut base = a;
    let mut exp: u8 = 254;
    while exp > 0 {
        if exp & 1 == 1 {
            result = gf_mul(result, base);
        }
        base = gf_mul(base, base);
        exp >>= 1;
    }
    result
}

/// Evaluate a polynomial (coefficients in ascending order: `c0, c1, ..., c_{k-1}`)
/// at point `x` over `GF(2^8)`. Horner form.
fn gf_poly_eval(coeffs: &[u8], x: u8) -> u8 {
    let mut acc: u8 = 0;
    for &c in coeffs.iter().rev() {
        acc = gf_add(gf_mul(acc, x), c);
    }
    acc
}

// ───────────────────── Split / combine ─────────────────────

/// Split `secret` into `config.n` shares such that any `config.t` of them
/// suffice to reconstruct. Each share is the polynomial evaluated at a
/// distinct point `1..=n`; the constant term is the secret byte.
///
/// Returns [`ThresholdError::InvalidConfig`] when `config.is_valid()` fails,
/// [`ThresholdError::EntropyFailure`] when `getrandom` cannot deliver the
/// per-byte polynomial coefficients.
pub fn split_secret(secret: &[u8], config: ThresholdConfig) -> Result<Vec<Share>, ThresholdError> {
    if !config.is_valid() {
        return Err(ThresholdError::InvalidConfig {
            t: config.t,
            n: config.n,
        });
    }

    let t = config.t as usize;
    let n = config.n as usize;

    // For each secret byte we need `t-1` random coefficients (c1..c_{t-1});
    // c0 is the secret byte itself. One contiguous draw minimises getrandom
    // syscalls.
    let mut random_coeffs = vec![0u8; secret.len() * (t - 1)];
    if !random_coeffs.is_empty() {
        os_getrandom(&mut random_coeffs)
            .map_err(|e| ThresholdError::EntropyFailure(e.to_string()))?;
    }

    let mut shares: Vec<Share> = Vec::with_capacity(n);
    for share_idx in 1..=(n as u8) {
        let mut ys = Vec::with_capacity(secret.len());
        for (byte_idx, &secret_byte) in secret.iter().enumerate() {
            // Polynomial `p(x) = secret_byte + c1*x + c2*x^2 + ...`.
            let start = byte_idx * (t - 1);
            let end = start + (t - 1);
            let mut poly = Vec::with_capacity(t);
            poly.push(secret_byte);
            poly.extend_from_slice(&random_coeffs[start..end]);
            ys.push(gf_poly_eval(&poly, share_idx));
        }
        shares.push(Share {
            index: share_idx,
            bytes: ys,
        });
    }
    // Best-effort wipe: the caller-visible shares carry the evaluations; the
    // coefficient buffer holds sensitive polynomial state.
    random_coeffs.fill(0);
    Ok(shares)
}

/// Reconstruct the original secret from at least `config.t` distinct shares.
/// Interpolates the polynomial at `x = 0` per byte via Lagrange basis.
///
/// Validates that every share index is in `1..=255`, that indices are
/// unique, that all share byte-lengths match, and that the caller supplied
/// at least `config.t` shares.
pub fn combine_shares(
    shares: &[Share],
    config: ThresholdConfig,
) -> Result<Vec<u8>, ThresholdError> {
    if shares.len() < config.t as usize {
        return Err(ThresholdError::InsufficientShares {
            need: config.t as usize,
            got: shares.len(),
        });
    }

    // Index 0 is reserved for the secret; `u8` rules out > 255.
    for s in shares {
        if s.index == 0 {
            return Err(ThresholdError::InvalidShareIndex { index: 0 });
        }
    }

    // Duplicate index check — O(n^2) is fine for the realistic `n <= 255`.
    for (i, a) in shares.iter().enumerate() {
        for b in &shares[i + 1..] {
            if a.index == b.index {
                return Err(ThresholdError::DuplicateShares);
            }
        }
    }

    let secret_len = shares[0].bytes.len();
    for s in shares {
        if s.bytes.len() != secret_len {
            return Err(ThresholdError::ShareLengthMismatch {
                expected: secret_len,
                got: s.bytes.len(),
            });
        }
    }

    // Use the first `t` shares — any subset reconstructs the same polynomial
    // since it is uniquely determined by any `t` evaluations.
    let selected = &shares[..config.t as usize];

    let mut secret = vec![0u8; secret_len];
    for (byte_idx, secret_byte) in secret.iter_mut().enumerate() {
        // Lagrange interpolation at x = 0:
        //   L(0) = Σ y_i * Π_{j != i}  (0 - x_j) / (x_i - x_j)
        //        = Σ y_i * Π_{j != i}  (x_j) / (x_i + x_j)         (GF(2) subtraction == addition).
        let mut acc: u8 = 0;
        for (i, s_i) in selected.iter().enumerate() {
            let mut num: u8 = 1;
            let mut den: u8 = 1;
            for (j, s_j) in selected.iter().enumerate() {
                if i == j {
                    continue;
                }
                num = gf_mul(num, s_j.index);
                den = gf_mul(den, gf_add(s_i.index, s_j.index));
            }
            let basis = gf_mul(num, gf_inv(den));
            acc = gf_add(acc, gf_mul(s_i.bytes[byte_idx], basis));
        }
        *secret_byte = acc;
    }

    Ok(secret)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn default_config_is_2_of_3() {
        let c = ThresholdConfig::DEFAULT;
        assert_eq!(c.t, 2);
        assert_eq!(c.n, 3);
        assert!(c.is_valid());
    }

    #[test]
    fn reject_t_greater_than_n() {
        let c = ThresholdConfig { t: 5, n: 3 };
        assert!(!c.is_valid());
    }

    #[test]
    fn reject_t_below_two() {
        let c = ThresholdConfig { t: 1, n: 3 };
        assert!(!c.is_valid());
    }

    #[test]
    fn split_invalid_config_errors() {
        let err = split_secret(b"secret", ThresholdConfig { t: 1, n: 3 }).unwrap_err();
        assert!(matches!(err, ThresholdError::InvalidConfig { .. }));
    }

    #[test]
    fn combine_insufficient_shares_errors() {
        let config = ThresholdConfig::DEFAULT;
        let shares = vec![Share {
            index: 1,
            bytes: vec![0u8; 32],
        }];
        let err = combine_shares(&shares, config).unwrap_err();
        assert_eq!(err, ThresholdError::InsufficientShares { need: 2, got: 1 });
    }

    #[test]
    fn roundtrip_2_of_3() {
        let secret = b"auto-promote-token-sample-bytes!";
        assert_eq!(secret.len(), 32);
        let shares = split_secret(secret, ThresholdConfig::DEFAULT).unwrap();
        assert_eq!(shares.len(), 3);
        // Any 2 of 3 reconstruct.
        for pair in [(0, 1), (0, 2), (1, 2)] {
            let subset = vec![shares[pair.0].clone(), shares[pair.1].clone()];
            let recovered = combine_shares(&subset, ThresholdConfig::DEFAULT).unwrap();
            assert_eq!(&recovered[..], &secret[..]);
        }
    }

    #[test]
    fn roundtrip_3_of_5() {
        let config = ThresholdConfig { t: 3, n: 5 };
        let secret: Vec<u8> = (0..64).collect();
        let shares = split_secret(&secret, config).unwrap();
        assert_eq!(shares.len(), 5);
        // Pick a non-trivial subset.
        let subset = vec![shares[0].clone(), shares[2].clone(), shares[4].clone()];
        let recovered = combine_shares(&subset, config).unwrap();
        assert_eq!(recovered, secret);
    }

    #[test]
    fn share_indices_are_one_through_n() {
        let shares = split_secret(b"hi", ThresholdConfig { t: 2, n: 4 }).unwrap();
        let indices: Vec<u8> = shares.iter().map(|s| s.index).collect();
        assert_eq!(indices, vec![1, 2, 3, 4]);
    }

    #[test]
    fn fewer_than_t_reveals_no_info_sampled() {
        // A single share cannot reconstruct the secret. We do not assert
        // zero-knowledge cryptographically here (that would need many
        // samples); we only check that the combine path rejects the input.
        let shares = split_secret(b"topsecret", ThresholdConfig::DEFAULT).unwrap();
        let one_share = vec![shares[0].clone()];
        let err = combine_shares(&one_share, ThresholdConfig::DEFAULT).unwrap_err();
        assert!(matches!(err, ThresholdError::InsufficientShares { .. }));
    }

    #[test]
    fn reject_duplicate_share_indices() {
        let shares = split_secret(b"hi", ThresholdConfig::DEFAULT).unwrap();
        // Forge a duplicate of share #1.
        let forged = vec![shares[0].clone(), shares[0].clone()];
        let err = combine_shares(&forged, ThresholdConfig::DEFAULT).unwrap_err();
        assert_eq!(err, ThresholdError::DuplicateShares);
    }

    #[test]
    fn reject_zero_index_share() {
        let shares = split_secret(b"hi", ThresholdConfig::DEFAULT).unwrap();
        let mut with_zero = shares.clone();
        with_zero[0].index = 0;
        let err = combine_shares(&with_zero, ThresholdConfig::DEFAULT).unwrap_err();
        assert_eq!(err, ThresholdError::InvalidShareIndex { index: 0 });
    }

    #[test]
    fn reject_mismatched_share_length() {
        let shares = split_secret(b"hello", ThresholdConfig::DEFAULT).unwrap();
        let mut bad = shares[1].clone();
        bad.bytes.push(0xFF);
        let combined = vec![shares[0].clone(), bad];
        let err = combine_shares(&combined, ThresholdConfig::DEFAULT).unwrap_err();
        assert!(matches!(
            err,
            ThresholdError::ShareLengthMismatch {
                expected: 5,
                got: 6
            }
        ));
    }

    #[test]
    fn tampered_share_yields_wrong_secret_silently() {
        // Core Shamir does not detect byte-level tampering — that is the
        // caller's integrity layer (`ConsumedTokenJournal.token_hash`). The
        // reconstruction path simply returns the wrong secret, which the
        // caller catches via BLAKE3 mismatch.
        let secret = b"integrity-marker";
        let mut shares = split_secret(secret, ThresholdConfig::DEFAULT).unwrap();
        shares[1].bytes[0] ^= 0xFF;
        let recovered = combine_shares(&shares[..2], ThresholdConfig::DEFAULT).unwrap();
        assert_ne!(&recovered[..], &secret[..]);
    }

    // ───── GF(256) arithmetic sanity ─────

    #[test]
    fn gf_mul_matches_aes_vectors() {
        // AES MixColumns reference: {02} · {d4} = {b3}, {03} · {bf} = {da}.
        assert_eq!(gf_mul(0x02, 0xD4), 0xB3);
        assert_eq!(gf_mul(0x03, 0xBF), 0xDA);
        // Identity.
        assert_eq!(gf_mul(0x00, 0xFF), 0x00);
        assert_eq!(gf_mul(0x01, 0x57), 0x57);
    }

    #[test]
    fn gf_inv_is_multiplicative_identity() {
        for x in 1..=255u8 {
            let inv = gf_inv(x);
            assert_ne!(inv, 0, "inverse of {x} was zero");
            assert_eq!(gf_mul(x, inv), 1, "x={x}");
        }
    }

    #[test]
    fn gf_inv_of_zero_is_zero() {
        assert_eq!(gf_inv(0), 0);
    }
}