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 #4733

Closed
wants to merge 107 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
0ce86be
Start on bitwise primitive implementations in Core
kozross Jun 24, 2022
8af8684
Merge branch 'master' into koz/bitwise
kozross Jun 26, 2022
2ec6f0e
Remaining implementations of bitwise operations
kozross Jun 27, 2022
0e0ae36
Plutus Core builtins for bitwise ops
kozross Jun 27, 2022
d1579a6
More bitwise operation checks, fix bug in complement
kozross Jun 28, 2022
e89cfd2
Finish tests for AND, IOR, XOR, complement
kozross Jun 28, 2022
139532e
Popcount bugfix, tests for popcount
kozross Jun 28, 2022
06d0f9b
Tests for bit indexing
kozross Jun 29, 2022
15206d4
Tests for bit writing, some for find first set
kozross Jun 29, 2022
ce10c0b
Merge branch 'master' into koz/bitwise
kozross Jun 29, 2022
fb1c854
Finish find first set tests
kozross Jun 29, 2022
8ef6322
Fix rotate bug, test for rotations
kozross Jun 30, 2022
b9ef84c
Merge branch 'master' into koz/bitwise
kozross Jul 3, 2022
23d5a38
Fix rotation bugs, rotation tests
kozross Jul 4, 2022
ecde7e4
Reimplement shift similarly to rotate, shift tests
kozross Jul 4, 2022
0576121
Shifting tests
kozross Jul 4, 2022
409f710
Finish bytestring conversion tests
kozross Jul 5, 2022
e984cf1
Merge branch 'master' into koz/bitwise
kozross Jul 5, 2022
a2f1748
PlutusTx bitwise builtins
kozross Jul 6, 2022
5acf262
Fix Nix issues, ensure we don't break on line length
kozross Jul 6, 2022
9b9ea9e
Tests for IntegerToByteString primitive, finish #4252
kozross Jul 6, 2022
86c96d2
Merge branch 'master' into koz/bitwise
kozross Jul 10, 2022
25968aa
Remove -fsimpl-tick-factor parameter
kozross Jul 10, 2022
7e5a1fb
Add note for overPtrLen safety, remove commented code
kozross Jul 10, 2022
2610321
Restore simpl-tick with comment, document Bitwise
kozross Jul 11, 2022
b7180e4
Remove -Werror in source files
kozross Jul 11, 2022
bd53cdd
Merge branch 'master' into koz/bitwise
kozross Jul 18, 2022
2e3d054
Benchmarks for bitwise binary ops
kozross Jul 19, 2022
c80681e
Merge branch 'master' into koz/bitwise
kozross Jul 20, 2022
470baee
Refactor of bitwise binary ops, more accurate measurements
kozross Jul 20, 2022
1eb89ce
More rigid benching methodology, 3-stage loop
kozross Jul 20, 2022
30d7884
Merge branch 'master' into koz/bitwise
kozross Jul 20, 2022
293f4c8
Rewrite to use arrays of Ptrs
kozross Jul 21, 2022
86b9973
Define a big-smaller-small loop generically
kozross Jul 21, 2022
620e51e
Merge branch 'master' into koz/bitwise
kozross Jul 21, 2022
2af430a
Merge branch 'master' into koz/bitwise
kozross Jul 25, 2022
5ed1dd3
Better benches, settle on implementation
kozross Jul 25, 2022
22d329c
Add benches for remaining binary bitwise ops
kozross Jul 26, 2022
b97329c
Merge branch 'master' into koz/bitwise
kozross Jul 31, 2022
d8077c1
NOINLINE packZipWith
kozross Aug 1, 2022
7d735d8
Retry 2-step loop unrolls
kozross Aug 1, 2022
adad03d
3-step inlining
kozross Aug 1, 2022
089044e
More bitwise AND implementations, big comparison
kozross Aug 1, 2022
d162ead
Refactor, more measurements, measure complement
kozross Aug 1, 2022
1040977
Popcount benchmarks
kozross Aug 2, 2022
0a9f7e3
Refactor bench implementations, add rotation benches
kozross Aug 4, 2022
04f1531
Start benchmark writeup
kozross Aug 4, 2022
8144f87
Merge branch 'master' into koz/bitwise
kozross Aug 7, 2022
09dd2dd
Writeup for popcount, another narrow bench
kozross Aug 8, 2022
3bc7b48
Merge branch 'master' into koz/bitwise
kozross Aug 8, 2022
812672c
Implement more options for bigger inputs, make benches bigger
kozross Aug 9, 2022
801b581
Merge branch 'master' into koz/bitwise
kozross Aug 9, 2022
c6b6ba9
Tune FFI settings, more AND benches
kozross Aug 10, 2022
c659c50
Merge branch 'master' into koz/bitwise
kozross Aug 10, 2022
079e568
Merge branch 'master' into koz/bitwise
kozross Aug 11, 2022
e6166c7
Refactor popcount benchmarks
kozross Aug 11, 2022
87e21f9
Refactor complement benches
kozross Aug 12, 2022
c83c7f6
Merge branch 'master' into koz/bitwise
kozross Aug 14, 2022
0b82ca5
More benches for popcount, complement
kozross Aug 16, 2022
2f7dc4c
Merge branch 'master' into koz/bitwise
kozross Aug 17, 2022
581924f
Benches for homogeneity testing
kozross Aug 17, 2022
996fe3c
Better AND benches, CLZ benches
kozross Aug 18, 2022
c293800
Merge branch 'master' into koz/bitwise
kozross Aug 18, 2022
3ad984e
Benches for bit access, compare CLZ C implementations
kozross Aug 19, 2022
2e1feca
Merge branch 'master' into koz/bitwise
kozross Aug 21, 2022
03bc299
Bit write benches
kozross Aug 21, 2022
8d0f21b
Merge branch 'master' into koz/bitwise
kozross Aug 22, 2022
78dcf10
Bitwise shift bench preliminary
kozross Aug 24, 2022
4cdd39b
Define and bench a faster byte-size shift
kozross Aug 24, 2022
87b659b
Much faster shift in C
kozross Aug 24, 2022
cc76263
Merge branch 'master' into koz/bitwise
kozross Aug 24, 2022
4a9c80b
Remove Implementation module, bitwise rotation benches
kozross Aug 24, 2022
c4d9af9
C bitwise rotation, clear away un-needed benches and implementations
kozross Aug 24, 2022
d0fcc9e
Benches for BS to Integer conversion
kozross Aug 25, 2022
677666e
Merge branch 'master' into koz/bitwise
kozross Aug 25, 2022
2a258a5
Merge block converter with other benches
kozross Aug 25, 2022
c6a675c
Integer to ByteString conversion benches
kozross Aug 26, 2022
5f3d2ed
Merge branch 'master' into koz/bitwise
kozross Aug 28, 2022
438de66
Merge branch 'master' into koz/bitwise
kozross Aug 30, 2022
dbf6af8
Merge branch 'master' into koz/bitwise
L-as Mar 7, 2023
4134f2c
Style fixes
L-as Mar 7, 2023
d8fede4
Fix issues from the previous merge
L-as Mar 9, 2023
e44b148
Fix `integerToByteString` and its inverse according to spec
L-as Mar 10, 2023
db8d52d
Reduce one copy of the input in `byteStringToInteger`
L-as Mar 10, 2023
d0497d4
Make plutus-tx compile again
L-as Mar 10, 2023
266ac1e
Improve documentation, remove unnececessary pragmata, slightly clean …
L-as Mar 10, 2023
28c359b
Merge commit 'refs/pull/4733/head' of github.com:input-output-hk/plut…
kwxm Jun 30, 2023
6895223
Complete merge
kwxm Jun 30, 2023
371a26d
Fix some more merge problems
kwxm Jun 30, 2023
87d8f73
Restore missing exports
kwxm Jun 30, 2023
3133777
WIP: cost model stubs
kwxm Jun 30, 2023
e98929e
WIP: cost model stubs
kwxm Jun 30, 2023
dc4bdef
WIP: cost model stubs
kwxm Jun 30, 2023
fd60348
Merge branch 'master' into kwxm/mlabs-bitwise
kwxm Jul 7, 2023
b502617
Merge branch 'master' into kwxm/mlabs-bitwise
kwxm Jul 18, 2023
ee0075c
Update builtin tags
kwxm Jul 18, 2023
af4fd3e
Update costing code
kwxm Jul 19, 2023
181488a
_var -> _ver
kwxm Jul 20, 2023
df99b27
Merge branch 'master' into kwxm/mlabs-bitwise
kwxm Jul 25, 2023
3938faf
Fix typo in builtin tags
kwxm Jul 25, 2023
3e011d5
Merge branch 'master' into kwxm/mlabs-bitwise
kwxm Jul 26, 2023
89254ba
Merge branch 'master' into kwxm/mlabs-bitwise
kwxm Jul 26, 2023
ac95847
Remove some duplicated imports
kwxm Aug 1, 2023
1aca9c5
Merge branch 'koz/bitwise' of github.com:mlabs-haskell/plutus into ko…
kozross Aug 8, 2023
221da85
Merge branch 'kwxm/mlabs-bitwise' into koz/bitwise
kozross Aug 8, 2023
7996c8d
Format fix
kozross Aug 8, 2023
00ffb88
Changelogs for bitwise primops
kozross Aug 8, 2023
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/20230809_093202_koz.ross_bitwise.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

- Bitwise primitive operations, according to
[CIP-0058](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0058).

<!--
### 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.

-->
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ builtinCostModelNames = BuiltinCostModelBase
, paramBls12_381_finalVerify = "bls12_381_finalVerifyModel"
, paramBlake2b_224 = "blake2b_224Model"
, paramKeccak_256 = "keccak_256Model"
, paramIntegerToByteString = "integerToByteStringModel"
, paramByteStringToInteger = "byteStringToIntegerModel"
, paramAndByteString = "andByteStringModel"
, paramIorByteString = "iorByteStringModel"
, paramXorByteString = "xorByteStringModel"
, paramComplementByteString = "complementByteStringModel"
, paramShiftByteString = "shiftByteStringModel"
, paramRotateByteString = "rotateByteStringModel"
, paramPopCountByteString = "popCountByteStringModel"
, paramTestBitByteString = "testBitByteStringModel"
, paramWriteBitByteString = "writeBitByteStringModel"
, paramFindFirstSetByteString = "findFirstSetByteStringModel"
}


Expand Down Expand Up @@ -233,6 +245,19 @@ createBuiltinCostModel bmfile rfile = do
paramBls12_381_millerLoop <- getParams bls12_381_millerLoop paramBls12_381_millerLoop
paramBls12_381_mulMlResult <- getParams bls12_381_mulMlResult paramBls12_381_mulMlResult
paramBls12_381_finalVerify <- getParams bls12_381_finalVerify paramBls12_381_finalVerify
-- Bitwise operations
paramIntegerToByteString <- getParams integerToByteString paramIntegerToByteString
paramByteStringToInteger <- getParams byteStringToInteger paramByteStringToInteger
paramAndByteString <- getParams andByteString paramAndByteString
paramIorByteString <- getParams iorByteString paramIorByteString
paramXorByteString <- getParams xorByteString paramXorByteString
paramComplementByteString <- getParams complementByteString paramComplementByteString
paramShiftByteString <- getParams shiftByteString paramShiftByteString
paramRotateByteString <- getParams rotateByteString paramRotateByteString
paramPopCountByteString <- getParams popCountByteString paramPopCountByteString
paramTestBitByteString <- getParams testBitByteString paramTestBitByteString
paramWriteBitByteString <- getParams writeBitByteString paramWriteBitByteString
paramFindFirstSetByteString <- getParams findFirstSetByteString paramFindFirstSetByteString

paramKeccak_256 <- getParams keccak_256 paramKeccak_256
paramBlake2b_224 <- getParams blake2b_224 paramBlake2b_224
Expand Down Expand Up @@ -922,9 +947,80 @@ bls12_381_mulMlResult cpuModelR = do
pure $ CostingFun cpuModel memModel

bls12_381_finalVerify :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
bls12_381_finalVerify cpuModelR= do
bls12_381_finalVerify cpuModelR = do
cpuModel <- ModelTwoArgumentsConstantCost <$> readModelConstantCost cpuModelR
let memModel = boolMemModel
pure $ CostingFun cpuModel memModel

integerToByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
integerToByteString cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentLinearCost $ ModelLinearSize 0 1
pure $ CostingFun cpuModel memModel

byteStringToInteger :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
byteStringToInteger cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentLinearCost $ ModelLinearSize 0 1
pure $ CostingFun cpuModel memModel

andByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
andByteString cpuModelR = do
cpuModel <- ModelTwoArgumentsMaxSize <$> readModelMaxSize cpuModelR
let memModel = ModelTwoArgumentsMaxSize $ ModelMaxSize 0 1
pure $ CostingFun cpuModel memModel

iorByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
iorByteString cpuModelR = do
cpuModel <- ModelTwoArgumentsMaxSize <$> readModelMaxSize cpuModelR
let memModel = ModelTwoArgumentsMaxSize $ ModelMaxSize 0 1
pure $ CostingFun cpuModel memModel

xorByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
xorByteString cpuModelR = do
cpuModel <- ModelTwoArgumentsMaxSize <$> readModelMaxSize cpuModelR
let memModel = ModelTwoArgumentsMaxSize $ ModelMaxSize 0 1
pure $ CostingFun cpuModel memModel

complementByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
complementByteString cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentLinearCost $ ModelLinearSize 0 1
pure $ CostingFun cpuModel memModel

shiftByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
shiftByteString cpuModelR = do
cpuModel <- undefined
let memModel = undefined
pure $ CostingFun cpuModel memModel

rotateByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
rotateByteString cpuModelR = do
cpuModel <- undefined -- FIXME
let memModel = undefined -- FIXME
pure $ CostingFun cpuModel memModel

popCountByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
popCountByteString cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentLinearCost $ ModelLinearSize 0 1 -- FIXME
pure $ CostingFun cpuModel memModel

testBitByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelTwoArguments)
testBitByteString cpuModelR = do
cpuModel <- undefined -- FIXME
let memModel = ModelTwoArgumentsConstantCost 1
pure $ CostingFun cpuModel memModel

writeBitByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelThreeArguments)
writeBitByteString cpuModelR = do
cpuModel <- undefined -- FIXME
let memModel = undefined -- FIXME
pure $ CostingFun cpuModel memModel

findFirstSetByteString :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
findFirstSetByteString cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = undefined -- FIXME
pure $ CostingFun cpuModel memModel

192 changes: 192 additions & 0 deletions plutus-core/cost-model/data/builtinCostModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,5 +899,197 @@
"arguments": 10,
"type": "constant_cost"
}
},
"integerToByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_cost"
},
"memory": {
"arguments": {
"intercept": 0,
"slope": 1
},
"type": "linear_cost"
}
},
"byteStringToInteger": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_cost"
},
"memory": {
"arguments": {
"intercept": 1,
"slope": 1
},
"type": "linear_cost"
}
},
"andByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "max_size"
},
"memory": {
"arguments": {
"intercept": 1,
"slope": 1
},
"type": "max_size"
}
},
"iorByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "max_size"
},
"memory": {
"arguments": {
"intercept": 1,
"slope": 1
},
"type": "max_size"
}
},
"xorByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "max_size"
},
"memory": {
"arguments": {
"intercept": 1,
"slope": 1
},
"type": "max_size"
}
},
"complementByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_cost"
},
"memory": {
"arguments": {
"intercept": 0,
"slope": 1
},
"type": "linear_cost"
}
},
"shiftByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "max_size"
},
"memory": {
"arguments": {
"intercept": 1,
"slope": 1
},
"type": "max_size"
}
},
"rotateByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "max_size"
},
"memory": {
"arguments": {
"intercept": 1,
"slope": 1
},
"type": "max_size"
}
},
"popCountByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_cost"
},
"memory": {
"arguments": {
"intercept": 0,
"slope": 1
},
"type": "linear_cost"
}
},
"testBitByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_in_x"
},
"memory": {
"arguments": {
"intercept": 0,
"slope": 1
},
"type": "linear_in_x"
}
},
"writeBitByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_in_x"
},
"memory": {
"arguments": {
"intercept": 0,
"slope": 1
},
"type": "linear_in_x"
}
},
"findFirstSetByteString": {
"cpu": {
"arguments": {
"intercept": 9999999999,
"slope": 9999999999
},
"type": "linear_cost"
},
"memory": {
"arguments": {
"intercept": 0,
"slope": 1
},
"type": "linear_cost"
}
}
}
Loading