diff options
Diffstat (limited to 'src/crash.rs')
| -rw-r--r-- | src/crash.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/crash.rs b/src/crash.rs index 9d6e98c..40b8743 100644 --- a/src/crash.rs +++ b/src/crash.rs | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | //! clock, never from anything the client sends. | 6 | //! clock, never from anything the client sends. |
| 7 | 7 | ||
| 8 | use serde::{Deserialize, Serialize}; | 8 | use serde::{Deserialize, Serialize}; |
| 9 | use sha2::{Digest, Sha256}; | ||
| 9 | use std::time::Duration; | 10 | use std::time::Duration; |
| 10 | 11 | ||
| 11 | /// `exp(GROWTH_RATE * t)` doubles the multiplier every five seconds. | 12 | /// `exp(GROWTH_RATE * t)` doubles the multiplier every five seconds. |
| @@ -47,6 +48,53 @@ pub fn crash_point_from_uniform(u: f64) -> f64 { | |||
| 47 | ((1.0 - HOUSE_EDGE) / (1.0 - u)).clamp(1.0, MAX_MULTIPLIER) | 48 | ((1.0 - HOUSE_EDGE) / (1.0 - u)).clamp(1.0, MAX_MULTIPLIER) |
| 48 | } | 49 | } |
| 49 | 50 | ||
| 51 | /// How many bytes a round seed contains. The first eight bytes choose the | ||
| 52 | /// crash point; the rest are revealed and hashed with it. | ||
| 53 | pub const SEED_LEN: usize = 32; | ||
| 54 | |||
| 55 | /// Hex-encoded SHA-256 of a round seed, sent before the round starts. | ||
| 56 | pub fn commit_seed(seed: &[u8]) -> String { | ||
| 57 | let digest = Sha256::digest(seed); | ||
| 58 | format!("{digest:x}") | ||
| 59 | } | ||
| 60 | |||
| 61 | /// Uniform draw encoded by a seed: the first eight bytes, big-endian. | ||
| 62 | pub fn seed_to_uniform(seed: &[u8]) -> f64 { | ||
| 63 | let mut bytes = [0u8; 8]; | ||
| 64 | let take = seed.len().min(8); | ||
| 65 | bytes[..take].copy_from_slice(&seed[..take]); | ||
| 66 | u64::from_be_bytes(bytes) as f64 / 18_446_744_073_709_551_616.0 | ||
| 67 | } | ||
| 68 | |||
| 69 | /// The crash point a seed commits to. A client recomputes this after the | ||
| 70 | /// reveal and checks it against the published commit. | ||
| 71 | pub fn crash_point_from_seed(seed: &[u8]) -> f64 { | ||
| 72 | crash_point_from_uniform(seed_to_uniform(seed)) | ||
| 73 | } | ||
| 74 | |||
| 75 | /// Decode a lowercase or uppercase hex seed. Returns `None` for odd lengths, | ||
| 76 | /// bad digits or a seed that is not exactly [`SEED_LEN`] bytes. | ||
| 77 | pub fn seed_from_hex(raw: &str) -> Option<Vec<u8>> { | ||
| 78 | if raw.len() != SEED_LEN * 2 { | ||
| 79 | return None; | ||
| 80 | } | ||
| 81 | let mut seed = Vec::with_capacity(SEED_LEN); | ||
| 82 | for pair in raw.as_bytes().chunks_exact(2) { | ||
| 83 | let high = (pair[0] as char).to_digit(16)?; | ||
| 84 | let low = (pair[1] as char).to_digit(16)?; | ||
| 85 | seed.push((high * 16 + low) as u8); | ||
| 86 | } | ||
| 87 | Some(seed) | ||
| 88 | } | ||
| 89 | |||
| 90 | /// Check a revealed seed against the commit published before the round. | ||
| 91 | pub fn verify_seed(seed_hex: &str, commit: &str) -> bool { | ||
| 92 | let Some(seed) = seed_from_hex(seed_hex) else { | ||
| 93 | return false; | ||
| 94 | }; | ||
| 95 | commit_seed(&seed).eq_ignore_ascii_case(commit) | ||
| 96 | } | ||
| 97 | |||
| 50 | /// Whole-Kr cashout value. Balances are integers, so the payout is floored. | 98 | /// Whole-Kr cashout value. Balances are integers, so the payout is floored. |
| 51 | pub fn payout(bet: u64, multiplier: f64) -> u64 { | 99 | pub fn payout(bet: u64, multiplier: f64) -> u64 { |
| 52 | if bet == 0 || !multiplier.is_finite() || multiplier <= 0.0 { | 100 | if bet == 0 || !multiplier.is_finite() || multiplier <= 0.0 { |
| @@ -72,10 +120,16 @@ pub struct CrashState { | |||
| 72 | pub multiplier: f64, | 120 | pub multiplier: f64, |
| 73 | /// `floor(bet * multiplier)` for the value above. | 121 | /// `floor(bet * multiplier)` for the value above. |
| 74 | pub potential: u64, | 122 | pub potential: u64, |
| 123 | /// Server Unix time when the round started, milliseconds. | ||
| 124 | pub started_at: u64, | ||
| 75 | /// Hidden until the round ends. | 125 | /// Hidden until the round ends. |
| 76 | pub crash_point: Option<f64>, | 126 | pub crash_point: Option<f64>, |
| 77 | pub payout: u64, | 127 | pub payout: u64, |
| 78 | pub result: String, | 128 | pub result: String, |
| 129 | /// `sha256(seed)`, present for the whole round. | ||
| 130 | pub commit: Option<String>, | ||
| 131 | /// The seed itself, present only after the round ends. | ||
| 132 | pub seed: Option<String>, | ||
| 79 | } | 133 | } |
| 80 | 134 | ||
| 81 | impl CrashState { | 135 | impl CrashState { |
