Exemple #1
0
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file for Ketje.
        """
        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]

        with open(stp_filename, 'w') as stp_file:
            stp_file.write("% Input File for STP\n% Ketje w={} rounds={}"
                           "\n\n\n".format(wordsize, rounds))

            # Setup variables
            # 5x5 lanes of wordsize
            s = ["s{}{}{}".format(x, y, i) for i in range(rounds + 1)
                 for y in range(5) for x in range(5)]
            a = ["a{}{}{}".format(x, y, i) for i in range(rounds)
                 for y in range(5) for x in range(5)]
            b = ["b{}{}{}".format(x, y, i) for i in range(rounds)
                 for y in range(5) for x in range(5)]
            c = ["c{}{}".format(x, i) for i in range(rounds + 1) for x in range(5)]
            d = ["d{}{}".format(x, i) for i in range(rounds + 1) for x in range(5)]
            m = ["m{}{}".format(x, i) for i in range(rounds +1) for x in range(2)]
            xin = ["xin{}{}{}".format(y, z, i) for i in range(rounds)
                   for y in range(5) for z in range (wordsize)]
            xout = ["xout{}{}{}".format(y, z, i) for i in range(rounds)
                    for y in range(5) for z in range (wordsize)]
            andOut = ["andOut{}{}{}".format(y, z, i) for i in range(rounds)
                      for y in range(5) for z in range (wordsize)]

	        # w = weight
            w = ["w{}".format(i) for i in range(rounds)]
            tmp = ["tmp{}{}{}".format(y, z, i) for i in range(rounds) 
                   for y in range(5) for z in range (wordsize)]
            stpcommands.setupVariables(stp_file, s, wordsize)
            stpcommands.setupVariables(stp_file, a, wordsize)
            stpcommands.setupVariables(stp_file, b, wordsize)
            stpcommands.setupVariables(stp_file, c, wordsize)
            stpcommands.setupVariables(stp_file, d, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)
            stpcommands.setupVariables(stp_file, tmp, 5)
            stpcommands.setupWeightComputationSum(stp_file, weight, w, wordsize)
            stpcommands.setupVariables(stp_file, xin, 5)
            stpcommands.setupVariables(stp_file, xout, 5)
            stpcommands.setupVariables(stp_file, andOut, 5)
            stpcommands.setupVariables(stp_file, m, wordsize)

            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, a, wordsize)

            for rnd in range(rounds):
                self.setupKeccakRound(stp_file, rnd, s, a, b, c, d, wordsize, 
                                      tmp, w, m, xin, xout, andOut)

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            stpcommands.setupQuery(stp_file)

        return
Exemple #2
0
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file for Ascon.
        """
        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]

        sboxsize = 5  # TODO: support arbitrary sizes
        capacity = 0
        rate = (wordsize * sboxsize) - capacity

        if "rate" in parameters:
            rate = parameters["rate"]

        if "capacity" in parameters:
            capacity = parameters["capacity"]

        assert (rate + capacity) == wordsize * sboxsize

        with open(stp_filename, 'w') as stp_file:
            stp_file.write("% Input File for STP\n% Ascon w={} rate={} "
                           "capacity={} round={}\n\n\n".format(
                               wordsize, rate, capacity, rounds))

            # Setup variables
            # 5 x wordsize state
            s = [
                "s{}{}".format(x, i) for i in range(rounds + 1)
                for x in range(sboxsize)
            ]

            # Output after S-box Linear part 1
            a = [
                "a{}{}".format(x, i) for i in range(rounds + 1)
                for x in range(sboxsize)
            ]
            # Output after S-box Non-Linear part
            b = [
                "b{}{}".format(x, i) for i in range(rounds + 1)
                for x in range(sboxsize)
            ]
            # Output after S-box Linear part 2
            c = [
                "c{}{}".format(x, i) for i in range(rounds + 1)
                for x in range(sboxsize)
            ]

            # Inputs/Output to the S-box
            xin = [
                "inx{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(sboxsize) for z in range(wordsize)
            ]
            xout = [
                "outx{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(sboxsize) for z in range(wordsize)
            ]
            andout = [
                "andout{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(sboxsize) for z in range(wordsize)
            ]

            # w = weight
            w = ["w{}".format(i) for i in range(rounds)]
            tmp = [
                "tmp{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(sboxsize) for z in range(wordsize)
            ]

            stpcommands.setupVariables(stp_file, s, wordsize)
            stpcommands.setupVariables(stp_file, a, wordsize)
            stpcommands.setupVariables(stp_file, b, wordsize)
            stpcommands.setupVariables(stp_file, c, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)
            stpcommands.setupVariables(stp_file, tmp, sboxsize)
            stpcommands.setupWeightComputationSum(stp_file, weight, w,
                                                  wordsize)
            stpcommands.setupVariables(stp_file, xin, sboxsize)
            stpcommands.setupVariables(stp_file, xout, sboxsize)
            stpcommands.setupVariables(stp_file, andout, sboxsize)

            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, s, wordsize)

            # Fix variables for capacity, only works if rate/capacity is
            # multiple of wordsize.
            for i in range(rate // wordsize, (rate + capacity) // wordsize):
                stpcommands.assertVariableValue(
                    stp_file, s[i], "0hex{}".format("0" * (wordsize // 4)))

            for rnd in range(rounds):
                self.setupAsconRound(stp_file, rnd, s, a, b, c, wordsize, tmp,
                                     w, xin, xout, andout)

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            for char in parameters["blockedCharacteristics"]:
                stpcommands.blockCharacteristic(stp_file, char, wordsize)

            stpcommands.setupQuery(stp_file)

        return
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file for Keccak.
        """
        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]

        capacity = 160
        rate = (wordsize * 25) - capacity

        if "rate" in parameters:
            rate = parameters["rate"]

        if "capacity" in parameters:
            capacity = parameters["capacity"]

        assert (rate + capacity) == wordsize * 25

        with open(stp_filename, 'w') as stp_file:
            stp_file.write("% Input File for STP\n% Keccak w={} rate={} "
                           "capacity={}\n\n\n".format(wordsize, rate, capacity,
                                                      rounds))

            # Setup variables
            # 5x5 lanes of wordsize
            s = [
                "s{}{}{}".format(x, y, i) for i in range(rounds + 1)
                for y in range(5) for x in range(5)
            ]

            b = [
                "b{}{}{}".format(x, y, i) for i in range(rounds)
                for y in range(5) for x in range(5)
            ]
            c = ["c{}{}".format(x, i) for i in range(rounds) for x in range(5)]
            d = ["d{}{}".format(x, i) for i in range(rounds) for x in range(5)]
            xin = [
                "xin{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(5) for z in range(wordsize)
            ]
            xout = [
                "xout{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(5) for z in range(wordsize)
            ]
            andOut = [
                "andOut{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(5) for z in range(wordsize)
            ]

            # w = weight
            w = ["w{}".format(i) for i in range(rounds)]
            tmp = [
                "tmp{}{}{}".format(y, z, i) for i in range(rounds)
                for y in range(5) for z in range(wordsize)
            ]
            stpcommands.setupVariables(stp_file, s, wordsize)
            stpcommands.setupVariables(stp_file, b, wordsize)
            stpcommands.setupVariables(stp_file, c, wordsize)
            stpcommands.setupVariables(stp_file, d, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)
            stpcommands.setupVariables(stp_file, tmp, 5)
            stpcommands.setupWeightComputationSum(stp_file, weight, w,
                                                  wordsize)
            stpcommands.setupVariables(stp_file, xin, 5)
            stpcommands.setupVariables(stp_file, xout, 5)
            stpcommands.setupVariables(stp_file, andOut, 5)

            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, s, wordsize)

            # Fix variables for capacity, only works if rate/capacity is
            # multiple of wordsize.
            for i in range(rate // wordsize, (rate + capacity) // wordsize):
                stpcommands.assertVariableValue(
                    stp_file, s[i], "0hex{}".format("0" * (wordsize // 4)))

            for rnd in range(rounds):
                self.setupKeccakRound(stp_file, rnd, s, b, c, d, wordsize, tmp,
                                      w, xin, xout, andOut)

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            stpcommands.setupQuery(stp_file)

        return
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file to find a characteristic for BAT diff pattern with
        the given parameters.
        """

        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]
        if wordsize == 32:
            p = [7, 4, 1, 6, 3, 0, 5, 2]
        elif wordsize == 64:
            p = [14, 15, 8, 9, 2, 3, 12, 13, 6, 7, 0, 1, 10, 11, 4, 5]
        else:
            raise Exception("Wrong wordsize!")
        self.PERM = GenPerm.GenNibblePerms(wordsize, p)

        with open(stp_filename, 'w') as stp_file:
            header = ("% Input File for STP: BAT diff actsbox\n"
                      "% w = {} alpha = {} beta = {}\n"
                      "% rounds = {}\n\n".format(wordsize, self.rot_alpha,
                                                 self.rot_beta, rounds))
            stp_file.write(header)

            # Setup variables
            # x as left, y as right
            x = ["x{}".format(i) for i in range(rounds + 1)]
            y = ["y{}".format(i) for i in range(rounds + 1)]
            out_G0 = ["outG0{}".format(i) for i in range(rounds)]
            out_G1 = ["outG1{}".format(i) for i in range(rounds)]
            rot_G0 = ["rotG0{}".format(i) for i in range(rounds)]
            rot_G1 = ["rotG1{}".format(i) for i in range(rounds)]
            xor_G = ["xorG{}".format(i) for i in range(rounds)]
            perm_G = ["permG{}".format(i) for i in range(rounds)]

            # w = weight
            w = ["sumw{}".format(i) for i in range(rounds)]
            act_flag = ["actflag{}".format(i) for i in range(rounds)]

            stpcommands.setupVariables(stp_file, x, wordsize)
            stpcommands.setupVariables(stp_file, y, wordsize)
            stpcommands.setupVariables(stp_file, out_G0, wordsize)
            stpcommands.setupVariables(stp_file, out_G1, wordsize)
            stpcommands.setupVariables(stp_file, rot_G0, wordsize)
            stpcommands.setupVariables(stp_file, rot_G1, wordsize)
            stpcommands.setupVariables(stp_file, xor_G, wordsize)
            stpcommands.setupVariables(stp_file, perm_G, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)
            stpcommands.setupVariables(stp_file, act_flag, wordsize // 4)

            stpcommands.setupWeightComputationSum(stp_file, weight, w, 16)

            self.SBOX_ACT_ASSERT(stp_file)

            for i in range(rounds):
                self.setupRound(stp_file, x[i], y[i], x[i + 1], y[i + 1],
                                out_G0[i], out_G1[i], rot_G0[i], rot_G1[i],
                                xor_G[i], perm_G[i], act_flag[i], w[i],
                                wordsize)

            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, [x[0], y[0]], wordsize)

            # Iterative characteristics only
            # Input difference = Output difference
            if parameters["iterative"]:
                stpcommands.assertVariableValue(stp_file, x[0], x[rounds])
                stpcommands.assertVariableValue(stp_file, y[0], y[rounds])

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            for char in parameters["blockedCharacteristics"]:
                stpcommands.blockCharacteristic(stp_file, char, wordsize)

            stpcommands.setupQuery(stp_file)

        return
Exemple #5
0
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file for Ascon.
        """
        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]

        sboxsize = 5 # TODO: support arbitrary sizes
        capacity = 0
        rate = (wordsize * sboxsize) - capacity

        if "rate" in parameters:
            rate = parameters["rate"]

        if "capacity" in parameters:
            capacity = parameters["capacity"]

        assert (rate + capacity) == wordsize * sboxsize

        with open(stp_filename, 'w') as stp_file:
            stp_file.write("% Input File for STP\n% Ascon w={} rate={} "
                           "capacity={} round={}\n\n\n".format(wordsize,
                                                               rate, capacity,
                                                               rounds))

            # Setup variables
            # 5 x wordsize state
            s = ["s{}{}".format(x, i) for i in range(rounds+1)
                 for x in range(sboxsize)]

            # Output after S-box Linear part 1
            a = ["a{}{}".format(x, i) for i in range(rounds+1)
                 for x in range(sboxsize)]
            # Output after S-box Non-Linear part
            b = ["b{}{}".format(x, i) for i in range(rounds+1)
                 for x in range(sboxsize)]
            # Output after S-box Linear part 2
            c = ["c{}{}".format(x, i) for i in range(rounds+1)
                 for x in range(sboxsize)]


            # Inputs/Output to the S-box
            xin = ["inx{}{}{}".format(y, z, i) for i in range(rounds)
                   for y in range(sboxsize) for z in range (wordsize)]
            xout = ["outx{}{}{}".format(y, z, i) for i in range(rounds)
                    for y in range(sboxsize) for z in range (wordsize)]
            andout = ["andout{}{}{}".format(y, z, i) for i in range(rounds)
                      for y in range(sboxsize) for z in range (wordsize)]

	        # w = weight
            w = ["w{}".format(i) for i in range(rounds)]
            tmp = ["tmp{}{}{}".format(y, z, i) for i in range(rounds)
                   for y in range(sboxsize) for z in range (wordsize)]

            stpcommands.setupVariables(stp_file, s, wordsize)
            stpcommands.setupVariables(stp_file, a, wordsize)
            stpcommands.setupVariables(stp_file, b, wordsize)
            stpcommands.setupVariables(stp_file, c, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)
            stpcommands.setupVariables(stp_file, tmp, sboxsize)
            stpcommands.setupWeightComputationSum(stp_file, weight, w, wordsize)
            stpcommands.setupVariables(stp_file, xin, sboxsize)
            stpcommands.setupVariables(stp_file, xout, sboxsize)
            stpcommands.setupVariables(stp_file, andout, sboxsize)

            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, s, wordsize)

            # Fix variables for capacity, only works if rate/capacity is
            # multiple of wordsize.
            for i in range(rate // wordsize, (rate + capacity) // wordsize):
                stpcommands.assertVariableValue(stp_file, s[i],
                                                "0hex{}".format("0"*(wordsize // 4)))

            for rnd in range(rounds):
                self.setupAsconRound(stp_file, rnd, s, a, b, c, wordsize, tmp,
                                     w, xin, xout, andout)

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            for char in parameters["blockedCharacteristics"]:
                stpcommands.blockCharacteristic(stp_file, char, wordsize)

            stpcommands.setupQuery(stp_file)

        return
Exemple #6
0
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file to find a differential trail for Gimli with
        the given parameters.
        """
        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]

        if "rotationconstants" in parameters:
            self.d = parameters["rotationconstants"][0]
            self.e = parameters["rotationconstants"][1]
            self.f = parameters["rotationconstants"][2]


        with open(stp_filename, 'w') as stp_file:
            stp_file.write("% Input File for STP\n% Gimli w={}"
                           "rounds={}\n\n\n".format(wordsize, rounds))

            # Setup variables
            x = ["x{}r{}".format(j, i) for i in range(rounds + 1) for j in range(4)]
            xsb = ["xsb{}r{}".format(j, i) for i in range(rounds) for j in range(4)]
            y = ["y{}r{}".format(j, i) for i in range(rounds + 1) for j in range(4)]
            ysb = ["ysb{}r{}".format(j, i) for i in range(rounds) for j in range(4)]
            z = ["z{}r{}".format(j, i) for i in range(rounds + 1) for j in range(4)]
            zsb = ["zsb{}r{}".format(j, i) for i in range(rounds + 1) for j in range(4)]
            w = ["rw{}".format(i) for i in range(rounds)]
            wp = ["rwp{}r{}".format(j, i) for i in range(rounds) for j in range(4)]

            xtmp = ["xtmp{}r{}".format(j, i) for i in range(rounds) for j in range(4)]
            ytmp = ["ytmp{}r{}".format(j, i) for i in range(rounds) for j in range(4)]
            ztmp = ["ztmp{}r{}".format(j, i) for i in range(rounds) for j in range(4)]

            stpcommands.setupVariables(stp_file, x, wordsize)
            stpcommands.setupVariables(stp_file, y, wordsize)
            stpcommands.setupVariables(stp_file, z, wordsize)
            stpcommands.setupVariables(stp_file, xtmp, wordsize)
            stpcommands.setupVariables(stp_file, ytmp, wordsize)
            stpcommands.setupVariables(stp_file, ztmp, wordsize)
            stpcommands.setupVariables(stp_file, xsb, wordsize)
            stpcommands.setupVariables(stp_file, ysb, wordsize)
            stpcommands.setupVariables(stp_file, zsb, wordsize)
            stpcommands.setupVariables(stp_file, wp, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)

            for rnd in range(rounds):
                stp_file.write(stpcommands.getWeightString(wp[4*rnd:4*rnd + 4], 
                               wordsize, 0, w[rnd]) + "\n")

            stpcommands.setupWeightComputationSum(stp_file, weight, w, wordsize)

            # Rounds
            for rnd in range(rounds):
                if ((rnd) & 3) == 0:
                    # Small Swap
                    for perm in range(4):
                        self.setupRound(stp_file,
                                        x[4*rnd + perm],
                                        y[4*rnd + perm],
                                        z[4*rnd + perm],
                                        xtmp[4*rnd + perm],
                                        ytmp[4*rnd + perm],
                                        ztmp[4*rnd + perm],
                                        xsb[4*rnd + perm],
                                        ysb[4*rnd + perm],
                                        zsb[4*rnd + perm],
                                        wp[4*rnd + perm],
                                        wordsize)

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1)], xtmp[4*rnd + 1]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1)], ytmp[4*rnd]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1)], ztmp[4*rnd]))

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1) + 1], xtmp[4*rnd]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1) + 1], ytmp[4*rnd + 1]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1) + 1], ztmp[4*rnd + 1]))

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1) + 2], xtmp[4*rnd + 3]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1) + 2], ytmp[4*rnd + 2]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1) + 2], ztmp[4*rnd + 2]))

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1) + 3], xtmp[4*rnd + 2]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1) + 3], ytmp[4*rnd + 3]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1) + 3], ztmp[4*rnd + 3]))

                elif ((rnd) & 3) == 2:
                    # Big Swap
                    for perm in range(4):
                        self.setupRound(stp_file,
                                        x[4*rnd + perm],
                                        y[4*rnd + perm],
                                        z[4*rnd + perm],
                                        xtmp[4*rnd + perm],
                                        ytmp[4*rnd + perm],
                                        ztmp[4*rnd + perm],
                                        xsb[4*rnd + perm],
                                        ysb[4*rnd + perm],
                                        zsb[4*rnd + perm],
                                        wp[4*rnd + perm],
                                        wordsize)

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1)], xtmp[4*rnd + 2]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1)], ytmp[4*rnd]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1)], ztmp[4*rnd]))

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1) + 1], xtmp[4*rnd + 3]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1) + 1], ytmp[4*rnd + 1]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1) + 1], ztmp[4*rnd + 1]))

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1) + 2], xtmp[4*rnd]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1) + 2], ytmp[4*rnd + 2]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1) + 2], ztmp[4*rnd + 2]))

                    stp_file.write("ASSERT({} = {});\n".format(x[4*(rnd + 1) + 3], xtmp[4*rnd + 1]))
                    stp_file.write("ASSERT({} = {});\n".format(y[4*(rnd + 1) + 3], ytmp[4*rnd + 3]))
                    stp_file.write("ASSERT({} = {});\n".format(z[4*(rnd + 1) + 3], ztmp[4*rnd + 3]))
                else:
                    # No Swap
                    for perm in range(4):
                        self.setupRound(stp_file,
                                        x[4*rnd + perm],
                                        y[4*rnd + perm],
                                        z[4*rnd + perm],
                                        x[4*(rnd + 1) + perm],
                                        y[4*(rnd + 1) + perm],
                                        z[4*(rnd + 1) + perm],
                                        xsb[4*rnd + perm],
                                        ysb[4*rnd + perm],
                                        zsb[4*rnd + perm],
                                        wp[4*rnd + perm],
                                        wordsize)


            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, x + y + z, wordsize)

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            for char in parameters["blockedCharacteristics"]:
                stpcommands.blockCharacteristic(stp_file, char, wordsize)

            stpcommands.setupQuery(stp_file)

        return
Exemple #7
0
    def createSTP(self, stp_filename, parameters):
        """
        Creates an STP file for Keccak.
        """
        wordsize = parameters["wordsize"]
        rounds = parameters["rounds"]
        weight = parameters["sweight"]

        capacity = 160
        rate = (wordsize * 25) - capacity

        if "rate" in parameters:
            rate = parameters["rate"]

        if "capacity" in parameters:
            capacity = parameters["capacity"]

        assert (rate + capacity) == wordsize * 25
        
        with open(stp_filename, 'w') as stp_file:
            stp_file.write("% Input File for STP\n% Keccak w={} rate={} "
                           "capacity={}\n\n\n".format(wordsize, rate, capacity,
                                                      rounds))

            # Setup variables
            # 5x5 lanes of wordsize
            s = ["s{}{}{}".format(x, y, i) for i in range(rounds+1)
                 for y in range(5) for x in range(5)]

            b = ["b{}{}{}".format(x, y, i) for i in range(rounds)
                 for y in range(5) for x in range(5)]
            c = ["c{}{}".format(x, i) for i in range(rounds) for x in range(5)]
            d = ["d{}{}".format(x, i) for i in range(rounds) for x in range(5)]
            xin = ["xin{}{}{}".format(y, z, i) for i in range(rounds)
                   for y in range(5) for z in range (wordsize)]
            xout = ["xout{}{}{}".format(y, z, i) for i in range(rounds)
                    for y in range(5) for z in range (wordsize)]
            andOut = ["andOut{}{}{}".format(y, z, i) for i in range(rounds)
                      for y in range(5) for z in range (wordsize)]

	        # w = weight
            w = ["w{}".format(i) for i in range(rounds)]
            tmp = ["tmp{}{}{}".format(y, z, i) for i in range(rounds) 
                   for y in range(5) for z in range (wordsize)]
            stpcommands.setupVariables(stp_file, s, wordsize)
            stpcommands.setupVariables(stp_file, b, wordsize)
            stpcommands.setupVariables(stp_file, c, wordsize)
            stpcommands.setupVariables(stp_file, d, wordsize)
            stpcommands.setupVariables(stp_file, w, 16)
            stpcommands.setupVariables(stp_file, tmp, 5)
            stpcommands.setupWeightComputationSum(stp_file, weight, w, wordsize)
            stpcommands.setupVariables(stp_file, xin, 5)
            stpcommands.setupVariables(stp_file, xout, 5)
            stpcommands.setupVariables(stp_file, andOut, 5)

            # No all zero characteristic
            stpcommands.assertNonZero(stp_file, s, wordsize)

            # Fix variables for capacity, only works if rate/capacity is
            # multiple of wordsize.
            for i in range(rate // wordsize, (rate + capacity) // wordsize):
               stpcommands.assertVariableValue(stp_file, s[i],
                                               "0hex{}".format("0"*(wordsize // 4)))

            for rnd in range(rounds):
                self.setupKeccakRound(stp_file, rnd, s, b, c, d, wordsize, 
                                      tmp, w, xin, xout, andOut)

            for key, value in parameters["fixedVariables"].items():
                stpcommands.assertVariableValue(stp_file, key, value)

            stpcommands.setupQuery(stp_file)

        return