-
Notifications
You must be signed in to change notification settings - Fork 5
/
scheme_atomic_swap_test.cc
122 lines (104 loc) · 3.8 KB
/
scheme_atomic_swap_test.cc
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
#include "scheme_atomic_swap_test.h"
#include "scheme_atomic_swap_alice.h"
#include "scheme_atomic_swap_bob.h"
#include "scheme_atomic_swap_notary.h"
#include "scheme_atomic_swap_protocol.h"
#include "scheme_plain.h"
#include "scheme_plain_alice_data.h"
#include "scheme_plain_bob_data.h"
#include "scheme_table.h"
#include "scheme_table_alice_data.h"
#include "scheme_table_bob_data.h"
namespace {
// The alice id must be hash(AliceAddr), and the bob id must be hash(BobAddr).
// Here just use two dummy values for test.
const h256_t kDummyAliceId = h256_t{{1}};
const h256_t kDummyBobId = h256_t{{2}};
} // namespace
namespace scheme::atomic_swap {
template <typename AliceData, typename BobData>
bool Test(std::string const& output_path, std::shared_ptr<AliceData> alice_data,
std::shared_ptr<BobData> bob_data, std::vector<Range> const& demands,
bool evil) {
Tick _tick_(__FUNCTION__);
auto output_file = output_path + "/decrypted_data";
Alice alice(alice_data, kDummyAliceId, kDummyBobId);
Bob bob(bob_data, kDummyBobId, kDummyAliceId, demands);
if (evil) alice.TestSetEvil();
Request request;
bob.GetRequest(request);
Response response;
if (!alice.OnRequest(request, response)) {
assert(false);
return false;
}
Receipt receipt;
if (!bob.OnResponse(std::move(response), receipt)) {
assert(false);
return false;
}
Secret secret;
if (!alice.OnReceipt(receipt, secret)) {
assert(false);
return false;
}
if (!evil) {
if (!bob.OnSecret(secret)) {
assert(false);
return false;
}
if (!bob.SaveDecrypted(output_file)) {
assert(false);
return false;
}
std::cout << "success: save to " << output_file << "\n";
} else {
if (bob.OnSecret(secret)) {
assert(false);
return false;
}
}
return true;
}
} // namespace scheme::atomic_swap
namespace scheme::plain::atomic_swap {
bool Test(std::string const& publish_path, std::string const& output_path,
std::vector<Range> const& demands, bool test_evil) {
try {
using scheme::plain::AliceData;
using scheme::plain::BobData;
auto alice_data = std::make_shared<AliceData>(publish_path);
std::string bulletin_file = publish_path + "/bulletin";
std::string public_path = publish_path + "/public";
auto bob_data = std::make_shared<BobData>(bulletin_file, public_path);
auto const& bulletin = bob_data->bulletin();
std::cout << "n: " << bulletin.n << ", s: " << bulletin.s
<< ", size: " << bulletin.size << "\n";
return scheme::atomic_swap::Test(output_path, alice_data, bob_data, demands,
test_evil);
} catch (std::exception& e) {
std::cerr << __FUNCTION__ << "\t" << e.what() << "\n";
return false;
}
}
} // namespace scheme::plain::atomic_swap
namespace scheme::table::atomic_swap {
bool Test(std::string const& publish_path, std::string const& output_path,
std::vector<Range> const& demands, bool test_evil) {
try {
using scheme::table::AliceData;
using scheme::table::BobData;
auto alice_data = std::make_shared<AliceData>(publish_path);
std::string bulletin_file = publish_path + "/bulletin";
std::string public_path = publish_path + "/public";
auto bob_data = std::make_shared<BobData>(bulletin_file, public_path);
auto const& bulletin = bob_data->bulletin();
std::cout << "n: " << bulletin.n << ", s: " << bulletin.s << "\n";
return scheme::atomic_swap::Test(output_path, alice_data, bob_data, demands,
test_evil);
} catch (std::exception& e) {
std::cerr << __FUNCTION__ << "\t" << e.what() << "\n";
return false;
}
}
} // namespace scheme::table::atomic_swap