Skip to content

Commit d25ffeb

Browse files
committed
File persistance for GF(2^n).
1 parent 6449d00 commit d25ffeb

File tree

11 files changed

+115
-84
lines changed

11 files changed

+115
-84
lines changed

Compiler/instructions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,7 @@ class closeclientconnection(base.IOInstruction):
19891989
code = base.opcodes['CLOSECLIENTCONNECTION']
19901990
arg_format = ['ci']
19911991

1992+
@base.gf2n
19921993
class writesharestofile(base.VectorInstruction, base.IOInstruction):
19931994
""" Write shares to ``Persistence/Transactions-P<playerno>.data``
19941995
(appending at the end).
@@ -2001,12 +2002,13 @@ class writesharestofile(base.VectorInstruction, base.IOInstruction):
20012002
"""
20022003
__slots__ = []
20032004
code = base.opcodes['WRITEFILESHARE']
2004-
arg_format = tools.chain(['ci'], itertools.repeat('s'))
2005+
arg_format = tools.chain(['ci'], tools.cycle(['s']))
20052006
vector_index = 1
20062007

20072008
def has_var_args(self):
20082009
return True
20092010

2011+
@base.gf2n
20102012
class readsharesfromfile(base.VectorInstruction, base.IOInstruction):
20112013
""" Read shares from ``Persistence/Transactions-P<playerno>.data``.
20122014
@@ -2018,7 +2020,7 @@ class readsharesfromfile(base.VectorInstruction, base.IOInstruction):
20182020
"""
20192021
__slots__ = []
20202022
code = base.opcodes['READFILESHARE']
2021-
arg_format = tools.chain(['ci', 'ciw'], itertools.repeat('sw'))
2023+
arg_format = tools.chain(['ci', 'ciw'], tools.cycle(['sw']))
20222024
vector_index = 2
20232025

20242026
def has_var_args(self):

Compiler/instructions_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def reformat(arg_format):
369369
if isinstance(arg_format, list):
370370
__format = []
371371
for __f in arg_format:
372-
if __f in ('int', 'long', 'p', 'ci', 'str'):
372+
if __f in ('int', 'long', 'p', 'ci', 'ciw', 'str'):
373373
__format.append(__f)
374374
else:
375375
__format.append(__f[0] + 'g' + __f[1:])

Compiler/types.py

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,47 @@ def raw_mod2m(self, m):
24302430
def output(self):
24312431
print_reg_plains(self)
24322432

2433+
@classmethod
2434+
@set_instruction_type
2435+
def read_from_file(cls, start, n_items=1, crash_if_missing=True, size=1):
2436+
""" Read shares from
2437+
``Persistence/Transactions-[gf2n-]P<playerno>.data``. See :ref:`this
2438+
section <persistence>` for details on the data format.
2439+
2440+
:param start: starting position in number of shares from beginning (int/regint/cint)
2441+
:param n_items: number of items (int)
2442+
:param crash_if_missing: crash if file not found (default)
2443+
:param size: vector size (int)
2444+
:returns: destination for final position, -1 for eof reached, or -2 for file not found (regint)
2445+
:returns: list of shares
2446+
"""
2447+
shares = [cls(size=size) for i in range(n_items)]
2448+
stop = regint()
2449+
readsharesfromfile(regint.conv(start), stop, *shares)
2450+
if crash_if_missing:
2451+
library.runtime_error_if(stop == -2, 'Persistence not found')
2452+
return stop, shares
2453+
2454+
@classmethod
2455+
@set_instruction_type
2456+
def write_to_file(cls, shares, position=None):
2457+
""" Write shares to ``Persistence/Transactions-[gf2n-]P<playerno>.data``
2458+
(appending at the end). See :ref:`this section <persistence>`
2459+
for details on the data format.
2460+
2461+
:param shares: (list or iterable of shares)
2462+
:param position: start position (int/regint/cint),
2463+
defaults to end of file
2464+
"""
2465+
if isinstance(shares, cls):
2466+
shares = [shares]
2467+
for share in shares:
2468+
assert isinstance(share, cls)
2469+
assert share.size == shares[0].size
2470+
if position is None:
2471+
position = -1
2472+
writesharestofile(regint.conv(position), *shares)
2473+
24332474
class sint(_secret, _int):
24342475
"""
24352476
Secret integer in the protocol-specific domain. It supports
@@ -2699,45 +2740,6 @@ def write_shares_to_socket(cls, client_id, values,
26992740
"""
27002741
writesocketshare(client_id, message_type, values[0].size, *values)
27012742

2702-
@classmethod
2703-
def read_from_file(cls, start, n_items=1, crash_if_missing=True, size=1):
2704-
""" Read shares from
2705-
``Persistence/Transactions-P<playerno>.data``. See :ref:`this
2706-
section <persistence>` for details on the data format.
2707-
2708-
:param start: starting position in number of shares from beginning (int/regint/cint)
2709-
:param n_items: number of items (int)
2710-
:param crash_if_missing: crash if file not found (default)
2711-
:param size: vector size (int)
2712-
:returns: destination for final position, -1 for eof reached, or -2 for file not found (regint)
2713-
:returns: list of shares
2714-
"""
2715-
shares = [cls(size=size) for i in range(n_items)]
2716-
stop = regint()
2717-
readsharesfromfile(regint.conv(start), stop, *shares)
2718-
if crash_if_missing:
2719-
library.runtime_error_if(stop == -2, 'Persistence not found')
2720-
return stop, shares
2721-
2722-
@staticmethod
2723-
def write_to_file(shares, position=None):
2724-
""" Write shares to ``Persistence/Transactions-P<playerno>.data``
2725-
(appending at the end). See :ref:`this section <persistence>`
2726-
for details on the data format.
2727-
2728-
:param shares: (list or iterable of sint)
2729-
:param position: start position (int/regint/cint),
2730-
defaults to end of file
2731-
"""
2732-
if isinstance(shares, sint):
2733-
shares = [shares]
2734-
for share in shares:
2735-
assert isinstance(share, sint)
2736-
assert share.size == shares[0].size
2737-
if position is None:
2738-
position = -1
2739-
writesharestofile(regint.conv(position), *shares)
2740-
27412743
@vectorized_classmethod
27422744
def load_mem(cls, address, mem_type=None):
27432745
""" Load from memory by public address. """

Processor/Binary_File_IO.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ using namespace std;
1616
* Intended for MPC application specific file IO.
1717
*/
1818

19+
template<class T>
1920
class Binary_File_IO
2021
{
2122
public:
2223

2324
static string filename(int my_number);
25+
static void reset(int my_number);
2426

2527
/*
2628
* Append the buffer values as binary to the filename.
2729
* Throws file_error.
2830
*/
29-
template <class T>
3031
void write_to_file(const string filename, const vector<T>& buffer,
3132
long start_pos);
3233

@@ -36,7 +37,6 @@ class Binary_File_IO
3637
* Returns the current posn in the file or -1 if at eof.
3738
* Throws file_error.
3839
*/
39-
template <class T>
4040
void read_from_file(const string filename, vector<T>& buffer,
4141
const long start_posn, long& end_posn);
4242
};

Processor/Binary_File_IO.hpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@
66
* Intended for application specific file IO.
77
*/
88

9-
inline string Binary_File_IO::filename(int my_number)
9+
template<class T>
10+
inline string Binary_File_IO<T>::filename(int my_number)
1011
{
1112
string dir = "Persistence";
1213
mkdir_p(dir.c_str());
13-
return dir + "/Transactions-P" + to_string(my_number) + ".data";
14+
string res = dir + "/Transactions";
15+
if (T::clear::characteristic_two)
16+
res += "-gf2n";
17+
return res + "-P" + to_string(my_number) + ".data";
1418
}
1519

16-
template<class T>
20+
template<class T>
21+
void Binary_File_IO<T>::reset(int my_number)
22+
{
23+
string filename = Binary_File_IO<T>::filename(my_number);
24+
ifstream pers(filename);
25+
try
26+
{
27+
check_file_signature<T>(pers, filename);
28+
}
29+
catch (signature_mismatch&)
30+
{
31+
ofstream pers(filename, ios::binary);
32+
file_signature<T>().output(pers);
33+
}
34+
}
1735

18-
void Binary_File_IO::write_to_file(const string filename,
36+
template<class T>
37+
void Binary_File_IO<T>::write_to_file(const string filename,
1938
const vector<T>& buffer, long start_pos)
2039
{
2140
ofstream outf;
@@ -44,7 +63,7 @@ void Binary_File_IO::write_to_file(const string filename,
4463
}
4564

4665
template<class T>
47-
void Binary_File_IO::read_from_file(const string filename, vector<T>& buffer,
66+
void Binary_File_IO<T>::read_from_file(const string filename, vector<T>& buffer,
4867
const long start_posn, long& end_posn)
4968
{
5069
ifstream inf;

Processor/Instruction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ enum
311311
GRAWOUTPUT = 0x1B7,
312312
GSTARTPRIVATEOUTPUT = 0x1B8,
313313
GSTOPPRIVATEOUTPUT = 0x1B9,
314+
GWRITEFILESHARE = 0x1BD,
315+
GREADFILESHARE = 0x1BE,
314316
// Commsec ops
315317
INITSECURESOCKET = 0x1BA,
316318
RESPSECURESOCKET = 0x1BB

Processor/Instruction.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
328328
// read from file, input is opcode num_args,
329329
// start_file_posn (read), end_file_posn(write) var1, var2, ...
330330
case READFILESHARE:
331+
case GREADFILESHARE:
331332
case CALL_TAPE:
332333
num_var_args = get_int(s) - 2;
333334
r[0] = get_int(s);
@@ -397,6 +398,7 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
397398
case EDABIT:
398399
case SEDABIT:
399400
case WRITEFILESHARE:
401+
case GWRITEFILESHARE:
400402
case CONCATS:
401403
num_var_args = get_int(s) - 1;
402404
r[0] = get_int(s);
@@ -1371,11 +1373,21 @@ inline void Instruction::execute(Processor<sint, sgf2n>& Proc) const
13711373
break;
13721374
case WRITEFILESHARE:
13731375
// Write shares to file system
1374-
Proc.write_shares_to_file(Proc.read_Ci(r[0]), start, size);
1376+
Procp.write_shares_to_file(Proc.read_Ci(r[0]), start, size);
13751377
return;
13761378
case READFILESHARE:
13771379
// Read shares from file system
1378-
Proc.read_shares_from_file(Proc.read_Ci(r[0]), r[1], start, size);
1380+
Procp.read_shares_from_file(Proc.read_Ci(r[0]), r[1], start, size,
1381+
Proc);
1382+
return;
1383+
case GWRITEFILESHARE:
1384+
// Write shares to file system
1385+
Proc2.write_shares_to_file(Proc.read_Ci(r[0]), start, size);
1386+
return;
1387+
case GREADFILESHARE:
1388+
// Read shares from file system
1389+
Proc2.read_shares_from_file(Proc.read_Ci(r[0]), r[1], start, size,
1390+
Proc);
13791391
return;
13801392
case PUBINPUT:
13811393
Proc.get_Cp_ref(r[0]) = Proc.template

Processor/Machine.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,8 @@ void Machine<sint, sgf2n>::prepare(const string& progname_str)
165165
{
166166
if (prog.writes_persistence)
167167
{
168-
string filename = Binary_File_IO::filename(my_number);
169-
ifstream pers(filename);
170-
try
171-
{
172-
check_file_signature<sint>(pers, filename);
173-
}
174-
catch (signature_mismatch&)
175-
{
176-
ofstream pers(filename, ios::binary);
177-
file_signature<sint>().output(pers);
178-
}
168+
Binary_File_IO<sint>::reset(my_number);
169+
Binary_File_IO<sgf2n>::reset(my_number);
179170
break;
180171
}
181172
}

Processor/Processor.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class SubProcessor
3838
DataPositions bit_usage;
3939
NamedStats stats;
4040

41+
Binary_File_IO<T> binary_file_io;
42+
4143
void resize(size_t size) { C.resize(size); S.resize(size); }
4244

4345
void matmulsm_prep(int ii, int j, const MemoryPart<T>& source,
@@ -128,6 +130,13 @@ class SubProcessor
128130
void push_stack();
129131
void push_args(const vector<int>& args);
130132
void pop_stack(const vector<int>& results);
133+
134+
// Read and write secret numeric data to file (name hardcoded at present)
135+
template<class U>
136+
void read_shares_from_file(long start_file_pos, int end_file_pos_register,
137+
const vector<int>& data_registers, size_t vector_size, U& Proc);
138+
void write_shares_to_file(long start_pos, const vector<int>& data_registers,
139+
size_t vector_size);
131140
};
132141

133142
class ArithmeticProcessor : public ProcessorBase
@@ -228,7 +237,6 @@ class Processor : public ArithmeticProcessor
228237
TempVars<sint, sgf2n> temp;
229238

230239
ExternalClients& external_clients;
231-
Binary_File_IO binary_file_io;
232240

233241
CommStats client_stats;
234242
Timer& client_timer;
@@ -289,12 +297,6 @@ class Processor : public ArithmeticProcessor
289297
void read_socket_private(int client_id, const vector<int>& registers,
290298
int size, bool send_macs);
291299

292-
// Read and write secret numeric data to file (name hardcoded at present)
293-
void read_shares_from_file(long start_file_pos, int end_file_pos_register,
294-
const vector<int>& data_registers, size_t vector_size);
295-
void write_shares_to_file(long start_pos, const vector<int>& data_registers,
296-
size_t vector_size);
297-
298300
cint get_inverse2(unsigned m);
299301

300302
void fixinput(const Instruction& instruction);

0 commit comments

Comments
 (0)