-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlogRT.cpp
More file actions
49 lines (43 loc) · 1.12 KB
/
dlogRT.cpp
File metadata and controls
49 lines (43 loc) · 1.12 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
#include<iostream>
#include<time.h>
using namespace std;
//求方幂模,求解res,计算res=g^x mod p
int ModularExponent(int g, int x, int p)
{//计算g^x mod p
int res = 1;
for (int i = 0; i < x; i++)
{
res = res*g%p;
}
return res;
}
//计算离散对数,求x,条件:a=g^x mod p
int dlog(int a, int g, int p)
{//a为余数,g为底数,模p
int x = 1, y = 1;
y = y*g%p;
while (a!=y&&x<p)
{
x++;
y = y*g%p;
}
return x;
}
//sherwood算法变换,求a的离散对数x,先转换为求c的离散对数,最后变换成求a的离散对数
//好处:当要求第三方计算时,能够不使原始数据信息泄露
int dlogRH(int a, int g, int p)
{
srand((unsigned)time(NULL));
int r = 0 + rand() % (p - 2);
int b = ModularExponent(g, r, p); //求指数为r时的模
int c = b*a%p; //变换求c的离散对数
int y = dlog(c, g, p);
return (y - r) % (p - 1); //因为离散对数是循环群,需要模(p-1)
}
void main()
{
cout << dlog(3, 2, 5) << endl;
cout << ModularExponent(3, 2, 5) << endl;
cout << dlogRH(3, 2, 5)<<endl; //sherwood算法计算,结果和dlog计算一样
system("pause");
}