Example #1
0
 def test_mixed_branch(self):
     assert if_then_else(PrivValBool(0), PrivVal(1),
                         PrivValFxp(2.5)).val() == 2.5
     assert if_then_else(PrivValBool(1), PrivValFxp(1.5),
                         PrivValBool(1)).val() == 1.5
     assert if_then_else(PrivValBool(1), PrivValBool(1),
                         PrivVal(2)).val() == 1
Example #2
0
def _():
    global i, j
    for i in range(10):
        i = i + 1
        j = j * 2
        z = if_then_else(j < 8, lambda: j.to_bits(3)[0], 0)
        yield i != max
Example #3
0
def factorial_if_comparison(n):
    ret = 1

    for i in range(2, FAC_MAX + 1):
        ret = ret * if_then_else(i <= n, i, 1)

    return ret
Example #4
0
def factorial_while(n):
    ret = 1
    busy = 1

    for i in range(2, FAC_MAX + 1):
        busy = busy & (i != n + 1)
        ret = ret * if_then_else(busy, i, 1)

    return ret
Example #5
0
 def test_branch_condition(self):
     with pytest.raises(RuntimeError):
         if_then_else(PrivVal(True), PrivVal(1), PrivVal(2))
     with pytest.raises(RuntimeError):
         if_then_else(PrivVal(1), PrivVal(1), PrivVal(2))
     with pytest.raises(RuntimeError):
         if_then_else(PrivValFxp(1), PrivVal(1), PrivVal(2))
Example #6
0
def random_permutation(n, cur_rp):
    perm = []
    taken = [0] * n
    for i in range(n, 0, -1):
        cur = 0
        for p in range(n):
            #print("cur", p, cur_rp, n-i)
            inp = pperms2[p][1][cur_rp][n - i] if i != 1 else 0
            cur = (cur + inp) % i

        hasseen = 0
        for j in range(n):
            #print("do",~hasseen,taken[j],(~hasseen)&taken[j])
            cur += if_then_else((1 - hasseen) & taken[j], 1, 0)
            iscur = cur == j
            hasseen = hasseen | iscur
            if i > 1: taken[j] = taken[j] | iscur
        perm.append(cur)

    return perm
Example #7
0
def _(_={"j": PrivVal(1)}):
    for i in range(10):
        _["j"] = _["j"] * 2
        z = if_then_else(j < 8, lambda: _["j"].to_bits(3)[0], 0)
        yield i != max
Example #8
0
def cube(x):
    y = if_then_else((x == 10) | (x == 11), 10, x * x * x)
    return if_then_else(y > 50, 50, y)
Example #9
0
 def __if_then_else__(self,other,cond):
     return Tester(if_then_else(cond,self.a,other.a), if_then_else(cond,self.b,other.b))
Example #10
0
 def test_lincomb_branch(self):
     assert if_then_else(PrivValBool(0), PrivVal(1), PrivVal(2)).val() == 2
     assert if_then_else(PrivValBool(1), PrivVal(1), PrivVal(2)).val() == 1
Example #11
0
 def test_lincombfxp_branch(self):
     assert if_then_else(PrivValBool(0), PrivValFxp(1.5),
                         PrivValFxp(2.5)).val() == 2.5
     assert if_then_else(PrivValBool(1), PrivValFxp(1.5),
                         PrivValFxp(2.5)).val() == 1.5
Example #12
0
    _.found = 0

    for cur_rp in _range(ntries):
        _.ret = random_permutation(n, cur_rp)
        isderangement = (functools.reduce(
            lambda x, y: x * y, [_.ret[i] - i for i in range(n)]) != 0)
        _.found = _.found | isderangement
        _breakif(isderangement)
    _endfor()

    return (_.found, _.ret)


(found, ret) = random_derangement(nparties)

print("Found permutation", found, ret)

ret2 = [
    if_then_else(found, (ret[i] + pperms2[i][0]) % nparties, 0)
    for i in range(nparties)
]

ppout = PackList([PackBool(), PackRepeat(PackIntMod(nparties), nparties)])

val = LinComb.from_bits(ppout.pack([found, ret2])).val()
print("Output", val)

print(ppout.unpack(PackIntMod(1 << ppout.bitlen()).pack(val), 0))

print(ret, found, [if_then_else(found, ret2i, 0) for ret2i in ret2])