def pad(cnf: Formula, a: List[Variable], b: List[Variable]) -> Tuple[List[Variable], List[Variable]]: aa = a[:] bb = b[:] if len(aa) < len(bb): for i in range(len(aa), len(bb)): aa.append(cnf.const_false()) elif len(bb) < len(aa): for i in range(len(bb), len(aa)): bb.append(cnf.const_false()) assert (len(aa) == len(bb)) return aa, bb
def cnf_enforce_pyth_trip(cnf: Formula, a: List[Variable], b: List[Variable], c: List[Variable], primitive: bool) -> None: assert (len(a) == len(b)) assert (len(a) == len(c)) bitdepth = len(a) m = new_integer(cnf, bitdepth) n = new_integer(cnf, bitdepth) m2 = cnf_square(cnf, m) n2 = cnf_square(cnf, n) m2n2 = cnf_add(cnf, m2, n2) two = [cnf.const_false(), cnf.const_true()] mn = cnf_mult(cnf, m, n) mn2 = cnf_mult(cnf, mn, two) # a disgusting way to do subtraction. fix me please m2subn2 = new_integer(cnf, bitdepth) m2_tmp = cnf_add(cnf, m2subn2, n2) cnf_equal(cnf, m2_tmp, m2) if not primitive: k = new_integer(cnf, bitdepth) m2subn2 = cnf_mult(cnf, m2subn2, k) mn2 = cnf_mult(cnf, mn2, k) m2n2 = cnf_mult(cnf, m2n2, k) cnf_equal(cnf, a, m2subn2) cnf_equal(cnf, b, mn2) cnf_equal(cnf, c, m2n2)
def cnf_add(cnf: Formula, a: List[Variable], b: List[Variable]) -> List[Variable]: #if the lengths are incorrect, just pad up with zero variables a, b = cnf_padout(a, b) carry = cnf.const_false() # The first carry is always 0 out = [] for (ai, bi) in zip(a, b): res, carry = cnf_1bitadder(cnf, ai, bi, carry) out.append(res) out.append(carry) return out