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
use rand::prelude::*;
use rug::integer::Order;
use rug::ops::Pow;
use rug::Integer;
use std::convert::TryInto;
use std::fmt;
const VERSION: &'static str = "s";
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct KctfPow {
pub modulus: Integer,
pub exponent: Integer,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ChallengeParams {
pub difficulty: u32,
pub val: Integer,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Challenge<'a> {
pub params: ChallengeParams,
pub pow: &'a KctfPow,
}
impl ChallengeParams {
pub fn decode_challenge(chall_string: &str) -> Result<ChallengeParams, &'static str> {
let mut parts = chall_string.split('.');
if parts.next() != Some(VERSION) {
return Err("Incorrect version");
}
let data: Vec<_> = parts.collect();
if data.len() != 2 {
return Err("Incorrect number of parts");
}
let decoded_data: Vec<_> = data
.into_iter()
.map(|x| base64::decode(x).map_err(|_| "Parts aren't valid base64"))
.collect::<Result<_, _>>()?;
let difficulty_bytes = &decoded_data[0];
let difficulty: u32;
if difficulty_bytes.len() > 4 {
let (first, last) = difficulty_bytes.split_at(difficulty_bytes.len() - 4);
if first.iter().any(|&x| x != 0) {
return Err("Difficulty is too large");
}
difficulty = u32::from_be_bytes(last.try_into().unwrap())
} else {
let mut difficulty_array = [0; 4];
difficulty_array[4 - difficulty_bytes.len()..].copy_from_slice(difficulty_bytes);
difficulty = u32::from_be_bytes(difficulty_array);
}
Ok(Self {
val: Integer::from_digits(&decoded_data[1], Order::Msf),
difficulty,
})
}
pub fn generate_challenge(difficulty: u32) -> ChallengeParams {
let mut bytes: [u8; 16] = [0; 16];
thread_rng().fill(&mut bytes[..]);
Self {
val: Integer::from_digits(&bytes, Order::Msf),
difficulty,
}
}
pub fn solve(mut self, pow: &KctfPow) -> String {
for _ in 0..self.difficulty {
let _ = self.val.pow_mod_mut(&pow.exponent, &pow.modulus);
self.val ^= 1;
}
format!(
"{}.{}",
VERSION,
base64::encode(self.val.to_digits(Order::Msf))
)
}
pub fn check(&self, pow: &KctfPow, sol: &str) -> Result<bool, &'static str> {
let mut parts = sol.split('.');
if parts.next() != Some(VERSION) {
return Err("Incorrect version");
}
let data = match parts.next() {
Some(x) => x,
None => return Err("Incorrect number of parts"),
};
if let Some(_) = parts.next() {
return Err("Incorrect number of parts");
}
let decoded_data = base64::decode(data).map_err(|_| "Parts aren't valid base64")?;
let mut sol_val = Integer::from_digits(&decoded_data, Order::Msf);
for _ in 0..self.difficulty {
sol_val ^= 1;
let _ = sol_val.pow_mod_mut(&2.into(), &pow.modulus);
}
Ok(self.val == sol_val || Integer::from(&pow.modulus - &self.val) == sol_val)
}
}
impl KctfPow {
pub fn new() -> Self {
let modulus: Integer = Integer::from(2).pow(1279) - 1;
let exponent = (modulus.clone() + 1) / 4;
Self { modulus, exponent }
}
pub fn decode_challenge(&self, chall_string: &str) -> Result<Challenge, &'static str> {
Ok(Challenge {
params: ChallengeParams::decode_challenge(chall_string)?,
pow: self
})
}
pub fn generate_challenge(&self, difficulty: u32) -> Challenge {
Challenge {
params: ChallengeParams::generate_challenge(difficulty),
pow: self
}
}
}
impl Default for KctfPow {
fn default() -> Self {
Self::new()
}
}
impl<'a> Challenge<'a> {
pub fn solve(self) -> String {
self.params.solve(self.pow)
}
pub fn check(&self, sol: &str) -> Result<bool, &'static str> {
self.params.check(self.pow, sol)
}
}
impl fmt::Display for ChallengeParams {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
fmt,
"{}.{}.{}",
VERSION,
base64::encode(&self.difficulty.to_be_bytes()),
base64::encode(&self.val.to_digits(Order::Msf))
)
}
}
impl<'a> fmt::Display for Challenge<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{}", self.params)
}
}