Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ library
Cardano.Api.Internal.Utils
Cardano.Api.Key.Internal
Cardano.Api.Key.Internal.Class
Cardano.Api.Key.Internal.Leios
Cardano.Api.Key.Internal.Mnemonic
Cardano.Api.Key.Internal.Praos
Cardano.Api.Key.Internal.SomeAddressVerificationKey
Expand Down
8 changes: 7 additions & 1 deletion cardano-api/src/Cardano/Api/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Cardano.Api.Key
, generateInsecureSigningKey
, CastVerificationKeyRole (..)
, CastSigningKeyRole (..)
, AsType (AsVerificationKey, AsSigningKey)
, AsType (AsVerificationKey, AsSigningKey, AsBlsKey)

-- * Hash
, Hash (..)
Expand Down Expand Up @@ -68,6 +68,11 @@ module Cardano.Api.Key
-- ** Signing
, signArbitraryBytesKes

-- * Leios

-- | BLS12-381 key type
, BlsKey

-- ** Type proxy
, HasTypeProxy (..)
, asType
Expand All @@ -79,6 +84,7 @@ where
import Cardano.Api.Hash
import Cardano.Api.Key.Internal
import Cardano.Api.Key.Internal.Class
import Cardano.Api.Key.Internal.Leios
import Cardano.Api.Key.Internal.Mnemonic
import Cardano.Api.Key.Internal.Praos
import Cardano.Api.Key.Internal.SomeAddressVerificationKey
141 changes: 141 additions & 0 deletions cardano-api/src/Cardano/Api/Key/Internal/Leios.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

-- | Leios specific key types and their 'Key' class instances
module Cardano.Api.Key.Internal.Leios
( -- * Key types
BlsKey

-- * Data family instances
, AsType (..)
, Hash (..)
, VerificationKey (..)
, SigningKey (..)
)
where

import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.Key.Internal.Class
import Cardano.Api.Pretty
import Cardano.Api.Serialise.Bech32
import Cardano.Api.Serialise.Cbor
import Cardano.Api.Serialise.Raw
import Cardano.Api.Serialise.SerialiseUsing
import Cardano.Api.Serialise.TextEnvelope.Internal

import Cardano.Crypto.DSIGN.BLS12381 qualified as Crypto
import Cardano.Crypto.DSIGN.Class qualified as Crypto
import Cardano.Crypto.Hash.Class qualified as Crypto
import Cardano.Ledger.Hashes (HASH)

import Data.Either.Combinators (maybeToRight)
import Data.String (IsString (..))

--
-- BLS keys
--

data BlsKey

instance HasTypeProxy BlsKey where
data AsType BlsKey = AsBlsKey
proxyToAsType _ = AsBlsKey

instance Key BlsKey where
newtype VerificationKey BlsKey
= BlsVerificationKey (Crypto.VerKeyDSIGN Crypto.BLS12381MinSigDSIGN)
deriving stock Eq
deriving (Show, Pretty) via UsingRawBytesHex (VerificationKey BlsKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

newtype SigningKey BlsKey
= BlsSigningKey (Crypto.SignKeyDSIGN Crypto.BLS12381MinSigDSIGN)
deriving (Show, Pretty) via UsingRawBytesHex (SigningKey BlsKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

deterministicSigningKey :: AsType BlsKey -> Crypto.Seed -> SigningKey BlsKey
deterministicSigningKey AsBlsKey =
BlsSigningKey . Crypto.genKeyDSIGN

deterministicSigningKeySeedSize :: AsType BlsKey -> Word
deterministicSigningKeySeedSize AsBlsKey =
Crypto.seedSizeDSIGN proxy
where
proxy :: Proxy Crypto.BLS12381MinSigDSIGN
proxy = Proxy

getVerificationKey :: SigningKey BlsKey -> VerificationKey BlsKey
getVerificationKey (BlsSigningKey sk) =
BlsVerificationKey (Crypto.deriveVerKeyDSIGN sk)

verificationKeyHash :: VerificationKey BlsKey -> Hash BlsKey
verificationKeyHash (BlsVerificationKey vkey) =
BlsKeyHash (Crypto.hashVerKeyDSIGN vkey)

instance SerialiseAsRawBytes (VerificationKey BlsKey) where
serialiseToRawBytes (BlsVerificationKey vk) =
Crypto.rawSerialiseVerKeyDSIGN vk

deserialiseFromRawBytes (AsVerificationKey AsBlsKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey BlsKey") $
BlsVerificationKey <$> Crypto.rawDeserialiseVerKeyDSIGN bs

instance SerialiseAsRawBytes (SigningKey BlsKey) where
serialiseToRawBytes (BlsSigningKey sk) =
Crypto.rawSerialiseSignKeyDSIGN sk

deserialiseFromRawBytes (AsSigningKey AsBlsKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey BlsKey") $
BlsSigningKey <$> Crypto.rawDeserialiseSignKeyDSIGN bs

instance SerialiseAsBech32 (VerificationKey BlsKey) where
bech32PrefixFor _ = unsafeHumanReadablePartFromText "bls_vk"
bech32PrefixesPermitted _ = unsafeHumanReadablePartFromText <$> ["bls_vk"]

instance SerialiseAsBech32 (SigningKey BlsKey) where
bech32PrefixFor _ = unsafeHumanReadablePartFromText "bls_sk"
bech32PrefixesPermitted _ = unsafeHumanReadablePartFromText <$> ["bls_sk"]

newtype instance Hash BlsKey
= BlsKeyHash
( Crypto.Hash
HASH
(Crypto.VerKeyDSIGN Crypto.BLS12381MinSigDSIGN)
)
deriving stock (Eq, Ord)
deriving (Show, Pretty) via UsingRawBytesHex (Hash BlsKey)
deriving (ToCBOR, FromCBOR) via UsingRawBytes (Hash BlsKey)
deriving anyclass SerialiseAsCBOR

instance SerialiseAsRawBytes (Hash BlsKey) where
serialiseToRawBytes (BlsKeyHash vkh) =
Crypto.hashToBytes vkh

deserialiseFromRawBytes (AsHash AsBlsKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash BlsKey") $
BlsKeyHash <$> Crypto.hashFromBytes bs

instance HasTextEnvelope (VerificationKey BlsKey) where
textEnvelopeType _ =
"BlsVerificationKey_"
<> fromString (Crypto.algorithmNameDSIGN proxy)
where
proxy :: Proxy Crypto.BLS12381MinSigDSIGN
proxy = Proxy

instance HasTextEnvelope (SigningKey BlsKey) where
textEnvelopeType _ =
"BlsSigningKey_"
<> fromString (Crypto.algorithmNameDSIGN proxy)
where
proxy :: Proxy Crypto.BLS12381MinSigDSIGN
proxy = Proxy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Cardano.Api.Byron.Internal.Key
import Cardano.Api.HasTypeProxy
import Cardano.Api.Key.Internal
import Cardano.Api.Key.Internal.Class
import Cardano.Api.Key.Internal.Leios (AsType (..), BlsKey)
import Cardano.Api.Key.Internal.Praos
import Cardano.Api.Serialise.Bech32
import Cardano.Api.Serialise.DeserialiseAnyOf
Expand All @@ -44,6 +45,7 @@ data SomeAddressVerificationKey
(VerificationKey GenesisDelegateExtendedKey)
| AKesVerificationKey (VerificationKey KesKey)
| AVrfVerificationKey (VerificationKey VrfKey)
| ABlsVerificationKey (VerificationKey BlsKey)
| AStakeVerificationKey (VerificationKey StakeKey)
| AStakeExtendedVerificationKey (VerificationKey StakeExtendedKey)
| AStakePoolVerificationKey (VerificationKey StakePoolKey)
Expand Down Expand Up @@ -75,6 +77,7 @@ renderSomeAddressVerificationKey =
in serialiseToBech32 stakePoolKey
AKesVerificationKey vk -> serialiseToBech32 vk
AVrfVerificationKey vk -> serialiseToBech32 vk
ABlsVerificationKey vk -> serialiseToBech32 vk
AStakeVerificationKey vk -> serialiseToBech32 vk
AStakePoolVerificationKey vk -> serialiseToBech32 vk
AStakePoolExtendedVerificationKey vk -> serialiseToBech32 vk
Expand All @@ -100,6 +103,7 @@ mapSomeAddressVerificationKey f = \case
AGenesisDelegateExtendedVerificationKey vk -> f vk
AGenesisExtendedVerificationKey vk -> f vk
AVrfVerificationKey vk -> f vk
ABlsVerificationKey vk -> f vk
AStakeVerificationKey vk -> f vk
AStakePoolVerificationKey vk -> f vk
AStakePoolExtendedVerificationKey vk -> f vk
Expand Down Expand Up @@ -154,6 +158,7 @@ deserialiseAnyVerificationKeyBech32 =
, FromSomeType (AsVerificationKey AsPaymentExtendedKey) APaymentExtendedVerificationKey
, FromSomeType (AsVerificationKey AsKesKey) AKesVerificationKey
, FromSomeType (AsVerificationKey AsVrfKey) AVrfVerificationKey
, FromSomeType (AsVerificationKey AsBlsKey) ABlsVerificationKey
, FromSomeType (AsVerificationKey AsStakeKey) AStakeVerificationKey
, FromSomeType (AsVerificationKey AsStakeExtendedKey) AStakeExtendedVerificationKey
, FromSomeType (AsVerificationKey AsStakePoolKey) AStakePoolVerificationKey
Expand Down
Loading