-
Notifications
You must be signed in to change notification settings - Fork 88
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
Spike: costs of hashing #155
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c215b7e
Add a Hydra.Contract.Hash script to test different hashing algorithms
ch1bo 4f13d15
Add a property to ContractSpec for Hash validator
ch1bo 9dd40f1
Refactor into a pure function and print results in an hspec 'it'
ch1bo fba191b
Test powers of 8 and print number of words (s)
ch1bo 4bcd5bd
Make Hash results relative to Baseline
ch1bo 47ebf04
Add a bit more computation to baseline
ch1bo 917127f
Improve printed output
ch1bo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# OPTIONS_GHC -fno-specialize #-} | ||
|
||
-- | An experimental validator which simply hashes a bytestring stored in the | ||
-- datum using one of three supported algorithms. | ||
module Hydra.Contract.Hash where | ||
|
||
import Ledger hiding (validatorHash) | ||
import PlutusTx.Prelude | ||
|
||
import qualified Hydra.Prelude as Haskell | ||
|
||
import Ledger.Typed.Scripts (TypedValidator, ValidatorType, ValidatorTypes (..)) | ||
import qualified Ledger.Typed.Scripts as Scripts | ||
import PlutusTx (CompiledCode) | ||
import qualified PlutusTx | ||
import PlutusTx.Builtins (equalsByteString) | ||
import PlutusTx.IsData.Class (ToData (..)) | ||
|
||
data Hash | ||
|
||
data HashAlgorithm | ||
= Base | ||
| SHA2 | ||
| SHA3 | ||
-- Blake2b | ||
deriving (Haskell.Show, Haskell.Generic, Haskell.Enum, Haskell.Bounded) | ||
|
||
PlutusTx.unstableMakeIsData ''HashAlgorithm | ||
|
||
instance Haskell.Arbitrary HashAlgorithm where | ||
arbitrary = Haskell.genericArbitrary | ||
|
||
instance Scripts.ValidatorTypes Hash where | ||
type DatumType Hash = BuiltinByteString | ||
type RedeemerType Hash = HashAlgorithm | ||
|
||
-- NOTE: Plutus is strict, thus this still occurs cost for hashing | ||
validator :: DatumType Hash -> RedeemerType Hash -> ScriptContext -> Bool | ||
validator bytes algorithm _ctx = | ||
case algorithm of | ||
Base -> equalsByteString bytes bytes | ||
SHA2 -> not . equalsByteString bytes $ sha2_256 bytes | ||
SHA3 -> not . equalsByteString bytes $ sha3_256 bytes | ||
|
||
-- Blake2b -> not . equalsByteString "" $ blake2b_256 bytes | ||
|
||
compiledValidator :: CompiledCode (ValidatorType Hash) | ||
compiledValidator = $$(PlutusTx.compile [||validator||]) | ||
|
||
{- ORMOLU_DISABLE -} | ||
typedValidator :: TypedValidator Hash | ||
typedValidator = Scripts.mkTypedValidator @Hash | ||
compiledValidator | ||
$$(PlutusTx.compile [|| wrap ||]) | ||
where | ||
wrap = Scripts.wrapValidator @(DatumType Hash) @(RedeemerType Hash) | ||
{- ORMOLU_ENABLE -} | ||
|
||
-- | Get the actual plutus script. Mainly used to serialize and use in | ||
-- transactions. | ||
validatorScript :: Script | ||
validatorScript = unValidatorScript $ Scripts.validatorScript typedValidator | ||
|
||
validatorHash :: ValidatorHash | ||
validatorHash = Scripts.validatorHash typedValidator | ||
|
||
datum :: DatumType Hash -> Datum | ||
datum a = Datum (toBuiltinData a) | ||
|
||
redeemer :: RedeemerType Hash -> Redeemer | ||
redeemer a = Redeemer (toBuiltinData a) | ||
|
||
address :: Address | ||
address = scriptHashAddress validatorHash |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have moved this to a dedicated
HashSpec
module.