protocol: fix big-endian decoding
authorJulien Moutinho <julm+hjugement@autogeree.net>
Sun, 23 Jun 2019 00:03:34 +0000 (00:03 +0000)
committerJulien Moutinho <julm+hjugement@autogeree.net>
Sun, 23 Jun 2019 00:03:34 +0000 (00:03 +0000)
hjugement-protocol/hjugement-protocol.cabal
hjugement-protocol/src/Voting/Protocol/Credential.hs
hjugement-protocol/src/Voting/Protocol/FFC.hs
hjugement-protocol/tests/HUnit/Credential.hs
hjugement-protocol/tests/HUnit/FFC.hs

index 8d708698c753675ba4136199b215dc382e835c3c..262cb43a8e580c8d25f7eecbbe676d45470b97ee 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.0.20190519
+version: 0.0.1.20190623
 category: Politic
 synopsis: A cryptographic protocol for the Majority Judgment.
 description:
index 9ad56ce65ed3d34d191d112e53243bc31e94a9bf..1eaa14d4137a8ef1cf363447a0ea9353f93e4c7f 100644 (file)
@@ -144,20 +144,17 @@ randomSecretKey = random
 -- using 'Crypto.fastPBKDF2_SHA256'.
 credentialSecretKey :: Reifies c FFC => UUID -> Credential -> (SecretKey c)
 credentialSecretKey (UUID uuid) (Credential cred) =
-       fromNatural $ BS.foldl'
-        (\acc b -> acc`shiftL`3 + fromIntegral b)
-        (0::Natural)
-        (ByteArray.convert deriv)
-       where
-       deriv :: BS.ByteString
-       deriv =
-               Crypto.fastPBKDF2_SHA256
-                Crypto.Parameters
-                { Crypto.iterCounts   = 1000
-                , Crypto.outputLength = 256 `div` 8
-                }
-                (Text.encodeUtf8 cred)
-                (Text.encodeUtf8 uuid)
+       fromNatural $
+       BS.foldl' -- NOTE: interpret the SHA256 as a big-endian number.
+        (\acc b -> acc`shiftL`8 + fromIntegral b)
+        (0::Natural) $
+       Crypto.fastPBKDF2_SHA256
+        Crypto.Parameters
+        { Crypto.iterCounts   = 1000
+        , Crypto.outputLength = 32 -- bytes, ie. 256 bits
+        }
+        (Text.encodeUtf8 cred)
+        (Text.encodeUtf8 uuid)
 
 -- ** Type 'PublicKey'
 type PublicKey = G
index 5917ffa0e4107ead40c83dd3f715f7cd88fa30b3..729aef8317f4e27612a9bd1a81e2d2fad6919c40 100644 (file)
@@ -306,8 +306,12 @@ groupGenPowers = go one
 hash :: Reifies c FFC => BS.ByteString -> [G c] -> E c
 hash bs gs = do
        let s = bs <> BS.intercalate (fromString ",") (bytesNat <$> gs)
-       let h = ByteArray.convert (Crypto.hashWith Crypto.SHA256 s)
-       fromNatural (BS.foldl' (\acc b -> acc`shiftL`3 + fromIntegral b) (0::Natural) h)
+       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)
 
 -- * Type 'E'
 -- | An exponent of a (necessarily cyclic) subgroup of a Finite Prime Field.
index 10d8a92bb14e5c9fed7b08431d0bdc737c6280ee..960261222cc7976b2d17822b23a800dde64aa025 100644 (file)
@@ -1,7 +1,6 @@
 {-# LANGUAGE OverloadedStrings #-}
 module HUnit.Credential where
 
-import Control.Applicative (Applicative(..))
 import Test.Tasty.HUnit
 import qualified Control.Monad.Trans.State.Strict as S
 import qualified System.Random as Random
@@ -36,18 +35,15 @@ hunit = testGroup "Credential"
         , "xLcs7ev6Jy6FHHE"  ==> Right (Credential "xLcs7ev6Jy6FHHE")
         ]
  , testGroup "credentialSecretKey" $
-        [ testSecretKey weakFFC 0 122
-        , testSecretKey weakFFC 1 35
-        , testSecretKey beleniosFFC 0 2317630607062989137269685509390
-        , testSecretKey beleniosFFC 1 1968146140481358915910346867611
+        [ testSecretKey beleniosFFC
+                (UUID "xLcs7ev6Jy6FHH")
+                (Credential "xLcs7ev6Jy6FHHE")
+                24202898752499029126606335829564687069186982035759723128887013101942425902424
         ]
  ]
 
-testSecretKey :: FFC -> Int -> Natural -> TestTree
-testSecretKey ffc seed exp =
+testSecretKey :: FFC -> UUID -> Credential -> Natural -> TestTree
+testSecretKey ffc uuid cred exp =
        reify ffc $ \(Proxy::Proxy c) ->
-       let (uuid@(UUID u), cred@(Credential c)) =
-               (`S.evalState` Random.mkStdGen seed) $
-                       (,) <$> randomUUID <*> randomCredential in
-       testCase (show (u,c)) $
+       testCase (show (uuid,cred)) $
                credentialSecretKey @c uuid cred @?= E exp
index 07cd2e43ac684e2373fe615bf99bcd97fd523a41..aa74a435fb68bf1c1486076afa178a67a6816bd7 100644 (file)
@@ -28,19 +28,19 @@ hunit = testGroup "FFC"
                reify weakFFC $ \(Proxy::Proxy c) ->
                 [ testCase "[groupGen]" $
                        hash "start" [groupGen @c] @?=
-                               fromNatural 100
+                               fromNatural 62
                 , testCase "[groupGen, groupGen]" $
                        hash "start" [groupGen @c, groupGen] @?=
-                               fromNatural 16
+                               fromNatural 31
                 ]
         , testGroup "BeleniosParams" $
                reify beleniosFFC $ \(Proxy::Proxy c) ->
                 [ testCase "[groupGen]" $
                        hash "start" [groupGen @c] @?=
-                               fromNatural 1832875488615060263192702367259
+                               fromNatural 75778590284190557660612328423573274641033882642784670156837892421285248292707
                 , testCase "[groupGen, groupGen]" $
                        hash "start" [groupGen @c, groupGen] @?=
-                               fromNatural 2495277906542783643199702546512
+                               fromNatural 28798937720387703653439047952832768487958170248947132321730024269734141660223
                 ]
         ]
  ]