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

Bitwise operations #6090

Merged
merged 24 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
02ab54f
Initial bitwise primitives
kozross May 22, 2024
1a1ce70
Wire up new builtins
kozross May 22, 2024
0c32b48
Merge branch 'master' into koz/bitwise-2
kozross May 22, 2024
3556946
Tests
kozross May 23, 2024
249489a
Changelogs
kozross May 23, 2024
ad5cc7b
Fix failing goldens
kozross May 24, 2024
f51656e
Merge branch 'master' into koz/bitwise-2
kozross Jun 6, 2024
9520bc3
Fix cost model for tests
kozross Jun 6, 2024
aa5f83d
Bitwise primitives are not in Conway
kozross Jun 6, 2024
56e90ad
Merge branch 'master' into koz/bitwise-2
kozross Jun 9, 2024
a01acc0
Finish shift tests
kozross Jun 9, 2024
2fc1e8d
Fix goldens
kozross Jun 9, 2024
d23d03b
Rest of tests
kozross Jun 10, 2024
7d6ba9c
Merge branch 'master' into koz/bitwise-2
kozross Jun 10, 2024
30a7435
Merge branch 'master' into koz/bitwise-2
kozross Jun 11, 2024
50b9171
Rename operations
kozross Jun 11, 2024
2d73e9a
Note about split composition for shift property
kozross Jun 11, 2024
6ef45c0
Explain bitwise tests in comments, remove AND and OR tests for findin…
kozross Jun 11, 2024
c25541f
Goldens for bitwise primops
kozross Jun 11, 2024
6a6ee69
Chop down property test running times a bit
kozross Jun 11, 2024
cf668f4
Add test for finding first in zero byte string, rename some tests for…
kozross Jun 11, 2024
4f550df
Clarify implementation choices in the comments
kozross Jun 11, 2024
96d42bf
Tidy up helpers for property tests
kozross Jun 12, 2024
4a71fc5
Consolidate all bitwise ops, retarget links to CIPs
kozross Jun 12, 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
41 changes: 41 additions & 0 deletions plutus-core/changelog.d/20240523_124004_koz.ross_bitwise_2.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

- Implementation and tests for primitive operations in [this
CIP](https://github.com/mlabs-haskell/CIPs/blob/koz/bitwise/CIP-XXXX/CIP-XXXX.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.

-->
2 changes: 2 additions & 0 deletions plutus-core/plutus-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ library
PlutusCore.Annotation
PlutusCore.Arity
PlutusCore.Bitwise.Convert
PlutusCore.Bitwise.Other
PlutusCore.Builtin
PlutusCore.Builtin.Debug
PlutusCore.Builtin.Elaborate
Expand Down Expand Up @@ -415,6 +416,7 @@ test-suite untyped-plutus-core-test
DeBruijn.Spec
DeBruijn.UnDeBruijnify
Evaluation.Builtins
Evaluation.Builtins.Bitwise
Evaluation.Builtins.BLS12_381
Evaluation.Builtins.BLS12_381.TestClasses
Evaluation.Builtins.BLS12_381.Utils
Expand Down
510 changes: 510 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Bitwise/Other.hs
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the comments just about outnumber the lines of actual code in this file. That's a good thing!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With code this fiddly (and this unfamiliar to many Haskellers, I am sure), that density of comments is important.

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import PlutusCore.Evaluation.Result (EvaluationResult (..))
import PlutusCore.Pretty (PrettyConfigPlc)

import PlutusCore.Bitwise.Convert as Convert
import PlutusCore.Bitwise.Other as Other
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 @@ -152,6 +153,11 @@ data DefaultFun
-- Conversions
| IntegerToByteString
| ByteStringToInteger
-- Bitwise
| BitwiseShift
kozross marked this conversation as resolved.
Show resolved Hide resolved
| BitwiseRotate
| CountSetBits
| FindFirstSetBit
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Ix)
deriving anyclass (NFData, Hashable, PrettyBy PrettyConfigPlc)

Expand Down Expand Up @@ -1820,6 +1826,38 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
in makeBuiltinMeaning
byteStringToIntegerDenotation
(runCostingFunTwoArguments . paramByteStringToInteger)

toBuiltinMeaning _semvar BitwiseShift =
kozross marked this conversation as resolved.
Show resolved Hide resolved
let bitwiseShiftDenotation :: BS.ByteString -> Int -> BS.ByteString
bitwiseShiftDenotation = Other.bitwiseShift
{-# INLINE bitwiseShiftDenotation #-}
in makeBuiltinMeaning
bitwiseShiftDenotation
(runCostingFunTwoArguments . unimplementedCostingFun)

toBuiltinMeaning _semvar BitwiseRotate =
let bitwiseRotateDenotation :: BS.ByteString -> Int -> BS.ByteString
bitwiseRotateDenotation = Other.bitwiseRotate
{-# INLINE bitwiseRotateDenotation #-}
in makeBuiltinMeaning
bitwiseRotateDenotation
(runCostingFunTwoArguments . unimplementedCostingFun)

toBuiltinMeaning _semvar CountSetBits =
let countSetBitsDenotation :: BS.ByteString -> Int
countSetBitsDenotation = Other.countSetBits
{-# INLINE countSetBitsDenotation #-}
in makeBuiltinMeaning
countSetBitsDenotation
(runCostingFunOneArgument . unimplementedCostingFun)

toBuiltinMeaning _semvar FindFirstSetBit =
let findFirstSetBitDenotation :: BS.ByteString -> Int
findFirstSetBitDenotation = Other.findFirstSetBit
{-# INLINE findFirstSetBitDenotation #-}
in makeBuiltinMeaning
findFirstSetBitDenotation
(runCostingFunOneArgument . unimplementedCostingFun)
-- See Note [Inlining meanings of builtins].
{-# INLINE toBuiltinMeaning #-}

Expand Down Expand Up @@ -1947,6 +1985,11 @@ instance Flat DefaultFun where
IntegerToByteString -> 73
ByteStringToInteger -> 74

BitwiseShift -> 82
BitwiseRotate -> 83
CountSetBits -> 84
FindFirstSetBit -> 85

decode = go =<< decodeBuiltin
where go 0 = pure AddInteger
go 1 = pure SubtractInteger
Expand Down Expand Up @@ -2023,6 +2066,10 @@ instance Flat DefaultFun where
go 72 = pure Blake2b_224
go 73 = pure IntegerToByteString
go 74 = pure ByteStringToInteger
go 82 = pure BitwiseShift
go 83 = pure BitwiseRotate
go 84 = pure CountSetBits
go 85 = pure FindFirstSetBit
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
Expand Up @@ -129,3 +129,7 @@ isCommutative = \case
MkNilPairData -> False
IntegerToByteString -> False
ByteStringToInteger -> False
BitwiseShift -> False
BitwiseRotate -> False
CountSetBits -> False
FindFirstSetBit -> False
Loading