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

Spike: costs of hashing #155

Merged
merged 7 commits into from
Jan 5, 2022
Merged

Spike: costs of hashing #155

merged 7 commits into from
Jan 5, 2022

Conversation

ch1bo
Copy link
Member

@ch1bo ch1bo commented Jan 4, 2022

❄️ Indeed the formula s * slope + intercept where s = number of Word64 is used to calculate builtin hash function costs in the cost model

❄️ Given the cost model parameters in mainnet-alonzo-genesis.json

"blake2b-cpu-arguments-intercept": 2477736,
"blake2b-cpu-arguments-slope": 29175,
"blake2b-memory-arguments": 4,
"sha2_256-cpu-arguments-intercept": 2477736,
"sha2_256-cpu-arguments-slope": 29175,
"sha2_256-memory-arguments": 4,
"sha3_256-cpu-arguments-intercept": 0,
"sha3_256-cpu-arguments-slope": 82363,
"sha3_256-memory-arguments": 4,

we expect

  • memory costs to be constant ModelOneArgumentConstantCost == 4
  • cpu costs with some number of words lengths (org-mode table)
|             | slope | intercept | s= |       1 |       8 |      64 |      512 |
|-------------+-------+-----------+----+---------+---------+---------+----------|
| blake2b_256 | 29175 |   2477736 |    | 2506911 | 2711136 | 4344936 | 17415336 |
| sha2_256    | 29175 |   2477736 |    | 2506911 | 2711136 | 4344936 | 17415336 |
| sha3_256    | 82363 |         0 |    |   82363 |  658904 | 5271232 | 42169856 |
#+TBLFM: @2$5..@4$8=@1*$2+$3

❄️ Add a Hash contract to hash a given datum using supported hashing algorithms

❄️ Add test to ContractSpec which calculates units for multiple bytes lengths and all algorithms

❄️ The observed execution unit (deltas) output of cabal test hydra-node --test-options '--match "Contract/Hash"'

image

show that memory usage is constant and somewhat confirms the expected cpu usage values with an additional offset between [-20k..-200k] and [-870k..-1M] units for SHA2 and SHA3 respectively. It seems like the baseline is not good enough or maybe there had been some tweaks on the cost model?

@ch1bo ch1bo requested review from a user and KtorZ January 4, 2022 20:18
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

I am surprised by the fact plutus compiler does not balk at the lack of INLINABLE pragmas for the Hash validator. There's something I still don't understand here.

@@ -100,6 +118,54 @@ spec = describe "On-chain contracts" $ do
prop "does not survive random adversarial mutations" $
propMutation healthyCloseTx genCloseMutation

describe "Hash" $
Copy link

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.

@KtorZ
Copy link
Collaborator

KtorZ commented Jan 5, 2022

The approach looks fine, but the results look odd for SHA3 with a negative delta on the first cases? Could it be because of the equalsByteString in the validator which takes longer for short bytestring than hashing it and comparing a longer hash? (This makes no sense to me... but since the code for the baseline is slightly different than the code for hashes, then it may explain things..).

Note that, for the encoding spike, I ended up writing validator as \_ \arg \_ -> let _whatever = myComputation arg in True which still evaluates the computation because of the evaluator strictness. The baseline validator is then just \_ \_ \_ -> True.

@ch1bo
Copy link
Member Author

ch1bo commented Jan 5, 2022

The approach looks fine, but the results look odd for SHA3 with a negative delta on the first cases? Could it be because of the equalsByteString in the validator which takes longer for short bytestring than hashing it and comparing a longer hash? (This makes no sense to me... but since the code for the baseline is slightly different than the code for hashes, then it may explain things..).

Yes. That was also my guess.

Note that, for the encoding spike, I ended up writing validator as \_ \arg \_ -> let _whatever = myComputation arg in True which still evaluates the computation because of the evaluator strictness. The baseline validator is then just \_ \_ \_ -> True.

I tried this as well and it resulted in much more surprising results 😕
image

@KtorZ
Copy link
Collaborator

KtorZ commented Jan 5, 2022

I tried this as well and it resulted in much more surprising results confused

o.O

ch1bo added 7 commits January 5, 2022 16:41
The inspect-script binary reports a serialized size of 5KB for this
script validator!
Ideally we would have a standalone executable for things like this, but
the 'evaluateTx' and included 'Fixture's are not available outside of
hydra-node tests.
@ch1bo ch1bo force-pushed the ch1bo/spike-hash-script branch from e315dcd to 917127f Compare January 5, 2022 15:41
@github-actions
Copy link

github-actions bot commented Jan 5, 2022

Unit Test Results

    5 files    68 suites   3m 40s ⏱️
191 tests 188 ✔️ 3 💤 0

Results for commit 917127f.

@ch1bo ch1bo merged commit c109558 into master Jan 5, 2022
@ch1bo ch1bo deleted the ch1bo/spike-hash-script branch January 5, 2022 16:46
ch1bo added a commit that referenced this pull request May 4, 2022
The results are not so useful and have been historically recorded in our
reports and in #155
ch1bo added a commit that referenced this pull request May 4, 2022
The results are not so useful and have been historically recorded in our
reports and in #155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants