diff options
Diffstat (limited to 'tests/crash.rs')
| -rw-r--r-- | tests/crash.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/crash.rs b/tests/crash.rs index 797874b..513f9c8 100644 --- a/tests/crash.rs +++ b/tests/crash.rs | |||
| @@ -102,3 +102,42 @@ fn crash_points_are_clamped_to_the_liability_cap() { | |||
| 102 | assert_eq!(crash_point_from_uniform(0.999_999), MAX_MULTIPLIER); | 102 | assert_eq!(crash_point_from_uniform(0.999_999), MAX_MULTIPLIER); |
| 103 | assert_eq!(crash_point_from_uniform(1.0), MAX_MULTIPLIER); | 103 | assert_eq!(crash_point_from_uniform(1.0), MAX_MULTIPLIER); |
| 104 | } | 104 | } |
| 105 | |||
| 106 | #[test] | ||
| 107 | fn a_seed_commits_to_its_crash_point_and_can_be_verified() { | ||
| 108 | use zagan_bet::crash::{ | ||
| 109 | SEED_LEN, commit_seed, crash_point_from_seed, seed_from_hex, seed_to_uniform, verify_seed, | ||
| 110 | }; | ||
| 111 | |||
| 112 | let mut seed = [0u8; SEED_LEN]; | ||
| 113 | let value = (0.67_f64 * 18_446_744_073_709_551_616.0) as u64; | ||
| 114 | seed[..8].copy_from_slice(&value.to_be_bytes()); | ||
| 115 | let commit = commit_seed(&seed); | ||
| 116 | let hex = format!( | ||
| 117 | "{}", | ||
| 118 | seed.iter() | ||
| 119 | .map(|byte| format!("{byte:02x}")) | ||
| 120 | .collect::<String>() | ||
| 121 | ); | ||
| 122 | |||
| 123 | assert_eq!(seed_from_hex(&hex).as_deref(), Some(seed.as_slice())); | ||
| 124 | assert!(verify_seed(&hex, &commit)); | ||
| 125 | assert!(!verify_seed(&hex, &"00".repeat(32)), "a wrong commit fails"); | ||
| 126 | assert!(!verify_seed("nope", &commit), "bad hex fails"); | ||
| 127 | |||
| 128 | // The point the client recomputes is the point the server committed to. | ||
| 129 | close(crash_point_from_seed(&seed), 3.0, 1e-9); | ||
| 130 | close(crash_point_from_seed(&seed), 3.0, 1e-9); | ||
| 131 | |||
| 132 | // The first eight bytes are the uniform; the rest are still hashed. | ||
| 133 | close(seed_to_uniform(&seed), 0.67, 1e-9); | ||
| 134 | let mut altered = seed; | ||
| 135 | altered[SEED_LEN - 1] = 1; | ||
| 136 | assert!(!verify_seed( | ||
| 137 | &altered | ||
| 138 | .iter() | ||
| 139 | .map(|byte| format!("{byte:02x}")) | ||
| 140 | .collect::<String>(), | ||
| 141 | &commit | ||
| 142 | )); | ||
| 143 | } | ||
