forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfp.cpp
135 lines (112 loc) · 2.5 KB
/
gfp.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
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
// (C) 2017 University of Bristol. See License.txt
#include "Math/gfp.h"
#include "Exceptions/Exceptions.h"
Zp_Data gfp::ZpD;
void gfp::almost_randomize(PRNG& G)
{
G.get_octets((octet*)a.x,t()*sizeof(mp_limb_t));
a.x[t()-1]&=ZpD.mask;
}
void gfp::AND(const gfp& x,const gfp& y)
{
bigint bi1,bi2;
to_bigint(bi1,x);
to_bigint(bi2,y);
mpz_and(bi1.get_mpz_t(), bi1.get_mpz_t(), bi2.get_mpz_t());
to_gfp(*this, bi1);
}
void gfp::OR(const gfp& x,const gfp& y)
{
bigint bi1,bi2;
to_bigint(bi1,x);
to_bigint(bi2,y);
mpz_ior(bi1.get_mpz_t(), bi1.get_mpz_t(), bi2.get_mpz_t());
to_gfp(*this, bi1);
}
void gfp::XOR(const gfp& x,const gfp& y)
{
bigint bi1,bi2;
to_bigint(bi1,x);
to_bigint(bi2,y);
mpz_xor(bi1.get_mpz_t(), bi1.get_mpz_t(), bi2.get_mpz_t());
to_gfp(*this, bi1);
}
void gfp::AND(const gfp& x,const bigint& y)
{
bigint bi;
to_bigint(bi,x);
mpz_and(bi.get_mpz_t(), bi.get_mpz_t(), y.get_mpz_t());
to_gfp(*this, bi);
}
void gfp::OR(const gfp& x,const bigint& y)
{
bigint bi;
to_bigint(bi,x);
mpz_ior(bi.get_mpz_t(), bi.get_mpz_t(), y.get_mpz_t());
to_gfp(*this, bi);
}
void gfp::XOR(const gfp& x,const bigint& y)
{
bigint bi;
to_bigint(bi,x);
mpz_xor(bi.get_mpz_t(), bi.get_mpz_t(), y.get_mpz_t());
to_gfp(*this, bi);
}
void gfp::SHL(const gfp& x,int n)
{
if (!x.is_zero())
{
if (n != 0)
{
bigint bi;
to_bigint(bi,x,false);
mpn_lshift(bi.get_mpz_t()->_mp_d, bi.get_mpz_t()->_mp_d, bi.get_mpz_t()->_mp_size,n);
to_gfp(*this, bi);
}
else
assign(x);
}
else
{
assign_zero();
}
}
void gfp::SHR(const gfp& x,int n)
{
if (!x.is_zero())
{
if (n != 0)
{
bigint bi;
to_bigint(bi,x);
mpn_rshift(bi.get_mpz_t()->_mp_d, bi.get_mpz_t()->_mp_d, bi.get_mpz_t()->_mp_size,n);
to_gfp(*this, bi);
}
else
assign(x);
}
else
{
assign_zero();
}
}
void gfp::SHL(const gfp& x,const bigint& n)
{
SHL(x,mpz_get_si(n.get_mpz_t()));
}
void gfp::SHR(const gfp& x,const bigint& n)
{
SHR(x,mpz_get_si(n.get_mpz_t()));
}
gfp gfp::sqrRoot()
{
// Temp move to bigint so as to call sqrRootMod
bigint ti;
to_bigint(ti, *this);
ti = sqrRootMod(ti, ZpD.pr);
if (!isOdd(ti))
ti = ZpD.pr - ti;
gfp temp;
to_gfp(temp, ti);
return temp;
}