python - Z3:如何从8位数组中选择4个字节?

标签 python z3

使用 Python Z3,我有一个字节数组,并且可以使用 Select 读取 1 个字节,如下所示。

MI = BitVecSort(32)
MV = BitVecSort(8)
Mem = Array('Mem', MI, MV)

pmt = BitVec('pmt', 32)
pmt2 = BitVec('pmt2', 8)

g = True
g = And(g, pmt2 == Select(Mem, pmt))

到目前为止,一切正常。但是,现在我想从 Mem 数组中读取 4 个字节,如下所示。

t3 = BitVec('t3', 32)
g = And(g, t3 == Select(Mem, pmt))

事实证明这是错误的,因为 t3 是 32 位,而不是 8 位,而 Mem 是 8 位数组。

问题是:如何使用 Select 读出 4 个字节,而不是上例中的 1 个字节?

我想我可以创建一个新函数来读出 4 个字节,比如说 Select4(),但我不知道如何在 Z3 python 中创建一个函数。

非常感谢!

最佳答案

我们可以将Select4定义为

def Select4(M, I):
  return Concat(Select(M, I + 3), Select(M, I + 2), Select(M, I+1), Select(M, I))

操作Concat本质上是附加四个位向量。 Z3还支持Extract操作。这两个操作可用于对 C 等编程语言中可用的转换操作进行编码。

这是完整的示例(也可以在线获取 here ):

MI = BitVecSort(32)
MV = BitVecSort(8)
Mem = Array('Mem', MI, MV)

pmt = BitVec('pmt', 32)
pmt2 = BitVec('pmt2', 8)

def Select4(M, I):
  return Concat(Select(M, I + 3), Select(M, I + 2), Select(M, I+1), Select(M, I))

g = True
g = And(g, pmt2 == Select(Mem, pmt))
t3 = BitVec('t3', 32)
g = And(g, t3 == Select4(Mem, pmt))

solve(g, pmt2 > 10)

关于python - Z3:如何从8位数组中选择4个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15926947/

相关文章:

Z3 使用和不使用推送/弹出返回不同的答案?

c++ - Z3:如何在不使用硬编码索引的情况下从model()访问变量?

python - PIL : Image resizing : Algorithm similar to firefox's

python - Django ManyToManyField 是否创建具有冗余索引的表?

python - 无法在 z3 python 上验证 : ! m_var2expr.empty()

z3 - 将 Z3 用于 MAX-SAT 的问题

z3 - 模型无法令人满意的性能问题

python - 在公差范围内查找 Python 中两个矩阵的交集?

python - 调整列表的 numpy 数组的大小,以便所有列表都具有相同的长度,并且可以正确推断 numpy 数组的 dtype

python - 简短(且有用)的 python 片段