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
|
//! Test vectors from the C tor implementation
use hex_literal::hex;
use tor_hscrypto::pk::HsBlindId;
use tor_hspow::v1::{Effort, Instance, Nonce, Seed, Solution, SolutionByteArray, SolutionError};
use tor_hspow::Error;
/// Short test vectors from C tor, covering verification only
#[test]
fn verify_only() {
// All zero, but only claims an effort of 1.
// Expect it will last until hash sum checks before failing.
assert!(matches!(
Instance::new(
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("0000000000000000000000000000000000000000000000000000000000000000").into(),
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("00000000000000000000000000000000").into(),
1_u32.into(),
hex!("00000000").into(),
&hex!("00000000000000000000000000000000"),
)
.unwrap(),
),
Err(Error::BadSolution(tor_hspow::SolutionError::V1(
SolutionError::HashSum
)))
));
// All zero, but a higher effort claim. Should fail the effort check.
assert!(matches!(
Instance::new(
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("0000000000000000000000000000000000000000000000000000000000000000").into(),
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("00000000000000000000000000000000").into(),
10_u32.into(),
hex!("00000000").into(),
&hex!("00000000000000000000000000000000"),
)
.unwrap(),
),
Err(Error::BadSolution(tor_hspow::SolutionError::V1(
SolutionError::Effort
)))
));
// Seed head mismatch
assert!(matches!(
Instance::new(
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("0000000000000000000000000000000000000000000000000000000000000000").into(),
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("00000000000000000000000000000000").into(),
0_u32.into(),
hex!("00000001").into(),
&hex!("00000000000000000000000000000000"),
)
.unwrap(),
),
Err(Error::BadSolution(tor_hspow::SolutionError::V1(
SolutionError::Seed
)))
));
// Valid zero-effort solution
assert!(Instance::new(
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into()
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("55555555555555555555555555555555").into(),
0_u32.into(),
hex!("aaaaaaaa").into(),
&hex!("4312f87ceab844c78e1c793a913812d7")
)
.unwrap()
)
.is_ok());
// Valid high-effort solution
assert!(Instance::new(
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into()
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("59217255555555555555555555555555").into(),
1_000_000_u32.into(),
hex!("aaaaaaaa").into(),
&hex!("0f3db97b9cac20c1771680a1a34848d3")
)
.unwrap()
)
.is_ok());
// The claimed effort must exactly match what's was in the challenge
// when the Equi-X proof was created, or it will fail either the
// Effort or HashSum checks.
assert!(matches!(
Instance::new(
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into()
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("2eff9fdbc34326d9d2f18ed277469c63").into(),
99_999_u32.into(),
hex!("86fb0acf").into(),
&hex!("400cb091139f86b352119f6e131802d6")
)
.unwrap()
),
Err(Error::BadSolution(tor_hspow::SolutionError::V1(
SolutionError::Effort
)))
));
// Otherwise good solution but with a corrupted nonce. This may fail
// either the Effort or HashSum checks.
assert!(matches!(
Instance::new(
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into()
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("2eff9fdbc34326d9a2f18ed277469c63").into(),
100_000_u32.into(),
hex!("86fb0acf").into(),
&hex!("400cb091139f86b352119f6e131802d6")
)
.unwrap()
),
Err(Error::BadSolution(tor_hspow::SolutionError::V1(
SolutionError::Effort
)))
));
assert!(Instance::new(
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into()
)
.verifier()
.check(
&Solution::try_from_bytes(
hex!("2eff9fdbc34326d9d2f18ed277469c63").into(),
100_000_u32.into(),
hex!("86fb0acf").into(),
&hex!("400cb091139f86b352119f6e131802d6")
)
.unwrap()
)
.is_ok());
}
/// Utility to solve and verify one puzzle
fn solve_and_verify(
effort: Effort,
first_nonce: Nonce,
seed: Seed,
service: HsBlindId,
expected_nonce: Nonce,
expected_proof: SolutionByteArray,
) {
let instance = Instance::new(service, seed);
let solution = instance
.clone()
.with_effort(effort)
.solve_with_nonce(&first_nonce)
.run()
.unwrap();
assert_eq!(solution.seed_head(), instance.seed().head());
assert_eq!(solution.effort(), effort);
assert_eq!(solution.nonce(), &expected_nonce);
assert_eq!(solution.proof_to_bytes(), expected_proof);
assert!(Solution::try_from_bytes(
expected_nonce,
effort,
instance.seed().head(),
&expected_proof
)
.is_ok());
assert!(instance.verifier().check(&solution).is_ok());
}
/// Longer running tests from C tor, covering solve and verify both
///
/// These tests are still optimized to complete without wasting too much time,
/// by artificially choosing a `first_nonce` only slightly lower than the
/// `expected_nonce` we want to find.
#[test]
fn solver() {
solve_and_verify(
0_u32.into(),
hex!("55555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("55555555555555555555555555555555").into(),
hex!("4312f87ceab844c78e1c793a913812d7"),
);
solve_and_verify(
1_u32.into(),
hex!("55555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("55555555555555555555555555555555").into(),
hex!("84355542ab2b3f79532ef055144ac5ab"),
);
solve_and_verify(
1_u32.into(),
hex!("55555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111110").into(),
hex!("55555555555555555555555555555555").into(),
hex!("115e4b70da858792fc205030b8c83af9"),
);
solve_and_verify(
2_u32.into(),
hex!("55555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("55555555555555555555555555555555").into(),
hex!("4600a93a535ed76dc746c99942ab7de2"),
);
solve_and_verify(
10_u32.into(),
hex!("55555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("56555555555555555555555555555555").into(),
hex!("128bbda5df2929c3be086de2aad34aed"),
);
solve_and_verify(
10_u32.into(),
hex!("ffffffffffffffffffffffffffffffff").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("01000000000000000000000000000000").into(),
hex!("203af985537fadb23f3ed5873b4c81ce"),
);
solve_and_verify(
1337_u32.into(),
hex!("7fffffffffffffffffffffffffffffff").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("4111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("01000000000000000000000000000000").into(),
hex!("31c377cb72796ed80ae77df6ac1d6bfd"),
);
solve_and_verify(
31337_u32.into(),
hex!("34a20000000000000000000000000000").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("36a20000000000000000000000000000").into(),
hex!("ca6899b91113aaf7536f28db42526bff"),
);
solve_and_verify(
100_u32.into(),
hex!("55555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("56555555555555555555555555555555").into(),
hex!("3a4122a240bd7abfc922ab3cbb9479ed"),
);
solve_and_verify(
1000_u32.into(),
hex!("d3555555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("d4555555555555555555555555555555").into(),
hex!("338cc08f57697ce8ac2e4b453057d6e9"),
);
solve_and_verify(
10_000_u32.into(),
hex!("c5715555555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("c8715555555555555555555555555555").into(),
hex!("9f2d3d4ed831ac96ad34c25fb59ff3e2"),
);
solve_and_verify(
100_000_u32.into(),
hex!("418d5655555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("428d5655555555555555555555555555").into(),
hex!("9863f3acd2d15adfd244a7ca61d4c6ff"),
);
solve_and_verify(
1_000_000_u32.into(),
hex!("58217255555555555555555555555555").into(),
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("1111111111111111111111111111111111111111111111111111111111111111").into(),
hex!("59217255555555555555555555555555").into(),
hex!("0f3db97b9cac20c1771680a1a34848d3"),
);
solve_and_verify(
1_u32.into(),
hex!("d0aec1669384bfe5ed39cd724d6c7954").into(),
hex!("c52be1f8a5e6cc3b8fb71cfdbe272cbc91d4d035400f2f94fb0d0074794e0a07").into(),
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("d1aec1669384bfe5ed39cd724d6c7954").into(),
hex!("462606e5f8c2f3f844127b8bfdd6b4ff"),
);
solve_and_verify(
1_u32.into(),
hex!("b4d0e611e6935750fcf9406aae131f62").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into(),
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("b4d0e611e6935750fcf9406aae131f62").into(),
hex!("9f3fbd50b1a83fb63284bde44318c0fd"),
);
solve_and_verify(
1_u32.into(),
hex!("b4d0e611e6935750fcf9406aae131f62").into(),
hex!("9dfbd06d86fed8e12de3ab214e1a63ea61f46253fe08346a20378da70c4a327d").into(),
hex!("bec632eb76123956f99a06d394fcbee8f135b8ed01f2e90aabe404cb0346744a").into(),
hex!("b4d0e611e6935750fcf9406aae131f62").into(),
hex!("161baa7490356292d020065fdbe55ffc"),
);
solve_and_verify(
1_u32.into(),
hex!("40559fdbc34326d9d2f18ed277469c63").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into(),
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("40559fdbc34326d9d2f18ed277469c63").into(),
hex!("fa649c6a2c5c0bb6a3511b9ea4b448d1"),
);
solve_and_verify(
10_000_u32.into(),
hex!("34569fdbc34326d9d2f18ed277469c63").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into(),
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("36569fdbc34326d9d2f18ed277469c63").into(),
hex!("2802951e623c74adc443ab93e99633ee"),
);
solve_and_verify(
100_000_u32.into(),
hex!("2cff9fdbc34326d9d2f18ed277469c63").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into(),
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("2eff9fdbc34326d9d2f18ed277469c63").into(),
hex!("400cb091139f86b352119f6e131802d6"),
);
solve_and_verify(
1_000_000_u32.into(),
hex!("5243b3dbc34326d9d2f18ed277469c63").into(),
hex!("86fb0acf4932cda44dbb451282f415479462dd10cb97ff5e7e8e2a53c3767a7f").into(),
hex!("bfd298428562e530c52bdb36d81a0e293ef4a0e94d787f0f8c0c611f4f9e78ed").into(),
hex!("5543b3dbc34326d9d2f18ed277469c63").into(),
hex!("b47c718b56315e9697173a6bac1feaa4"),
);
}
|