protocol: add decodeBigEndian
authorJulien Moutinho <julm+hjugement@autogeree.net>
Mon, 24 Jun 2019 23:04:01 +0000 (23:04 +0000)
committerJulien Moutinho <julm+hjugement@autogeree.net>
Tue, 25 Jun 2019 17:46:01 +0000 (17:46 +0000)
GNUmakefile
hjugement-protocol/hjugement-protocol.cabal
hjugement-protocol/src/Voting/Protocol/Credential.hs
hjugement-protocol/src/Voting/Protocol/FFC.hs

index b899b88cce32ae9f40d752c9b8c1280221c4b0ec..7fe00772242ecf4e8d0ddf5d95bebe26e5a0d306 100644 (file)
@@ -1,6 +1,7 @@
 PKGS := \
  hjugement \
- hjugement-protocol
+ hjugement-protocol \
+ hjugement-cli
 hjugement-protocol/test: TEST_FLAGS:=
 
 HS = $(shell find $(PKGS) -name '*.hs' -not -name 'HLint.hs')
index 262cb43a8e580c8d25f7eecbbe676d45470b97ee..e6c2b1dafcb2be209f7c1376b637ccc5c493044f 100644 (file)
@@ -2,7 +2,7 @@ name: hjugement-protocol
 -- PVP:  +-+------- breaking API changes
 --       | | +----- non-breaking API additions
 --       | | | +--- code changes with no API change
-version: 0.0.1.20190623
+version: 0.0.2.20190624
 category: Politic
 synopsis: A cryptographic protocol for the Majority Judgment.
 description:
index 1eaa14d4137a8ef1cf363447a0ea9353f93e4c7f..dd3b35474913c26944cef68183bda04c65eca3f6 100644 (file)
@@ -5,7 +5,6 @@ module Voting.Protocol.Credential where
 
 import Control.DeepSeq (NFData)
 import Control.Monad (Monad(..), forM_, replicateM)
-import Data.Bits
 import Data.Bool
 import Data.Char (Char)
 import Data.Either (Either(..), either)
@@ -18,15 +17,12 @@ import Data.Ord (Ord(..))
 import Data.Semigroup (Semigroup(..))
 import Data.Text (Text)
 import GHC.Generics (Generic)
-import Numeric.Natural (Natural)
-import Prelude (Integral(..), fromIntegral, div)
+import Prelude (Integral(..), fromIntegral)
 import Text.Show (Show(..))
 import qualified Control.Monad.Trans.State.Strict as S
 import qualified Crypto.KDF.PBKDF2 as Crypto
 import qualified Data.Aeson as JSON
 import qualified Data.Aeson.Types as JSON
-import qualified Data.ByteArray as ByteArray
-import qualified Data.ByteString as BS
 import qualified Data.Char as Char
 import qualified Data.List as List
 import qualified Data.Text as Text
@@ -144,10 +140,7 @@ randomSecretKey = random
 -- using 'Crypto.fastPBKDF2_SHA256'.
 credentialSecretKey :: Reifies c FFC => UUID -> Credential -> (SecretKey c)
 credentialSecretKey (UUID uuid) (Credential cred) =
-       fromNatural $
-       BS.foldl' -- NOTE: interpret the SHA256 as a big-endian number.
-        (\acc b -> acc`shiftL`8 + fromIntegral b)
-        (0::Natural) $
+       fromNatural $ decodeBigEndian $
        Crypto.fastPBKDF2_SHA256
         Crypto.Parameters
         { Crypto.iterCounts   = 1000
index e288808276c03f3fe38c29f56a33610718674995..738a90f1d236af80f42a0810119288c8cfdd96e8 100644 (file)
@@ -16,11 +16,8 @@ module Voting.Protocol.FFC
  ) where
 
 import Control.Arrow (first)
-import Control.Applicative (Applicative(..))
 import Control.DeepSeq (NFData)
 import Control.Monad (Monad(..), unless)
-import Control.Monad.Trans.Reader (ReaderT(..), asks)
-import Control.Monad.Trans.Class (MonadTrans(..))
 import Data.Aeson (ToJSON(..),FromJSON(..),(.:),(.:?),(.=))
 import Data.Bits
 import Data.Bool
@@ -332,10 +329,14 @@ hash bs gs = do
        let s = bs <> BS.intercalate (fromString ",") (bytesNat <$> gs)
        let h = Crypto.hashWith Crypto.SHA256 s
        fromNatural $
-               BS.foldl' -- NOTE: interpret the SHA256 as a big-endian number.
-                (\acc b -> acc`shiftL`8 + fromIntegral b)
-                (0::Natural)
-                (ByteArray.convert h)
+               decodeBigEndian $ ByteArray.convert h
+
+-- | @('decodeBigEndian' bs)@ interpret @bs@ as big-endian number.
+decodeBigEndian :: BS.ByteString -> Natural
+decodeBigEndian =
+       BS.foldl'
+        (\acc b -> acc`shiftL`8 + fromIntegral b)
+        (0::Natural)
 
 -- * Type 'E'
 -- | An exponent of a (necessarily cyclic) subgroup of a Finite Prime Field.