Skip to main content
 首页 » 编程设计

python之Z3py:Float32() 和 BitVecSort(32) 之间的强制和值提取

2025年01月19日9Free-Thinker

我目前正在结合位向量试验浮点 (FP) 理论;我正在使用 Z3 4.6.0.0 .

我发现有关使用 FP API 的文档有限(似乎唯一真正成功的是 z3.py 本身),所以我尝试提供“完整”示例,以演示我如何使用(相信)应该使用 API。

这是我第一次尝试在 Z3py 中使用 FP 理论:

#!/usr/bin/env python 
 
from z3 import * 
 
s = Solver() 
 
# 
# Create our value 
# 
to_find = "-12.345" 
fp_val = FPVal(to_find, Float32()) 
 
# 
# Create our variable 
# 
fp_var = Const("fp_var", Float32()) 
 
# 
# Create the constraints 
# 
s.add(fp_var == fp_val) 
 
assert s.check() == sat, "assertion never fails because the instance is SAT" 
 
m = s.model() 
 
# 
# Prints -12.3449993134 
# 
print eval(str(m.eval(fp_var, model_completion=True))) 
 
# EOF 

如果你运行这个例子,它会按预期工作,我们确实得到了 fp_var等于(我假设)最接近 -12.345 的 float (到目前为止一切顺利;除了使用 Python 的 eval 来获取 Python float 的值)。

我的下一步是尝试将浮点值强制转换为位向量,同时检查非整数值:

#!/usr/bin/env python 
 
from z3 import * 
 
s = Solver() 
 
# 
# Create our value 
# 
to_find = "-12.345" 
fp_val = FPVal(to_find, Float32()) 
 
# 
# Create our variable 
# 
bv_var = Const("bv_var", BitVecSort(32)) 
 
# 
# We now use a "cast" to allow us to do the floating point comparison 
# 
fp_var = fpSignedToFP(RNE(), bv_var, Float32()) 
 
# 
# Create the constraints 
# 
s.add(fp_var == fp_val) 
 
# 
# This is UNSAT because fpSignedToFP only supports _integers_! 
# 
assert s.check() == unsat, "instance is UNSAT, so assertion doesn't fail" 
 
# EOF 

在此示例中,我们尝试将位向量“转换”为浮点值(使用 fpSignedToFP ),然后断言浮点值等于我们正在寻找的值。然而,这与 Z3 的文档相匹配,我们得到 UNSAT,因为 fpSignedToFP仅支持整数。

然后我开始“发挥创意”,看看我是否可以对 fpToSBV 使用传递性API 调用,没有声明它具有仅限于整数 float 的限制:

#!/usr/bin/env python 
 
from z3 import * 
 
print get_full_version() 
 
s = Solver() 
 
# 
# Create our value 
# 
to_find = "-12.345" 
fp_val = FPVal(to_find, Float32()) 
 
# 
# Create our variable 
# 
bv_var = Const("bv_var", BitVecSort(32)) 
 
# 
# We now create an additional, ancillary variable 
# 
fp_var = Const("fp_var", Float32()) 
 
# 
# Create the constraints 
# 
s.add(fp_var == fp_val) 
s.add(bv_var == fpToSBV(RNE(), fp_var, BitVecSort(32))) 
 
assert s.check() == sat, "this example works" 
 
m = s.model() 
 
# 
# Prints -12.3449993134 
# 
print eval(str(m.eval(fp_var, model_completion=True))) 
 
# 
# To read out the value from the BV, we create a new variable, assert it is 
# the same as the originating BV, and then extract our new variable 
# 
eval_fp = Const("eval_fp", Float32()) 
s.add(bv_var == fpToSBV(RNE(), eval_fp, BitVecSort(32))) 
 
assert s.check() == sat, "this cannot change the satisfiability" 
 
m = s.model() 
 
# 
# Prints +oo True True 
# 
print str(m.eval(eval_fp, model_completion=True)), 
print m.eval(eval_fp, model_completion=True).isInf(), 
print m.eval(eval_fp, model_completion=True).isPositive() 
 
# EOF 

稍微解释一下这个例子:我们像往常一样对我们的问题进行编码,而不是为我们的浮点表达式创建一个项,我们创建一个全新的常量,并断言我们的位向量等于浮点值,通过fpToSBV .我们对希望找到的值的断言与我们的浮点值相对。如果这个模型是可满足的,我们然后创建另一个浮点常量,它指向我们的原始位向量,我们尝试提取该常量的值。

这给出了这样一个链:

  • bv_var == fpToSBV(fp_var)
  • fp_var == -12.345
  • fpToSBV(eval_fp) == bv_var

我曾希望通过传递性为 fp_var 赋予相同的值和 eval_fp .然而,虽然fp_var得到正确的值,eval_fp结果是正无穷大!

我还尝试使用 fpToSBV 进行试验和 fpSignedToFP一起尝试看看这是否可行(由于 fpSignedToFP 中的限制,我对此持怀疑态度):

#!/usr/bin/env python 
 
from z3 import * 
 
s = Solver() 
 
# 
# Create our value 
# 
to_find = "-12.345" 
bv_val = fpToSBV(RNE(), FPVal(to_find, Float32()), BitVecSort(32)) 
 
# 
# Create our variable 
# 
bv_var = Const("bv_var", BitVecSort(32)) 
 
# 
# Create the constraints 
# 
s.add(bv_var == bv_val) 
 
assert s.check() == sat, "this example works" 
 
m = s.model() 
 
# 
# Floating point constant to evaluate 
# 
eval_fp = fpSignedToFP(RNE(), bv_var, Float32()) 
 
# 
# Prints -12.0 
# 
print eval(str(m.eval(eval_fp, model_completion=True))) 
 
# EOF 

这取得了最大的成功,并且确实给出了一个整数 close 我们约束的浮点值(即 -12.0)。但是,它仍然不包含小数点后的值。

所以,我的问题是:在 Z3 中,是否可以将浮点值强制转换为位向量,然后再次“退出”?如果可以,应该怎么做?或者,位向量/浮点运算是否仅“部分解释”(如 int2bv 和 friend )?

更新

经过更多实验后,我发现以下方法有效(使用 Z3 特定扩展 fpToIEEEBV ):

#!/usr/bin/env python 
 
from z3 import * 
 
s = Solver() 
 
# 
# Create our value 
# 
to_find = "-12.345" 
fp_val = FPVal(to_find, Float32()) 
 
# 
# Create our variable 
# 
bv_var = Const("bv_var", BitVecSort(32)) 
 
# 
# Convert the value to check to a BV 
# 
bv_val = fpToIEEEBV(fp_val) 
 
# 
# Create the constraints 
# 
s.add(bv_var == bv_val) 
 
assert s.check() == sat, "this example is SAT, so don't see this" 
 
m = s.model() 
 
# 
# Evaluation expression 
# 
eval_expr = fpBVToFP(bv_var, Float32()) 
 
# 
# Prints -12.3449993134 
# 
print eval(str(m.eval(eval_expr))) 
 
# EOF 

这是处理这个问题的正确方法吗?

请您参考如下方法:

撇开 Python API 的工作原理不谈,这已经在浮点逻辑的定义中指定,参见此处:http://smtlib.cs.uiowa.edu/theories-FloatingPoint.shtml

请特别注意其中一些函数的偏向性:

"All fp.to_* functions are unspecified for NaN and infinity input values. In addition, fp.to_ubv and fp.to_sbv are unspecified for finite number inputs that are out of range (which includes all negative numbers for fp.to_ubv). This means for instance that the formula

(= (fp.to_real (_ NaN 8 24)) (fp.to_real (fp c1 c2 c3)))  

is satisfiable in this theory for all binary constants c1, c2, and c3 (of the proper sort). "

因此,您正在做的事情(待定的 Python 特定方面)符合标准,并带有上述警告。

使用交换格式

对于与 IEEE754 交换格式之间的转换,该逻辑提供了一种将给定位向量转换为相应 float 的方法:

; from single bitstring representation in IEEE 754-2008 interchange format, ; with m = eb + sb ((_ to_fp eb sb) (_ BitVec m) (_ FloatingPoint eb sb))

在另一个方向上,逻辑是这样说的:

There is no function for converting from (_ FloatingPoint eb sb) to the corresponding IEEE 754-2008 binary format, as a bit vector (_ BitVec m) with m = eb + sb, because (_ NaN eb sb) has multiple, well-defined representations. Instead, an encoding of the kind below is recommended, where f is a term of sort (_ FloatingPoint eb sb):

(declare-fun b () (_ BitVec m))
(assert (= ((_ to_fp eb sb) b) f))

同样,所有这些都在 http://smtlib.cs.uiowa.edu/theories-FloatingPoint.shtml 中