示例#1
0
def MSB(b, k):
    # calculation of z
    # x in order 0 - k
    if (k > types.program.bit_length):
        raise OverflowError("The supported bit \
        lenght of the application is smaller than k")

    x_order = b.bit_decompose(k)
    x = [0] * k
    # x i now inverted
    for i in range(k - 1, -1, -1):
        x[k - 1 - i] = x_order[i]
    # y is inverted for PReOR and then restored
    y_order = AdvInteger.PreOR(x)

    # y in order (restored in orginal order
    y = [0] * k
    for i in range(k - 1, -1, -1):
        y[k - 1 - i] = y_order[i]

    # obtain z
    z = [0] * (k + 1 - k % 2)
    for i in range(k - 1):
        z[i] = y[i] - y[i + 1]
    z[k - 1] = y[k - 1]

    return z
示例#2
0
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
示例#3
0
def Int2FL(a, gamma, l, kappa):
    lam = gamma - 1
    s = types.sint()
    AdvInteger.LTZ(s, a, gamma, kappa)
    z = AdvInteger.EQZ(a, gamma, kappa)
    a = (1 - 2 * s) * a

    a_bits = AdvInteger.BitDec(a, lam, lam, kappa)
    a_bits.reverse()
    b = AdvInteger.PreOR(a_bits)
    t = a * (1 + sum(2**i * (1 - b_i) for i, b_i in enumerate(b)))
    p = -(lam - sum(b))
    if lam > l:
        if types.sfloat.round_nearest:
            v, overflow = TruncRoundNearestAdjustOverflow(
                t, gamma - 1, l, kappa)
            p = p + overflow
        else:
            v = types.sint()
            AdvInteger.Trunc(v, t, gamma - 1, gamma - l - 1, kappa, False)
            #TODO: Shouldnt this be only gamma
    else:
        v = 2**(l - gamma + 1) * t
    p = (p + gamma - 1 - l) * (1 - z)
    return v, p, z, s