diff --git a/Compiler/instructions.py b/Compiler/instructions.py index 5f5b82db..aac0c34c 100644 --- a/Compiler/instructions.py +++ b/Compiler/instructions.py @@ -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) diff --git a/Compiler/instructions_base.py b/Compiler/instructions_base.py index d598d8a7..3a56e604 100644 --- a/Compiler/instructions_base.py +++ b/Compiler/instructions_base.py @@ -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 diff --git a/Compiler/types.py b/Compiler/types.py index 735fddea..93991df1 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -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`. diff --git a/Processor/Instruction.h b/Processor/Instruction.h index fd91e35d..f3caf565 100644 --- a/Processor/Instruction.h +++ b/Processor/Instruction.h @@ -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, diff --git a/Processor/Instruction.hpp b/Processor/Instruction.hpp index a0f7a490..5b0589b6 100644 --- a/Processor/Instruction.hpp +++ b/Processor/Instruction.hpp @@ -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& 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; diff --git a/Protocols/Rep3Share.h b/Protocols/Rep3Share.h index 78627697..fb02d26f 100644 --- a/Protocols/Rep3Share.h +++ b/Protocols/Rep3Share.h @@ -71,7 +71,7 @@ public: template static void shrsi(SubProcessor& proc, const Instruction& inst) { - shrsi(proc, inst, T::invertible); + shrsi(proc, inst, T::prime_field); } template diff --git a/Protocols/Semi2kShare.h b/Protocols/Semi2kShare.h index cc41d023..3d98cf1b 100644 --- a/Protocols/Semi2kShare.h +++ b/Protocols/Semi2kShare.h @@ -85,17 +85,6 @@ public: } } } - - template - static void shrsi(SubProcessor& 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_ */ diff --git a/Protocols/SemiShare.h b/Protocols/SemiShare.h index 432b599b..8d9b1146 100644 --- a/Protocols/SemiShare.h +++ b/Protocols/SemiShare.h @@ -130,6 +130,31 @@ public: { super::unpack(os, n_bits); } + + template + static void shrsi(SubProcessor& proc, const Instruction& inst) + { + shrsi(proc, inst, T::prime_field); + } + + template + static void shrsi(SubProcessor&, const Instruction&, + true_type) + { + throw runtime_error("shrsi not implemented"); + } + + template + static void shrsi(SubProcessor& 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_ */