-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (76 loc) · 3.5 KB
/
Copy pathmain.cpp
File metadata and controls
99 lines (76 loc) · 3.5 KB
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
#include <memory>
#include <ranges>
#include "todo/include/SecretShare.h"
#include "todo/include/Party.h"
#include "todo/include/utils/constants.h"
#include "todo/include/utils/ShareUtils.h"
#include "todo/include/ShamirSharing.h"
#include "todo/overflow/utilities.h"
void testFunction();
void testShamir();
int main() {
// testFunction();
testShamir();
return 0;
}
void testFunction() {
std::uint16_t value_count;
std::cout << "*************** Offline Phase ***************" << std::endl;
std::cout << "Modulus value: " << CONSTANTS::MODULUS << std::endl;
std::cout << "Share Count: " << CONSTANTS::SHARE_COUNT << std::endl;
std::cout << "Enter a No.of Values: ";
std::cin >> value_count;
std::vector<int> values(value_count);
static_assert(std::is_same<decltype(value_count), std::uint16_t>{}, "Value Count should be positive integer");
// for_each loop IOTA-creates a view from 0, value_count
std::ranges::for_each(std::views::iota(0, static_cast<int>(value_count)), [&](const int i) {
std::cout << "Enter the value of " << (i+1) << "th value: ", std::cin >> values[i];
});
std::vector<std::shared_ptr<AdditiveSecretShare>> secretShares;
secretShares.reserve(value_count);
for (size_t i = 0; i < value_count; i ++)
secretShares.emplace_back(std::make_shared<AdditiveSecretShare>(values[i], CONSTANTS::MODULUS, value_count));
std::vector<std::vector<int>> shares(value_count);
for (size_t i = 0; i < value_count; ++i) {
shares[i] = secretShares[i]->getShares(value_count);
}
std::cout << "\nPrinting shares of each party....\n" << std::endl;
RQ_SMPC_Utils::printShareMatrix(shares);
std::vector<std::vector<int>> share_distributed = RQ_SMPC_Utils::getTranspose(shares);
std::cout << "\nPrinting shares of to be distributed ....\n" << std::endl;
RQ_SMPC_Utils::printShareMatrix(share_distributed);
// Distributing the shares to all the parities.
std::cout << "****************** Online Phase *******************" << std::endl;
std::cout << "Distributing the shares to three parties..." << std::endl;
std::vector<SMPCAdditionParty*> parties(value_count);
for (int i = 0; i < value_count; ++i) {
parties[i] = new SMPCAdditionParty(share_distributed[i], CONSTANTS::MODULUS); // Creating a vector of parties
}
std::cout << "\nComputing the partial sum...\n" << std::endl;
// Computing the partial sum of all parties.
// High Computational task.
// Creating a timer here to test the efficiency.
Timer timer1(__FUNCTION__);
int total_sum = 0;
for (const auto& smpc_party: parties) {
const int temp = smpc_party->computePartialSum();
total_sum += temp;
std::cout << temp << std::endl;
}
std::cout << "\nTotal Sum....\n" << std::endl;
std::cout << total_sum % CONSTANTS::MODULUS << std::endl;
timer1.lapse();
//TODO: improve this using smart pointers.
for (const auto& smpc_party: parties)
delete smpc_party;
parties.clear();
}
void testShamir() {
int secret, threshold;
std::cout << "Enter Secret Value: ", std::cin >> secret;
std::cout << "Enter Threshold Value: ", std::cin >> threshold;
auto poly = ShamirSharing::generateShares(secret, 5, threshold);
std::ranges::for_each(poly, [&](const std::pair<int, int>& i) { std::cout << i.first << " " << i.second << "\n"; });
const int secret_ = ShamirSharing::reconstructSecret(poly);
std::cout << "Secret Value: " << secret_ << std::endl;
}