Skip to content

Commit

Permalink
[ADP-322] Introduce crypto-primitives package (#4489)
Browse files Browse the repository at this point in the history
<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
✓ Self-review your changes to make sure nothing unexpected slipped
through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->

We are renaming crypto-hash-extra to crypto-primitives.
We are adding all cryptonite resources that are used elsewhere and
adopting logical structure.
We are removing cryptonite dependence from other packages and base on
crypto-primitives.

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number
adp-322
<!-- Reference the Jira/GitHub issue that this PR relates to, and which
requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->
  • Loading branch information
paweljakubas authored Mar 8, 2024
2 parents b2b331d + 8a285f0 commit a626766
Show file tree
Hide file tree
Showing 59 changed files with 239 additions and 140 deletions.
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ packages:
lib/balance-tx/
lib/benchmarks/
lib/cardano-api-extra/
lib/crypto-hash-extra/
lib/crypto-primitives/
lib/coin-selection/
lib/customer-deposit-wallet/
lib/delta-store/
Expand Down
4 changes: 2 additions & 2 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ cradle:
- path: "lib/coin-selection/bench"
component: "cardano-coin-selection:bench:utxo-index"

- path: "lib/crypto-hash-extra/src"
component: "lib:crypto-hash-extra"
- path: "lib/crypto-primitives/src"
component: "lib:crypto-primitives"

- path: "lib/customer-deposit-wallet/src"
component: "lib:customer-deposit-wallet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ library
, cardano-wallet-secrets
, cborg
, containers
, crypto-hash-extra
, cryptonite
, crypto-primitives
, data-interval
, deepseq
, digest
Expand Down
14 changes: 7 additions & 7 deletions lib/address-derivation-discovery/lib/Cardano/Byron/Codec/Cbor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ import Control.Monad
( replicateM
, when
)
import Crypto.Error
import Cryptography.Core
( CryptoError (..)
, CryptoFailable (..)
)
import Crypto.Hash
( hash
)
import Crypto.Hash.Algorithms
import Cryptography.Hash.Blake
( Blake2b_224
, SHA3_256
)
import Cryptography.Hash.Core
( SHA3_256
, hash
)
import Data.ByteString
( ByteString
Expand All @@ -107,7 +107,7 @@ import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Read as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.Cipher.ChaChaPoly1305 as Poly
import qualified Cryptography.Cipher.ChaChaPoly1305 as Poly
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ import Control.Lens
( Lens
, lens
)
import Crypto.Hash.Algorithms
( SHA512 (..)
)
import Crypto.Hash.Extra
import Cryptography.Hash.Blake
( blake2b256
)
import Cryptography.Hash.Core
( SHA512 (..)
)
import Data.ByteArray
( ScrubbedBytes
)
Expand All @@ -114,7 +114,7 @@ import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Wallet.Address.Derivation as W
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.KDF.PBKDF2 as PBKDF2
import qualified Cryptography.KDF.PBKDF2 as PBKDF2
import qualified Data.ByteArray as BA

{-------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ import Control.Lens
import Control.Monad
( (<=<)
)
import Crypto.Error
import Cryptography.Core
( eitherCryptoError
)
import Crypto.Hash.Algorithms
( SHA256 (..)
, SHA512 (..)
)
import Crypto.MAC.HMAC
import Cryptography.Hash.Core
( HMAC
, SHA256 (..)
, SHA512 (..)
, hmac
)
import Data.Bifunctor
Expand Down Expand Up @@ -144,8 +142,8 @@ import GHC.Generics
import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Crypto.Wallet as CC
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.ECC.Edwards25519 as Ed25519
import qualified Crypto.KDF.PBKDF2 as PBKDF2
import qualified Cryptography.ECC.Edwards25519 as Ed25519
import qualified Cryptography.KDF.PBKDF2 as PBKDF2
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import qualified Data.Text as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ import Cardano.Wallet.Primitive.Types.Address
import Control.Monad
( (<=<)
)
import Crypto.Hash.Algorithms
( Blake2b_224 (..)
)
import Crypto.Hash.Extra
import Cryptography.Hash.Blake
( blake2b224
)
import Crypto.Hash.IO
( HashAlgorithm (hashDigestSize)
, hashSizeBlake2b224
)
import Data.ByteString
( ByteString
Expand Down Expand Up @@ -168,7 +163,7 @@ instance MkKeyFingerprint SharedKey Address where
enterpriseAddr = 0b01110000 -- scripthash
rewardAcct = 0b11110000 -- scripthash
in if addrType `elem` [baseAddr, enterpriseAddr, rewardAcct] then
Right $ KeyFingerprint $ BS.take hashSize rest
Right $ KeyFingerprint $ BS.take hashSizeBlake2b224 rest
else
Left $ ErrInvalidAddress addr (Proxy @SharedKey)

Expand All @@ -181,10 +176,3 @@ instance
where
paymentKeyFingerprint (_, paymentK) =
Right $ KeyFingerprint $ blake2b224 $ xpubPublicKey $ getKey paymentK

{-------------------------------------------------------------------------------
Internals
-------------------------------------------------------------------------------}

hashSize :: Int
hashSize = hashDigestSize Blake2b_224
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,9 @@ import Control.Monad
( guard
, (<=<)
)
import Crypto.Hash.Algorithms
( Blake2b_224 (..)
)
import Crypto.Hash.Extra
import Cryptography.Hash.Blake
( blake2b224
)
import Crypto.Hash.IO
( HashAlgorithm (hashDigestSize)
, hashSizeBlake2b224
)
import Data.Binary.Put
( putByteString
Expand Down Expand Up @@ -359,7 +354,7 @@ instance MkKeyFingerprint ShelleyKey Address where
enterpriseAddr = 0b01100000 -- keyhash
rewardAcct = 0b11100000 -- keyhash
in if addrType `elem` [baseAddr, enterpriseAddr, rewardAcct] then
Right $ KeyFingerprint $ BS.take hashSize rest
Right $ KeyFingerprint $ BS.take hashSizeBlake2b224 rest
else
Left $ ErrInvalidAddress addr (Proxy @ShelleyKey)

Expand Down Expand Up @@ -436,11 +431,3 @@ instance PersistPublicKey (ShelleyKey depth) where
either err ShelleyKey . (xpub <=< fromHex @ByteString)
where
err _ = error "unsafeDeserializeXPub: unable to deserialize ShelleyKey"

{-------------------------------------------------------------------------------
Internals
-------------------------------------------------------------------------------}

hashSize :: Int
hashSize =
hashDigestSize Blake2b_224
4 changes: 2 additions & 2 deletions lib/benchmarks/cardano-wallet-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ benchmark restore
, cardano-wallet:{cardano-wallet, cardano-wallet-api-http}
, containers
, contra-tracer
, crypto-hash-extra
, crypto-primitives
, filepath
, fmt
, iohk-monitoring
Expand Down Expand Up @@ -173,7 +173,7 @@ benchmark db
, containers
, contra-tracer
, criterion
, cryptonite
, crypto-primitives
, deepseq
, directory
, filepath
Expand Down
2 changes: 1 addition & 1 deletion lib/benchmarks/exe/db-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ import Criterion.Main
, defaultMain
, perRunEnvWithCleanup
)
import Crypto.Hash
import Cryptography.Hash.Core
( hash
)
import Data.ByteString
Expand Down
2 changes: 1 addition & 1 deletion lib/benchmarks/exe/restore-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ import Control.Tracer
( Tracer (..)
, traceWith
)
import Crypto.Hash.Extra
import Cryptography.Hash.Blake
( blake2b256
)
import Data.Aeson
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 3.0
name: crypto-hash-extra
name: crypto-primitives
version: 0.1.0.0
synopsis: Extra functionality for cryptographic hashing
synopsis: Cryptographic primitives
license: Apache-2.0
author: Cardano Foundation (High Assurance Lab)
maintainer: [email protected]
Expand All @@ -24,7 +24,13 @@ library
-freverse-errors

hs-source-dirs: src
exposed-modules: Crypto.Hash.Extra
exposed-modules: Cryptography.Cipher.ChaChaPoly1305
Cryptography.Core
Cryptography.ECC.Edwards25519
Cryptography.Hash.Blake
Cryptography.Hash.Core
Cryptography.KDF.PBKDF2
Cryptography.KDF.Scrypt
build-depends:
, base >= 4.14.3 && < 4.19
, bytestring >= 0.10.12 && < 0.13
Expand Down
17 changes: 17 additions & 0 deletions lib/crypto-primitives/src/Cryptography/Cipher/ChaChaPoly1305.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Cryptography.Cipher.ChaChaPoly1305
( decrypt
, encrypt
, initialize
, finalize
, finalizeAAD
, nonce12
) where

import Crypto.Cipher.ChaChaPoly1305
( decrypt
, encrypt
, finalize
, finalizeAAD
, initialize
, nonce12
)
16 changes: 16 additions & 0 deletions lib/crypto-primitives/src/Cryptography/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Cryptography.Core
( CryptoError (..)
, CryptoFailable (..)
, eitherCryptoError

, MonadRandom (..)
) where

import Crypto.Error
( CryptoError (..)
, CryptoFailable (..)
, eitherCryptoError
)
import Crypto.Random.Types
( MonadRandom (..)
)
11 changes: 11 additions & 0 deletions lib/crypto-primitives/src/Cryptography/ECC/Edwards25519.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Cryptography.ECC.Edwards25519
( pointEncode
, scalarDecodeLong
, toPoint
) where

import Crypto.ECC.Edwards25519
( pointEncode
, scalarDecodeLong
, toPoint
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}

module Crypto.Hash.Extra
( blake2b256
module Cryptography.Hash.Blake
( Blake2b_160
, Blake2b_224
, Blake2b_256

, blake2b256
, blake2b224
, hashSizeBlake2b224
) where

import Prelude
Expand All @@ -11,8 +17,12 @@ import Crypto.Hash
( hash
)
import Crypto.Hash.Algorithms
( Blake2b_224
, Blake2b_256
( Blake2b_160 (..)
, Blake2b_224 (..)
, Blake2b_256 (..)
)
import Crypto.Hash.IO
( HashAlgorithm (hashDigestSize)
)
import Data.ByteArray
( ByteArrayAccess
Expand All @@ -30,3 +40,6 @@ blake2b256 = BA.convert . hash @_ @Blake2b_256
-- | Hash a byte string using Blake2b with a 224-bit (28-byte) digest.
blake2b224 :: ByteArrayAccess a => a -> ByteString
blake2b224 = BA.convert . hash @_ @Blake2b_224

hashSizeBlake2b224 :: Int
hashSizeBlake2b224 = hashDigestSize Blake2b_224
38 changes: 38 additions & 0 deletions lib/crypto-primitives/src/Cryptography/Hash/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Cryptography.Hash.Core
( HashAlgorithm
, MD4 (..)
, MD5 (..)
, SHA1 (..)
, SHA224 (..)
, SHA256 (..)
, SHA512 (..)
, SHA3_256 (..)
, hash

, HMAC
, hmac

, Digest
, digestFromByteString

) where

import Crypto.Hash
( Digest
, digestFromByteString
, hash
)
import Crypto.Hash.Algorithms
( HashAlgorithm
, MD4 (..)
, MD5 (..)
, SHA1 (..)
, SHA224 (..)
, SHA256 (..)
, SHA3_256 (..)
, SHA512 (..)
)
import Crypto.MAC.HMAC
( HMAC
, hmac
)
13 changes: 13 additions & 0 deletions lib/crypto-primitives/src/Cryptography/KDF/PBKDF2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Cryptography.KDF.PBKDF2
( Parameters (..)
, fastPBKDF2_SHA512
, generate
, prfHMAC
) where

import Crypto.KDF.PBKDF2
( Parameters (..)
, fastPBKDF2_SHA512
, generate
, prfHMAC
)
Loading

0 comments on commit a626766

Please sign in to comment.