-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathPraosBatchCompat.hs
565 lines (482 loc) · 19.3 KB
/
PraosBatchCompat.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
-- | Verifiable Random Function (VRF) implemented as FFI wrappers around the
-- implementation in https://github.com/input-output-hk/libsodium
module Cardano.Crypto.VRF.PraosBatchCompat (
-- * VRFAlgorithm API
PraosBatchCompatVRF,
-- * Low-level size specifiers
--
-- Sizes of various value types involved in the VRF calculations. Users of
-- this module will not need these, we are only exporting them for unit
-- testing purposes.
crypto_vrf_ietfdraft13_bytes_batchcompat,
crypto_vrf_ietfdraft13_publickeybytes,
crypto_vrf_ietfdraft13_secretkeybytes,
crypto_vrf_ietfdraft13_seedbytes,
crypto_vrf_ietfdraft13_outputbytes,
io_crypto_vrf_ietfdraft13_publickeybytes,
io_crypto_vrf_ietfdraft13_secretkeybytes,
-- * Key sizes
certSizeVRF,
signKeySizeVRF,
verKeySizeVRF,
vrfKeySizeVRF,
-- * Seed and key generation
Seed,
genSeed,
keypairFromSeed,
-- * Conversions
unsafeRawSeed,
outputBytes,
outputFromBytes,
outputFromProof,
proofBytes,
proofFromBytes,
skBytes,
skFromBytes,
vkBytes,
vkFromBytes,
skToVerKey,
skToSeed,
-- * Core VRF operations
prove,
verify,
SignKeyVRF (..),
VerKeyVRF (..),
CertVRF (..),
Proof,
Output,
)
where
import Cardano.Binary (
FromCBOR (..),
ToCBOR (..),
)
import Cardano.Crypto.RandomBytes (randombytes_buf)
import Cardano.Crypto.Seed (getBytesFromSeedT)
import Cardano.Crypto.Util (SignableRepresentation (..))
import Cardano.Crypto.VRF.Class
import Control.DeepSeq (NFData (..))
import Control.Monad (void)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Coerce (coerce)
import Data.Maybe (fromMaybe)
import Data.Proxy (Proxy (..))
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils
import Foreign.Ptr
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks, OnlyCheckWhnf (..), OnlyCheckWhnfNamed (..))
import System.IO.Unsafe (unsafePerformIO)
-- Value types.
--
-- These are all transparent to the Haskell side of things, all we ever do
-- with these is pass pointers to them around. We don't want to know anything
-- about them, hence, we make them uninhabited.
--
-- The actual values are kept entirely in C memory, allocated when a value is
-- created, and freed when the value's finalizer runs.
--
-- The reason we have them at all, rather than duplicating C's void pointers,
-- is because we want to distinguish them at the type level.
data SeedValue
data SignKeyValue
data VerKeyValue
data ProofValue
data OutputValue
-- Type aliases for raw pointers
--
-- These will not leave this module, they are only here for our convenience,
-- so we can afford to not newtype them.
type SeedPtr = Ptr SeedValue
type SignKeyPtr = Ptr SignKeyValue
type VerKeyPtr = Ptr VerKeyValue
type ProofPtr = Ptr ProofValue
type OutputPtr = Ptr OutputValue
-- The exported (via the 'VRFAlgorithm' typeclass) types.
--
-- These are wrappers around 'ForeignPtr's; we don't export the constructors,
-- so callers have to go through our blessed API to create any of them. This
-- way we can make sure that we always allocate the correct sizes, and attach
-- finalizers that automatically free the memory for us.
-- | A random seed, used to derive a key pair.
newtype Seed = Seed {unSeed :: ForeignPtr SeedValue}
deriving (NoThunks) via OnlyCheckWhnf Seed
-- | Signing key. In this implementation, the signing key is actually a 64-byte
-- value that contains both the 32-byte signing key and the corresponding
-- 32-byte verification key.
newtype SignKey = SignKey {unSignKey :: ForeignPtr SignKeyValue}
deriving (Generic)
deriving (NoThunks) via OnlyCheckWhnf SignKey
instance NFData SignKey where
rnf a = seq a ()
-- | Verification key.
newtype VerKey = VerKey {unVerKey :: ForeignPtr VerKeyValue}
deriving (Generic)
deriving (NoThunks) via OnlyCheckWhnf VerKey
instance NFData VerKey where
rnf a = seq a ()
-- | A proof, as constructed by the 'prove' function.
newtype Proof = Proof {unProof :: ForeignPtr ProofValue}
deriving (Generic)
deriving (NoThunks) via OnlyCheckWhnf Proof
instance NFData Proof where
rnf a = seq a ()
-- | Hashed output of a proof verification, as returned by the 'verify'
-- function.
newtype Output = Output {unOutput :: ForeignPtr OutputValue}
deriving (Generic)
deriving (NoThunks) via OnlyCheckWhnf Output
-- Raw low-level FFI bindings.
--
foreign import ccall "crypto_vrf_ietfdraft13_bytes_batchcompat"
crypto_vrf_ietfdraft13_bytes_batchcompat :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_publickeybytes"
crypto_vrf_ietfdraft13_publickeybytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_secretkeybytes"
crypto_vrf_ietfdraft13_secretkeybytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_seedbytes"
crypto_vrf_ietfdraft13_seedbytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_outputbytes"
crypto_vrf_ietfdraft13_outputbytes :: CSize
foreign import ccall "crypto_vrf_ietfdraft13_publickeybytes"
io_crypto_vrf_ietfdraft13_publickeybytes :: IO CSize
foreign import ccall "crypto_vrf_ietfdraft13_secretkeybytes"
io_crypto_vrf_ietfdraft13_secretkeybytes :: IO CSize
foreign import ccall "crypto_vrf_seed_keypair"
crypto_vrf_ietfdraft13_keypair_from_seed :: VerKeyPtr -> SignKeyPtr -> SeedPtr -> IO CInt
foreign import ccall "crypto_vrf_sk_to_pk"
crypto_vrf_ietfdraft13_sk_to_pk :: VerKeyPtr -> SignKeyPtr -> IO CInt
foreign import ccall "crypto_vrf_sk_to_seed"
crypto_vrf_ietfdraft13_sk_to_seed :: SeedPtr -> SignKeyPtr -> IO CInt
foreign import ccall "crypto_vrf_ietfdraft13_prove_batchcompat"
crypto_vrf_ietfdraft13_prove_batchcompat ::
ProofPtr -> SignKeyPtr -> Ptr CChar -> CULLong -> IO CInt
foreign import ccall "crypto_vrf_ietfdraft13_verify_batchcompat"
crypto_vrf_ietfdraft13_verify_batchcompat ::
OutputPtr -> VerKeyPtr -> ProofPtr -> Ptr CChar -> CULLong -> IO CInt
foreign import ccall "crypto_vrf_ietfdraft13_proof_to_hash_batchcompat"
crypto_vrf_ietfdraft13_proof_to_hash_batchcompat :: OutputPtr -> ProofPtr -> IO CInt
-- Key size constants
certSizeVRF :: Int
certSizeVRF = fromIntegral $! crypto_vrf_ietfdraft13_bytes_batchcompat
signKeySizeVRF :: Int
signKeySizeVRF = fromIntegral $! crypto_vrf_ietfdraft13_secretkeybytes
verKeySizeVRF :: Int
verKeySizeVRF = fromIntegral $! crypto_vrf_ietfdraft13_publickeybytes
vrfKeySizeVRF :: Int
vrfKeySizeVRF = fromIntegral $! crypto_vrf_ietfdraft13_outputbytes
ioSignKeySizeVRF :: IO Int
ioSignKeySizeVRF = fromIntegral <$> io_crypto_vrf_ietfdraft13_secretkeybytes
ioVerKeySizeVRF :: IO Int
ioVerKeySizeVRF = fromIntegral <$> io_crypto_vrf_ietfdraft13_publickeybytes
-- | Allocate a 'Seed' and attach a finalizer. The allocated memory will not be initialized.
mkSeed :: IO Seed
mkSeed = do
ptr <- mallocBytes (fromIntegral crypto_vrf_ietfdraft13_seedbytes)
Seed <$> newForeignPtr finalizerFree ptr
-- | Generate a random seed.
-- Uses 'randombytes_buf' to create random data.
--
-- This function provides an alternative way of generating seeds specifically
-- for the 'PraosVRF' algorithm. Unlike the 'genKeyPairVRF' method, which uses
-- a 'ByteString'-based 'Cardano.Crypto.Seed.Seed', this seed generation method
-- bypasses the GHC heap, keeping the seed in C-allocated memory instead.
--
-- This provides two advantages:
-- 1. It avoids the overhead of unnecessary GHC-side heap allocations.
-- 2. It avoids leaking the seed via the GHC heap; the 'Seed' type itself
-- takes care of zeroing out its memory upon finalization.
genSeed :: IO Seed
genSeed = do
seed <- mkSeed
withForeignPtr (unSeed seed) $ \ptr ->
randombytes_buf ptr crypto_vrf_ietfdraft13_seedbytes
return seed
copyFromByteString :: Ptr a -> ByteString -> Int -> IO ()
copyFromByteString ptr bs lenExpected =
BS.useAsCStringLen bs $ \(cstr, lenActual) ->
if lenActual >= lenExpected
then
copyBytes (castPtr ptr) cstr lenExpected
else
error $
"Invalid input size, expected at least " <> show lenExpected <> ", but got " <> show lenActual
seedFromBytes :: ByteString -> Seed
seedFromBytes bs
| BS.length bs /= fromIntegral crypto_vrf_ietfdraft13_seedbytes =
error $ "Expected " ++ show crypto_vrf_ietfdraft13_seedbytes ++ " bytes"
seedFromBytes bs = unsafePerformIO $ do
seed <- mkSeed
withForeignPtr (unSeed seed) $ \ptr ->
copyFromByteString ptr bs (fromIntegral crypto_vrf_ietfdraft13_seedbytes)
return seed
-- | Convert an opaque 'Seed' into a 'ByteString' that we can inspect.
-- Note that this will copy the seed into RTS-managed memory; this is not
-- currently a problem, but if at any point we decide that we want to make
-- sure the seed is properly mlocked, then this function will leak such a
-- secured seed into non-locked (swappable) memory.
unsafeRawSeed :: Seed -> IO ByteString
unsafeRawSeed (Seed fp) = withForeignPtr fp $ \ptr ->
BS.packCStringLen (castPtr ptr, fromIntegral crypto_vrf_ietfdraft13_seedbytes)
-- | Convert a proof verification output hash into a 'ByteString' that we can
-- inspect.
outputBytes :: Output -> ByteString
outputBytes (Output op) = unsafePerformIO $ withForeignPtr op $ \ptr ->
BS.packCStringLen (castPtr ptr, fromIntegral crypto_vrf_ietfdraft13_outputbytes)
-- | Convert a proof into a 'ByteString' that we can inspect.
proofBytes :: Proof -> ByteString
proofBytes (Proof op) = unsafePerformIO $ withForeignPtr op $ \ptr ->
BS.packCStringLen (castPtr ptr, certSizeVRF)
-- | Convert a verification key into a 'ByteString' that we can inspect.
vkBytes :: VerKey -> ByteString
vkBytes (VerKey op) = unsafePerformIO $ withForeignPtr op $ \ptr ->
BS.packCStringLen (castPtr ptr, verKeySizeVRF)
-- | Convert a signing key into a 'ByteString' that we can inspect.
skBytes :: SignKey -> ByteString
skBytes (SignKey op) = unsafePerformIO $ withForeignPtr op $ \ptr ->
BS.packCStringLen (castPtr ptr, signKeySizeVRF)
instance Show Proof where
show = show . proofBytes
instance Eq Proof where
a == b = proofBytes a == proofBytes b
instance ToCBOR Proof where
toCBOR = toCBOR . proofBytes
encodedSizeExpr _ _ =
encodedSizeExpr (\_ -> fromIntegral certSizeVRF) (Proxy :: Proxy ByteString)
instance FromCBOR Proof where
fromCBOR = proofFromBytes <$> fromCBOR
instance Show SignKey where
show = show . skBytes
instance Eq SignKey where
a == b = skBytes a == skBytes b
instance ToCBOR SignKey where
toCBOR = toCBOR . skBytes
encodedSizeExpr _ _ =
encodedSizeExpr (\_ -> fromIntegral signKeySizeVRF) (Proxy :: Proxy ByteString)
instance FromCBOR SignKey where
fromCBOR = skFromBytes <$> fromCBOR
instance Show VerKey where
show = show . vkBytes
instance Eq VerKey where
a == b = vkBytes a == vkBytes b
instance ToCBOR VerKey where
toCBOR = toCBOR . vkBytes
encodedSizeExpr _ _ =
encodedSizeExpr (\_ -> fromIntegral verKeySizeVRF) (Proxy :: Proxy ByteString)
instance FromCBOR VerKey where
fromCBOR = vkFromBytes <$> fromCBOR
-- | Allocate a Verification Key and attach a finalizer. The allocated memory will
-- not be initialized.
mkVerKey :: IO VerKey
mkVerKey = fmap VerKey $ newForeignPtr finalizerFree =<< mallocBytes verKeySizeVRF
-- | Allocate a Signing Key and attach a finalizer. The allocated memory will
-- not be initialized.
mkSignKey :: IO SignKey
mkSignKey = fmap SignKey $ newForeignPtr finalizerFree =<< mallocBytes signKeySizeVRF
-- | Allocate a Proof and attach a finalizer. The allocated memory will
-- not be initialized.
mkProof :: IO Proof
mkProof = fmap Proof $ newForeignPtr finalizerFree =<< mallocBytes certSizeVRF
proofFromBytes :: ByteString -> Proof
proofFromBytes bs
| BS.length bs /= certSizeVRF =
error "Invalid proof length"
| otherwise =
unsafePerformIO $ do
proof <- mkProof
withForeignPtr (unProof proof) $ \ptr ->
copyFromByteString ptr bs certSizeVRF
return proof
skFromBytes :: ByteString -> SignKey
skFromBytes bs = unsafePerformIO $ do
if bsLen /= signKeySizeVRF
then do
ioSize <- ioSignKeySizeVRF
error
( "Invalid sk length "
<> show @Int bsLen
<> ", expecting "
<> show @Int signKeySizeVRF
<> " or "
<> show @Int ioSize
)
else do
sk <- mkSignKey
withForeignPtr (unSignKey sk) $ \ptr ->
copyFromByteString ptr bs signKeySizeVRF
return sk
where
bsLen = BS.length bs
vkFromBytes :: ByteString -> VerKey
vkFromBytes bs = unsafePerformIO $ do
if BS.length bs /= verKeySizeVRF
then do
ioSize <- ioVerKeySizeVRF
error
( "Invalid pk length "
<> show @Int bsLen
<> ", expecting "
<> show @Int verKeySizeVRF
<> " or "
<> show @Int ioSize
)
else do
pk <- mkVerKey
withForeignPtr (unVerKey pk) $ \ptr ->
copyFromByteString ptr bs verKeySizeVRF
return pk
where
bsLen = BS.length bs
-- | Allocate an Output and attach a finalizer. The allocated memory will
-- not be initialized.
mkOutput :: IO Output
mkOutput =
fmap Output $
newForeignPtr finalizerFree =<< mallocBytes (fromIntegral crypto_vrf_ietfdraft13_outputbytes)
outputFromBytes :: MonadFail m => ByteString -> m Output
outputFromBytes bs = do
if bsLen /= fromIntegral @CSize @Int crypto_vrf_ietfdraft13_outputbytes
then
fail
( "Invalid output length "
<> show bsLen
<> ", expecting "
<> show crypto_vrf_ietfdraft13_outputbytes
)
else pure $! unsafePerformIO $ do
output <- mkOutput
withForeignPtr (unOutput output) $ \ptr ->
copyFromByteString ptr bs bsLen
pure output
where
bsLen = BS.length bs
-- | Derive a key pair (Sign + Verify) from a seed.
keypairFromSeed :: Seed -> (VerKey, SignKey)
keypairFromSeed seed =
unsafePerformIO $ withForeignPtr (unSeed seed) $ \sptr -> do
pk <- mkVerKey
sk <- mkSignKey
withForeignPtr (unVerKey pk) $ \pkPtr -> do
withForeignPtr (unSignKey sk) $ \skPtr -> do
void $ crypto_vrf_ietfdraft13_keypair_from_seed pkPtr skPtr sptr
return $ pk `seq` sk `seq` (pk, sk)
-- | Derive a Verification Key from a Signing Key.
skToVerKey :: SignKey -> VerKey
skToVerKey sk =
unsafePerformIO $ withForeignPtr (unSignKey sk) $ \skPtr -> do
pk <- mkVerKey
withForeignPtr (unVerKey pk) $ \pkPtr -> do
void $ crypto_vrf_ietfdraft13_sk_to_pk pkPtr skPtr
return pk
-- | Get the seed used to generate a given Signing Key
skToSeed :: SignKey -> Seed
skToSeed sk =
unsafePerformIO $ withForeignPtr (unSignKey sk) $ \skPtr -> do
seed <- mkSeed
_ <- withForeignPtr (unSeed seed) $ \seedPtr -> do
crypto_vrf_ietfdraft13_sk_to_seed seedPtr skPtr
return seed
-- | Construct a proof from a Signing Key and a message.
-- Returns 'Just' the proof on success, 'Nothing' if the signing key could not
-- be decoded.
prove :: SignKey -> ByteString -> Maybe Proof
prove sk msg =
unsafePerformIO $
withForeignPtr (unSignKey sk) $ \skPtr -> do
proof <- mkProof
BS.useAsCStringLen msg $ \(m, mlen) -> do
withForeignPtr (unProof proof) $ \proofPtr -> do
crypto_vrf_ietfdraft13_prove_batchcompat proofPtr skPtr m (fromIntegral mlen) >>= \case
0 -> return $ Just $! proof
_ -> return Nothing
-- | Verify a VRF proof and validate the Verification Key. Returns 'Just' a hash of
-- the verification result on success, 'Nothing' if the verification did not
-- succeed.
--
-- For a given verification key and message, there are many possible proofs but only
-- one possible output hash.
verify :: VerKey -> Proof -> ByteString -> Maybe Output
verify pk proof msg =
unsafePerformIO $
withForeignPtr (unVerKey pk) $ \pkPtr -> do
withForeignPtr (unProof proof) $ \proofPtr -> do
output <- mkOutput
BS.useAsCStringLen msg $ \(m, mlen) -> do
withForeignPtr (unOutput output) $ \outputPtr -> do
crypto_vrf_ietfdraft13_verify_batchcompat outputPtr pkPtr proofPtr m (fromIntegral mlen) >>= \case
0 -> return $ Just $! output
_ -> return Nothing
outputFromProof :: Proof -> Maybe Output
outputFromProof (Proof p) =
unsafePerformIO $
withForeignPtr p $ \ptr -> do
output <- mkOutput
withForeignPtr (unOutput output) $ \outputPtr -> do
crypto_vrf_ietfdraft13_proof_to_hash_batchcompat outputPtr ptr >>= \case
0 -> return $ Just $! output
_ -> return Nothing
data PraosBatchCompatVRF
instance VRFAlgorithm PraosBatchCompatVRF where
newtype VerKeyVRF PraosBatchCompatVRF = VerKeyPraosBatchCompatVRF VerKey
deriving stock (Show, Eq, Generic)
deriving newtype (ToCBOR, FromCBOR)
deriving (NoThunks) via OnlyCheckWhnfNamed "VerKeyVRF PraosBatchCompatVRF" VerKey
deriving newtype (NFData)
newtype SignKeyVRF PraosBatchCompatVRF = SignKeyPraosBatchCompatVRF SignKey
deriving stock (Show, Eq, Generic)
deriving newtype (ToCBOR, FromCBOR)
deriving (NoThunks) via OnlyCheckWhnfNamed "SignKeyVRF PraosBatchCompatVRF" SignKey
deriving newtype (NFData)
newtype CertVRF PraosBatchCompatVRF = CertPraosBatchCompatVRF Proof
deriving stock (Show, Eq, Generic)
deriving newtype (ToCBOR, FromCBOR)
deriving (NoThunks) via OnlyCheckWhnfNamed "CertKeyVRF PraosBatchCompatVRF" Proof
deriving newtype (NFData)
type Signable PraosBatchCompatVRF = SignableRepresentation
algorithmNameVRF = const "PraosBatchCompatVRF"
deriveVerKeyVRF = coerce skToVerKey
evalVRF = \_ msg (SignKeyPraosBatchCompatVRF sk) ->
let msgBS = getSignableRepresentation msg
proof = fromMaybe (error "Invalid Key") $ prove sk msgBS
output = fromMaybe (error "Invalid Proof") $ outputFromProof proof
in output `seq`
proof `seq`
(OutputVRF (outputBytes output), CertPraosBatchCompatVRF proof)
verifyVRF = \_ (VerKeyPraosBatchCompatVRF pk) msg (CertPraosBatchCompatVRF proof) ->
(OutputVRF . outputBytes) <$> verify pk proof (getSignableRepresentation msg)
sizeOutputVRF _ = fromIntegral crypto_vrf_ietfdraft13_outputbytes
seedSizeVRF _ = fromIntegral crypto_vrf_ietfdraft13_seedbytes
genKeyPairVRF = \cryptoseed ->
let seed =
seedFromBytes . fst . getBytesFromSeedT (fromIntegral crypto_vrf_ietfdraft13_seedbytes) $ cryptoseed
(pk, sk) = keypairFromSeed seed
in sk `seq` pk `seq` (SignKeyPraosBatchCompatVRF sk, VerKeyPraosBatchCompatVRF pk)
rawSerialiseVerKeyVRF (VerKeyPraosBatchCompatVRF pk) = vkBytes pk
rawSerialiseSignKeyVRF (SignKeyPraosBatchCompatVRF sk) = skBytes sk
rawSerialiseCertVRF (CertPraosBatchCompatVRF proof) = proofBytes proof
rawDeserialiseVerKeyVRF = fmap (VerKeyPraosBatchCompatVRF . vkFromBytes) . assertLength verKeySizeVRF
rawDeserialiseSignKeyVRF = fmap (SignKeyPraosBatchCompatVRF . skFromBytes) . assertLength signKeySizeVRF
rawDeserialiseCertVRF = fmap (CertPraosBatchCompatVRF . proofFromBytes) . assertLength certSizeVRF
sizeVerKeyVRF _ = fromIntegral verKeySizeVRF
sizeSignKeyVRF _ = fromIntegral signKeySizeVRF
sizeCertVRF _ = fromIntegral certSizeVRF
assertLength :: Int -> ByteString -> Maybe ByteString
assertLength l bs
| BS.length bs == l =
Just bs
| otherwise =
Nothing