cardano-crypto-class-2.2.0.0: Type classes abstracting over cryptography primitives for Cardano
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cardano.Crypto.PinnedSizedBytes

Synopsis

Documentation

data PinnedSizedBytes (n ∷ Nat) Source #

n bytes. Storable.

We have two *Bytes types:

  • PinnedSizedBytes is backed by pinned ByteArray.
  • MLockedSizedBytes is backed by ForeignPtr to mlock-ed memory region.

The ByteString is pinned datatype, but it's represented by ForeignPtr + offset (and size).

I'm sorry for adding more types for bytes. :(

Instances

Instances details
KnownNat n ⇒ IsString (Q (TExp (PinnedSizedBytes n))) Source #

This instance is meant to be used with TemplateHaskell

>>> import Cardano.Crypto.PinnedSizedBytes
>>> :set -XTemplateHaskell
>>> :set -XOverloadedStrings
>>> :set -XDataKinds
>>> print ($$("0xdeadbeef") :: PinnedSizedBytes 4)
"deadbeef"
>>> print ($$("deadbeef") :: PinnedSizedBytes 4)
"deadbeef"
>>> let bsb = $$("0xdeadbeef") :: PinnedSizedBytes 5
<interactive>:9:14: error:
    • <PinnedSizedBytes>: Expected in decoded form to be: 5 bytes, but got: 4
    • In the Template Haskell splice $$("0xdeadbeef")
      In the expression: $$("0xdeadbeef") :: PinnedSizedBytes 5
      In an equation for ‘bsb’:
          bsb = $$("0xdeadbeef") :: PinnedSizedBytes 5
>>> let bsb = $$("nogood") :: PinnedSizedBytes 5
<interactive>:11:14: error:
    • <PinnedSizedBytes>: Malformed hex: invalid character at offset: 0
    • In the Template Haskell splice $$("nogood")
      In the expression: $$("nogood") :: PinnedSizedBytes 5
      In an equation for ‘bsb’: bsb = $$("nogood") :: PinnedSizedBytes 5
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

KnownNat n ⇒ Storable (PinnedSizedBytes n) Source # 
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

Show (PinnedSizedBytes n) Source # 
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

NFData (PinnedSizedBytes n) Source # 
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

Methods

rnfPinnedSizedBytes n → () Source #

KnownNat n ⇒ Eq (PinnedSizedBytes n) Source #

The comparison is done in constant time for a given size n.

Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

KnownNat n ⇒ Ord (PinnedSizedBytes n) Source # 
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

NoThunks (PinnedSizedBytes n) Source # 
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

KnownNat n ⇒ IsString (Code Q (PinnedSizedBytes n)) Source # 
Instance details

Defined in Cardano.Crypto.PinnedSizedBytes

Initialization

psbZeroKnownNat n ⇒ PinnedSizedBytes n Source #

Deprecated: This is not referentially transparent

Conversions

psbFromBytes ∷ ∀ n. KnownNat n ⇒ [Word8] → PinnedSizedBytes n Source #

Deprecated: This is not referentially transparent

See IsString (PinnedSizedBytes n) instance.

>>> psbToBytes . (id @(PinnedSizedBytes 4)) . psbFromBytes $ [1,2,3,4]
[1,2,3,4]
>>> psbToBytes . (id @(PinnedSizedBytes 4)) . psbFromBytes $ [1,2]
[0,0,1,2]
>>> psbToBytes . (id @(PinnedSizedBytes 4)) . psbFromBytes $ [1,2,3,4,5,6]
[3,4,5,6]

psbFromByteStringKnownNat n ⇒ ByteStringPinnedSizedBytes n Source #

Convert a ByteString into PinnedSizedBytes. Input should contain the exact number of bytes as expected by type level n size, otherwise error.

C usage

psbUseAsCPtr ∷ ∀ (n ∷ Nat) (r ∷ Type) (m ∷ TypeType). MonadST m ⇒ PinnedSizedBytes n → (Ptr Word8 → m r) → m r Source #

Use a PinnedSizedBytes in a setting where its size is 'forgotten' temporarily.

Note

The Ptr given to the function argument must not be used as the result of type r.

psbUseAsCPtrLen ∷ ∀ (n ∷ Nat) (r ∷ Type) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ PinnedSizedBytes n → (Ptr Word8CSize → m r) → m r Source #

As psbUseAsCPtr, but also gives the function argument the size we are allowed to use as a CSize.

This is mostly boilerplate removal, as it is quite common for C APIs to take a combination of a pointer to some data and its length. A possible use case (and one we run into) is where we know that we can expect a certain data length (using PinnedSizedBytes as its representation), but the C API allows any length we like, provided we give the right argument to indicate this. Therefore, having a helper like this one allows us to avoid having to manually natVal a Proxy, as well as ensuring we don't get mismatches accidentally.

The same caveats apply to the use of this function as to the use of psbUseAsCPtr.

psbUseAsSizedPtr ∷ ∀ (n ∷ Nat) (r ∷ Type) (m ∷ TypeType). MonadST m ⇒ PinnedSizedBytes n → (SizedPtr n → m r) → m r Source #

As psbUseAsCPtr, but does not 'forget' the size.

The same caveats apply to this use of this function as to the use of psbUseAsCPtr.

psbCreate ∷ ∀ (n ∷ Nat) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ (Ptr Word8 → m ()) → m (PinnedSizedBytes n) Source #

As psbCreateResult, but presumes that no useful value is produced: that is, the function argument is run only for its side effects.

psbCreateLen ∷ ∀ (n ∷ Nat) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ (Ptr Word8CSize → m ()) → m (PinnedSizedBytes n) Source #

As psbCreateResultLen, but presumes that no useful value is produced: that is, the function argument is run only for its side effects.

psbCreateSized ∷ ∀ (n ∷ Nat) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ (SizedPtr n → m ()) → m (PinnedSizedBytes n) Source #

As psbCreateSizedResult, but presumes that no useful value is produced: that is, the function argument is run only for its side effects.

psbCreateResult ∷ ∀ (n ∷ Nat) (r ∷ Type) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ (Ptr Word8 → m r) → m (PinnedSizedBytes n, r) Source #

Given an 'initialization action', which also produces some result, allocate new pinned memory of the specified size, perform the action, then return the result together with the initialized pinned memory (as a PinnedSizedBytes).

Note

It is essential that r is not the Ptr given to the function argument. Returning this Ptr is extremely unsafe:

  • It breaks referential transparency guarantees by aliasing supposedly immutable memory; and
  • This Ptr could refer to memory which has already been garbage collected, which can lead to segfaults or out-of-bounds reads.

This poses both correctness and security risks, so please don't do it.

psbCreateResultLen ∷ ∀ (n ∷ Nat) (r ∷ Type) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ (Ptr Word8CSize → m r) → m (PinnedSizedBytes n, r) Source #

As psbCreateResult, but also gives the number of bytes we are allowed to operate on as a CSize.

This function is provided for two reasons:

  • It is a common practice in C libraries to pass a pointer to data plus a length. While our use case might know the size we expect, the C function we are calling might be more general. This simplifies calling such functions.
  • We avoid natValing a Proxy twice, since we have to do it anyway.

The same caveats apply to this function as to psbCreateResult: the Ptr given to the function argument must not be returned as r.

psbCreateSizedResult ∷ ∀ (n ∷ Nat) (r ∷ Type) (m ∷ TypeType). (KnownNat n, MonadST m) ⇒ (SizedPtr n → m r) → m (PinnedSizedBytes n, r) Source #

As psbCreateResult, but gives a SizedPtr to the function argument. The same caveats apply to this function as to psbCreateResult: the SizedPtr given to the function argument must not be resulted as r.