Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verified compilation certification component #6413

Merged
merged 59 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
dfde178
WIP
kwxm Aug 13, 2024
afa23a1
Merge branch 'master' into kwxm/metatheory/fix-test2
kwxm Aug 13, 2024
21607ed
Fix broken metatheory tests using textual comparison
kwxm Aug 14, 2024
f18ebe7
Refactor detailed tests
kwxm Aug 14, 2024
32c5a26
Refactor detailed tests
kwxm Aug 14, 2024
9f17fba
Re-enable plutus-metatheory:test2 in CI
kwxm Aug 14, 2024
4d32e29
Add raw certification component
ana-pantilie Aug 14, 2024
540b95b
Merge branch 'master' into kwxm/metatheory/fix-test2
kwxm Aug 14, 2024
225ac43
Fixes in project.nix
kwxm Aug 14, 2024
aa67c66
Remove unused files
kwxm Aug 14, 2024
16d004c
Remove the preCheck hack from plutus-metatheory.components.tests.test…
zeme-wana Aug 14, 2024
68652b3
Extra comments
kwxm Aug 14, 2024
5411479
Merge branch 'kwxm/metatheory/fix-test2' of github.com:IntersectMBO/p…
kwxm Aug 14, 2024
b3d582b
WIP
kwxm Aug 13, 2024
a2dfbcd
Fix broken metatheory tests using textual comparison
kwxm Aug 14, 2024
602d274
Refactor detailed tests
kwxm Aug 14, 2024
48543fd
Refactor detailed tests
kwxm Aug 14, 2024
2f43b7b
Re-enable plutus-metatheory:test2 in CI
kwxm Aug 14, 2024
526e3eb
Fixes in project.nix
kwxm Aug 14, 2024
5d177dc
Remove unused files
kwxm Aug 14, 2024
8fe533f
Extra comments
kwxm Aug 14, 2024
948bc91
Remove the preCheck hack from plutus-metatheory.components.tests.test…
zeme-wana Aug 14, 2024
fda7740
Rebase and remove dead code
zeme-wana Aug 14, 2024
3568d60
WIP: fix compilation errors
ana-pantilie Aug 14, 2024
89f9b08
Remove newline in error message
kwxm Aug 15, 2024
35ee520
Merge branch 'kwxm/metatheory/fix-test2' of github.com:IntersectMBO/p…
kwxm Aug 15, 2024
bcb1607
Fix compilation errors
ana-pantilie Aug 15, 2024
a2445ad
Small clean-up
ana-pantilie Aug 15, 2024
e5ddb90
Address some PR comments
kwxm Aug 16, 2024
3d9c46a
WIP
kwxm Aug 16, 2024
ee0e691
WIP
kwxm Aug 16, 2024
f389181
Add Output option to plc to make it consistent with uplc
kwxm Aug 16, 2024
36f8947
Tidy up output of evaluation/typecheching results
kwxm Aug 16, 2024
8c51c63
Tidy up output of evaluation/typecheching results
kwxm Aug 16, 2024
f9d5e2f
Name change
kwxm Aug 16, 2024
7ff26ec
WIP: clean-up
ana-pantilie Aug 16, 2024
f52e423
Merge remote-tracking branch 'origin/kwxm/metatheory/fix-test2' into …
ana-pantilie Aug 16, 2024
7778a19
Added plutus-executables package; moved uplc/pc/pir and metatheory tests
ana-pantilie Aug 16, 2024
33b6482
Add certifier to uplc optimiser
ana-pantilie Aug 16, 2024
1873aa8
Add certifier to uplc optimise
ana-pantilie Aug 16, 2024
99bd8ba
Merge remote-tracking branch 'origin/master' into ana/add-certificati…
ana-pantilie Aug 19, 2024
8d8d0a2
Fix
ana-pantilie Aug 19, 2024
5733c83
Clean-up
ana-pantilie Aug 19, 2024
f451581
Clean-up cabal file
ana-pantilie Aug 19, 2024
6c70e1c
Fix metatheory tests
ana-pantilie Aug 19, 2024
c24e54e
Fix isTransformation?
ana-pantilie Aug 19, 2024
d06b49b
Certifier properly handles results
ana-pantilie Aug 19, 2024
9e42982
Add documentation
ana-pantilie Aug 19, 2024
9c1b3e5
Merge remote-tracking branch 'origin/master' into ana/add-certificati…
ana-pantilie Sep 4, 2024
74a2b0e
Fix dependency ranges
ana-pantilie Sep 4, 2024
45136b6
Fix
ana-pantilie Sep 4, 2024
eac3c6b
Address review comment
ana-pantilie Sep 4, 2024
a04d246
Fix nix stuff
ana-pantilie Sep 4, 2024
110e694
Add changelog
ana-pantilie Sep 4, 2024
4823cff
Fix CI
ana-pantilie Sep 5, 2024
14a861c
Maybe maybe
ana-pantilie Sep 6, 2024
8ab0f74
Merge remote-tracking branch 'origin/master' into ana/add-certificati…
ana-pantilie Sep 10, 2024
393695f
Fix master merge
ana-pantilie Sep 10, 2024
a2f5900
Fix
ana-pantilie Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ packages: cardano-constitution
plutus-benchmark
plutus-conformance
plutus-core
plutus-executables
plutus-ledger-api
plutus-metatheory
plutus-tx
Expand Down
12 changes: 9 additions & 3 deletions plutus-core/executables/plutus/AnyProgram/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import Control.Lens hiding ((%~))
import Control.Monad.Error.Lens
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.State (StateT (runStateT))
import Data.Singletons.Decide
import Data.Text
import PlutusCore.Compiler.Types (initUPLCSimplifierTrace)
import PlutusPrelude hiding ((%~))

-- Note that we use for erroring the original term's annotation
Expand Down Expand Up @@ -111,7 +113,7 @@ compileProgram = curry $ \case
-- first self-"compile" to plc (just for reusing code)
compileProgram sng1 (SPlc n2 a2)
-- PLC.compileProgram subsumes uplcOptimise
>=> (PLC.runQuoteT . flip runReaderT PLC.defaultCompilationOpts .
>=> (PLC.runQuoteT . PLC.evalCompile PLC.defaultCompilationOpts .
plcToUplcViaName n2 PLC.compileProgram)
>=> pure . UPLC.UnrestrictedProgram

Expand Down Expand Up @@ -213,7 +215,10 @@ plcTypecheck sngN sngA p = PLC.runQuoteT $ do
tcConfig <- withA @Monoid sngA $ PLC.getDefTypeCheckConfig mempty
void $ plcToName sngN (PLC.inferTypeOfProgram tcConfig) p

uplcOptimise :: (?opts :: Opts, PLC.AsFreeVariableError e, MonadError e m)
uplcOptimise :: (?opts :: Opts
, PLC.AsFreeVariableError e
, MonadError e m
)
=> SNaming n1
-> UPLC.UnrestrictedProgram (FromName n1) DefaultUni DefaultFun a
-> m (UPLC.UnrestrictedProgram (FromName n1) DefaultUni DefaultFun a)
Expand All @@ -225,7 +230,8 @@ uplcOptimise =
case safeOrUnsafe of
SafeOptimise -> set UPLC.soConservativeOpts True
UnsafeOptimise -> id
in fmap PLC.runQuoteT
in (fmap . fmap) fst
. fmap (PLC.runQuoteT . flip runStateT initUPLCSimplifierTrace)
. _Wrapped
. uplcViaName (UPLC.simplifyProgram sOpts def)

Expand Down
16 changes: 15 additions & 1 deletion plutus-core/executables/src/PlutusCore/Executable/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,28 @@ nameformat =
<> short 'j'
<> help "Output evaluation result with de Bruijn indices (default: show textual names)")

certifier :: Parser Certifier
certifier =
optional
$ strOption
(long "certify"
<> help
("[EXPERIMENTAL] Produce a certificate ARG.agda proving that the program"
<> " transformaton is correct; the certificate is an Agda proof object, which"
<> " can be checked using the Agda proof assistant"
)
)

printOpts :: Parser PrintOptions
printOpts = PrintOptions <$> input <*> output <*> printmode

convertOpts :: Parser ConvertOptions
convertOpts = ConvertOptions <$> input <*> inputformat <*> output <*> outputformat <*> printmode

optimiseOpts :: Parser OptimiseOptions
optimiseOpts = OptimiseOptions <$> input <*> inputformat <*> output <*> outputformat <*> printmode
optimiseOpts =
OptimiseOptions
<$> input <*> inputformat <*> output <*> outputformat <*> printmode <*> certifier

exampleMode :: Parser ExampleMode
exampleMode = exampleAvailable <|> exampleSingle
Expand Down
4 changes: 3 additions & 1 deletion plutus-core/executables/src/PlutusCore/Executable/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ instance Show Format where
show (Flat DeBruijn) = "flat-deBruijn"
show (Flat NamedDeBruijn) = "flat-namedDeBruijn"

type Certifier = Maybe String

data ConvertOptions = ConvertOptions Input Format Output Format PrintMode
data OptimiseOptions = OptimiseOptions Input Format Output Format PrintMode
data OptimiseOptions = OptimiseOptions Input Format Output Format PrintMode Certifier
data PrintOptions = PrintOptions Input Output PrintMode
newtype ExampleOptions = ExampleOptions ExampleMode
data ApplyOptions = ApplyOptions Files Format Output Format PrintMode
Expand Down
45 changes: 0 additions & 45 deletions plutus-core/plutus-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -463,35 +463,6 @@ test-suite untyped-plutus-core-test
, text
, vector

executable plc
import: lang
main-is: plc/Main.hs
hs-source-dirs: executables
build-depends:
, base >=4.9 && <5
, bytestring
, flat ^>=0.6
, optparse-applicative
, plutus-core:{plutus-core, plutus-core-execlib} ^>=1.32

executable uplc
import: lang
main-is: uplc/Main.hs
hs-source-dirs: executables
build-depends:
, base >=4.9 && <5
, bytestring
, criterion
, deepseq
, flat ^>=0.6
, haskeline
, mtl
, optparse-applicative
, plutus-core:{plutus-core, plutus-core-execlib} ^>=1.32
, prettyprinter
, split
, text

----------------------------------------------
-- plutus-ir
----------------------------------------------
Expand Down Expand Up @@ -663,22 +634,6 @@ test-suite plutus-ir-test
, text
, unordered-containers

executable pir
import: lang
main-is: pir/Main.hs
hs-source-dirs: executables
build-depends:
, base >=4.9 && <5
, bytestring
, cassava
, containers
, lens
, megaparsec
, optparse-applicative
, plutus-core:{plutus-core, plutus-core-execlib, plutus-ir} ^>=1.32
, text
, transformers

executable plutus
import: lang
main-is: Main.hs
Expand Down
40 changes: 36 additions & 4 deletions plutus-core/plutus-core/src/PlutusCore/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ module PlutusCore.Compiler
( module Opts
, compileTerm
, compileProgram
, runCompile
, evalCompile
) where

import PlutusCore.Compiler.Erase
import PlutusCore.Compiler.Opts as Opts
import PlutusCore.Compiler.Types
import PlutusCore.Core
import PlutusCore.Name.Unique
Expand All @@ -13,12 +16,15 @@ import UntypedPlutusCore.Core.Type qualified as UPLC
import UntypedPlutusCore.Simplify qualified as UPLC

import Control.Lens (view)
import Control.Monad.Reader (MonadReader)
import PlutusCore.Compiler.Opts as Opts
import Control.Monad.Reader (MonadReader, ReaderT (runReaderT))
import Control.Monad.State (MonadState (..), StateT (runStateT))

-- | Compile a PLC term to UPLC, and optimize it.
compileTerm
:: (Compiling m uni fun name a, MonadReader (CompilationOpts name fun a) m)
:: (Compiling m uni fun name a
, MonadReader (CompilationOpts name fun a) m
, MonadState (UPLCSimplifierTrace name uni fun a) m
)
=> Term tyname name uni fun a
-> m (UPLC.Term name uni fun a)
compileTerm t = do
Expand All @@ -30,7 +36,33 @@ compileTerm t = do

-- | Compile a PLC program to UPLC, and optimize it.
compileProgram
:: (Compiling m uni fun name a, MonadReader (CompilationOpts name fun a) m)
:: (Compiling m uni fun name a
, MonadReader (CompilationOpts name fun a) m
, MonadState (UPLCSimplifierTrace name uni fun a) m
)
=> Program tyname name uni fun a
-> m (UPLC.Program name uni fun a)
compileProgram (Program a v t) = UPLC.Program a v <$> compileTerm t

type Compile m name uni fun a =
ReaderT
(CompilationOpts name fun a)
(StateT
(UPLCSimplifierTrace name uni fun a)
m
)

runCompile
:: CompilationOpts name fun a
-> Compile m name uni fun a b
-> m (b, UPLCSimplifierTrace name uni fun a)
runCompile opts =
flip runStateT initUPLCSimplifierTrace
. flip runReaderT opts

evalCompile
:: Functor m
=> CompilationOpts name fun a
-> Compile m name uni fun a b
-> m b
evalCompile opts = fmap fst . runCompile opts
15 changes: 14 additions & 1 deletion plutus-core/plutus-core/src/PlutusCore/Compiler/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

module PlutusCore.Compiler.Types where

import Data.Hashable
import PlutusCore.Builtin
import PlutusCore.Default qualified as PLC
import PlutusCore.Name.Unique
import PlutusCore.Quote
import UntypedPlutusCore.Core.Type qualified as UPLC

import Data.Hashable
-- TODO1: move somewhere more appropriate?
-- TODO2: we probably don't want this in memory so after MVP
-- we should consider serializing this to disk
newtype UPLCSimplifierTrace name uni fun a =
UPLCSimplifierTrace
{ uplcSimplifierTrace
:: [UPLC.Term name uni PLC.DefaultFun a]
}

initUPLCSimplifierTrace :: UPLCSimplifierTrace name uni fun a
initUPLCSimplifierTrace = UPLCSimplifierTrace []

type Compiling m uni fun name a =
( ToBuiltinMeaning uni fun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ compileTplcProgramOrFail
compileTplcProgramOrFail plcProgram =
handlePirErrorByFailing @SrcSpan =<< do
TPLC.compileProgram plcProgram
& flip runReaderT TPLC.defaultCompilationOpts
& TPLC.evalCompile TPLC.defaultCompilationOpts
& runQuoteT
& runExceptT

Expand Down
2 changes: 1 addition & 1 deletion plutus-core/testlib/PlutusCore/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ instance
toUPlc =
pure
. TPLC.runQuote
. flip runReaderT TPLC.defaultCompilationOpts
. TPLC.evalCompile TPLC.defaultCompilationOpts
. TPLC.compileProgram

instance ToUPlc (UPLC.Program UPLC.NamedDeBruijn uni fun ()) uni fun where
Expand Down
26 changes: 23 additions & 3 deletions plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Simplify.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}

module UntypedPlutusCore.Simplify (
Expand All @@ -22,12 +23,16 @@ import UntypedPlutusCore.Transform.ForceDelay (forceDelay)
import UntypedPlutusCore.Transform.Inline (InlineHints (..), inline)

import Control.Monad
import Control.Monad.State.Class (MonadState)
import Control.Monad.State.Class qualified as State
import Data.List as List (foldl')
import Data.Typeable

simplifyProgram ::
forall name uni fun m a.
(Compiling m uni fun name a) =>
(Compiling m uni fun name a
, MonadState (UPLCSimplifierTrace name uni fun a) m
) =>
SimplifyOpts name a ->
BuiltinSemanticsVariant fun ->
Program name uni fun a ->
Expand All @@ -37,7 +42,9 @@ simplifyProgram opts builtinSemanticsVariant (Program a v t) =

simplifyTerm ::
forall name uni fun m a.
(Compiling m uni fun name a) =>
( Compiling m uni fun name a
, MonadState (UPLCSimplifierTrace name uni fun a) m
) =>
SimplifyOpts name a ->
BuiltinSemanticsVariant fun ->
Term name uni fun a ->
Expand All @@ -57,11 +64,17 @@ simplifyTerm opts builtinSemanticsVariant =
-- generate simplification step
simplifyStep :: Int -> Term name uni fun a -> m (Term name uni fun a)
simplifyStep _ =
floatDelay
traceAST
>=> floatDelay
>=> traceAST
>=> pure . forceDelay
>=> traceAST
>=> pure . caseOfCase'
>=> traceAST
>=> pure . caseReduce
>=> traceAST
>=> inline (_soInlineConstants opts) (_soInlineHints opts) builtinSemanticsVariant
>=> traceAST

caseOfCase' :: Term name uni fun a -> Term name uni fun a
caseOfCase' = case eqT @fun @DefaultFun of
Expand All @@ -74,4 +87,11 @@ simplifyTerm opts builtinSemanticsVariant =
(Just Refl, Just Refl) -> cse builtinSemanticsVariant
_ -> pure

traceAST ast =
case eqT @fun @DefaultFun of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correctly, it's to convert to the Agda representation of terms, the Haskell -> Agda intermediate module uses that:

conv :: Term NamedDeBruijn DefaultUni DefaultFun a -> UTerm
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it. If it was for that function, you'd also need to check equality of uni and DefaultUni. What breaks if you remove the eqT check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I accidentally didn't use the fun type param in the SimplifierTrace type. Fixed now, thanks.

Just Refl -> do
State.modify' (\st -> st { uplcSimplifierTrace = uplcSimplifierTrace st ++ [ast] })
return ast
Nothing -> return ast

cseTimes = if _soConservativeOpts opts then 0 else _soMaxCseIterations opts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
module Transform.Simplify.Lib where

import Control.Lens ((&), (.~))
import Control.Monad.State (evalStateT)
import Data.ByteString.Lazy qualified as BSL
import Data.Text.Encoding (encodeUtf8)
import PlutusCore qualified as PLC
import PlutusCore.Builtin (BuiltinSemanticsVariant)
import PlutusCore.Compiler.Types (initUPLCSimplifierTrace)
import PlutusCore.Pretty (PrettyPlc, Render (render), prettyPlcReadableSimple)
import PlutusPrelude (Default (def))
import Test.Tasty (TestTree)
Expand All @@ -25,6 +27,7 @@ goldenVsSimplified :: String -> Term Name PLC.DefaultUni PLC.DefaultFun () -> Te
goldenVsSimplified name =
goldenVsPretty ".uplc.golden" name
. PLC.runQuote
. flip evalStateT initUPLCSimplifierTrace
. simplifyTerm
( defaultSimplifyOpts
-- Just run one iteration, to see what that does
Expand All @@ -37,6 +40,7 @@ goldenVsCse :: String -> Term Name PLC.DefaultUni PLC.DefaultFun () -> TestTree
goldenVsCse name =
goldenVsPretty ".uplc.golden" name
. PLC.runQuote
. flip evalStateT initUPLCSimplifierTrace
. simplifyTerm
( defaultSimplifyOpts
-- Just run one iteration, to see what that does
Expand Down
5 changes: 5 additions & 0 deletions plutus-executables/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for plutus-executables

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
Loading