def FLRound(x, mode): """ Rounding with floating point output. *mode*: 0 -> floor, 1 -> ceil, -1 > trunc """ v1, p1, z1, s1, l, k = x.v, x.p, x.z, x.s, x.vlen, x.plen a = types.sint() AdvInteger.LTZ(a, p1, k, x.kappa) b = p1.less_than(-l + 1, k, x.kappa) v2, inv_2pow_p1 = AdvInteger.Oblivious_Trunc(v1, l, -a * (1 - b) * x.p, x.kappa, True) c = AdvInteger.EQZ(v2, l, x.kappa) if mode == -1: away_from_zero = 0 mode = x.s else: away_from_zero = mode + s1 - 2 * mode * s1 v = v1 - v2 + (1 - c) * inv_2pow_p1 * away_from_zero d = v.equal(AdvInteger.two_power(l), l + 1, x.kappa) v = d * AdvInteger.two_power(l - 1) + (1 - d) * v v = a * ((1 - b) * v + b * away_from_zero * AdvInteger.two_power(l - 1)) + (1 - a) * v1 s = (1 - b * mode) * s1 z = AdvInteger.or_op(AdvInteger.EQZ(v, l, x.kappa), z1) v = v * (1 - z) p = ((p1 + d * a) * (1 - b) + b * away_from_zero * (1 - l)) * (1 - z) return v, p, z, s
def Norm(b, k, f, kappa, simplex_flag=False): """ Computes secret integer values [c] and [v_prime] st. 2^{k-1} <= c < 2^k and c = b*v_prime """ # For simplex, we can get rid of computing abs(b) temp = None if simplex_flag == False: temp = types.sint(b < 0) elif simplex_flag == True: temp = types.cint(0) sign = 1 - 2 * temp # 1 - 2 * [b < 0] absolute_val = sign * b #next 2 lines actually compute the SufOR for little indian encoding bits = absolute_val.bit_decompose(k)[::-1] suffixes = AdvInteger.PreOR(bits)[::-1] z = [0] * k for i in range(k - 1): z[i] = suffixes[i] - suffixes[i + 1] z[k - 1] = suffixes[k - 1] #doing complicated stuff to compute v = 2^{k-m} acc = types.cint(0) for i in range(k): acc += AdvInteger.two_power(k - i - 1) * z[i] part_reciprocal = absolute_val * acc signed_acc = sign * acc return part_reciprocal, signed_acc
def sint_cint_division(a, b, k, f, kappa): """ type(a) = sint, type(b) = cint """ from types import cint, sint, Array from library import for_range theta = int(ceil(log(k / 3.5) / log(2))) two = cint(2) * AdvInteger.two_power(f) sign_b = cint(1) - 2 * cint(b < 0) sign_a = sint(1) - 2 * sint(a < 0) absolute_b = b * sign_b absolute_a = a * sign_a w0 = approximate_reciprocal(absolute_b, k, f, theta) A = Array(theta, sint) B = Array(theta, cint) W = Array(theta, cint) A[0] = absolute_a B[0] = absolute_b W[0] = w0 @for_range(1, theta) def block(i): A[i] = AdvInteger.TruncPr(A[i - 1] * W[i - 1], 2 * k, f, kappa) temp = (B[i - 1] * W[i - 1]) >> f # no reading and writing to the same variable in a for loop. W[i] = two - temp B[i] = temp return (sign_a * sign_b) * A[theta - 1]
def cint_cint_division(a, b, k, f): """ Goldschmidt method implemented with SE aproximation: http://stackoverflow.com/questions/2661541/picking-good-first-estimates-for-goldschmidt-division """ from types import cint, Array from library import for_range # theta can be replaced with something smaller # for safety we assume that is the same theta from previous GS method theta = int(ceil(log(k / 3.5) / log(2))) two = cint(2) * AdvInteger.two_power(f) sign_b = cint(1) - 2 * cint(b < 0) sign_a = cint(1) - 2 * cint(a < 0) absolute_b = b * sign_b absolute_a = a * sign_a w0 = approximate_reciprocal(absolute_b, k, f, theta) A = Array(theta, cint) B = Array(theta, cint) W = Array(theta, cint) A[0] = absolute_a B[0] = absolute_b W[0] = w0 @for_range(1, theta) def block(i): A[i] = (A[i - 1] * W[i - 1]) >> f B[i] = (B[i - 1] * W[i - 1]) >> f W[i] = two - B[i] return (sign_a * sign_b) * A[theta - 1]
def approximate_reciprocal(divisor, k, f, theta): """ returns aproximation of 1/divisor where type(divisor) = cint """ from types import cint, Array, MemValue, regint from library import for_range, if_ def twos_complement(x): bits = x.bit_decompose(k)[::-1] bit_array = Array(k, cint) bit_array.assign(bits) twos_result = MemValue(cint(0)) @for_range(k) def block(i): val = twos_result.read() val <<= 1 val += 1 - bit_array[i] twos_result.write(val) return twos_result.read() + 1 bit_array = Array(k, cint) bits = divisor.bit_decompose(k)[::-1] bit_array.assign(bits) cnt_leading_zeros = MemValue(regint(0)) flag = MemValue(regint(0)) cnt_leading_zeros = MemValue(regint(0)) normalized_divisor = MemValue(divisor) @for_range(k) def block(i): flag.write(flag.read() | bit_array[i] == 1) @if_(flag.read() == 0) def block(): cnt_leading_zeros.write(cnt_leading_zeros.read() + 1) normalized_divisor.write(normalized_divisor << 1) q = MemValue(AdvInteger.two_power(k)) e = MemValue(twos_complement(normalized_divisor.read())) @for_range(theta) def block(i): qread = q.read() eread = e.read() qread += (qread * eread) >> k eread = (eread * eread) >> k q.write(qread) e.write(eread) res = q >> cint(2 * k - 2 * f - cnt_leading_zeros) return res
def SDiv(a, b, l, kappa): theta = int(ceil(log(l / 3.5) / log(2))) alpha = AdvInteger.two_power(2 * l) beta = 1 / types.cint(AdvInteger.two_power(l)) w = types.cint(int(2.9142 * AdvInteger.two_power(l))) - 2 * b x = alpha - b * w y = a * w y = AdvInteger.TruncPr(y, 2 * l, l, kappa) x2 = types.sint() AdvInteger.Mod2m(x2, x, 2 * l + 1, l, kappa, False) x1 = (x - x2) * beta for i in range(theta - 1): y = y * (x1 + two_power(l)) + AdvInteger.TruncPr( y * x2, 2 * l, l, kappa) y = AdvInteger.TruncPr(y, 2 * l + 1, l + 1, kappa) x = x1 * x2 + AdvInteger.TruncPr(x2**2, 2 * l + 1, l + 1, kappa) x = x1 * x1 + AdvInteger.TruncPr(x, 2 * l + 1, l - 1, kappa) x2 = types.sint() AdvInteger.Mod2m(x2, x, 2 * l, l, kappa, False) x1 = (x - x2) * beta y = y * (x1 + two_power(l)) + AdvInteger.TruncPr(y * x2, 2 * l, l, kappa) y = AdvInteger.TruncPr(y, 2 * l + 1, l - 1, kappa) return y
def Div(a, b, k, f, kappa, simplex_flag=False): theta = int(ceil(log(k / 3.5) / log(2))) alpha = AdvInteger.two_power(2 * f) w = AppRcr(b, k, f, kappa, simplex_flag) x = alpha - b * w y = a * w y = AdvInteger.TruncPr(y, 2 * k, f, kappa) for i in range(theta): y = y * (alpha + x) x = x * x y = AdvInteger.TruncPr(y, 2 * k, 2 * f, kappa) x = AdvInteger.TruncPr(x, 2 * k, 2 * f, kappa) y = y * (alpha + x) y = AdvInteger.TruncPr(y, 2 * k, 2 * f, kappa) return y
def TruncRoundNearestAdjustOverflow(a, length, target_length, kappa): t = AdvInteger.TruncRoundNearest(a, length, length - target_length, kappa) overflow = t.greater_equal(AdvInteger.two_power(target_length), target_length + 1, kappa) s = (1 - overflow) * t + overflow * t / 2 return s, overflow