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

Keccak-256 and Blake2b-224 (PLT-6305, PLT-6306, PLT-6311, PLT-6820) #5431

Merged
merged 26 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import PlutusBenchmark.Common
import System.IO (Handle)

import PlutusCore (DefaultFun, DefaultUni)
import PlutusCore.Crypto.Hash qualified as Hash
import PlutusTx qualified as Tx
import UntypedPlutusCore qualified as UPLC

Expand All @@ -31,7 +32,6 @@ import Cardano.Crypto.DSIGN.Ed25519 (Ed25519DSIGN)
import Cardano.Crypto.Seed (mkSeedFromBytes)

import Data.ByteString (ByteString)
import Data.ByteString.Hash qualified as Hash
import Hedgehog.Internal.Gen qualified as G
import Hedgehog.Internal.Range qualified as R
import System.IO.Unsafe (unsafePerformIO)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Added

- `keccak_256` builtin
- `blake2b_224` builtin

Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,5 @@ makeBenchmarks gen = [ benchVerifyEd25519Signature
, benchVerifyEcdsaSecp256k1Signature
, benchVerifySchnorrSecp256k1Signature
]
<> (benchByteStringOneArgOp <$> [Sha2_256, Sha3_256, Blake2b_256])
<> (benchByteStringOneArgOp <$> [Sha2_256, Sha3_256, Blake2b_224, Blake2b_256, Keccak_256])
<> blsBenchmarks gen
26 changes: 22 additions & 4 deletions plutus-core/cost-model/create-cost-model/CreateBuiltinCostModel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module CreateBuiltinCostModel where
import PlutusCore.Crypto.BLS12_381.G1 qualified as G1
import PlutusCore.Crypto.BLS12_381.G2 qualified as G2
import PlutusCore.Crypto.BLS12_381.Pairing qualified as Pairing
import PlutusCore.Crypto.Hash qualified as Hash
import PlutusCore.Evaluation.Machine.BuiltinCostModel
import PlutusCore.Evaluation.Machine.CostStream
import PlutusCore.Evaluation.Machine.ExMemory
Expand All @@ -20,7 +21,6 @@ import Barbies (bmap, bsequence)
import Control.Applicative (Const (Const, getConst))
import Control.Exception (TypeError (TypeError))
import Control.Monad.Catch (throwM)
import Data.ByteString.Hash qualified as PlutusHash
import Data.ByteString.Lazy qualified as BSL (fromStrict)
import Data.Coerce (coerce)
import Data.Csv (FromNamedRecord, FromRecord, HasHeader (HasHeader), decode, parseNamedRecord, (.:))
Expand Down Expand Up @@ -121,6 +121,8 @@ builtinCostModelNames = BuiltinCostModelBase
, paramBls12_381_millerLoop = "bls12_381_millerLoopModel"
, paramBls12_381_mulMlResult = "bls12_381_mulMlResultModel"
, paramBls12_381_finalVerify = "bls12_381_finalVerifyModel"
, paramBlake2b_224 = "blake2b_224Model"
, paramKeccak_256 = "keccak_256Model"
}


Expand Down Expand Up @@ -232,6 +234,9 @@ createBuiltinCostModel bmfile rfile = do
paramBls12_381_mulMlResult <- getParams bls12_381_mulMlResult paramBls12_381_mulMlResult
paramBls12_381_finalVerify <- getParams bls12_381_finalVerify paramBls12_381_finalVerify

paramKeccak_256 <- getParams keccak_256 paramKeccak_256
paramBlake2b_224 <- getParams blake2b_224 paramBlake2b_224

pure $ BuiltinCostModelBase {..}

-- The output of `tidy(model)` on the R side.
Expand Down Expand Up @@ -477,21 +482,34 @@ lessThanEqualsByteString = lessThanByteString
sha2_256 :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
sha2_256 cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ PlutusHash.sha2_256 "")
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ Hash.sha2_256 "")
pure $ CostingFun cpuModel memModel

sha3_256 :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
sha3_256 cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ PlutusHash.sha3_256 "")
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ Hash.sha3_256 "")
pure $ CostingFun cpuModel memModel

blake2b_224 :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
blake2b_224 cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ Hash.blake2b_224 "")
pure $ CostingFun cpuModel memModel

blake2b_256 :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
blake2b_256 cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ PlutusHash.blake2b_256 "")
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ Hash.blake2b_256 "")
pure $ CostingFun cpuModel memModel

keccak_256 :: MonadR m => (SomeSEXP (Region m)) -> m (CostingFun ModelOneArgument)
keccak_256 cpuModelR = do
cpuModel <- ModelOneArgumentLinearCost <$> readModelLinearInX cpuModelR
let memModel = ModelOneArgumentConstantCost (memoryUsageAsCostingInteger $ Hash.keccak_256 "")
pure $ CostingFun cpuModel memModel


-- NB: the R model is based purely on the size of the second argument (since the
-- first and third are constant size), so we have to rearrange things a bit to
-- get it to work with a three-argument costing function.
Expand Down
Loading