This repository was archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (125 loc) · 4.5 KB
/
main.cpp
File metadata and controls
153 lines (125 loc) · 4.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
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
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX_SECRET 100000
#define MIN_SECRET 10000
#define MAX_PEOPLE 5
#define MIN_PEOPLE 2
bool is_prime(int n)
{
if(n == 1 || n == 0) return false;
for(int i = 2; i < n; i++)
{
if(n % i == 0) return false;
}
return true;
}
vector<int> generate_prime(int size){
vector<int> result;
for(int i = 1; i <= size; i++)
{
if(is_prime(i))
{
result.push_back(i);
}
}
return result;
}
long long int inverse_element(long long int value, long long int module){
for(int i = 1; i < module; i++){
if((i * value) % module == 1){
return i;
}
}
return 0;
}
int main()
{
int secret = 0;
int n, k;
cout << "-------------------------------------------------------------" << "\n";
cout << "----------------------->sharing phase<-----------------------" << "\n";
cout << "-------------------------------------------------------------" << "\n\n";
cout << "input:" << "\n";
cout << "please enter the secret (integer from 10000 to 100000): ";
cin >> secret;
if(secret > MAX_SECRET || secret < MIN_SECRET){
cout << "error. the secret must be a number between 10000 and 100000" << endl;
return 0;
}
cout << "please enter the number of users (integer from 2 to 5): ";
cin >> n;
if(n > MAX_PEOPLE || n < MIN_PEOPLE){
cout << "error. the number of users must be a number between 2 and 5" << endl;
return 0;
}
cout << "please enter the number of users needed to get the secret (integer from 2 to 5):";
cin >> k;
if(k > MAX_PEOPLE || k < MIN_PEOPLE){
cout << "error. the number of users needed to get the secret must be a number between 2 and 5;" << endl;
return 0;
}
if(n < k){
cout << "error. the number of secret users cannot be greater than the number of users" << endl;
return 0;
}
vector<int> primes = generate_prime(secret);
vector<int> prime_parts;
vector<int> value_parts;
srand(time(0));
for(int i = 0; i < n; i++){
int index = rand() % (primes.size() - 1);
double min_value = pow((double)secret, (double)pow(k, -1));
double max_value = pow((double)secret, (double)pow(k - 1, -1));
while(((double)primes[index] <= min_value) || ((double)primes[index] > max_value) || (find(prime_parts.begin(), prime_parts.end(), primes[index]) != prime_parts.end())){
index = rand() % (primes.size() - 1);
}
prime_parts.push_back(primes[index]);
}
cout << "\n";
for(int i = 0; i < prime_parts.size(); i++){
value_parts.push_back(secret % prime_parts[i]);
}
cout << "output:\n";
for(int i = 0; i < n; i++){
cout << "( x(" << i + 1<< ") = " << value_parts[i] << ", p(" << i + 1 << ") = " << prime_parts[i] << " )" << "\n";
}
cout << "\n-------------------------------------------------------------" << "\n";
cout << "------------------->secret phase recovery<-------------------" << "\n";
cout << "-------------------------------------------------------------" << "\n\n";
cout << "input:\n";
vector<int> positions;
positions.push_back(rand() % n);
for(int i = 1; i < k; i++){
int new_index = rand() % n;
while(find(positions.begin(), positions.end(), new_index) != positions.end()){
new_index = rand() % n;
}
positions.push_back(new_index);
int tmp = prime_parts[i];
prime_parts[i] = prime_parts[new_index];
prime_parts[new_index] = tmp;
tmp = value_parts[i];
value_parts[i] = value_parts[new_index];
value_parts[new_index] = tmp;
}
for(int i = 0; i < k; i++){
cout << "( x(" << i + 1 << ") = " << value_parts[i] << ", p(" << i + 1 << ") = " << prime_parts[i] << " )" << "\n";
}
long long int result_secret = 0;
long long int module = 1;
for(int i = 0; i < k; i++){
module *= prime_parts[i];
}
for(int i = 0; i < k; i++){
long long int first = (module / prime_parts[i]) % module;
long long int second = value_parts[i] % module;
long long int third = inverse_element(module / prime_parts[i], prime_parts[i]) % module;
result_secret += ((first * second * third) % module);
}
result_secret = result_secret % module;
cout << "\noutput:\nsecret = " << result_secret << "\n\n" << endl;
return 0;
}