def cond_swap(x, y): b = x < y if isinstance(x, sfloat): res = ([], []) for i, j in enumerate(('v', 'p', 'z', 's', 'err')): xx = x.__getattribute__(j) yy = y.__getattribute__(j) bx = b * xx by = b * yy res[0].append(bx + yy - by) res[1].append(xx - bx + by) return sfloat(*res[0]), sfloat(*res[1]) c = b * (x - y) return y + c, x - c
def cond_swap(x,y): b = x < y if isinstance(x, sfloat): res = ([], []) for i,j in enumerate(('v','p','z','s')): xx = x.__getattribute__(j) yy = y.__getattribute__(j) bx = b * xx by = b * yy res[0].append(bx + yy - by) res[1].append(xx - bx + by) return sfloat(*res[0]), sfloat(*res[1]) bx = b * x by = b * y return bx + y - by, x - bx + by
def load_float_to_secret(value, sec=40): def _bit_length(x): return len(bin(x).lstrip('-0b')) num,den = value.as_integer_ratio() exp = int(round(math.log(den, 2))) nbits = _bit_length(num) if nbits > sfloat.vlen: num >>= (nbits - sfloat.vlen) exp -= (nbits - sfloat.vlen) elif nbits < sfloat.vlen: num <<= (sfloat.vlen - nbits) exp += (sfloat.vlen - nbits) if _bit_length(exp) > sfloat.plen: raise CompilerException('Cannot load floating point to secret: overflow') if num < 0: s = load_int_to_secret(1) z = load_int_to_secret(0) else: s = load_int_to_secret(0) if num == 0: z = load_int_to_secret(1) else: z = load_int_to_secret(0) v = load_int_to_secret(num) p = load_int_to_secret(exp) err = load_int_to_secret(err) return sfloat(v, p, s, z, err)
def log2_fx(x): """ Returns the result of :math:`\log_2(x)` for any unbounded number. This is achieved by changing :py:obj:`x` into :math:`f \cdot 2^n` where f is bounded by :math:`[0.5, 1]`. Then the polynomials are used to calculate :math:`\log_2(f)`, which is then just added to :math:`n`. :param x: input for :math:`\log_2` (sfix, sint). :return: (sfix) the value of :math:`\log_2(x)` """ if isinstance(x, types._fix): # transforms sfix to f*2^n, where f is [o.5,1] bounded # obtain number bounded by [0,5 and 1] by transforming input to sfloat v, p, z, s = floatingpoint.Int2FL(x.v, x.k, x.f, x.kappa) p -= x.f vlen = x.f v = x._new(v, k=x.k, f=x.f) else: d = types.sfloat(x) v, p, vlen = d.v, d.p, d.vlen w = x.coerce(1.0 / (2 ** (vlen))) v *= w # isolates mantisa of d, now the n can be also substituted by the # secret shared p from d in the expresion above. # polynomials for the log_2 evaluation of f are calculated P = p_eval(p_2524, v) Q = p_eval(q_2524, v) # the log is returned by adding the result of the division plus p. a = P / Q + (vlen + p) return a # *(1-(f.z))*(1-f.s)*(1-f.error)
def trunc(x): if type(x) is types.sfix: return AdvInteger.Oblivious_Trunc(x.v, x.k, x.f, x.kappa) elif type(x) is types.sfloat: v, p, z, s = floatingpoint.FLRound(x, 0) return types.sfloat(v, p, z, s, x.err) return x
def trunc(x): if type(x) is types.sfix: return floatingpoint.Trunc(x.v, x.k - x.f, x.f, x.kappa) elif type(x) is types.sfloat: v, p, z, s = floatingpoint.FLRound(x, 0) return types.sfloat(v, p, z, s, x.err) return x
def load_float_to_secret(value, sec=40): def _bit_length(x): return len(bin(x).lstrip('-0b')) num,den = value.as_integer_ratio() exp = int(round(math.log(den, 2))) nbits = _bit_length(num) if nbits > sfloat.vlen: num >>= (nbits - sfloat.vlen) exp -= (nbits - sfloat.vlen) elif nbits < sfloat.vlen: num <<= (sfloat.vlen - nbits) exp += (sfloat.vlen - nbits) if _bit_length(exp) > sfloat.plen: raise CompilerException('Cannot load floating point to secret: overflow') if num < 0: s = load_int_to_secret(1) z = load_int_to_secret(0) else: s = load_int_to_secret(0) if num == 0: z = load_int_to_secret(1) else: z = load_int_to_secret(0) v = load_int_to_secret(num) p = load_int_to_secret(exp) return sfloat(v, p, s, z)
def trunc(x): if isinstance(x, types._fix): return x.v.right_shift(x.f, x.k, security=x.kappa, signed=True) elif type(x) is types.sfloat: v, p, z, s = floatingpoint.FLRound(x, 0) #return types.sfloat(v, p, z, s, x.err) return types.sfloat(v, p, z, s) return x
def log2_fx(x): # transforms sfix to f*2^n, where f is [o.5,1] bounded # obtain number bounded by [0,5 and 1] by transforming input to sfloat d = types.sfloat(x) # isolates mantisa of d, now the n can be also substituted by the # secret shared p from d in the expresion above. v = load_sint(d.v, type(x)) w = (1.0 / (2**(d.vlen))) v = v * w # polynomials for the log_2 evaluation of f are calculated P = p_eval(p_2524, v) Q = p_eval(q_2524, v) # the log is returned by adding the result of the division plus p. a = P / Q + load_sint(d.vlen + d.p, type(x)) return a # *(1-(f.z))*(1-f.s)*(1-f.error)
def FlAbs(a): return types.sfloat(v=a.v, p=a.p, z=a.z, s=types.sint(0), err=a.err)
def load_sint(x, l_type): if l_type is types.sfix: return types.sfix.load_sint(x) elif l_type is types.sfloat: return types.sfloat(x) return x