Skip to content

Commit

Permalink
[PLT-8182] CIP-0087 support (#5654)
Browse files Browse the repository at this point in the history
* Move conversion code into Plutus Core

* Documentation and notes on implementations

* Wrap implementations into builtins

* Properties as per CIP-0087

* CIP-0087 examples as tests

* Add new builtins to PlutusTx

* Document fromIntegral usage as a note

* Changelogs for CIP-0087 primitives

* Ensure conversions don't break on too-large arguments

* Ensure that conversions are available in V3

* Remove unnecessary pragmata on tests

* Fix overly-long test names, clarify test meaning in comments

* CIP link consistency

* Re-order integerToByteString arguments, avoid unnecessary padding

* Better documentation for implementations

* Correct properties for ByteStringToInteger

* Address feedback
  • Loading branch information
kozross authored Jan 2, 2024
1 parent 91a814c commit f734591
Show file tree
Hide file tree
Showing 13 changed files with 1,237 additions and 3 deletions.
41 changes: 41 additions & 0 deletions plutus-core/changelog.d/20231127_134852_koz.ross_cip_0087.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
### Added

- Implementations for the primitive operations described in
[CIP-0087](https://github.com/mlabs-haskell/CIPs/blob/koz/to-from-bytestring/CIP-0087/CIP-0087.md)

<!--
### Changed
- A bullet item for the Changed category.
-->
<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
<!--
### Fixed
- A bullet item for the Fixed category.
-->
<!--
### Security
- A bullet item for the Security category.
-->
3 changes: 3 additions & 0 deletions plutus-core/plutus-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ library
PlutusCore.Annotation
PlutusCore.Arity
PlutusCore.Builtin
PlutusCore.Builtin.Convert
PlutusCore.Builtin.Debug
PlutusCore.Builtin.Elaborate
PlutusCore.Builtin.Emitter
Expand Down Expand Up @@ -274,6 +275,7 @@ library
, base64-bytestring
, bimap
, bytestring
, bytestring-strict-builder
, cardano-crypto
, cardano-crypto-class ^>=2.1.2
, cassava
Expand Down Expand Up @@ -401,6 +403,7 @@ test-suite untyped-plutus-core-test
Evaluation.Builtins.BLS12_381.TestClasses
Evaluation.Builtins.BLS12_381.Utils
Evaluation.Builtins.Common
Evaluation.Builtins.Conversion
Evaluation.Builtins.Costing
Evaluation.Builtins.Definition
Evaluation.Builtins.MakeRead
Expand Down
504 changes: 504 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Builtin/Convert.hs

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import PlutusCore.Evaluation.Machine.ExMemoryUsage
import PlutusCore.Evaluation.Result
import PlutusCore.Pretty

import PlutusCore.Builtin.Convert as Convert
import PlutusCore.Crypto.BLS12_381.G1 qualified as BLS12_381.G1
import PlutusCore.Crypto.BLS12_381.G2 qualified as BLS12_381.G2
import PlutusCore.Crypto.BLS12_381.Pairing qualified as BLS12_381.Pairing
Expand Down Expand Up @@ -147,6 +148,9 @@ data DefaultFun
-- Keccak_256, Blake2b_224
| Keccak_256
| Blake2b_224
-- Conversions
| IntegerToByteString
| ByteStringToInteger
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Ix)
deriving anyclass (NFData, Hashable, PrettyBy PrettyConfigPlc)

Expand Down Expand Up @@ -1803,6 +1807,22 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
in makeBuiltinMeaning
blake2b_224Denotation
(runCostingFunOneArgument . paramBlake2b_224)

-- Conversions
toBuiltinMeaning _semvar IntegerToByteString =
let integerToByteStringDenotation :: Bool -> Integer -> Integer -> Emitter (EvaluationResult BS.ByteString)
integerToByteStringDenotation = integerToByteStringWrapper
in makeBuiltinMeaning
integerToByteStringDenotation
-- FIXME: Cost this function.
(runCostingFunThreeArguments . const def)
toBuiltinMeaning _semvar ByteStringToInteger =
let byteStringToIntegerDenotation :: Bool -> BS.ByteString -> Integer
byteStringToIntegerDenotation = byteStringToIntegerWrapper
in makeBuiltinMeaning
byteStringToIntegerDenotation
-- FIXME: Cost this function.
(runCostingFunTwoArguments . const def)
-- See Note [Inlining meanings of builtins].
{-# INLINE toBuiltinMeaning #-}

Expand Down Expand Up @@ -1911,6 +1931,9 @@ instance Flat DefaultFun where
Keccak_256 -> 71
Blake2b_224 -> 72

IntegerToByteString -> 73
ByteStringToInteger -> 74

decode = go =<< decodeBuiltin
where go 0 = pure AddInteger
go 1 = pure SubtractInteger
Expand Down Expand Up @@ -1985,6 +2008,8 @@ instance Flat DefaultFun where
go 70 = pure Bls12_381_finalVerify
go 71 = pure Keccak_256
go 72 = pure Blake2b_224
go 73 = pure IntegerToByteString
go 74 = pure ByteStringToInteger
go t = fail $ "Failed to decode builtin tag, got: " ++ show t

size _ n = n + builtinTagWidth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bool -> bytestring -> integer
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bool -> integer -> integer -> bytestring
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ isCommutative = \case
MkPairData -> False
MkNilData -> False
MkNilPairData -> False
IntegerToByteString -> False
ByteStringToInteger -> False
Loading

1 comment on commit f734591

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: f734591 Previous: 91a814c Ratio
validation-decode-future-increase-margin-2 333.2 μs 306.3 μs 1.09
validation-decode-future-increase-margin-3 339.1 μs 305.4 μs 1.11
validation-decode-future-pay-out-2 334.2 μs 305.7 μs 1.09
validation-decode-future-settle-early-2 321.7 μs 305.2 μs 1.05
validation-decode-future-settle-early-3 343.3 μs 306.9 μs 1.12
validation-decode-multisig-sm-1 592.8 μs 554.4 μs 1.07
validation-decode-multisig-sm-9 594.8 μs 556 μs 1.07
validation-decode-prism-2 544.2 μs 496.2 μs 1.10
validation-decode-stablecoin_1-1 874.4 μs 822.4 μs 1.06
validation-decode-stablecoin_2-3 867.4 μs 819.8 μs 1.06

This comment was automatically generated by workflow using github-action-benchmark.

CC: @input-output-hk/plutus-core

Please sign in to comment.