packages feed

crypto-numbers 0.2.4 → 0.2.5

raw patch · 2 files changed

+9/−20 lines, 2 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

Crypto/Number/Generate.hs view
@@ -11,31 +11,20 @@     , generateOfSize     ) where -import Crypto.Number.Basic import Crypto.Number.Serialize import Crypto.Random.API import qualified Data.ByteString as B-import Data.Bits ((.|.), (.&.), shiftL)+import Data.Bits ((.|.)) --- | generate a positive integer x, s.t. 0 <= x < m, uniformly at random+-- | generate a positive integer x, s.t. 0 <= x < m+--+-- Note that depending on m value, the number distribution+-- generated by this function is not necessarily uniform. generateMax :: CPRG g => g -> Integer -> (Integer, g)-generateMax rng m-    | m < 1 = error "generateMax: m must be >= 1"-    | m == 1 = (0,rng)-    | otherwise =-        let (tentativeResult, rng') =-                withRandomBytes rng (lengthBytes m) $ \bs ->-                    let lengthBits = (log2 (m-1) + 1)-                        mask = if lengthBits `mod` 8 == 0-                               then 0xff-                               else (1 `shiftL` (lengthBits `mod` 8)) - 1 in-                    os2ip $ snd $ B.mapAccumL (\acc w -> (0xff, w .&. acc))-                                      mask bs in-        if tentativeResult < m-            then (tentativeResult, rng')-            else generateMax rng' m+generateMax rng m = withRandomBytes rng (lengthBytes m) $ \bs ->+    os2ip bs `mod` m --- | generate a number between the inclusive bound [low,high] uniformly at random.+-- | generate a number between the inclusive bound [low,high]. generateBetween :: CPRG g => g -> Integer -> Integer -> (Integer, g) generateBetween rng low high = (low + v, rng')     where (v, rng') = generateMax rng (high - low + 1)
crypto-numbers.cabal view
@@ -1,5 +1,5 @@ Name:                crypto-numbers-Version:             0.2.4+Version:             0.2.5 Description:         Cryptographic numbers: functions and algorithms License:             BSD3 License-file:        LICENSE