Skip to content

Commit

Permalink
Input by run-time player id.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkskeller committed Feb 11, 2020
1 parent 5130145 commit b17d4b6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
27 changes: 18 additions & 9 deletions Compiler/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,10 +918,8 @@ def add_usage(self, req_node):
req_node.increment((self.field_type, 'input', player), \
4 * self.get_size())

@base.vectorize
class inputmixed(base.TextInputInstruction):
class inputmixed_base(base.TextInputInstruction):
__slots__ = []
code = base.opcodes['INPUTMIXED']
field_type = 'modp'
# the following has to match TYPE: (N_DEST, N_PARAM)
types = {
Expand All @@ -936,11 +934,8 @@ class inputmixed(base.TextInputInstruction):
}

def __init__(self, name, *args):
try:
type_id = self.type_ids[name]
except:
pass
super(inputmixed_class, self).__init__(type_id, *args)
type_id = self.type_ids[name]
super(inputmixed_base, self).__init__(type_id, *args)

@property
def arg_format(self):
Expand All @@ -951,14 +946,19 @@ def arg_format(self):
yield 'sw'
for j in range(self.types[t][1]):
yield 'int'
yield 'p'
yield self.player_arg_type

def bases(self):
i = 0
while i < len(self.args):
yield i
i += sum(self.types[self.args[i]]) + 2

@base.vectorize
class inputmixed(inputmixed_base):
code = base.opcodes['INPUTMIXED']
player_arg_type = 'p'

def add_usage(self, req_node):
for i in self.bases():
t = self.args[i]
Expand All @@ -967,6 +967,15 @@ def add_usage(self, req_node):
req_node.increment((self.field_type, 'input', player), \
n_dest * self.get_size())

@base.vectorize
class inputmixedreg(inputmixed_base):
code = base.opcodes['INPUTMIXEDREG']
player_arg_type = 'ci'

def add_usage(self, req_node):
# player 0 as proxy
req_node.increment((self.field_type, 'input', 0), float('inf'))

@base.gf2n
class startinput(base.RawInputInstruction):
r""" Receive inputs from player $p$. """
Expand Down
1 change: 1 addition & 0 deletions Compiler/instructions_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
INPUTFIX = 0xF0,
INPUTFLOAT = 0xF1,
INPUTMIXED = 0xF2,
INPUTMIXEDREG = 0xF3,
STARTINPUT = 0x61,
STOPINPUT = 0x62,
READSOCKETC = 0x63,
Expand Down
7 changes: 7 additions & 0 deletions Compiler/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from . import comparison, floatingpoint
import math
from . import util
from . import instructions
from .util import is_zero, is_one
import operator
from functools import reduce
Expand Down Expand Up @@ -208,6 +209,12 @@ def read_mem_operation(self, *args, **kwargs):
copy_doc(read_mem_operation, operation)
return read_mem_operation

def inputmixed(*args):
# helper to cover both cases
if isinstance(args[-1], int):
instructions.inputmixed(*args)
else:
instructions.inputmixedreg(*args)

class _number(object):
""" Number functionality. """
Expand Down
4 changes: 3 additions & 1 deletion Processor/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class InputBase
template<class U>
static void input(SubProcessor<T>& Proc, const vector<int>& args, int size);

static void input_mixed(SubProcessor<T>& Proc, const vector<int>& args, int size);
static int get_player(SubProcessor<T>& Proc, int arg, bool player_from_arg);
static void input_mixed(SubProcessor<T>& Proc, const vector<int>& args,
int size, bool player_from_reg);
template<class U>
static void prepare(SubProcessor<T>& Proc, int player, const int* params, int size);
template<class U>
Expand Down
23 changes: 20 additions & 3 deletions Processor/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,24 @@ void InputBase<T>::input(SubProcessor<T>& Proc,
}
}

template<class T>
int InputBase<T>::get_player(SubProcessor<T>& Proc, int arg, bool player_from_reg)
{
if (player_from_reg)
{
assert(Proc.Proc);
auto res = Proc.Proc->read_Ci(arg);
if (res >= Proc.P.num_players())
throw runtime_error("player id too large: " + to_string(res));
return res;
}
else
return arg;
}

template<class T>
void InputBase<T>::input_mixed(SubProcessor<T>& Proc, const vector<int>& args,
int size)
int size, bool player_from_reg)
{
auto& input = Proc.input;
input.reset_all(Proc.P);
Expand All @@ -293,7 +308,7 @@ void InputBase<T>::input_mixed(SubProcessor<T>& Proc, const vector<int>& args,
#define X(U) \
case U::TYPE: \
n_arg_tuple = U::N_DEST + U::N_PARAM + 2; \
player = args[i + n_arg_tuple - 1]; \
player = get_player(Proc, args[i + n_arg_tuple - 1], player_from_reg); \
if (type != last_type and Proc.Proc and Proc.Proc->use_stdin()) \
cout << "Please input " << U::NAME << "s:" << endl; \
prepare<U>(Proc, player, &args[i + U::N_DEST + 1], size); \
Expand All @@ -313,12 +328,14 @@ void InputBase<T>::input_mixed(SubProcessor<T>& Proc, const vector<int>& args,
{
int n_arg_tuple;
int type = args[i];
int player;
switch (type)
{
#define X(U) \
case U::TYPE: \
n_arg_tuple = U::N_DEST + U::N_PARAM + 2; \
finalize<U>(Proc, args[i + n_arg_tuple - 1], &args[i + 1], size); \
player = get_player(Proc, args[i + n_arg_tuple - 1], player_from_reg); \
finalize<U>(Proc, player, &args[i + 1], size); \
break;
X(IntInput<typename T::clear>) X(FixInput) X(FloatInput)
#undef X
Expand Down
1 change: 1 addition & 0 deletions Processor/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ enum
INPUTFIX = 0xF0,
INPUTFLOAT = 0xF1,
INPUTMIXED = 0xF2,
INPUTMIXEDREG = 0xF3,
STARTINPUT = 0x61,
STOPINPUT = 0x62,
READSOCKETC = 0x63,
Expand Down
6 changes: 5 additions & 1 deletion Processor/Instruction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
case INPUTFIX:
case INPUTFLOAT:
case INPUTMIXED:
case INPUTMIXEDREG:
case TRUNC_PR:
num_var_args = get_int(s);
get_vector(num_var_args, start, s);
Expand Down Expand Up @@ -1145,7 +1146,10 @@ inline void Instruction::execute(Processor<sint, sgf2n>& Proc) const
sint::Input::template input<FloatInput>(Proc.Procp, start, size);
return;
case INPUTMIXED:
sint::Input::input_mixed(Proc.Procp, start, size);
sint::Input::input_mixed(Proc.Procp, start, size, false);
return;
case INPUTMIXEDREG:
sint::Input::input_mixed(Proc.Procp, start, size, true);
return;
case STARTINPUT:
Proc.Procp.input.start(r[0],n);
Expand Down
19 changes: 10 additions & 9 deletions Processor/Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class SubProcessor

class ArithmeticProcessor : public ProcessorBase
{
protected:
vector<long> Ci;

public:
int thread_num;

Expand All @@ -103,13 +106,18 @@ class ArithmeticProcessor : public ProcessorBase
{
return thread_num == 0 and opts.interactive;
}

const long& read_Ci(int i) const
{ return Ci[i]; }
long& get_Ci_ref(int i)
{ return Ci[i]; }
void write_Ci(int i,const long& x)
{ Ci[i]=x; }
};

template<class sint, class sgf2n>
class Processor : public ArithmeticProcessor
{
vector<long> Ci;

int reg_max2,reg_maxp,reg_maxi;

// Data structure used for reading/writing data to/from a socket (i.e. an external party to SPDZ)
Expand Down Expand Up @@ -184,13 +192,6 @@ class Processor : public ArithmeticProcessor
void write_Sp(int i,const sint & x)
{ Procp.S[i]=x; }

const long& read_Ci(int i) const
{ return Ci[i]; }
long& get_Ci_ref(int i)
{ return Ci[i]; }
void write_Ci(int i,const long& x)
{ Ci[i]=x; }

void dabit(const Instruction& instruction);

// Access to external client sockets for reading clear/shared data
Expand Down

0 comments on commit b17d4b6

Please sign in to comment.