serokell / haskell-crypto

Haskell cryptography done right
https://hackage.haskell.org/package/crypto-sodium
15 stars 6 forks source link

crypto-sodium: Add blake2b hash #32

Closed lierdakil closed 2 years ago

lierdakil commented 2 years ago

So, I did something, and it seems to work with tests stolen from libsodium. Unfortunate that the result is in IO though.

Right, I'm being stupid, we can unsafePerformIO here. I've pushed a fixup commit.

Resolves #30.

lierdakil commented 2 years ago

Okay, I think I've done that, but I'm not sure about the changelog (basically it's a little confusing? There were 4 releases -- well, pre-releases -- but the changelog says "unreleased" still)

kirelagin commented 2 years ago

Yeah, those are pre-releases, so nothing has been “released” yet :).

lierdakil commented 2 years ago

I think I've made the requested changes. Let me know if you want me to squash the commits at some point.

kirelagin commented 2 years ago

Looks like we have a small problem. When the function is called without a key, the key variable becomes ambiguous :(.

I guess we’ll need to have two separate functions? Alternatively, we can fix the key type, but I’m not sure if this is a good or a bad idea.

lierdakil commented 2 years ago

That's a bit unfortunate, although I'm more disappointed that I didn't notice.

I guess it's not such a strange idea to require pt ~ key, but two functions (with and w/o key) would be more flexible.

Alternatively, and I don't know if it's a good idea or not, we could have a function with pt ~ key and a function with ambiguity. The latter might need explicit type signature / type applications to compile, but would offer more flexibility, while the former would be a bit easier to use.

blake2b'
  ::  forall len hashBytes pt key.
      ( ByteArrayAccess pt
      , ByteArrayAccess key
      , ByteArray hashBytes
      , KnownNat len
      , Na.CRYPTO_GENERICHASH_BYTES_MIN <= len
      , len <= Na.CRYPTO_GENERICHASH_BYTES_MAX
      )
  => Maybe key -- ^ Hash key
  -> pt  -- ^ Message to hash
  -> I.HashBlake2b len hashBytes

and

blake2b
  ::  forall len hashBytes pt.
      ( ByteArrayAccess pt
      , ByteArray hashBytes
      , KnownNat len
      , Na.CRYPTO_GENERICHASH_BYTES_MIN <= len
      , len <= Na.CRYPTO_GENERICHASH_BYTES_MAX
      )
  => Maybe pt -- ^ Hash key
  -> pt  -- ^ Message to hash
  -> I.HashBlake2b len hashBytes
blake2b = blake2b'

Finally, we could just export

noKey :: Maybe ByteString
noKey = Nothing

and that would also be fine (-ish).