-- | Proof certificates and determinism (plan T26c exit gate, ledger D92): a run without the -- proof cache accepts committed certificates by re-checking their cores; an edited core or -- key is rejected with B8008 and the check proved by search; the outcome does not depend on -- the number of solver sessions. module Verada.Proof.CertificateSpec (spec) where import Verada.Cli (Environment (..), runCli) import Verada.Cli qualified as Cli (Outcome (..)) import Data.List (isPrefixOf) import Data.Map.Strict qualified as Map import Data.Text (Text) import Data.Text qualified as Text import Data.Text.IO qualified as TextIO import System.Directory (copyFile, createDirectoryIfMissing, getTemporaryDirectory, listDirectory, removePathForcibly) import System.Exit (ExitCode (..)) import System.FilePath (()) import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy) environment :: Environment environment = Environment {stderrIsTerminal = False, stdinIsTerminal = False, noColour = True, surface = Nothing, buffers = Map.empty} -- | A fresh copy of corpus-l1 in its own directory, with no proof cache or certificates. corpus :: String -> IO FilePath corpus name = do tmp <- getTemporaryDirectory let dir = tmp "verada-certificates" name removePathForcibly dir createDirectoryIfMissing True (dir "src") copyFile "test/corpus-l1/verada.toml" (dir "verada.toml") files <- listDirectory "test/corpus-l1/src" mapM_ (\f -> copyFile ("test/corpus-l1/src" f) (dir "src" f)) files pure dir prove :: FilePath -> [String] -> IO Cli.Outcome prove dir extra = runCli environment (["prove", "--certificates", "--project", dir "verada.toml"] <> extra) -- | Forget everything but the certificates, as another machine would have them (the proofs -- kept with their facts and the digests are caches too: D208, D209). withoutCache :: FilePath -> IO () withoutCache dir = mapM_ (\f -> removePathForcibly (dir ".verada" f)) ["proof-cache", "fact-proofs", "digests", "lib", "obj"] -- | Change the first certificate line that starts with a prefix. editFirst :: FilePath -> Text -> (Text -> Text) -> IO () editFirst dir prefix change = do let certs = dir ".verada" "certs" files <- listDirectory certs let go = \case [] -> pure () f : more -> do text <- TextIO.readFile (certs f) let (before, after) = break (prefix `Text.isPrefixOf`) (Text.lines text) case after of l : rest -> TextIO.writeFile (certs f) (Text.unlines (before <> [change l] <> rest)) [] -> go more go files spec :: Spec spec = describe "proof certificates" do it "are accepted on another machine by re-checking their cores, without search" do dir <- corpus "replay" first <- prove dir [] first.exitCode `shouldBe` ExitSuccess withoutCache dir second <- prove dir [] second.exitCode `shouldBe` ExitSuccess second.stdout `shouldSatisfy` Text.isInfixOf "41 by the solver, 41 of them replayed from certificates" -- (each names the solver that found its cores: provenance, G15) certificates <- listDirectory (dir ".verada" "certs") texts <- mapM (TextIO.readFile . ((dir ".verada" "certs") )) certificates texts `shouldSatisfy` all (\t -> any ("solver Z3 version" `Text.isPrefixOf`) (Text.lines t)) it "and `veradic --version` names the solvers found (G15)" do outcome <- runCli environment ["--version"] outcome.exitCode `shouldBe` ExitSuccess outcome.stdout `shouldSatisfy` Text.isPrefixOf "veradic " outcome.stdout `shouldSatisfy` Text.isInfixOf "z3: Z3 version" it "reject an edited core with B8008, and prove the check by search" do dir <- corpus "edited-core" _ <- prove dir [] withoutCache dir -- a core that names a hypothesis no check has editFirst dir "core " (\l -> Text.unwords (take 1 (Text.words l) <> [Text.replicate 64 "0"] <> drop 1 (Text.words l))) again <- prove dir [] again.exitCode `shouldBe` ExitSuccess again.stderr `shouldSatisfy` Text.isInfixOf "warning[B8008]: a proof certificate was rejected: its core does not prove this check" again.stdout `shouldSatisfy` Text.isInfixOf "40 of them replayed from certificates" it "reject an edited key with B8008" do dir <- corpus "edited-key" _ <- prove dir [] withoutCache dir editFirst dir "vc " (\l -> case Text.words l of tag : key : rest -> Text.unwords (tag : Text.reverse key : rest) ws -> Text.unwords ws) again <- prove dir [] again.exitCode `shouldBe` ExitSuccess again.stderr `shouldSatisfy` Text.isInfixOf "matches no check of this program" it "give the same outcome with one solver session and with eight" do one <- corpus "jobs-1" eight <- corpus "jobs-8" a <- runCli environment ["prove", "--no-cache", "-j", "1", "--project", one "verada.toml"] b <- runCli environment ["prove", "--no-cache", "-j", "8", "--project", eight "verada.toml"] a.stdout `shouldBe` b.stdout [l | l <- lines (Text.unpack a.stderr), not ("-->" `isPrefixOf` dropWhile (== ' ') l)] `shouldBe` [l | l <- lines (Text.unpack b.stderr), not ("-->" `isPrefixOf` dropWhile (== ' ') l)]