This repository was archived by the owner on Apr 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathMemory.cpp
More file actions
166 lines (136 loc) · 3.92 KB
/
Memory.cpp
File metadata and controls
166 lines (136 loc) · 3.92 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
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
161
162
163
164
165
166
// (C) 2018 University of Bristol. See License.txt
#include "Processor/Memory.h"
#include "Processor/Instruction.h"
#include "Math/gf2n.h"
#include "Math/gfp.h"
#include "Math/Integer.h"
#include <fstream>
template<class T>
void Memory<T>::minimum_size(RegType reg_type, const Program& program, string threadname)
{
const int* sizes = program.direct_mem(reg_type);
if (sizes[SECRET] > size_s())
{
cerr << threadname << " needs more secret " << T::type_string() << " memory, resizing to "
<< sizes[SECRET] << endl;
resize_s(sizes[SECRET]);
}
if (sizes[CLEAR] > size_c())
{
cerr << threadname << " needs more clear " << T::type_string() << " memory, resizing to "
<< sizes[CLEAR] << endl;
resize_c(sizes[CLEAR]);
}
}
#ifdef MEMPROTECT
template<class T>
void Memory<T>::protect_s(unsigned int start, unsigned int end)
{
protected_s.insert(pair<unsigned int,unsigned int>(start, end));
}
template<class T>
void Memory<T>::protect_c(unsigned int start, unsigned int end)
{
protected_c.insert(pair<unsigned int,unsigned int>(start, end));
}
template<class T>
bool Memory<T>::is_protected_s(unsigned int index)
{
for (set< pair<unsigned int,unsigned int> >::iterator it = protected_s.begin();
it != protected_s.end(); it++)
if (it->first <= index and it->second > index)
return true;
return false;
}
template<class T>
bool Memory<T>::is_protected_c(unsigned int index)
{
for (set< pair<unsigned int,unsigned int> >::iterator it = protected_c.begin();
it != protected_c.end(); it++)
if (it->first <= index and it->second > index)
return true;
return false;
}
#endif
template<class T>
ostream& operator<<(ostream& s,const Memory<T>& M)
{
s << M.MS.size() << endl;
s << M.MC.size() << endl;
#ifdef DEBUG
for (unsigned int i=0; i<M.MS.size(); i++)
{ M.MS[i].output(s,true); s << endl; }
s << endl;
for (unsigned int i=0; i<M.MC.size(); i++)
{ M.MC[i].output(s,true); s << endl; }
s << endl;
#else
for (unsigned int i=0; i<M.MS.size(); i++)
{ M.MS[i].output(s,false); }
for (unsigned int i=0; i<M.MC.size(); i++)
{ M.MC[i].output(s,false); }
#endif
return s;
}
template<class T>
istream& operator>>(istream& s,Memory<T>& M)
{
int len;
s >> len;
M.resize_s(len);
s >> len;
M.resize_c(len);
s.seekg(1, istream::cur);
for (unsigned int i=0; i<M.MS.size(); i++)
{ M.MS[i].input(s,false); }
for (unsigned int i=0; i<M.MC.size(); i++)
{ M.MC[i].input(s,false); }
return s;
}
template<class T>
void Load_Memory(Memory<T>& M,ifstream& inpf)
{
int a;
T val;
Share<T> S;
inpf >> a;
M.resize_s(a);
inpf >> a;
M.resize_c(a);
cerr << "Reading Clear Memory" << endl;
// Read clear memory
inpf >> a;
val.input(inpf,true);
while (a!=-1)
{ M.write_C(a,val);
inpf >> a;
val.input(inpf,true);
}
cerr << "Reading Shared Memory" << endl;
// Read shared memory
inpf >> a;
S.input(inpf,true);
while (a!=-1)
{ M.write_S(a,S);
inpf >> a;
S.input(inpf,true);
}
}
template class Memory<gfp>;
template class Memory<gf2n>;
template class Memory<Integer>;
template istream& operator>>(istream& s,Memory<gfp>& M);
template istream& operator>>(istream& s,Memory<gf2n>& M);
template istream& operator>>(istream& s,Memory<Integer>& M);
template ostream& operator<<(ostream& s,const Memory<gfp>& M);
template ostream& operator<<(ostream& s,const Memory<gf2n>& M);
template ostream& operator<<(ostream& s,const Memory<Integer>& M);
template void Load_Memory(Memory<gfp>& M,ifstream& inpf);
template void Load_Memory(Memory<gf2n>& M,ifstream& inpf);
template void Load_Memory(Memory<Integer>& M,ifstream& inpf);
#ifdef USE_GF2N_LONG
template class Memory<gf2n_short>;
template istream& operator>>(istream& s,Memory<gf2n_short>& M);
template ostream& operator<<(ostream& s,const Memory<gf2n_short>& M);
template void Load_Memory(Memory<gf2n_short>& M,ifstream& inpf);
#endif