def result_conv(x, y): if util.is_constant(x): if util.is_constant(y): return lambda x: x else: return type(y).conv if util.is_constant(y): return type(x).conv if type(x) is type(y): return type(x).conv return lambda x: x
def result_conv(x, y): try: if util.is_constant(x): if util.is_constant(y): return lambda x: x else: return type(y).conv if util.is_constant(y): return type(x).conv if type(x) is type(y): return type(x).conv except AttributeError: pass return lambda x: x
def __init__(self, elements=None, length=None, input_length=None): if length: assert isinstance(elements, sint) if Program.prog.use_split(): x = elements.split_to_two_summands(length) v = sbitint.carry_lookahead_adder(x[0], x[1], fewer_inv=True) else: prog = Program.prog if not prog.options.ring: # force the use of edaBits backup = prog.use_edabit() prog.use_edabit(True) from Compiler.floatingpoint import BitDecFieldRaw self.v = BitDecFieldRaw(elements, input_length or prog.bit_length, length, prog.security) prog.use_edabit(backup) return l = int(Program.prog.options.ring) r, r_bits = sint.get_edabit(length, size=elements.size) c = ((elements - r) << (l - length)).reveal() c >>= l - length cb = [(c >> i) for i in range(length)] x = sbitintvec.from_vec(r_bits) + sbitintvec.from_vec(cb) v = x.v self.v = v[:length] elif elements is not None and not (util.is_constant(elements) and \ elements == 0): self.v = sbits.trans(elements)
def load_mem(cls, address, mem_type=None): res = cls() if mem_type == 'sd': return cls.load_dynamic_mem(address) else: cls.load_inst[util.is_constant(address)](res, address) return res
def print_ln_if(cond, s): if util.is_constant(cond): if cond: print_ln(s) else: s += '\n' while s: cond.print_if(s[:4]) s = s[4:]
def store_in_mem(self, address): for x in self.v: assert util.is_constant(x) or x.n == 1 v = [sbit.conv(x) for x in self.v] if not isinstance(address, int) and len(address) == n: for x, y in zip(v, address): x.store_in_mem(y) else: for i in range(n): v[i].store_in_mem(address + i)
def load_mem(cls, address, mem_type=None, size=None): if size not in (None, 1): v = [cls.load_mem(address + i) for i in range(size)] return cls.vec(v) res = cls() if mem_type == 'sd': return cls.load_dynamic_mem(address) else: cls.load_inst[util.is_constant(address)](res, address) return res
def __init__(self, other=None, size=None): assert size in (None, 1) if other is not None: if util.is_constant(other): self.v = [sbit((other >> i) & 1) for i in range(n)] elif isinstance(other, _vec): self.v = self.bit_extend(other.v, n) elif isinstance(other, (list, tuple)): self.v = self.bit_extend(sbitvec(other).v, n) else: self.v = sbits.get_type(n)(other).bit_decompose() assert len(self.v) == n
def __add__(self, other): if isinstance(other, cbits): return NotImplemented else: if util.is_constant(other): if other >= 2**31 or other < -2**31: raise NotImplementedError('Immediate out of bound') res = cbits(n=max(self.n, len(bin(other)) - 2)) inst.xorci(res, self, other) return res else: return NotImplemented
def result_conv(x, y): try: def f(res): try: return t.conv(res) except: return res if util.is_constant(x): if util.is_constant(y): return lambda x: x else: t = type(y) return f if util.is_constant(y): t = type(x) return f if type(x) is type(y): t = type(x) return f except AttributeError: pass return lambda x: x
def wrapper(*args, **kwargs): if isinstance(args[0], sfix): for arg in args[1:]: assert util.is_constant(arg) assert not kwargs assert args[0].size == args[0].v.size k = args[0].k f = args[0].f res = sfix._new(sint(size=args[0].size), k=k, f=f) instruction(res.v, args[0].v, k, f, *args[1:]) return res else: return function(*args, **kwargs)
def mul_int(self, other): assert(util.is_constant(other)) if other == 0: return 0 elif other == 1: return self elif self.n == 1: bits = util.bit_decompose(other, util.int_len(other)) zero = sbit(0) mul_bits = [self if b else zero for b in bits] return self.bit_compose(mul_bits) else: print(self.n, other) return NotImplemented
def clear_op(self, other, c_inst, ci_inst, op): if isinstance(other, cbits): res = cbits(n=max(self.n, other.n)) c_inst(res, self, other) return res else: if util.is_constant(other): if other >= 2**31 or other < -2**31: return op(self, cbits(other)) res = cbits(n=max(self.n, len(bin(other)) - 2)) ci_inst(res, self, other) return res else: return op(self, cbits(other))
def clear_op(self, other, c_inst, ci_inst, op): if isinstance(other, cbits): res = cbits.get_type(max(self.n, other.n))() c_inst(res, self, other) return res elif isinstance(other, sbits): return NotImplemented else: if util.is_constant(other): if other >= 2**31 or other < -2**31: return op(self, cbits.new(other)) res = cbits.get_type(max(self.n, len(bin(other)) - 2))() ci_inst(res, self, other) return res else: return op(self, cbits(other))
def bit_compose(cls, bits): if len(bits) == 1: return bits[0] bits = list(bits) for i in range(len(bits)): if util.is_constant(bits[i]): bits[i] = sbit(bits[i]) res = cls.new(n=len(bits)) if len(bits) <= cls.unit: cls.bitcom(res, *(sbit.conv(bit) for bit in bits)) else: n_bak = bits[0].n bits[0].n = 1 res = cls.trans(bits)[0] bits[0].n = n_bak res.decomposed = bits return res
def __init__(self, elements=None, length=None): if length: assert isinstance(elements, sint) if Program.prog.use_split(): x = elements.split_to_two_summands(length) v = sbitint.carry_lookahead_adder(x[0], x[1], fewer_inv=True) else: assert Program.prog.options.ring l = int(Program.prog.options.ring) r, r_bits = sint.get_edabit(length, size=elements.size) c = ((elements - r) << (l - length)).reveal() c >>= l - length cb = [(c >> i) for i in range(length)] x = sbitintvec.from_vec(r_bits) + sbitintvec.from_vec(cb) v = x.v self.v = v[:length] elif elements is not None and not (util.is_constant(elements) and \ elements == 0): self.v = sbits.trans(elements)
def __init__(self, other=None): if other is not None: if util.is_constant(other): self.v = [sbit((other >> i) & 1) for i in range(n)] else: self.v = sbits(other, n=n).bit_decompose(n)
def size(self): if not self.v or util.is_constant(self.v[0]): return 1 else: return self.v[0].n
def coerce(self, other): if util.is_constant(other): return self.from_vec(util.bit_decompose(other, n)) else: return super(sbitvecn, self).coerce(other)
def new(cls, value=None, n=None): if util.is_constant(value): n = value.bit_length() return cls.get_type(n)(value)
def _check_input_player(player): if not util.is_constant(player): raise CompilerError('player must be known at compile time ' 'for binary circuit inputs')