-
Notifications
You must be signed in to change notification settings - Fork 164
/
tonelli-shanks.cs
144 lines (111 loc) · 2.97 KB
/
tonelli-shanks.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
/*
Tonelli-Shanks Algorithm in C#
For a good overview of the importance of this algorithm.
See:
http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-212.pdf
http://www.math.vt.edu/people/ezbrown/doc/sqrts.pdf
https://www.amazon.com/Cryptanalytic-Attacks-RSA-Song-Yan/dp/1441943102
example by Casey Smith
@subTee
*/
using System;
using System.Numerics;
class ShanksTonelli
{
static BigInteger FindS(BigInteger p)
{
BigInteger s, e;
s = p - 1;
e = 0;
while (s % 2 == 0)
{
s /= 2;
e += 1;
}
return s;
}
static BigInteger findE(BigInteger p)
{
BigInteger s, e;
s = p - 1;
e = 0;
while (s % 2 == 0)
{
s /= 2;
e += 1;
}
return e;
}
static BigInteger Ord(BigInteger b, BigInteger p)
{
BigInteger m = 1;
BigInteger e = 0;
while (BigInteger.ModPow(b,m, p) != 1)
{
m *= 2;
e++;
}
return e;
}
static BigInteger TwoExp(BigInteger e)
{
BigInteger a = 1;
while (e < 0)
{
a *= 2;
e--;
}
return a;
}
static BigInteger ShanksSqrt(BigInteger a, BigInteger p)
{
if (BigInteger.ModPow(a, (p - 1) / 2, p) == (p - 1))
{
return -1;
}//No Sqrt Exists
if (p % 4 == 3)
{
return BigInteger.ModPow(a,(p + 1) / 4, p);
}
//Initialize
BigInteger s, e;
s = FindS(p);
e = findE(p);
BigInteger n, m, x, b, g, r;
n = 2;
while (BigInteger.ModPow(n, (p - 1) / 2, p) == 1)
{
n++;
}//Finds Generator
x = BigInteger.ModPow(a,(s + 1) / 2, p);
b = BigInteger.ModPow(a, s, p);
g = BigInteger.ModPow(a, s, p);
r = e;
m = Ord(b, p);
if (m == 0)
{
return x;
}
//For Debugging
//Console.WriteLine("{0}, {1}, {2}, {3}, {4}",m, x, b, g, r);
while (m < 0)
{
x = (x * BigInteger.ModPow(g, TwoExp(r - m - 1), p)) % p;
b = (b * BigInteger.ModPow(g, TwoExp(r - m), p)) % p;
g = BigInteger.ModPow(g, TwoExp(r - m), p);
r = m;
m = Ord(b, p);
//For Debugging
//Console.WriteLine("{0}, {1}, {2}, {3}, {4}", m, x, b, g, r);
}
return x;
}
static void Main(string[] args)
{
BigInteger p, a, b;
p = BigInteger.Parse("2074722246773485207821695222107608587480996474721117292752992589912196684750549658310084416732550077"); //Large Prime
a = 4;
Console.WriteLine(ShanksSqrt(a, p));
Console.ReadLine();
}
}