Example #1
0
def make_FSM(length):
    s = MK85(verbose=0)
    states = [s.BitVec('states_%d' % i, BIT_WIDTH) for i in range(length)]
    inputs = [s.BitVec('inputs_%d' % i, BIT_WIDTH) for i in range(length - 1)]

    # initial state:
    s.add(states[0] == 0)

    # the last state must be equal to one of the acceping states
    s.add(Or(*[states[length - 1] == i for i in ACCEPTING_STATES]))

    # all states are in limits...
    for i in range(length):
        s.add(And(states[i] >= 0, states[i] <= 24))
        # redundant, though. however, we are not interesting in non-matched inputs, right?
        s.add(states[i] != INVALID_STATE)

    # "insert" transition() functions between subsequent states
    for i in range(length - 1):
        s.add(states[i + 1] == transition(s, states[i], inputs[i]))

    # enumerate results:
    results = []
    while s.check():
        m = s.model()
        #print m
        print_model(m, length, inputs)
        # add the current solution negated:
        tmp = []
        for pair in m:
            tmp.append(s.var_by_name(pair) == m[pair])
        s.add(expr.Not(And(*tmp)))
Example #2
0
def factor(n):
    print "factoring", n

    s = MK85()

    #TODO in1,in2,out=Bitvecs('in1 in2 out', 64)
    # TODO: 64-bit
    in1 = s.BitVec('in1', 32)
    in2 = s.BitVec('in2', 32)
    out = s.BitVec('out', 32)

    s.add(out == n)
    s.add(s.BVMulNoOverflow(in1, in2) == out)
    # inputs cannot be negative and must be non-1:
    s.add(in1 > 1)
    s.add(in2 > 1)

    if s.check() == False:
        print n, "is prime (unsat)"
        return [n]

    m = s.model()
    # get inputs of multiplier:
    in1_n = m["in1"]
    in2_n = m["in2"]

    print "factors of", n, "are", in1_n, "and", in2_n
    # factor factors recursively:
    rt = sorted(factor(in1_n) + factor(in2_n))
    # self-test:
    assert reduce(mul, rt, 1) == n
    return rt
# MS Excel or LibreOffice style.
# first top-left cell is A0, not A1
def coord_to_name(R, C):
    return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[R] + str(C)


# open file and parse it as list of lists:
f = open(sys.argv[1], "r")
# filter(None, ...) to remove empty sublists:
ar = filter(None, [item.rstrip().split() for item in f.readlines()])
f.close()

WIDTH = len(ar[0])
HEIGHT = len(ar)

s = MK85()

# cells{} is a dictionary with keys like "A0", "B9", etc:
cells = {}
for R in range(HEIGHT):
    for C in range(WIDTH):
        name = coord_to_name(R, C)
        cells[name] = s.BitVec(name, 32)

cur_R = 0
cur_C = 0

for row in ar:
    for c in row:
        # string like "A0+B2" becomes "cells["A0"]+cells["B2"]":
        c = re.sub(r'([A-Z]{1}[0-9]+)', r'cells["\1"]', c)
Example #4
0
from MK85 import *

# see: https://yurichev.com/blog/TAOCP_713_198/

s=MK85(verbose=0)

# FIXME: must work with 22-bit bitvecs:
a=s.BitVec('a', 32)
b=s.BitVec('b', 32)
c=s.BitVec('c', 32)

# make a,c more aesthetically appealing:
s.add((a&0xffff)==0)
s.add((c&0xffff00)==0)

def bytes_in_UTF8_seq(x):
    if (x>>7)==0:
        return 1
    if (x>>5)==0b110:
        return 2
    if (x>>4)==0b1110:
        return 3
    if (x>>3)==0b11110:
        return 4
    # invalid 1st byte
    return None

for x in range(256):
    t=bytes_in_UTF8_seq(x)
    if t!=None:
        #print x, t