English 中文(简体)
HaskellSizedByteArray的例子
原标题:Examples of SizedByteArray in Haskell

I ve正在尝试使用。 Library 以及 I with problems with keygen,从Crypto.BLST到签名

keygen :: (ByteArrayAccess ba, 32 <= n, KnownNat n) => SizedByteArray n ba -> SecretKey

我如何制定<条码>。 SizedByteArray n ba Object? 在link 实例

sk1 = keygen $ AS.unsafeSizedByteArray @32 @ByteString "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"

www.un.org/Depts/DGACM/index_french.htm 数据。 这个例子不利于我。 当我试图做同样的事时,这些话语不会奏效。

Then I tried this code

import Data.ByteArray.Sized qualified as AS
import Data.Text.Encoding
import qualified Data.ByteString as B
import qualified Data.Text as T
import Data.Proxy (Proxy(..))
import Crypto.BLST

sk ="79cc407e6917b5673a1f6966c23c9e15
     d257e5ab46cfe7b9b2f64200f2b2843e"
skp = T.pack sk
skpb = encodeUtf8 skp

姓名skpbByteString。 因此,根据<代码>大小ByteArray的定义。 (见下文)我应当获得<代码>Maybe SizedByteArray?

-- | create a  SizedByteArray  from the given  ByteArrayAccess  if the
-- size is the same as the target size.
--
sizedByteArray :: forall n ba . (KnownNat n, ByteArrayAccess ba)
               => ba
               -> Maybe (SizedByteArray n ba)
sizedByteArray ba
    | length ba == n = Just $ SizedByteArray ba
    | otherwise      = Nothing
  where
    n = fromInteger $ natVal (Proxy @n)

After trying this I get these errors:

ghci> AS.sizedByteArray skpb

<interactive>:7:1: error: [GHC-39999]
    • No instance for ‘GHC.TypeNats.KnownNat n0’
        arising from a use of ‘it’
    • In the first argument of ‘print’, namely ‘it’
      In a stmt of an interactive GHCi comm以及: print it

以及

ghci> keygen $ AS.sizedByteArray skpb

<interactive>:8:1: error: [GHC-64725]
    • Cannot satisfy: 32 <= n0
    • In the first argument of ‘($)’, namely ‘keygen’
      In the expression: keygen $ AS.sizedByteArray skpb
      In an equation for ‘it’: it = keygen $ AS.sizedByteArray skpb

我做了什么错误?

问题回答

定义:

sk1 :: SecretKey
sk1 = keygen $ AS.unsafeSizedByteArray @32 @B.ByteString
               "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"

将作罚款,但需使<代码>DataKinds>作为类型使用<编码>32<>>>>t/code>,以在字面上作为<代码>ByteString和TypeApplications,以允许这些编号<@-marks,这些编号适用于平级论点。 因此,应当汇编如下:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

import Crypto.BLST
import qualified Data.ByteArray.Sized as AS
import qualified Data.ByteString as B

sk1 :: SecretKey
sk1 = keygen $ AS.unsafeSizedByteArray @32 @B.ByteString
               "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"

更广泛地说,由于这些延期,你应当能够建造<条码>。 使用下列任何方法的SizedByteArray数值:

-- type applications
sba2 = AS.unsafeSizedByteArray @64 @B.ByteString
       "0123456789012345678901234567890123456789012345678901234567890123"

-- top-level type signature
sba1 :: AS.SizedByteArray 64 B.ByteString
sba1 = AS.unsafeSizedByteArray
       "0123456789012345678901234567890123456789012345678901234567890123"

-- in-line type signature
sba3 = AS.unsafeSizedByteArray
       "0123456789012345678901234567890123456789012345678901234567890123"
       :: AS.SizedByteArray 64 B.ByteString

请注意,在汇编时间时对这些大小进行了核对,因此将汇编如下:

sbabadlen = AS.unsafeSizedByteArray @100 @B.ByteString "too short"

但是,在评价时,会放弃一个暂时的例外。 “安全”版本还将汇编:

sbabadlen = AS.sizedByteArray @100 @B.ByteString "too short"

http://code.Nothing,而不是Just a SizedByteArray

Oh,我还要说:没有简单的办法来构建一个<条码>。 缩略语





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签