-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.cs
285 lines (248 loc) · 10.6 KB
/
Polynomial.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace shamirsSecretSharing
{
/// <summary>
/// Creates an class for polynomial Calculation on a finite group.
/// Only the zero coefficient can be set, the rest is calculated automatically
/// </summary>
class Polynomial
{
/// <summary>
/// The prime modulo of the finite group
/// </summary>
private BigInteger PrimeModulo;
/// <summary>
/// The number of coefficients to be used (degree + 1)
/// </summary>
private uint NumCoefficients;
/// <summary>
/// Size of the modulo in bits
/// </summary>
private uint ModuloBitSize;
/// <summary>
/// Coefficients of the polynomial
/// </summary>
private BigInteger[] Coefficients;
/// <summary>
/// RNG
/// </summary>
private SecureRandom Rand;
/// <summary>
/// Create the base polynomial
/// </summary>
/// <param name="inPrimeModulo"> Prime number of the finite group</param>
/// <param name="inNumCoefficients"> Number of coefficients of the polynomial (degree + 1)</param>
/// <param name="inModuloBitSize"> BitSize of the prime modulo</param>
public Polynomial(BigInteger inPrimeModulo, uint inNumCoefficients, uint inModuloBitSize)
{
if (inNumCoefficients < 2) throw new ArgumentException("inNumCoefficients has to be greater or equal to 2");
if(!Array.Exists(PublicKey.allowedSizes, element => element == inModuloBitSize)) throw new ArgumentException(string.Format("inModuloBitSize has to be in ( {0} )", string.Join(", ", PublicKey.allowedSizes)));
PrimeModulo = inPrimeModulo;
NumCoefficients = inNumCoefficients;
ModuloBitSize = inModuloBitSize;
Coefficients = new BigInteger[NumCoefficients];
Rand = new SecureRandom();
}
/// <summary>
/// Create the base polynomial
/// </summary>
/// <param name="inPrimeModulo"> Prime number of the finite group in byte </param>
/// <param name="inNumCoefficients"> Number of coefficients of the polynomial (degree + 1)</param>
/// <param name="inModuloBitSize"> BitSize of the prime modulo</param>
public Polynomial(byte[] inPrimeModulo, uint inNumCoefficients, uint inModuloBitSize)
: this(new BigInteger(1, inPrimeModulo), inNumCoefficients, inModuloBitSize) { }
/// <summary>
/// Create the base polynomial based on a SSS public key
/// </summary>
/// <param name="key"> SSS public key </param>
public Polynomial(PublicKey key) : this(key.PrimeModulo,key.N, key.ModSize) { }
/// <summary>
/// Initialize the polynomial with random coefficients and the given 0th coefficient
/// </summary>
/// <param name="zeroCoefficient"> 0th coefficient for the polynomial as a byte array</param>
public void Init(byte[] zeroCoefficient)
{
BigInteger coefficient, tmpCoefficient;
byte[] storage = new byte[ModuloBitSize / 8];
coefficient = new BigInteger(1, zeroCoefficient);
if (coefficient.CompareTo(PrimeModulo) != -1)
{
// destroy coefficient
//coefficient.Dispose();
throw new ArgumentException("the given zeroCoefficient is bigger than the modulo");
}
Coefficients[0] = coefficient;
for (int i = 1; i < Coefficients.Length; i++)
{
Rand.NextBytes(storage);
tmpCoefficient = new BigInteger(1, storage);
coefficient = tmpCoefficient.Mod(PrimeModulo);
// destroy tmpCoefficient
//tmpCoefficient.Dispose();
Coefficients[i] = coefficient;
}
// overwrite storage
for (int i = 0; i < storage.Length; i++)
{
storage[i] = 0;
}
}
/// <summary>
/// Calculates the value of the polynomial at the given x-position
/// </summary>
/// <param name="x"> x-position as a byte array</param>
private byte[] CalculatePoint(byte[] x)
{
BigInteger Y, help1, help2;
BigInteger X = new BigInteger(1, x);
Y = Coefficients[0].Mod(PrimeModulo);
for (int i = 1; i < Coefficients.Length; i++)
{
// a_i * x^i mod P
help2 = Coefficients[i].Multiply(X.ModPow(new BigInteger(i.ToString()),PrimeModulo));
help1 = Y.Add(help2);
// destroy Y, help2
//Y.Dispose();
// help2.Dispose();
Y = help1.Mod(PrimeModulo);
// destroy help1
//help1.Dispose();
}
byte[] res = Y.ToByteArrayUnsigned();
// destroy Y
//Y.Dispose();
return res;
}
/// <summary>
/// Calculates the values of the polynomial at the given x-positions
/// </summary>
/// <param name="xValues"> x-positions as a byte arrays. The array can't include 0 and every x-postion has to be unique</param>
public byte[][] CalculatePoints(byte[][] xValues)
{
TestXValues(xValues); // throws exceptions
byte[][] result = new byte[xValues.Length][];
for (int i = 0; i < xValues.Length; i++)
{
result[i] = CalculatePoint(xValues[i]);
}
return result;
}
/// <summary>
/// Test if the given x-positions are unique and don't contain x=0
/// </summary>
/// <param name="xValues"> x-positions as a byte arrays</param>
private static void TestXValues(byte[][] xValues)
{
for (int i = 0; i < xValues.Length - 1; i++)
{
if (new BigInteger(1, xValues[0]).Equals(BigInteger.Zero)) throw new ArgumentException(string.Format("xValues[{0}] is Zero", i));
for (int j = i + 1; j < xValues.Length; j++)
{
if (xValues[i].SequenceEqual(xValues[j])) throw new ArgumentException(string.Format("xValues {0} and {1} are identical", i, j));
}
}
}
/// <summary>
/// Reconstructs the polynomial with the given Points and calculates the value at the given position.
/// </summary>
/// <param name="xValues"> x-positions as a byte arrays</param>
/// <param name="yValues"> y-positions as a byte arrays</param>
/// <param name="xPos"> x-positions to calculate polynomial value for</param>
/// <param name="primeModulo"> prime modulo of the original polynom</param>
public static byte[] Reconstruct(byte[][] xValues, byte[][] yValues, byte[] xPos, byte[] primeModulo)
{
BigInteger[] rel, yVals, xVals, xCoeffs;
BigInteger help1, help2, ret;
byte[] res;
BigInteger bigPrimeModulo = new BigInteger(1, primeModulo);
xVals = new BigInteger[xValues.Length];
yVals = new BigInteger[xValues.Length];
for (int i = 0; i < xValues.Length; i++)
{
xVals[i] = new BigInteger(1, xValues[i]);
yVals[i] = new BigInteger(1, yValues[i]);
}
rel = RelativePosition(xVals, xPos);
xCoeffs = XCoeff(xVals, rel, bigPrimeModulo);
ret = BigInteger.Zero;
for (int i = 0; i < xValues.Length; i++)
{
help1 = yVals[i].Multiply(xCoeffs[i]);
//yVals[i].Dispose();
//xVals[i].Dispose();
help2 = help1.Mod(bigPrimeModulo);
//help1.Dispose();
//Destroy help1, yVals[i]
help1 = ret;
ret = ret.Add(help2);
// destroy help1,help2
//help1.Dispose();
//help2.Dispose();
}
help1 = ret;
ret = ret.Mod(bigPrimeModulo);
res = ret.ToByteArrayUnsigned();
// destroy help1, ret
//help1.Dispose();
//ret.Dispose();
return res;
}
/// <summary>
/// Calculates the relative Positions of the given xValues to the xPosition
/// </summary>
/// <param name="xValues"> x-positions as a byte arrays</param>
/// <param name="xPos"> x-positions to calculate polynomial value for</param>
private static BigInteger[] RelativePosition(BigInteger[] xValues, byte[] xPos)
{
BigInteger X = new BigInteger(1, xPos);
BigInteger[] res = new BigInteger[xValues.Length];
for (int i = 0; i < xValues.Length; i++)
{
res[i] = X.Subtract(xValues[i]);
}
return res;
}
/// <summary>
/// Calculates the Coefficients based on the x-positons of the points and the x-position to evaluate
/// </summary>
/// <param name="xValues"> x-positions as a byte arrays</param>
/// <param name="rel"> relative distance of the evaluated position and the given x-Values</param>
/// <param name="primeModulo"> prime modulo of the original polynomial</param>
private static BigInteger[] XCoeff(BigInteger[] xValues, BigInteger[] rel, BigInteger primeModulo)
{
BigInteger[] res = new BigInteger[xValues.Length];
BigInteger top, down;
for (int i = 0; i < xValues.Length; i++)
{
top = BigInteger.One;
down = BigInteger.One;
for (int j = 0; j < xValues.Length; j++)
{
if (i != j)
{
top = top.Multiply(rel[j]);
top = top.Mod(primeModulo);
down = down.Multiply(xValues[i].Subtract(xValues[j]));
down = down.Mod(primeModulo);
}
}
res[i] = top.Multiply(down.ModInverse(primeModulo)).Mod(primeModulo);
}
return res;
}
public void Destroy()
{
for (int i = 0; i < Coefficients.Length; i++)
{
// Destroy Coefficients
}
// Destroy Prime Modulo
}
}
}