group_prover.sage 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # This code supports verifying group implementations which have branches
  2. # or conditional statements (like cmovs), by allowing each execution path
  3. # to independently set assumptions on input or intermediary variables.
  4. #
  5. # The general approach is:
  6. # * A constraint is a tuple of two sets of of symbolic expressions:
  7. # the first of which are required to evaluate to zero, the second of which
  8. # are required to evaluate to nonzero.
  9. # - A constraint is said to be conflicting if any of its nonzero expressions
  10. # is in the ideal with basis the zero expressions (in other words: when the
  11. # zero expressions imply that one of the nonzero expressions are zero).
  12. # * There is a list of laws that describe the intended behaviour, including
  13. # laws for addition and doubling. Each law is called with the symbolic point
  14. # coordinates as arguments, and returns:
  15. # - A constraint describing the assumptions under which it is applicable,
  16. # called "assumeLaw"
  17. # - A constraint describing the requirements of the law, called "require"
  18. # * Implementations are transliterated into functions that operate as well on
  19. # algebraic input points, and are called once per combination of branches
  20. # exectured. Each execution returns:
  21. # - A constraint describing the assumptions this implementation requires
  22. # (such as Z1=1), called "assumeFormula"
  23. # - A constraint describing the assumptions this specific branch requires,
  24. # but which is by construction guaranteed to cover the entire space by
  25. # merging the results from all branches, called "assumeBranch"
  26. # - The result of the computation
  27. # * All combinations of laws with implementation branches are tried, and:
  28. # - If the combination of assumeLaw, assumeFormula, and assumeBranch results
  29. # in a conflict, it means this law does not apply to this branch, and it is
  30. # skipped.
  31. # - For others, we try to prove the require constraints hold, assuming the
  32. # information in assumeLaw + assumeFormula + assumeBranch, and if this does
  33. # not succeed, we fail.
  34. # + To prove an expression is zero, we check whether it belongs to the
  35. # ideal with the assumed zero expressions as basis. This test is exact.
  36. # + To prove an expression is nonzero, we check whether each of its
  37. # factors is contained in the set of nonzero assumptions' factors.
  38. # This test is not exact, so various combinations of original and
  39. # reduced expressions' factors are tried.
  40. # - If we succeed, we print out the assumptions from assumeFormula that
  41. # weren't implied by assumeLaw already. Those from assumeBranch are skipped,
  42. # as we assume that all constraints in it are complementary with each other.
  43. #
  44. # Based on the sage verification scripts used in the Explicit-Formulas Database
  45. # by Tanja Lange and others, see http://hyperelliptic.org/EFD
  46. class fastfrac:
  47. """Fractions over rings."""
  48. def __init__(self,R,top,bot=1):
  49. """Construct a fractional, given a ring, a numerator, and denominator."""
  50. self.R = R
  51. if parent(top) == ZZ or parent(top) == R:
  52. self.top = R(top)
  53. self.bot = R(bot)
  54. elif top.__class__ == fastfrac:
  55. self.top = top.top
  56. self.bot = top.bot * bot
  57. else:
  58. self.top = R(numerator(top))
  59. self.bot = R(denominator(top)) * bot
  60. def iszero(self,I):
  61. """Return whether this fraction is zero given an ideal."""
  62. return self.top in I and self.bot not in I
  63. def reduce(self,assumeZero):
  64. zero = self.R.ideal(map(numerator, assumeZero))
  65. return fastfrac(self.R, zero.reduce(self.top)) / fastfrac(self.R, zero.reduce(self.bot))
  66. def __add__(self,other):
  67. """Add two fractions."""
  68. if parent(other) == ZZ:
  69. return fastfrac(self.R,self.top + self.bot * other,self.bot)
  70. if other.__class__ == fastfrac:
  71. return fastfrac(self.R,self.top * other.bot + self.bot * other.top,self.bot * other.bot)
  72. return NotImplemented
  73. def __sub__(self,other):
  74. """Subtract two fractions."""
  75. if parent(other) == ZZ:
  76. return fastfrac(self.R,self.top - self.bot * other,self.bot)
  77. if other.__class__ == fastfrac:
  78. return fastfrac(self.R,self.top * other.bot - self.bot * other.top,self.bot * other.bot)
  79. return NotImplemented
  80. def __neg__(self):
  81. """Return the negation of a fraction."""
  82. return fastfrac(self.R,-self.top,self.bot)
  83. def __mul__(self,other):
  84. """Multiply two fractions."""
  85. if parent(other) == ZZ:
  86. return fastfrac(self.R,self.top * other,self.bot)
  87. if other.__class__ == fastfrac:
  88. return fastfrac(self.R,self.top * other.top,self.bot * other.bot)
  89. return NotImplemented
  90. def __rmul__(self,other):
  91. """Multiply something else with a fraction."""
  92. return self.__mul__(other)
  93. def __div__(self,other):
  94. """Divide two fractions."""
  95. if parent(other) == ZZ:
  96. return fastfrac(self.R,self.top,self.bot * other)
  97. if other.__class__ == fastfrac:
  98. return fastfrac(self.R,self.top * other.bot,self.bot * other.top)
  99. return NotImplemented
  100. def __pow__(self,other):
  101. """Compute a power of a fraction."""
  102. if parent(other) == ZZ:
  103. if other < 0:
  104. # Negative powers require flipping top and bottom
  105. return fastfrac(self.R,self.bot ^ (-other),self.top ^ (-other))
  106. else:
  107. return fastfrac(self.R,self.top ^ other,self.bot ^ other)
  108. return NotImplemented
  109. def __str__(self):
  110. return "fastfrac((" + str(self.top) + ") / (" + str(self.bot) + "))"
  111. def __repr__(self):
  112. return "%s" % self
  113. def numerator(self):
  114. return self.top
  115. class constraints:
  116. """A set of constraints, consisting of zero and nonzero expressions.
  117. Constraints can either be used to express knowledge or a requirement.
  118. Both the fields zero and nonzero are maps from expressions to description
  119. strings. The expressions that are the keys in zero are required to be zero,
  120. and the expressions that are the keys in nonzero are required to be nonzero.
  121. Note that (a != 0) and (b != 0) is the same as (a*b != 0), so all keys in
  122. nonzero could be multiplied into a single key. This is often much less
  123. efficient to work with though, so we keep them separate inside the
  124. constraints. This allows higher-level code to do fast checks on the individual
  125. nonzero elements, or combine them if needed for stronger checks.
  126. We can't multiply the different zero elements, as it would suffice for one of
  127. the factors to be zero, instead of all of them. Instead, the zero elements are
  128. typically combined into an ideal first.
  129. """
  130. def __init__(self, **kwargs):
  131. if 'zero' in kwargs:
  132. self.zero = dict(kwargs['zero'])
  133. else:
  134. self.zero = dict()
  135. if 'nonzero' in kwargs:
  136. self.nonzero = dict(kwargs['nonzero'])
  137. else:
  138. self.nonzero = dict()
  139. def negate(self):
  140. return constraints(zero=self.nonzero, nonzero=self.zero)
  141. def __add__(self, other):
  142. zero = self.zero.copy()
  143. zero.update(other.zero)
  144. nonzero = self.nonzero.copy()
  145. nonzero.update(other.nonzero)
  146. return constraints(zero=zero, nonzero=nonzero)
  147. def __str__(self):
  148. return "constraints(zero=%s,nonzero=%s)" % (self.zero, self.nonzero)
  149. def __repr__(self):
  150. return "%s" % self
  151. def conflicts(R, con):
  152. """Check whether any of the passed non-zero assumptions is implied by the zero assumptions"""
  153. zero = R.ideal(map(numerator, con.zero))
  154. if 1 in zero:
  155. return True
  156. # First a cheap check whether any of the individual nonzero terms conflict on
  157. # their own.
  158. for nonzero in con.nonzero:
  159. if nonzero.iszero(zero):
  160. return True
  161. # It can be the case that entries in the nonzero set do not individually
  162. # conflict with the zero set, but their combination does. For example, knowing
  163. # that either x or y is zero is equivalent to having x*y in the zero set.
  164. # Having x or y individually in the nonzero set is not a conflict, but both
  165. # simultaneously is, so that is the right thing to check for.
  166. if reduce(lambda a,b: a * b, con.nonzero, fastfrac(R, 1)).iszero(zero):
  167. return True
  168. return False
  169. def get_nonzero_set(R, assume):
  170. """Calculate a simple set of nonzero expressions"""
  171. zero = R.ideal(map(numerator, assume.zero))
  172. nonzero = set()
  173. for nz in map(numerator, assume.nonzero):
  174. for (f,n) in nz.factor():
  175. nonzero.add(f)
  176. rnz = zero.reduce(nz)
  177. for (f,n) in rnz.factor():
  178. nonzero.add(f)
  179. return nonzero
  180. def prove_nonzero(R, exprs, assume):
  181. """Check whether an expression is provably nonzero, given assumptions"""
  182. zero = R.ideal(map(numerator, assume.zero))
  183. nonzero = get_nonzero_set(R, assume)
  184. expl = set()
  185. ok = True
  186. for expr in exprs:
  187. if numerator(expr) in zero:
  188. return (False, [exprs[expr]])
  189. allexprs = reduce(lambda a,b: numerator(a)*numerator(b), exprs, 1)
  190. for (f, n) in allexprs.factor():
  191. if f not in nonzero:
  192. ok = False
  193. if ok:
  194. return (True, None)
  195. ok = True
  196. for (f, n) in zero.reduce(numerator(allexprs)).factor():
  197. if f not in nonzero:
  198. ok = False
  199. if ok:
  200. return (True, None)
  201. ok = True
  202. for expr in exprs:
  203. for (f,n) in numerator(expr).factor():
  204. if f not in nonzero:
  205. ok = False
  206. if ok:
  207. return (True, None)
  208. ok = True
  209. for expr in exprs:
  210. for (f,n) in zero.reduce(numerator(expr)).factor():
  211. if f not in nonzero:
  212. expl.add(exprs[expr])
  213. if expl:
  214. return (False, list(expl))
  215. else:
  216. return (True, None)
  217. def prove_zero(R, exprs, assume):
  218. """Check whether all of the passed expressions are provably zero, given assumptions"""
  219. r, e = prove_nonzero(R, dict(map(lambda x: (fastfrac(R, x.bot, 1), exprs[x]), exprs)), assume)
  220. if not r:
  221. return (False, map(lambda x: "Possibly zero denominator: %s" % x, e))
  222. zero = R.ideal(map(numerator, assume.zero))
  223. nonzero = prod(x for x in assume.nonzero)
  224. expl = []
  225. for expr in exprs:
  226. if not expr.iszero(zero):
  227. expl.append(exprs[expr])
  228. if not expl:
  229. return (True, None)
  230. return (False, expl)
  231. def describe_extra(R, assume, assumeExtra):
  232. """Describe what assumptions are added, given existing assumptions"""
  233. zerox = assume.zero.copy()
  234. zerox.update(assumeExtra.zero)
  235. zero = R.ideal(map(numerator, assume.zero))
  236. zeroextra = R.ideal(map(numerator, zerox))
  237. nonzero = get_nonzero_set(R, assume)
  238. ret = set()
  239. # Iterate over the extra zero expressions
  240. for base in assumeExtra.zero:
  241. if base not in zero:
  242. add = []
  243. for (f, n) in numerator(base).factor():
  244. if f not in nonzero:
  245. add += ["%s" % f]
  246. if add:
  247. ret.add((" * ".join(add)) + " = 0 [%s]" % assumeExtra.zero[base])
  248. # Iterate over the extra nonzero expressions
  249. for nz in assumeExtra.nonzero:
  250. nzr = zeroextra.reduce(numerator(nz))
  251. if nzr not in zeroextra:
  252. for (f,n) in nzr.factor():
  253. if zeroextra.reduce(f) not in nonzero:
  254. ret.add("%s != 0" % zeroextra.reduce(f))
  255. return ", ".join(x for x in ret)
  256. def check_symbolic(R, assumeLaw, assumeAssert, assumeBranch, require):
  257. """Check a set of zero and nonzero requirements, given a set of zero and nonzero assumptions"""
  258. assume = assumeLaw + assumeAssert + assumeBranch
  259. if conflicts(R, assume):
  260. # This formula does not apply
  261. return None
  262. describe = describe_extra(R, assumeLaw + assumeBranch, assumeAssert)
  263. ok, msg = prove_zero(R, require.zero, assume)
  264. if not ok:
  265. return "FAIL, %s fails (assuming %s)" % (str(msg), describe)
  266. res, expl = prove_nonzero(R, require.nonzero, assume)
  267. if not res:
  268. return "FAIL, %s fails (assuming %s)" % (str(expl), describe)
  269. if describe != "":
  270. return "OK (assuming %s)" % describe
  271. else:
  272. return "OK"
  273. def concrete_verify(c):
  274. for k in c.zero:
  275. if k != 0:
  276. return (False, c.zero[k])
  277. for k in c.nonzero:
  278. if k == 0:
  279. return (False, c.nonzero[k])
  280. return (True, None)