-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculate.cs
160 lines (136 loc) · 4.62 KB
/
Calculate.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
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
using System.Linq;
namespace Calculations
{
public class Calculate
{
public static TimeSpan Time(Action action)
{
Stopwatch stopwatch = Stopwatch.StartNew();
action();
stopwatch.Stop();
return stopwatch.Elapsed;
}
public static void difiAlgorithm()
{
Stopwatch sw = Stopwatch.StartNew();
BigInteger n, g;
TimeSpan timeN, timeG;
List<BigInteger> listG;
sw.Start();
n = generateNumber(1000);
Console.WriteLine(n);
sw.Stop();
timeN = sw.Elapsed;
sw.Start();
listG = primitiveRoots(n);
g = listG.First();
sw.Stop();
timeG = sw.Elapsed;
Console.WriteLine($"Primitive root: {isPrimitiveRoot(g, n)}");
Console.WriteLine($"n: {n}, generating [ms]: {timeN}");
Console.WriteLine($"g: {g}, generating [ms]: {timeG}");
BigInteger x = 0;
BigInteger y = 0;
sw.Start();
var ix = generateXY(10, 100);//male x
sw.Stop();
Console.WriteLine($"x generating [ms]: {sw.Elapsed}");
sw.Start();
var iy = generateXY(10, 100); //male y
sw.Stop();
Console.WriteLine($"y generating [ms]: {sw.Elapsed}");
sw.Start();
x = genPrivateKey(n, ix, g); //duze X
sw.Stop();
Console.WriteLine($"Private Key: {x}, generating [ms]: {sw.Elapsed}");
sw.Start();
y = genPublicKey(n, iy, g); //duze Y
sw.Stop();
Console.WriteLine($"Public Key: {y}, generating [ms]: {sw.Elapsed}");
var kA = calculateK(y, ix, n);
var kB = calculateK(x, iy, n);
Console.WriteLine($"kA: {kA}, kB: {kB}");
}
private static BigInteger generateNumber(BigInteger N)
{
Random rnx = new Random();
BigInteger sd = 0;
do
sd = rnx.Next(100, 10000);
while (BigInteger.GreatestCommonDivisor(sd, N) != 1);
return sd;
}
private static List<BigInteger> primitiveRoots(BigInteger modulo)
{
var coprimes = new List<BigInteger>();
for (int i = 1; i < modulo; i++)
{
if (BigInteger.GreatestCommonDivisor(i, modulo) == 1)
{
coprimes.Add(i);
}
}
var primitiveRoots = new List<BigInteger>();
for (int j = 1; j < modulo; j++)
{
var resultSet = new List<BigInteger>();
for (int i = 1; i < modulo; i++)
{
resultSet.Add(i % modulo);
}
if (coprimes.TrueForAll(resultSet.Contains))
{
primitiveRoots.Add(j);
}
}
return primitiveRoots;
}
private static bool isPrimitiveRoot(BigInteger modulo, BigInteger arg)
{
var coprimes = new List<BigInteger>();
for (int i = 0; i < modulo; i++)
{
if (BigInteger.GreatestCommonDivisor(i, modulo) == 1)
{
coprimes.Add(i);
}
}
var resultSet = new List<BigInteger>();
for (int i = 0; i < modulo; i++)
{
resultSet.Add(BigInteger.Pow(arg, i) % modulo);
}
return coprimes.TrueForAll(resultSet.Contains);
}
private static BigInteger generateXY(int min, int max)
{
var rnx = new Random();
return rnx.Next(min, max);
}
private static BigInteger genPrivateKey(BigInteger n, BigInteger ix, BigInteger g)
{
return BigInteger.Pow(g, (int)ix) % n;
}
private static BigInteger genPublicKey(BigInteger n, BigInteger iy, BigInteger g)
{
return BigInteger.Pow(g, (int)iy) % n;
}
private static BigInteger calculateK(BigInteger basis, BigInteger pow, BigInteger n)
{
return BigInteger.Pow(basis, (int)pow) % n;
}
/*private static BigInteger CheckInteger()
{
BigInteger x = 0;
string readString = Console.ReadLine();
if (BigInteger.TryParse(readString, out x))
return x;
else
return 0;
}*/
}
}