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
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
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
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
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))
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
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
def cube(x): y = if_then_else((x == 10) | (x == 11), 10, x * x * x) return if_then_else(y > 50, 50, y)
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))
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
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
_.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])