forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subroutines.cpp
94 lines (82 loc) · 2.25 KB
/
Subroutines.cpp
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
#include "Subroutines.h"
#include "Math/modp.h"
#include "Math/modp.hpp"
void Subs(modp& ans,const vector<int>& poly,const modp& x,const Zp_Data& ZpD)
{
modp one,co;
assignOne(one,ZpD);
assignZero(ans,ZpD);
for (int i=poly.size()-1; i>=0; i--)
{ Mul(ans,ans,x,ZpD);
if (poly[i] > 0)
{
for (int j = 0; j < poly[i]; j++)
Add(ans, ans, one, ZpD);
}
if (poly[i] < 0)
{
for (int j = 0; j < -poly[i]; j++)
Sub(ans, ans, one, ZpD);
}
}
}
/* Find a m'th primitive root moduli the current prime
* This is deterministic so all players have the same root of unity
* poly is Phi_m(X)
*/
modp Find_Primitive_Root_m(int m,const vector<int>& poly,const Zp_Data& ZpD)
{
modp ans,e,one,base;
assignOne(one,ZpD);
assignOne(base,ZpD);
bigint exp;
exp=(ZpD.pr-1)/m;
bool flag=true;
while (flag)
{ Add(base,base,one,ZpD); // Keep incrementing base until we hit the answer
Power(ans,base,exp,ZpD);
// e=Phi(ans)
Subs(e,poly,ans,ZpD);
if (isZero(e,ZpD)) { flag=false; }
}
return ans;
}
/* Find a (2m)'th primitive root moduli the current prime
* This is deterministic so all players have the same root of unity
* poly is Phi_m(X)
*/
modp Find_Primitive_Root_2m(int m,const vector<int>& poly,const Zp_Data& ZpD)
{
// Thin out poly, where poly is Phi_m(X) by 2
vector<int> poly2;
poly2.resize(2*poly.size());
for (unsigned int i=0; i<poly.size(); i++)
{ poly2[2*i]=poly[i];
poly2[2*i+1]=0;
}
modp ans=Find_Primitive_Root_m(2*m,poly2,ZpD);
return ans;
}
/* Find an mth primitive root moduli the current prime
* This is deterministic so all players have the same root of unity
* This assumes m is a power of two and so the cyclotomic polynomial
* is F=X^{m/2}+1
*/
modp Find_Primitive_Root_2power(int m,const Zp_Data& ZpD)
{
modp ans,e,one,base;
assignOne(one,ZpD);
assignOne(base,ZpD);
bigint exp;
exp=(ZpD.pr-1)/m;
bool flag=true;
while (flag)
{ Add(base,base,one,ZpD); // Keep incrementing base until we hit the answer
Power(ans,base,exp,ZpD);
// e=ans^{m/2}+1
Power(e,ans,m/2,ZpD);
Add(e,e,one,ZpD);
if (isZero(e,ZpD)) { flag=false; }
}
return ans;
}