Example #1
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, y, a, b, p, q = [z3_vars[v] for v in 'x y a b p q'.split()]
    x2, y2, a2, b2, p2, q2 = [z3_vars2[v] for v in 'x2 y2 a2 b2 p2 q2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = And(a != 0, b != 0)
    pre = And(x >= 1, y >= 1, x == a, y == b, p == 1, q == 0)
    rec = And(
        x2 == x, y2 == y,
        Or(
            And(a % 2 == 0, b % 2 == 0, a2 == a / 2, b2 == b / 2, p2 == 4 * p,
                q2 == q),
            And(a % 2 == 1, b % 2 == 0, a2 == a - 1, b2 == b, p2 == p,
                q2 == q + b * p),
            And(a % 2 == 0, b % 2 == 1, a2 == a, b2 == b - 1, p2 == p,
                q2 == q + a * p),
            And(a % 2 == 1, b % 2 == 1, a2 == a - 1, b2 == b - 1, p2 == p,
                q2 == q + (a2 + b2 + 1) * p)))
    post = And(q == x * y)
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #2
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, y, c, k = [z3_vars[v] for v in 'x y c k'.split()]
    x2, y2, c2, k2 = [z3_vars2[v] for v in 'x2 y2 c2 k2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = c < k
    pre = And(k >= 0, k <= 30, y == 0, x == 0, c == 0)
    rec = And(c2 == c + 1, k2 == k, y2 == y + 1, x2 == x + y2 * y2 * y2 * y2)
    post = And(6 * k * k * k * k * k + 15 * k * k * k * k + 10 * k * k * k -
               30 * x - k == 0)
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #3
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    A, R, u, v, r = [z3_vars[v] for v in 'A R u v r'.split()]
    A2, R2, u2, v2, r2 = [z3_vars2[v] for v in 'A2 R2 u2 v2 r2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = r != 0
    pre = And(A >= 1, (R - 1) * (R - 1) < A, A <= R * R, A % 2 == 1,
              u == 2 * R + 1, v == 1, r == R * R - A)
    rec = And(
        A2 == A, R2 == R,
        Or(And(r > 0, r2 == r - v, v2 == v + 2, u2 == u),
           And(r <= 0, r2 == r + u, u2 == u + 2, v2 == v)))
    post = And(A == ((u + v - 2) / 2) * ((u - v) / 2))
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #4
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    A, B, q, r, t = (z3_vars[v] for v in 'A B q r t'.split())
    A2, B2, q2, r2, t2 = [z3_vars2[v] for v in 'A2 B2 q2 r2 t2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = t != 0
    pre = And(A >= 0, B >= 1, q == 0, r == 0, t == A)
    rec = And(
        A2 == A, B2 == B,
        Or(And(r + 1 == B, q2 == q + 1, r2 == 0, t2 == t - 1),
           And(r + 1 != B, q2 == q, r2 == r + 1, t2 == t - 1)))
    temp = Int('temp')
    post = Exists(temp, And(A == q * B + temp, temp >= 0, temp < B))
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #5
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, a, r = [z3_vars[v] for v in 'x a r'.split()]
    x2, a2, r2 = [z3_vars2[v] for v in 'x2 a2 r2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = x > r
    pre = And(a > 0, r == 0, x == ToReal(a) / 2)
    rec = And(x2 == x - r, r2 == r + 1, a2 == a)
    invariant2_with_old_vars = z3.substitute(invariant, [(x, x - r),
                                                         (r, r + 1)])
    post = And(r * r + r >= a, r * r - r < a)
    solver.add(
        Not(
            And(
                Implies(pre, invariant),
                # Implies(And(invariant, lc, rec), invariant2),  # z3 timeout
                Implies(And(invariant, lc), invariant2_with_old_vars),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #6
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, y, a, b, p, r, q, s = [z3_vars[v] for v in 'x y a b p r q s'.split()]
    x2, y2, a2, b2, p2, r2, q2, s2 = [
        z3_vars2[v] for v in 'x2 y2 a2 b2 p2 r2 q2 s2'.split()
    ]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = a != b
    pre = And(x >= 1, y >= 1, a == x, b == y, p == 1, q == 0, r == 0, s == 1)
    rec = And(
        x2 == x, y2 == y,
        Or(
            And(a > b, a2 == a - b, p2 == p - q, r2 == r - s, b2 == b, q2 == q,
                s2 == s),
            And(a <= b, b2 == b - a, q2 == q - p, s2 == s - r, a2 == a,
                p2 == p, r2 == r)))
    # this post condition implies a==b==GCD(x,y), z3 does not support GCD so we write it in this way
    post = And(1 == p * s - r * q, a == y * r + x * p, b == x * q + y * s,
               a == b)
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #7
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, y, z, c, k, a = [z3_vars[v] for v in 'x y z c k a'.split()]
    x2, y2, z2, c2, k2, a2 = [z3_vars2[v] for v in 'x2 y2 z2 c2 k2 a2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = c < k
    pre = And(x == a, y == 1, c == 1, z >= 0, z <= 10, k >= 0, k <= 10)
    rec = And(c2 == c + 1, k2 == k, z2 == z, x2 == x * z + a, y2 == y * z,
              a2 == a)
    post = And(x * z - x + a - a * z * y == 0)
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #8
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    a, b, x, y, z = [z3_vars[v] for v in 'a b x y z'.split()]
    a2, b2, x2, y2, z2 = [z3_vars2[v] for v in 'a2 b2 x2 y2 z2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = y != 0
    pre = And(a >= 0, b >= 0, x == a, y == b, z == 0)
    rec = And(
        a2 == a, b2 == b,
        Or(And(y % 2 == 1, z2 == z + x, y2 == (y - 1) / 2, x2 == 2 * x),
           And(y % 2 == 0, z2 == z, y2 == y / 2, x2 == 2 * x)))
    post = And(z == a * b)
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #9
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, a, r, s = [z3_vars[v] for v in 'x a r s'.split()]
    x2, a2, r2, s2 = [z3_vars2[v] for v in 'x2 a2 r2 s2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = x > s
    pre = And(a > 0, r == 1, x == ToReal(a), s == 3.25)
    rec = And(x2 == x - s, r2 == r + 1, s2 == s + 6 * r + 3, a2 == a)
    post = And(4 * r * r * r + 6 * r * r + 3 * r >= 4 * a,
               4 * a > 4 * r * r * r - 6 * r * r + 3 * r - 1)
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #10
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    A, R, u, v, r = [z3_vars[v] for v in 'A R u v r'.split()]
    A2, R2, u2, v2, r2 = [z3_vars2[v] for v in 'A2 R2 u2 v2 r2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    if loop_index == 1:
        lc = r != 0
        pre = And(A >= 1, (R - 1) * (R - 1) < A, A <= R * R, A % 2 == 1,
                  u == 2 * R + 1, v == 1, r == R * R - A)
        rec = And(A2 == A, R2 == R, u2 == u, v2 == v, r2 == r)
        # post = And(A % ((u - v)/2) == 0)  # z3 timeout
        # post = Exists(temp, A == temp * ((u-v)/2))  # z3 timeout
        post = And(A == ((u + v - 2) / 2) * ((u - v) / 2))
        pre2_original = And(4 * (A + r) == u * u - v * v - 2 * u + 2 * v,
                            v % 2 == 1, u % 2 == 1, A >= 1)
        pre2_substitute = z3.substitute(pre2_original, subs)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), pre2_substitute),
                    Implies(And(invariant, Not(lc)), post))))
    elif loop_index == 2:
        lc = r > 0
        pre = And(4 * (A + r) == u * u - v * v - 2 * u + 2 * v, v % 2 == 1,
                  u % 2 == 1, A >= 1)
        rec = And(r2 == r - v, v2 == v + 2, A2 == A, R2 == R, u2 == u)
        post = And(4 * (A + r) == u * u - v * v - 2 * u + 2 * v, v % 2 == 1,
                   u % 2 == 1, A >= 1)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    else:
        assert loop_index == 3
        lc = r < 0
        pre = And(4 * (A + r) == u * u - v * v - 2 * u + 2 * v, v % 2 == 1,
                  u % 2 == 1, A >= 1)
        rec = And(r2 == r + u, u2 == u + 2, A2 == A, R2 == R, v2 == v)
        post = And(4 * (A + r) == u * u - v * v - 2 * u + 2 * v, v % 2 == 1,
                   u % 2 == 1, A >= 1)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))

    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #11
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    x, y, q, a, b, r = [z3_vars[v] for v in 'x y q a b r'.split()]
    x2, y2, q2, a2, b2, r2 = [z3_vars2[v] for v in 'x2 y2 q2 a2 b2 r2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    if loop_index == 1:
        lc = r >= y
        pre = And(x > 0, y > 0, q == 0, r == x, a == 0, b == 0)
        pre2_original = And(x == q * y + r, r >= 0, x >= 1, y >= 1, a == 1,
                            b == y, r >= y)
        pre2_substitute = z3.substitute(pre2_original, subs)
        rec1 = And(a2 == 1, b2 == y, x2 == x, y2 == y, q2 == q, r2 == r)
        post2_original = And(r >= b, b == y * a, x == q * y + r, r >= 0,
                             x >= 1, y >= 1, r < 2 * b)
        post2_substitute = z3.substitute(post2_original, subs)
        rec2 = And(r2 == r - b, q2 == q + a, x2 == x, y2 == y, a2 == a,
                   b2 == b)
        post = And(x == q * y + r, r >= 0, r < y)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec1), pre2_substitute),
                    Implies(And(post2_substitute, rec2), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    else:
        assert loop_index == 2
        lc = r >= 2 * b
        pre = And(x == q * y + r, r >= 0, x >= 1, y >= 1, a == 1, b == y,
                  r >= y)
        rec = And(x2 == x, y2 == y, q2 == q, r2 == r, a2 == 2 * a, b2 == 2 * b)
        post = And(r >= b, b == y * a, x == q * y + r, r >= 0, x >= 1, y >= 1,
                   r < 2 * b)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #12
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    A, B, q, r, d, p = [z3_vars[v] for v in 'A B q r d p'.split()]
    A2, B2, q2, r2, d2, p2 = [z3_vars2[v] for v in 'A2 B2 q2 r2 d2 p2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    if loop_index == 1:
        lc = r >= d
        pre = And(A >= 0, B >= 1, r == A, d == B, p == 1, q == 0)
        rec = And(d2 == 2 * d, p2 == 2 * p, A2 == A, B2 == B, r2 == r, q2 == q)
        post = And(A >= 0, B >= 1, r == A, d == B * p, q == 0, r < d)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    else:
        assert loop_index == 2
        lc = p != 1
        pre = And(A >= 0, B >= 1, r == A, d == B * p, q == 0, r < d)
        rec = And(
            A2 == A, B2 == B, d2 == d / 2, d == 2 * d2, p2 == p / 2,
            p == 2 * p2,
            Or(And(r >= d2, r2 == r - d2, q2 == q + p2),
               And(r < d2, r2 == r, q2 == q)))
        temp = Int('temp')
        post = Exists(temp, And(temp >= 0, temp < B, A == q * B + temp))
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #13
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    A, B, q, r, b = [z3_vars[v] for v in 'A B q r b'.split()]
    A2, B2, q2, r2, b2 = [z3_vars2[v] for v in 'A2 B2 q2 r2 b2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    if loop_index == 1:
        lc = r >= b
        pre = And(A > 0, B > 0, q == 0, r == A, b == B)
        rec = And(A2 == A, B2 == B, q2 == q, r2 == r, b2 == 2 * b)
        post = And(q == 0, A == r, b > 0, r > 0, r < b)
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    else:
        assert loop_index == 2
        lc = b != B
        pre = And(q == 0, A == r, b > 0, r > 0, r < b)
        rec = And(
            A2 == A, B2 == B, b2 == b / 2, b == 2 * b2,
            Or(And(r >= b2, q2 == 2 * q + 1, r2 == r - b2),
               And(r < b2, q2 == 2 * q, r2 == r)))
        temp = Int('temp')
        post = Exists(temp, And(temp >= 0, temp < B, A == q * B + temp))
        solver.add(
            Not(
                And(Implies(pre, invariant),
                    Implies(And(invariant, lc, rec), invariant2),
                    Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #14
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    a, n, t, s = (z3_vars[v] for v in 'a n t s'.split())
    a2, n2, t2, s2 = [z3_vars2[v] for v in 'a2 n2 t2 s2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = s <= n
    pre = And(n >= 0, a == 0, s == 1, t == 1)
    rec = And(n2 == n, a2 == a + 1, t2 == t + 2, s2 == s + t2)
    post = And(a * a <= n, (a + 1) * (a + 1) > n)
    solver.add(Not(And(Implies(pre, invariant),
                       Implies(And(invariant, lc, rec), invariant2),
                       Implies(And(invariant, Not(lc)), post)
                       )))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()
Example #15
0
def full_check(z3_vars, invariant, loop_index):
    z3_vars2, subs = core.gen_var2s_subs(z3_vars)
    a, n, x, y, z = [z3_vars[v] for v in 'a n x y z'.split()]
    a2, n2, x2, y2, z2 = [z3_vars2[v] for v in 'a2 n2 x2 y2 z2'.split()]
    invariant2 = z3.substitute(invariant, subs)
    solver = z3.Solver()
    assert loop_index == 1
    lc = n <= a
    pre = And(n == 0, x == 0, y == 1, z == 6, a >= 0)
    rec = And(n2 == n + 1, x2 == x + y, y2 == y + z, z2 == z + 6, a2 == a)
    post = And(x == (a + 1) * (a + 1) * (a + 1))
    solver.add(
        Not(
            And(Implies(pre, invariant),
                Implies(And(invariant, lc, rec), invariant2),
                Implies(And(invariant, Not(lc)), post))))
    result = solver.check()
    if result == unsat:
        return True, None
    elif result == unknown:
        return False, None
    else:
        assert result == sat
        return False, solver.model()