|
| 1 | +use sha2::{Digest, Sha256}; |
| 2 | + |
| 3 | +use crate::error::EntropyError; |
| 4 | +use crate::utils::check_length; |
| 5 | +use crate::DeckEntropy; |
| 6 | + |
| 7 | +/// Minimum card draws required to fill an `N`-byte entropy buffer |
| 8 | +/// from a `CARDS`-deck. Each entry is the smallest `k` such that |
| 9 | +/// `log₂(CARDS! / (CARDS-k)!) ≥ 8·N`. |
| 10 | +pub const fn min_draws<const CARDS: u8, const N: usize>() -> Result<usize, EntropyError> { |
| 11 | + match (CARDS, N) { |
| 12 | + (40, 16) => Ok(28), |
| 13 | + (48, 16) => Ok(26), |
| 14 | + (52, 16) => Ok(25), |
| 15 | + (58, 16) => Ok(24), |
| 16 | + (58, 32) => Ok(55), |
| 17 | + (78, 16) => Ok(22), |
| 18 | + (78, 32) => Ok(45), |
| 19 | + (108, 16) => Ok(22), |
| 20 | + (108, 32) => Ok(41), |
| 21 | + (112, 16) => Ok(22), |
| 22 | + (112, 32) => Ok(42), |
| 23 | + _ => Err(EntropyError::UnsupportedConfig(CARDS, N)), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Practical upper bound on draw count — `2 * min_draws`, capped at `CARDS`. |
| 28 | +pub const fn max_draws<const CARDS: u8, const N: usize>() -> Result<usize, EntropyError> { |
| 29 | + match min_draws::<CARDS, N>() { |
| 30 | + Ok(m) => { |
| 31 | + let doubled = m * 2; |
| 32 | + let cap = CARDS as usize; |
| 33 | + Ok(if doubled > cap { cap } else { doubled }) |
| 34 | + } |
| 35 | + Err(e) => Err(e), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Fold a sequence of drawn cards into an `N`-byte entropy buffer. |
| 40 | +/// |
| 41 | +/// Cards are formatted as decimal indices, hyphen-separated, SHA-256 |
| 42 | +/// hashed, then truncated to `N` bytes. Mirrors Krux's d20 string |
| 43 | +/// shape (`"-".join`) for multi-digit values. |
| 44 | +/// |
| 45 | +/// # Example |
| 46 | +/// |
| 47 | +/// ``` |
| 48 | +/// use kdk_entropy::{deck_to_entropy, DeckEntropy}; |
| 49 | +/// |
| 50 | +/// // SHA-256("0-1-2-3-...-24")[:16]. |
| 51 | +/// let cards: [u8; 25] = [ |
| 52 | +/// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 53 | +/// 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 54 | +/// 20, 21, 22, 23, 24, |
| 55 | +/// ]; |
| 56 | +/// let e: DeckEntropy<52, 16> = deck_to_entropy(&cards).unwrap(); |
| 57 | +/// assert_eq!( |
| 58 | +/// e.expose_secret(), |
| 59 | +/// &[ |
| 60 | +/// 0x92, 0xe2, 0x92, 0xe3, 0x4c, 0x44, 0x48, 0xc1, |
| 61 | +/// 0x57, 0x73, 0x1a, 0xff, 0x3d, 0x40, 0x10, 0x38, |
| 62 | +/// ] |
| 63 | +/// ); |
| 64 | +/// ``` |
| 65 | +pub fn deck_to_entropy<const CARDS: u8, const N: usize>( |
| 66 | + cards: &[u8], |
| 67 | +) -> Result<DeckEntropy<CARDS, N>, EntropyError> { |
| 68 | + let required = min_draws::<CARDS, N>()?; |
| 69 | + let allowed = max_draws::<CARDS, N>()?; |
| 70 | + check_length(cards.len(), required, allowed)?; |
| 71 | + |
| 72 | + // Validation pass: range + uniqueness. `present[i]` is non-secret |
| 73 | + // bookkeeping for which indices remain undrawn. |
| 74 | + let mut present = [true; 256]; |
| 75 | + for (i, &card) in cards.iter().enumerate() { |
| 76 | + if card >= CARDS { |
| 77 | + return Err(EntropyError::RollOutOfRange(card, i)); |
| 78 | + } |
| 79 | + if !present[card as usize] { |
| 80 | + return Err(EntropyError::DuplicateCard(card, i)); |
| 81 | + } |
| 82 | + present[card as usize] = false; |
| 83 | + } |
| 84 | + |
| 85 | + let mut hasher = Sha256::new(); |
| 86 | + for (i, &card) in cards.iter().enumerate() { |
| 87 | + if i > 0 { |
| 88 | + hasher.update(b"-"); |
| 89 | + } |
| 90 | + if card >= 100 { |
| 91 | + hasher.update([b'0' + card / 100]); |
| 92 | + } |
| 93 | + if card >= 10 { |
| 94 | + hasher.update([b'0' + (card / 10) % 10]); |
| 95 | + } |
| 96 | + hasher.update([b'0' + card % 10]); |
| 97 | + } |
| 98 | + let digest = hasher.finalize(); |
| 99 | + |
| 100 | + let mut acc: DeckEntropy<CARDS, N> = DeckEntropy::new([0u8; N]); |
| 101 | + acc.expose_secret_mut().copy_from_slice(&digest[..N]); |
| 102 | + Ok(acc) |
| 103 | +} |
0 commit comments