Local right shift for GF(2^n).

This commit is contained in:
Marcel Keller
2022-06-23 14:42:54 +02:00
parent 4c8e616b58
commit ec1d302b03
8 changed files with 51 additions and 25 deletions

View File

@@ -1051,6 +1051,7 @@ class shrci(base.ClearShiftInstruction):
code = base.opcodes['SHRCI']
op = '__rshift__'
@base.gf2n
@base.vectorize
class shrsi(base.ClearShiftInstruction):
""" Bitwise right shift of secret register (vector) by (constant)

View File

@@ -207,8 +207,8 @@ opcodes = dict(
CONDPRINTPLAIN = 0xE1,
INTOUTPUT = 0xE6,
FLOATOUTPUT = 0xE7,
GBITDEC = 0x184,
GBITCOM = 0x185,
GBITDEC = 0x18A,
GBITCOM = 0x18B,
# Secure socket
INITSECURESOCKET = 0x1BA,
RESPSECURESOCKET = 0x1BB

View File

@@ -2126,6 +2126,21 @@ class _secret(_register, _secret_structure):
res = personal(player, masked.reveal() - mask[1])
return res
@set_instruction_type
@vectorize
def raw_right_shift(self, length):
""" Local right shift in supported protocols.
In integer-like protocols, the output is potentially off by one.
:param length: number of bits
"""
res = type(self)()
shrsi(res, self, length)
return res
def raw_mod2m(self, m):
return self - (self.raw_right_shift(m) << m)
class sint(_secret, _int):
"""
@@ -2668,15 +2683,6 @@ class sint(_secret, _int):
columns = self.split_to_n_summands(length, n)
return _bitint.wallace_tree_without_finish(columns, get_carry)
@vectorize
def raw_right_shift(self, length):
res = sint()
shrsi(res, self, length)
return res
def raw_mod2m(self, m):
return self - (self.raw_right_shift(m) << m)
@vectorize
def reveal_to(self, player):
""" Reveal secret value to :py:obj:`player`.

View File

@@ -284,8 +284,9 @@ enum
// Bitwise shifts
GSHLCI = 0x182,
GSHRCI = 0x183,
GBITDEC = 0x184,
GBITCOM = 0x185,
GSHRSI = 0x184,
GBITDEC = 0x18A,
GBITCOM = 0x18B,
// Conversion
GCONVINT = 0x1C0,
GCONVGF2N = 0x1C1,

View File

@@ -198,6 +198,7 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
case GORCI:
case GSHLCI:
case GSHRCI:
case GSHRSI:
case USE:
case USE_INP:
case USE_EDABIT:
@@ -1006,6 +1007,9 @@ inline void Instruction::execute(Processor<sint, sgf2n>& Proc) const
case SHRSI:
sint::shrsi(Procp, *this);
return;
case GSHRSI:
sgf2n::shrsi(Proc2, *this);
return;
case OPEN:
Proc.Procp.POpen(start, Proc.P, size);
return;

View File

@@ -71,7 +71,7 @@ public:
template<class U>
static void shrsi(SubProcessor<U>& proc, const Instruction& inst)
{
shrsi(proc, inst, T::invertible);
shrsi(proc, inst, T::prime_field);
}
template<class U>

View File

@@ -85,17 +85,6 @@ public:
}
}
}
template<class T>
static void shrsi(SubProcessor<T>& proc, const Instruction& inst)
{
for (int i = 0; i < inst.get_size(); i++)
{
auto& dest = proc.get_S_ref(inst.get_r(0) + i);
auto& source = proc.get_S_ref(inst.get_r(1) + i);
dest = source >> inst.get_n();
}
}
};
#endif /* PROTOCOLS_SEMI2KSHARE_H_ */

View File

@@ -130,6 +130,31 @@ public:
{
super::unpack(os, n_bits);
}
template<class U>
static void shrsi(SubProcessor<U>& proc, const Instruction& inst)
{
shrsi(proc, inst, T::prime_field);
}
template<class U>
static void shrsi(SubProcessor<U>&, const Instruction&,
true_type)
{
throw runtime_error("shrsi not implemented");
}
template<class U>
static void shrsi(SubProcessor<U>& proc, const Instruction& inst,
false_type)
{
for (int i = 0; i < inst.get_size(); i++)
{
auto& dest = proc.get_S_ref(inst.get_r(0) + i);
auto& source = proc.get_S_ref(inst.get_r(1) + i);
dest = source >> inst.get_n();
}
}
};
#endif /* PROTOCOLS_SEMISHARE_H_ */