From a9c60f98280ab2678b1e72721dcaf5de72cb2cb7 Mon Sep 17 00:00:00 2001 From: Andrew Kent Date: Mon, 19 Apr 2021 11:07:01 -0700 Subject: [PATCH 1/4] feat(rpc): safe for python api --- cryptol-remote-api/python/cryptol/__init__.py | 15 +++++++++++++-- .../python/tests/cryptol/test_AES.py | 4 ++-- .../python/tests/cryptol/test_cryptol_api.py | 9 +++++++++ cryptol-remote-api/src/CryptolServer/Sat.hs | 19 +++++++++++++------ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/cryptol-remote-api/python/cryptol/__init__.py b/cryptol-remote-api/python/cryptol/__init__.py index 3a2ceecb6..e5d1e8c0a 100644 --- a/cryptol-remote-api/python/cryptol/__init__.py +++ b/cryptol-remote-api/python/cryptol/__init__.py @@ -200,7 +200,7 @@ def process_result(self, res : Any) -> Any: if res['result'] == 'unsatisfiable': if self.qtype == 'sat': return False - elif self.qtype == 'prove': + elif self.qtype == 'prove' or self.qtype == 'safe': return True else: raise ValueError("Unknown prove/sat query type: " + self.qtype) @@ -212,7 +212,7 @@ def process_result(self, res : Any) -> Any: for m in res['models'] for arg in m] else: - raise ValueError("Unknown sat result " + str(res)) + raise ValueError("Unknown prove or sat result " + str(res)) class CryptolProve(CryptolProveSat): def __init__(self, connection : HasProtocolState, expr : Any, solver : solver.Solver) -> None: @@ -222,6 +222,10 @@ class CryptolSat(CryptolProveSat): def __init__(self, connection : HasProtocolState, expr : Any, solver : solver.Solver, count : int) -> None: super(CryptolSat, self).__init__(connection, 'sat', expr, solver, count) +class CryptolSafe(CryptolProveSat): + def __init__(self, connection : HasProtocolState, expr : Any, solver : solver.Solver) -> None: + super(CryptolSafe, self).__init__(connection, 'safe', expr, solver, 1) + class CryptolNames(argo.Command): def __init__(self, connection : HasProtocolState) -> None: super(CryptolNames, self).__init__('visible names', {}, connection) @@ -456,6 +460,13 @@ def prove(self, expr : Any, solver : solver.Solver = solver.Z3) -> argo.Command: self.most_recent_result = CryptolProve(self, expr, solver) return self.most_recent_result + def safe(self, expr : Any, solver : solver.Solver = solver.Z3) -> argo.Command: + """Check via an external SMT solver that the given term is safe for all inputs, + which means it cannot encounter a run-time error. + """ + self.most_recent_result = CryptolSafe(self, expr, solver) + return self.most_recent_result + def names(self) -> argo.Command: """Discover the list of names currently in scope in the current context.""" self.most_recent_result = CryptolNames(self) diff --git a/cryptol-remote-api/python/tests/cryptol/test_AES.py b/cryptol-remote-api/python/tests/cryptol/test_AES.py index 67e9cd9f3..9db6c6971 100644 --- a/cryptol-remote-api/python/tests/cryptol/test_AES.py +++ b/cryptol-remote-api/python/tests/cryptol/test_AES.py @@ -28,8 +28,8 @@ def test_AES(self): decrypted_ct = c.call("aesDecrypt", (ct, key)).result() self.assertEqual(pt, decrypted_ct) - # c.safe("aesEncrypt") - # c.safe("aesDecrypt") + self.assertTrue(c.safe("aesEncrypt")) + self.assertTrue(c.safe("aesDecrypt")) self.assertTrue(c.check("AESCorrect").result().success) # c.prove("AESCorrect") # probably takes too long for this script...? diff --git a/cryptol-remote-api/python/tests/cryptol/test_cryptol_api.py b/cryptol-remote-api/python/tests/cryptol/test_cryptol_api.py index c51500325..208fb7cde 100644 --- a/cryptol-remote-api/python/tests/cryptol/test_cryptol_api.py +++ b/cryptol-remote-api/python/tests/cryptol/test_cryptol_api.py @@ -138,6 +138,15 @@ def test_check(self): self.assertEqual(len(res.args), 1) self.assertIsInstance(res.error_msg, str) + def test_safe(self): + c = self.c + res = c.safe("\\x -> x==(x:[8])").result() + self.assertTrue(res) + + res = c.safe("\\x -> x / (x:[8])").result() + self.assertEqual(res, [BV(size=8, value=0)]) + + def test_many_usages_one_connection(self): c = self.c for i in range(0,100): diff --git a/cryptol-remote-api/src/CryptolServer/Sat.hs b/cryptol-remote-api/src/CryptolServer/Sat.hs index 029e1171d..a4224762d 100644 --- a/cryptol-remote-api/src/CryptolServer/Sat.hs +++ b/cryptol-remote-api/src/CryptolServer/Sat.hs @@ -157,6 +157,7 @@ instance FromJSON ProveSatParams where \case "sat" -> pure (SatQuery numResults) "prove" -> pure ProveQuery + "safe" -> pure SafetyQuery _ -> empty) num v = ((JSON.withText "all" $ \t -> if t == "all" then pure AllSat else empty) v) <|> @@ -174,17 +175,23 @@ instance Doc.DescribedParams ProveSatParams where ++ (concat (map (\p -> [Doc.Literal (T.pack p), Doc.Text ", "]) proverNames)) ++ [Doc.Text "."])) , ("expression", - Doc.Paragraph [Doc.Text "The predicate (i.e., function) to check for satisfiability; " - , Doc.Text "must be a monomorphic function with return type Bit." ]) + Doc.Paragraph [ Doc.Text "The function to check for validity, satisfiability, or safety" + , Doc.Text " depending on the specified value for " + , Doc.Literal "query type" + , Doc.Text ". For validity and satisfiability checks, the function must be a predicate" + , Doc.Text " (i.e., monomorphic function with return type Bit)." + ]) , ("result count", Doc.Paragraph [Doc.Text "How many satisfying results to search for; either a positive integer or " - , Doc.Literal "all", Doc.Text"."]) + , Doc.Literal "all", Doc.Text". Only affects satisfiability checks."]) , ("query type", - Doc.Paragraph [ Doc.Text "Whether to attempt to prove (" + Doc.Paragraph [ Doc.Text "Whether to attempt to prove the predicate is true for all possible inputs (" , Doc.Literal "prove" - , Doc.Text ") or satisfy (" + , Doc.Text "), find some inputs which make the predicate true (" , Doc.Literal "sat" - , Doc.Text ") the predicate." + , Doc.Text "), or prove a function is safe (" + , Doc.Literal "safe" + , Doc.Text ")." ] ) ] From 0abd60bf585ea0fb911441dc9810d3f23936ca49 Mon Sep 17 00:00:00 2001 From: Andrew Kent Date: Wed, 21 Apr 2021 16:14:36 -0700 Subject: [PATCH 2/4] refactor: use enum for smt query type --- cryptol-remote-api/python/cryptol/__init__.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cryptol-remote-api/python/cryptol/__init__.py b/cryptol-remote-api/python/cryptol/__init__.py index e5d1e8c0a..fa04955ca 100644 --- a/cryptol-remote-api/python/cryptol/__init__.py +++ b/cryptol-remote-api/python/cryptol/__init__.py @@ -4,6 +4,7 @@ import base64 import os +from enum import Enum from dataclasses import dataclass from distutils.spawn import find_executable from typing import Any, List, NoReturn, Optional, Union @@ -184,8 +185,13 @@ def __init__(self, connection : HasProtocolState, expr : Any) -> None: def process_result(self, res : Any) -> Any: return res['type schema'] +class SmtQueryType(str, Enum): + PROVE = 'prove' + SAFE = 'safe' + SAT = 'sat' + class CryptolProveSat(argo.Command): - def __init__(self, connection : HasProtocolState, qtype : str, expr : Any, solver : solver.Solver, count : Optional[int]) -> None: + def __init__(self, connection : HasProtocolState, qtype : SmtQueryType, expr : Any, solver : solver.Solver, count : Optional[int]) -> None: super(CryptolProveSat, self).__init__( 'prove or satisfy', {'query type': qtype, @@ -198,9 +204,9 @@ def __init__(self, connection : HasProtocolState, qtype : str, expr : Any, solve def process_result(self, res : Any) -> Any: if res['result'] == 'unsatisfiable': - if self.qtype == 'sat': + if self.qtype == SmtQueryType.SAT: return False - elif self.qtype == 'prove' or self.qtype == 'safe': + elif self.qtype == SmtQueryType.PROVE or self.qtype == SmtQueryType.SAT: return True else: raise ValueError("Unknown prove/sat query type: " + self.qtype) @@ -212,19 +218,19 @@ def process_result(self, res : Any) -> Any: for m in res['models'] for arg in m] else: - raise ValueError("Unknown prove or sat result " + str(res)) + raise ValueError("Unknown SMT result: " + str(res)) class CryptolProve(CryptolProveSat): def __init__(self, connection : HasProtocolState, expr : Any, solver : solver.Solver) -> None: - super(CryptolProve, self).__init__(connection, 'prove', expr, solver, 1) + super(CryptolProve, self).__init__(connection, SmtQueryType.PROVE, expr, solver, 1) class CryptolSat(CryptolProveSat): def __init__(self, connection : HasProtocolState, expr : Any, solver : solver.Solver, count : int) -> None: - super(CryptolSat, self).__init__(connection, 'sat', expr, solver, count) + super(CryptolSat, self).__init__(connection, SmtQueryType.SAT, expr, solver, count) class CryptolSafe(CryptolProveSat): def __init__(self, connection : HasProtocolState, expr : Any, solver : solver.Solver) -> None: - super(CryptolSafe, self).__init__(connection, 'safe', expr, solver, 1) + super(CryptolSafe, self).__init__(connection, SmtQueryType.SAFE, expr, solver, 1) class CryptolNames(argo.Command): def __init__(self, connection : HasProtocolState) -> None: @@ -273,7 +279,7 @@ def connect(command : Optional[str]=None, :param cryptol_path: A replacement for the contents of the ``CRYPTOLPATH`` environment variable (if provided). - :param url: A URL at which to connect to an already running Cryptol + :param url: A URL at which to connect to an already running Cryptol HTTP server. :param reset_server: If ``True``, the server that is connected to will be From 3088af11b191351393c0dd9d79f1281f60531d6c Mon Sep 17 00:00:00 2001 From: Andrew Kent Date: Thu, 22 Apr 2021 08:48:35 -0700 Subject: [PATCH 3/4] Update cryptol-remote-api/python/cryptol/__init__.py Co-authored-by: Ryan Scott --- cryptol-remote-api/python/cryptol/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptol-remote-api/python/cryptol/__init__.py b/cryptol-remote-api/python/cryptol/__init__.py index fa04955ca..75ee13e4d 100644 --- a/cryptol-remote-api/python/cryptol/__init__.py +++ b/cryptol-remote-api/python/cryptol/__init__.py @@ -209,7 +209,7 @@ def process_result(self, res : Any) -> Any: elif self.qtype == SmtQueryType.PROVE or self.qtype == SmtQueryType.SAT: return True else: - raise ValueError("Unknown prove/sat query type: " + self.qtype) + raise ValueError("Unknown SMT query type: " + self.qtype) elif res['result'] == 'invalid': return [from_cryptol_arg(arg['expr']) for arg in res['counterexample']] From df3d9360ebdb69a856c6026ab6a9c985861ac8af Mon Sep 17 00:00:00 2001 From: Andrew Kent Date: Thu, 22 Apr 2021 08:48:42 -0700 Subject: [PATCH 4/4] Update cryptol-remote-api/python/cryptol/__init__.py Co-authored-by: Ryan Scott --- cryptol-remote-api/python/cryptol/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptol-remote-api/python/cryptol/__init__.py b/cryptol-remote-api/python/cryptol/__init__.py index 75ee13e4d..b07c2d045 100644 --- a/cryptol-remote-api/python/cryptol/__init__.py +++ b/cryptol-remote-api/python/cryptol/__init__.py @@ -206,7 +206,7 @@ def process_result(self, res : Any) -> Any: if res['result'] == 'unsatisfiable': if self.qtype == SmtQueryType.SAT: return False - elif self.qtype == SmtQueryType.PROVE or self.qtype == SmtQueryType.SAT: + elif self.qtype == SmtQueryType.PROVE or self.qtype == SmtQueryType.SAFE: return True else: raise ValueError("Unknown SMT query type: " + self.qtype)