-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.c
More file actions
203 lines (166 loc) · 4.57 KB
/
split.c
File metadata and controls
203 lines (166 loc) · 4.57 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
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
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the value
// of y
// y = poly[0] + x*poly[1] + x^2*poly[2] + ...
int calculate_Y(int x, vector<int>& poly)
{
// Initializing y
int y = 0;
int temp = 1;
// Iterating through the array
for (auto coeff : poly) {
// Computing the value of y
y = (y + (coeff * temp));
temp = (temp * x);
}
return y;
}
// Function to perform the secret
// sharing algorithm and encode the
// given secret
void secret_sharing(int S, vector<pair<int, int> >& points,
int N, int K)
{
// A vector to store the polynomial
// coefficient of K-1 degree
vector<int> poly(K);
// Randomly choose K - 1 numbers but
// not zero and poly[0] is the secret
// create polynomial for this
poly[0] = S;
for (int j = 1; j < K; ++j) {
int p = 0;
while (p == 0)
// To keep the random values
// in range not too high
// we are taking mod with a
// prime number around 1000
p = (rand() % 997);
// This is to ensure we did not
// create a polynomial consisting
// of zeroes.
poly[j] = p;
}
// Generating N points from the
// polynomial we created
for (int j = 1; j <= N; ++j) {
int x = j;
int y = calculate_Y(x, poly);
// Points created on sharing
points[j - 1] = { x, y };
}
}
// This structure is used for fraction
// part handling multiplication
// and addition of fraction
struct fraction {
int num, den;
// A fraction consists of a
// numerator and a denominator
fraction(int n, int d)
{
num = n, den = d;
}
// If the fraction is not
// in its reduced form
// reduce it by dividing
// them with their GCD
void reduce_fraction(fraction& f)
{
int gcd = __gcd(f.num, f.den);
f.num /= gcd, f.den /= gcd;
}
// Performing multiplication on the
// fraction
fraction operator*(fraction f)
{
fraction temp(num * f.num, den * f.den);
reduce_fraction(temp);
return temp;
}
// Performing addition on the
// fraction
fraction operator+(fraction f)
{
fraction temp(num * f.den + den * f.num,
den * f.den);
reduce_fraction(temp);
return temp;
}
};
// Function to generate the secret
// back from the given points
// This function will use Lagrange Basis Polynomial
// Instead of finding the complete Polynomial
// We only required the poly[0] as our secret code,
// thus we can get rid of x terms
int Generate_Secret(int x[], int y[], int M)
{
fraction ans(0, 1);
// Loop to iterate through the given
// points
for (int i = 0; i < M; ++i) {
// Initializing the fraction
fraction l(y[i], 1);
for (int j = 0; j < M; ++j) {
// Computing the lagrange terms
if (i != j) {
fraction temp(-x[j], x[i] - x[j]);
l = l * temp;
}
}
ans = ans + l;
}
// Return the secret
return ans.num;
}
// Function to encode and decode the
// given secret by using the above
// defined functions
void operation(int S, int N, int K)
{
// Vector to store the points
vector<pair<int, int> > points(N);
// Sharing of secret Code in N parts
secret_sharing(S, points, N, K);
cout << "Secret is divided to " << N
<< " Parts - " << endl;
for (int i = 0; i < N; ++i) {
cout << points[i].first << " "
<< points[i].second << endl;
}
cout << "We can generate Secret from any of "
<< K << " Parts" << endl;
// Input any M points from these
// to get back our secret code.
int M = 2;
// M can be greater than or equal to threshold but
// for this example we are taking for threshold
if (M < K) {
cout << "Points are less than threshold "
<< K << " Points Required" << endl;
}
int* x = new int[M];
int* y = new int[M];
// Input M points you will get the secret
// Let these points are first M points from
// the N points which we shared above
// We can take any M points
for (int i = 0; i < M; ++i) {
x[i] = points[i].first;
y[i] = points[i].second;
}
// Get back our result again.
cout << "Our Secret Code is : "
<< Generate_Secret(x, y, M) << endl;
}
// Driver Code
int main()
{
int S = 65;
int N = 4;
int K = 2;
operation(S, N, K);
return 0;
}