Skip to content

Commit 5dc8ff0

Browse files
committed
Merge branch 'ana/flag-build-with-certifier' into certifier-update
2 parents 13111ff + e246521 commit 5dc8ff0

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

plutus-tx-plugin/plutus-tx-plugin.cabal

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ common os-support
5050
if (impl(ghcjs) || os(windows))
5151
buildable: False
5252

53+
flag certify
54+
default: False
55+
manual: True
56+
description: Build with the Agda-based compiler certification tool.
57+
5358
library
5459
import: lang, ghc-version-support, os-support
5560
hs-source-dirs: src
@@ -93,6 +98,13 @@ library
9398
, text
9499
, uniplate
95100

101+
if flag(certify)
102+
build-depends:
103+
, megaparsec
104+
, plutus-metatheory ^>=1.58
105+
106+
cpp-options: -DCERTIFY
107+
96108
executable gen-plugin-opts-doc
97109
import: lang, ghc-version-support, os-support
98110
main-is: GeneratePluginOptionsDoc.hs

plutus-tx-plugin/src/PlutusTx/Options.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE CPP #-}
22
-- editorconfig-checker-disable-file
3+
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE GADTs #-}
45
{-# LANGUAGE LambdaCase #-}
56
{-# LANGUAGE OverloadedStrings #-}
@@ -11,6 +12,7 @@
1112

1213
module PlutusTx.Options where
1314

15+
{- ORMOLU_DISABLE -}
1416
import PlutusCore.Error as PLC
1517
import PlutusCore.Parser as PLC
1618
import PlutusCore.Quote as PLC
@@ -44,6 +46,11 @@ import Prettyprinter
4446
import Text.Read (readMaybe)
4547
import Type.Reflection
4648

49+
#ifdef CERTIFY
50+
import Text.Megaparsec.Char (alphaNumChar, char, upperChar)
51+
import Control.Applicative (many, optional, (<|>))
52+
#endif
53+
4754
data PluginOptions = PluginOptions
4855
{ _posPlcTargetVersion :: PLC.Version
4956
, _posDoTypecheck :: Bool
@@ -82,6 +89,9 @@ data PluginOptions = PluginOptions
8289
-- Which effectively ignores the trace text.
8390
_posRemoveTrace :: Bool
8491
, _posDumpCompilationTrace :: Bool
92+
#ifdef CERTIFY
93+
, _posCertify :: Maybe String
94+
#endif
8595
}
8696

8797
makeLenses ''PluginOptions
@@ -314,6 +324,20 @@ pluginOptions =
314324
, let k = "dump-compilation-trace"
315325
desc = "Dump compilation trace for debugging"
316326
in (k, PluginOption typeRep (setTrue k) posDumpCompilationTrace desc [])
327+
#ifdef CERTIFY
328+
, let k = "certify"
329+
desc =
330+
"Produce a certificate for the compiled program, with the given name. "
331+
<> "This certificate provides evidence that the compiler optimizations have "
332+
<> "preserved the functional behavior of the original program. "
333+
<> "Currently, this is only supported for the UPLC compilation pipeline."
334+
p =
335+
optional $ do
336+
firstC <- upperChar
337+
rest <- many (alphaNumChar <|> char '_' <|> char '\\')
338+
pure (firstC : rest)
339+
in (k, PluginOption typeRep (plcParserOption p k) posCertify desc [])
340+
#endif
317341
]
318342

319343
flag :: (a -> a) -> OptionKey -> Maybe OptionValue -> Validation ParseError (a -> a)
@@ -387,6 +411,9 @@ defaultPluginOptions =
387411
, _posPreserveLogging = True
388412
, _posRemoveTrace = False
389413
, _posDumpCompilationTrace = False
414+
#ifdef CERTIFY
415+
, _posCertify = Nothing
416+
#endif
390417
}
391418

392419
processOne

plutus-tx-plugin/src/PlutusTx/Plugin.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
{-# LANGUAGE LambdaCase #-}
@@ -12,6 +13,7 @@
1213

1314
module PlutusTx.Plugin (plugin, plc) where
1415

16+
{- ORMOLU_DISABLE -}
1517
import PlutusPrelude
1618
import PlutusTx.AsData.Internal qualified
1719
import PlutusTx.Bool ((&&), (||))
@@ -86,6 +88,11 @@ import Prettyprinter qualified as PP
8688
import System.IO (openBinaryTempFile)
8789
import System.IO.Unsafe (unsafePerformIO)
8890

91+
#ifdef CERTIFY
92+
import Certifier (runCertifier, mkCertifier, prettyCertifierError, prettyCertifierSuccess)
93+
import System.IO (hPutStrLn, stderr)
94+
#endif
95+
8996
data PluginCtx = PluginCtx
9097
{ pcOpts :: PluginOptions
9198
, pcFamEnvs :: GHC.FamInstEnvs
@@ -637,7 +644,22 @@ runCompiler moduleName opts expr = do
637644
modifyError PLC.TypeErrorE $
638645
PLC.inferTypeOfProgram plcTcConfig (plcP $> annMayInline)
639646

647+
#ifdef CERTIFY
648+
let optCertify = opts ^. posCertify
649+
(uplcP, simplTrace) <- flip runReaderT plcOpts $ PLC.compileProgramWithTrace plcP
650+
liftIO $ case optCertify of
651+
Just certName -> do
652+
result <- runCertifier $ mkCertifier simplTrace certName
653+
case result of
654+
Right certSuccess ->
655+
hPutStrLn stderr $ prettyCertifierSuccess certSuccess
656+
Left err ->
657+
hPutStrLn stderr $ prettyCertifierError err
658+
Nothing -> pure ()
659+
#else
640660
(uplcP, _) <- flip runReaderT plcOpts $ PLC.compileProgramWithTrace plcP
661+
#endif
662+
641663
dbP <- liftExcept $ modifyError PLC.FreeVariableErrorE $ traverseOf UPLC.progTerm UPLC.deBruijnTerm uplcP
642664
when (opts ^. posDumpUPlc) . liftIO $
643665
dumpFlat

0 commit comments

Comments
 (0)