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
17 changes: 17 additions & 0 deletions cardano-crypto-class/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

## 2.3.0.0

* Rename types in the `KES` class to use the suffix of `Size` instead of the prefix.
* Rename types in the `Hash` class to use the suffix of `Size` instead of the prefix.
* Rename types in the `DSIGN` class to use the suffix of `Size` instead of the prefix.
* Deprecated type `SizeVerKeyDSIGN` in favor of `VerKeySizeDSIGN`
* Deprecated function `sizeVerKeyDSIGN` in favor of `verKeySizeDSIGN`
* Deprecated type `SizeSignKeyDSIGN` in favor of `SignKeySizeDSIGN`
* Deprecated function `sizeSignKeyDSIGN` in favor of `signKeySizeDSIGN`
* Deprecated type `SizeSigDSIGN` in favor of `SigSizeDSIGN`
* Deprecated function `sizeSigDSIGN` in favor of `sigSizeDSIGN`
* Deprecated type `SizeHash` in favor of `HashSize`
* Deprecated function `sizeHash` in favor of `hashSize`
* Deprecated type `SizeVerKeyKES` in favor of `VerKeySizeKES`
* Deprecated function `sizeVerKeyKES` in favor of `verKeySizeKES`
* Deprecated type `SizeSignKeyKES` in favor of `SignKeySizeKES`
* Deprecated function `sizeSignKeyKES` in favor of `signKeySizeKES`
* Deprecated type `SizeSigKES` in favor of `SigSizeKES`
* Deprecated function `sizeSigKES` in favor of `sigSizeKES`
* Expose `Cardano.Crypto.PackedBytes` module with a safe API
* Remove `Serialise` instance for `PackedBytes` as unused
* Switch `OutputVRF` to use `ByteArray` instead of `ByteString`. Change field accessor name to `getOutputVRFByteArray`
Expand Down
12 changes: 6 additions & 6 deletions cardano-crypto-class/src/Cardano/Crypto/DSIGN/BLS12381.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import Cardano.Crypto.DSIGN.Class (
KeyGenContextDSIGN,
SeedSizeDSIGN,
SigDSIGN,
SigSizeDSIGN,
SignKeyDSIGN,
SignKeySizeDSIGN,
Signable,
SizeSigDSIGN,
SizeSignKeyDSIGN,
SizeVerKeyDSIGN,
VerKeyDSIGN,
VerKeySizeDSIGN,
algorithmNameDSIGN,
deriveVerKeyDSIGN,
genKeyDSIGN,
Expand Down Expand Up @@ -161,12 +161,12 @@ instance
DSIGNAlgorithm (BLS12381DSIGN curve)
where
type SeedSizeDSIGN (BLS12381DSIGN curve) = CARDANO_BLST_SCALAR_SIZE
type SizeSignKeyDSIGN (BLS12381DSIGN curve) = CARDANO_BLST_SCALAR_SIZE
type SignKeySizeDSIGN (BLS12381DSIGN curve) = CARDANO_BLST_SCALAR_SIZE

-- These *Sizes* are used in the serialization/deserialization
-- so these use the compressed sizes of the BLS12-381 `Point curve`
type SizeVerKeyDSIGN (BLS12381DSIGN curve) = CompressedPointSize curve
type SizeSigDSIGN (BLS12381DSIGN curve) = CompressedPointSize (DualCurve curve)
type VerKeySizeDSIGN (BLS12381DSIGN curve) = CompressedPointSize curve
type SigSizeDSIGN (BLS12381DSIGN curve) = CompressedPointSize (DualCurve curve)
type Signable (BLS12381DSIGN curve) = SignableRepresentation

-- Context can hold domain separation tag and/or augmentation data for signatures
Expand Down
88 changes: 58 additions & 30 deletions cardano-crypto-class/src/Cardano/Crypto/DSIGN/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ module Cardano.Crypto.DSIGN.Class (
DSIGNAlgorithm (..),
Seed,
seedSizeDSIGN,
verKeySizeDSIGN,
signKeySizeDSIGN,
sigSizeDSIGN,

-- * Deprecated size synonyms
sizeVerKeyDSIGN,
sizeSignKeyDSIGN,
sizeSigDSIGN,
Expand Down Expand Up @@ -60,7 +65,7 @@ module Cardano.Crypto.DSIGN.Class (
-- * Aggregatable DSIGN algorithms with Proof of Possession
DSIGNAggregatable (..),
aggregateVerKeysDSIGN,
sizePossessionProofDSIGN,
possessionProofSizeDSIGN,
encodePossessionProofDSIGN,
decodePossessionProofDSIGN,
encodedPossessionProofDSIGNSizeExpr,
Expand Down Expand Up @@ -90,6 +95,10 @@ import Cardano.Crypto.Libsodium.MLockedSeed
import Cardano.Crypto.Seed
import Cardano.Crypto.Util (Empty)

{-# DEPRECATED SizeVerKeyDSIGN "In favor of `VerKeySizeDSIGN`" #-}
{-# DEPRECATED SizeSignKeyDSIGN "In favor of `SignKeySizeDSIGN`" #-}
{-# DEPRECATED SizeSigDSIGN "In favor of `SigSizeDSIGN`" #-}

-- | The pure DSIGN API, which supports the full set of DSIGN operations, but
-- does not allow for secure forgetting of private keys.
class
Expand All @@ -103,16 +112,23 @@ class
, NoThunks (SignKeyDSIGN v)
, NoThunks (VerKeyDSIGN v)
, KnownNat (SeedSizeDSIGN v)
, KnownNat (SizeVerKeyDSIGN v)
, KnownNat (SizeSignKeyDSIGN v)
, KnownNat (SizeSigDSIGN v)
, KnownNat (SignKeySizeDSIGN v)
, KnownNat (VerKeySizeDSIGN v)
, KnownNat (SigSizeDSIGN v)
) =>
DSIGNAlgorithm v
where
type SeedSizeDSIGN v :: Nat
type SizeVerKeyDSIGN v :: Nat
type SignKeySizeDSIGN v :: Nat
type VerKeySizeDSIGN v :: Nat
type SigSizeDSIGN v :: Nat

type SizeSignKeyDSIGN v :: Nat
type SizeSignKeyDSIGN v = SignKeySizeDSIGN v
type SizeVerKeyDSIGN v :: Nat
type SizeVerKeyDSIGN v = VerKeySizeDSIGN v
type SizeSigDSIGN v :: Nat
type SizeSigDSIGN v = SigSizeDSIGN v

--
-- Key and signature types
Expand Down Expand Up @@ -209,16 +225,28 @@ instance
where
compare = error "unsupported"

{-# DEPRECATED sizeVerKeyDSIGN "In favor of `verKeySizeDSIGN`" #-}
sizeVerKeyDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeVerKeyDSIGN = verKeySizeDSIGN

{-# DEPRECATED sizeSignKeyDSIGN "In favor of `signKeySizeDSIGN`" #-}
sizeSignKeyDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeSignKeyDSIGN = signKeySizeDSIGN

{-# DEPRECATED sizeSigDSIGN "In favor of `sigSizeDSIGN`" #-}
sizeSigDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeSigDSIGN = sigSizeDSIGN

-- | The upper bound on the 'Seed' size needed by 'genKeyDSIGN'
seedSizeDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
seedSizeDSIGN _ = fromInteger (natVal (Proxy @(SeedSizeDSIGN v)))

sizeVerKeyDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeVerKeyDSIGN _ = fromInteger (natVal (Proxy @(SizeVerKeyDSIGN v)))
sizeSignKeyDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeSignKeyDSIGN _ = fromInteger (natVal (Proxy @(SizeSignKeyDSIGN v)))
sizeSigDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sizeSigDSIGN _ = fromInteger (natVal (Proxy @(SizeSigDSIGN v)))
verKeySizeDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
verKeySizeDSIGN _ = fromInteger (natVal (Proxy @(VerKeySizeDSIGN v)))
signKeySizeDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
signKeySizeDSIGN _ = fromInteger (natVal (Proxy @(SignKeySizeDSIGN v)))
sigSizeDSIGN :: forall v proxy. DSIGNAlgorithm v => proxy v -> Word
sigSizeDSIGN _ = fromInteger (natVal (Proxy @(SigSizeDSIGN v)))

--
-- Convenient CBOR encoding/decoding
Expand All @@ -240,22 +268,22 @@ decodeVerKeyDSIGN = do
bs <- decodeBytes
case rawDeserialiseVerKeyDSIGN bs of
Just vk -> return vk
Nothing -> failSizeCheck "decodeVerKeyDSIGN" "key" bs (sizeVerKeyDSIGN (Proxy :: Proxy v))
Nothing -> failSizeCheck "decodeVerKeyDSIGN" "key" bs (verKeySizeDSIGN (Proxy :: Proxy v))
{-# INLINE decodeVerKeyDSIGN #-}

decodeSignKeyDSIGN :: forall v s. DSIGNAlgorithm v => Decoder s (SignKeyDSIGN v)
decodeSignKeyDSIGN = do
bs <- decodeBytes
case rawDeserialiseSignKeyDSIGN bs of
Just sk -> return sk
Nothing -> failSizeCheck "decodeSignKeyDSIGN" "key" bs (sizeSignKeyDSIGN (Proxy :: Proxy v))
Nothing -> failSizeCheck "decodeSignKeyDSIGN" "key" bs (signKeySizeDSIGN (Proxy :: Proxy v))

decodeSigDSIGN :: forall v s. DSIGNAlgorithm v => Decoder s (SigDSIGN v)
decodeSigDSIGN = do
bs <- decodeBytes
case rawDeserialiseSigDSIGN bs of
Just sig -> return sig
Nothing -> failSizeCheck "decodeSigDSIGN" "signature" bs (sizeSigDSIGN (Proxy :: Proxy v))
Nothing -> failSizeCheck "decodeSigDSIGN" "signature" bs (sigSizeDSIGN (Proxy :: Proxy v))
{-# INLINE decodeSigDSIGN #-}

-- | Helper function that always fails, but it provides a different message whenever
Expand Down Expand Up @@ -315,33 +343,33 @@ decodeSignedDSIGN = SignedDSIGN <$> decodeSigDSIGN
-- Encoded 'Size' expressions for 'ToCBOR' instances
--

-- | 'Size' expression for 'VerKeyDSIGN' which is using 'sizeVerKeyDSIGN'
-- | 'Size' expression for 'VerKeyDSIGN' which is using 'verKeySizeDSIGN'
-- encoded as 'Size'.
encodedVerKeyDSIGNSizeExpr :: forall v. DSIGNAlgorithm v => Proxy (VerKeyDSIGN v) -> Size
encodedVerKeyDSIGNSizeExpr _proxy =
-- 'encodeBytes' envelope
fromIntegral @Integer @Size (withWordSize (sizeVerKeyDSIGN (Proxy :: Proxy v)))
fromIntegral @Integer @Size (withWordSize (verKeySizeDSIGN (Proxy :: Proxy v)))
-- payload
+ fromIntegral @Word @Size (sizeVerKeyDSIGN (Proxy :: Proxy v))
+ fromIntegral @Word @Size (verKeySizeDSIGN (Proxy :: Proxy v))

-- | 'Size' expression for 'SignKeyDSIGN' which is using 'sizeSignKeyDSIGN'
-- | 'Size' expression for 'SignKeyDSIGN' which is using 'signKeySizeDSIGN'
-- encoded as 'Size'.
encodedSignKeyDSIGNSizeExpr :: forall v. DSIGNAlgorithm v => Proxy (SignKeyDSIGN v) -> Size
encodedSignKeyDSIGNSizeExpr _proxy =
-- 'encodeBytes' envelope
fromIntegral @Integer @Size
(withWordSize (sizeSignKeyDSIGN (Proxy :: Proxy v)))
(withWordSize (signKeySizeDSIGN (Proxy :: Proxy v)))
-- payload
+ fromIntegral @Word @Size (sizeSignKeyDSIGN (Proxy :: Proxy v))
+ fromIntegral @Word @Size (signKeySizeDSIGN (Proxy :: Proxy v))

-- | 'Size' expression for 'SigDSIGN' which is using 'sizeSigDSIGN' encoded as
-- | 'Size' expression for 'SigDSIGN' which is using 'sigSizeDSIGN' encoded as
-- 'Size'.
encodedSigDSIGNSizeExpr :: forall v. DSIGNAlgorithm v => Proxy (SigDSIGN v) -> Size
encodedSigDSIGNSizeExpr _proxy =
-- 'encodeBytes' envelope
fromIntegral @Integer @Size (withWordSize (sizeSigDSIGN (Proxy :: Proxy v)))
fromIntegral @Integer @Size (withWordSize (sigSizeDSIGN (Proxy :: Proxy v)))
-- payload
+ fromIntegral @Word @Size (sizeSigDSIGN (Proxy :: Proxy v))
+ fromIntegral @Word @Size (sigSizeDSIGN (Proxy :: Proxy v))

class (DSIGNAlgorithm v, NoThunks (SignKeyDSIGNM v)) => DSIGNMAlgorithm v where
data SignKeyDSIGNM v :: Type
Expand Down Expand Up @@ -469,7 +497,7 @@ decodeSignKeyDSIGNM = do
)
| otherwise -> error "decodeSignKeyDSIGNM: cannot decode key"
where
expected = fromIntegral @Word @Int (sizeSignKeyDSIGN (Proxy :: Proxy v))
expected = fromIntegral @Word @Int (signKeySizeDSIGN (Proxy :: Proxy v))
actual = BS.length bs

-- | Extension of the `DSIGNAlgorithm` to allow for aggregatable digital
Expand Down Expand Up @@ -546,8 +574,8 @@ aggregateVerKeysDSIGN ctx verKeysAndPoPs = do
forM_ verKeysAndPoPs $ uncurry (verifyPossessionProofDSIGN ctx)
uncheckedAggregateVerKeysDSIGN (map fst verKeysAndPoPs)

sizePossessionProofDSIGN :: forall v proxy. DSIGNAggregatable v => proxy v -> Word
sizePossessionProofDSIGN _ = fromInteger (natVal (Proxy @(PossessionProofSizeDSIGN v)))
possessionProofSizeDSIGN :: forall v proxy. DSIGNAggregatable v => proxy v -> Word
possessionProofSizeDSIGN _ = fromInteger (natVal (Proxy @(PossessionProofSizeDSIGN v)))

-- | Encode a PoP into CBOR.
encodePossessionProofDSIGN :: DSIGNAggregatable v => PossessionProofDSIGN v -> Encoding
Expand All @@ -565,14 +593,14 @@ decodePossessionProofDSIGN = do
"decodePossessionProof"
"proof of possession"
bs
(sizePossessionProofDSIGN (Proxy :: Proxy v))
(possessionProofSizeDSIGN (Proxy :: Proxy v))

-- | 'Size' expression for 'PossessionProofDSIGN' which is using 'sizePossessionProofDSIGN'
-- | 'Size' expression for 'PossessionProofDSIGN' which is using 'possessionProofSizeDSIGN'
-- encoded as 'Size'.
encodedPossessionProofDSIGNSizeExpr ::
forall v. DSIGNAggregatable v => Proxy (PossessionProofDSIGN v) -> Size
encodedPossessionProofDSIGNSizeExpr _proxy =
-- 'encodeBytes' envelope
fromIntegral @Integer @Size (withWordSize (sizePossessionProofDSIGN (Proxy :: Proxy v)))
fromIntegral @Integer @Size (withWordSize (possessionProofSizeDSIGN (Proxy :: Proxy v)))
-- payload
+ fromIntegral @Word @Size (sizePossessionProofDSIGN (Proxy :: Proxy v))
+ fromIntegral @Word @Size (possessionProofSizeDSIGN (Proxy :: Proxy v))
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ import Cardano.Crypto.DSIGN.Class (
DSIGNAlgorithm (
SeedSizeDSIGN,
SigDSIGN,
SigSizeDSIGN,
SignKeyDSIGN,
SignKeySizeDSIGN,
Signable,
SizeSigDSIGN,
SizeSignKeyDSIGN,
SizeVerKeyDSIGN,
VerKeyDSIGN,
VerKeySizeDSIGN,
algorithmNameDSIGN,
deriveVerKeyDSIGN,
genKeyDSIGN,
Expand All @@ -68,7 +68,7 @@ import Cardano.Crypto.DSIGN.Class (
encodedSignKeyDSIGNSizeExpr,
encodedVerKeyDSIGNSizeExpr,
)
import Cardano.Crypto.Hash.Class (HashAlgorithm (SizeHash, digest))
import Cardano.Crypto.Hash.Class (HashAlgorithm (HashSize, digest))
import Cardano.Crypto.PinnedSizedBytes (
PinnedSizedBytes,
psbCreateLen,
Expand Down Expand Up @@ -141,7 +141,7 @@ fromMessageHash (MH psb) = psbToByteString psb
-- | A helper to use with the 'HashAlgorithm' API, as this can ensure sizing.
hashAndPack ::
forall (h :: Type).
(HashAlgorithm h, SizeHash h ~ SECP256K1_ECDSA_MESSAGE_BYTES) =>
(HashAlgorithm h, HashSize h ~ SECP256K1_ECDSA_MESSAGE_BYTES) =>
Proxy h ->
ByteString ->
MessageHash
Expand All @@ -156,9 +156,9 @@ data EcdsaSecp256k1DSIGN

instance DSIGNAlgorithm EcdsaSecp256k1DSIGN where
type SeedSizeDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_PRIVKEY_BYTES
type SizeSigDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_SIGNATURE_BYTES
type SizeSignKeyDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_PRIVKEY_BYTES
type SizeVerKeyDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_PUBKEY_BYTES
type SigSizeDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_SIGNATURE_BYTES
type SignKeySizeDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_PRIVKEY_BYTES
type VerKeySizeDSIGN EcdsaSecp256k1DSIGN = SECP256K1_ECDSA_PUBKEY_BYTES
type Signable EcdsaSecp256k1DSIGN = ((~) MessageHash)
newtype VerKeyDSIGN EcdsaSecp256k1DSIGN
= VerKeyEcdsaSecp256k1 (PinnedSizedBytes SECP256K1_ECDSA_PUBKEY_BYTES_INTERNAL)
Expand Down
18 changes: 9 additions & 9 deletions cardano-crypto-class/src/Cardano/Crypto/DSIGN/Ed25519.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ instance NoThunks (SignKeyDSIGN Ed25519DSIGN)
instance NoThunks (SigDSIGN Ed25519DSIGN)

deriving via
(MLockedSizedBytes (SizeSignKeyDSIGN Ed25519DSIGN))
(MLockedSizedBytes (SignKeySizeDSIGN Ed25519DSIGN))
instance
NoThunks (SignKeyDSIGNM Ed25519DSIGN)

Expand Down Expand Up @@ -136,7 +136,7 @@ instance DSIGNAlgorithm Ed25519DSIGN where

-- \| Ed25519 key size is 32 octets
-- (per <https://tools.ietf.org/html/rfc8032#section-5.1.6>)
type SizeVerKeyDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_PUBLICKEYBYTES
type VerKeySizeDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_PUBLICKEYBYTES

-- \| Ed25519 secret key size is 32 octets; however, libsodium packs both
-- the secret key and the public key into a 64-octet compound and exposes
Expand All @@ -147,16 +147,16 @@ instance DSIGNAlgorithm Ed25519DSIGN where
-- (the libsodium \"seed\"). And because of this, we need to define the
-- sign key size to be SEEDBYTES (which is 32), not PRIVATEKEYBYTES (which
-- would be 64).
type SizeSignKeyDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_SEEDBYTES
type SignKeySizeDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_SEEDBYTES

-- \| Ed25519 signature size is 64 octets
type SizeSigDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_BYTES
type SigSizeDSIGN Ed25519DSIGN = CRYPTO_SIGN_ED25519_BYTES

--
-- Key and signature types
--

newtype VerKeyDSIGN Ed25519DSIGN = VerKeyEd25519DSIGN (PinnedSizedBytes (SizeVerKeyDSIGN Ed25519DSIGN))
newtype VerKeyDSIGN Ed25519DSIGN = VerKeyEd25519DSIGN (PinnedSizedBytes (VerKeySizeDSIGN Ed25519DSIGN))
deriving (Show, Eq, Generic)
deriving newtype (NFData)

Expand All @@ -170,7 +170,7 @@ instance DSIGNAlgorithm Ed25519DSIGN where
deriving (Show, Eq, Generic)
deriving newtype (NFData)

newtype SigDSIGN Ed25519DSIGN = SigEd25519DSIGN (PinnedSizedBytes (SizeSigDSIGN Ed25519DSIGN))
newtype SigDSIGN Ed25519DSIGN = SigEd25519DSIGN (PinnedSizedBytes (SigSizeDSIGN Ed25519DSIGN))
deriving (Show, Eq, Generic)
deriving newtype (NFData)

Expand Down Expand Up @@ -390,7 +390,7 @@ instance DirectSerialise (SignKeyDSIGNM Ed25519DSIGN) where
-- /Note:/ We only serialize the 32-byte seed, not the full 64-byte key. The
-- latter contains both the seed and the 32-byte verification key, which is
-- convenient, but redundant, since we can always reconstruct it from the
-- seed. This is also reflected in the 'SizeSignKeyDSIGNM', which equals
-- seed. This is also reflected in the 'SignKeySizeDSIGNM', which equals
-- 'SeedSizeDSIGNM' == 32, rather than reporting the in-memory size of 64.
directSerialise push sk = do
bracket
Expand Down Expand Up @@ -422,12 +422,12 @@ instance DirectSerialise (VerKeyDSIGN Ed25519DSIGN) where
psbUseAsCPtrLen psb $ \ptr _ ->
push
(castPtr ptr)
(fromIntegral @Word @CSize $ sizeVerKeyDSIGN (Proxy @Ed25519DSIGN))
(fromIntegral @Word @CSize $ verKeySizeDSIGN (Proxy @Ed25519DSIGN))

instance DirectDeserialise (VerKeyDSIGN Ed25519DSIGN) where
directDeserialise pull = do
psb <- psbCreate $ \ptr ->
pull
(castPtr ptr)
(fromIntegral @Word @CSize $ sizeVerKeyDSIGN (Proxy @Ed25519DSIGN))
(fromIntegral @Word @CSize $ verKeySizeDSIGN (Proxy @Ed25519DSIGN))
return $! VerKeyEd25519DSIGN psb
Loading
Loading