secret-sharing 1.0.0.1 → 1.0.0.2
raw patch · 6 files changed
+37/−72 lines, 6 files
Files
- secret-sharing.cabal +1/−3
- src/Crypto/SecretSharing.hs +1/−1
- src/Crypto/SecretSharing/FiniteField.hs +0/−34
- src/Crypto/SecretSharing/Internal.hs +35/−15
- src/Crypto/SecretSharing/Prime.hs +0/−17
- tests/Tests.hs +0/−2
secret-sharing.cabal view
@@ -1,5 +1,5 @@ name: secret-sharing-version: 1.0.0.1+version: 1.0.0.2 synopsis: Information-theoretic secure secret sharing description: Implementation of an (@m@,@n@)-threshold secret sharing scheme.@@ -44,8 +44,6 @@ hs-source-dirs: src exposed-modules: Crypto.SecretSharing Crypto.SecretSharing.Internal- Crypto.SecretSharing.FiniteField- Crypto.SecretSharing.Prime build-depends: base ==4.6.*, bytestring ==0.10.*,
src/Crypto/SecretSharing.hs view
@@ -5,7 +5,7 @@ -- License : LGPL -- -- Maintainer : Peter Robinson <peter.robinson@monoid.at>--- Stability : stable+-- Stability : experimental -- Portability : portable -- -- Implementation of an (@m@,@n@)-threshold secret sharing scheme.
− src/Crypto/SecretSharing/FiniteField.hs
@@ -1,34 +0,0 @@-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, GeneralizedNewtypeDeriving, TemplateHaskell #-} --------------------------------------------------------------------------------- |--- Module : Crypto.SecretSharing.FiniteField--- Copyright : Peter Robinson 2014--- License : LGPL--- --- Maintainer : Peter Robinson <peter.robinson@monoid.at>--- Stability : stable--- Portability : portable--- --------------------------------------------------------------------------------module Crypto.SecretSharing.FiniteField-where--import Data.Typeable-import GHC.Generics-import Data.FiniteField.PrimeField as PF-import Crypto.SecretSharing.Prime----- | A finite prime field. All computations are performed in this field.-newtype FField = FField { number :: $(primeField $ fromIntegral prime) }- deriving(Show,Read,Ord,Eq,Num,Fractional,Generic,Typeable)- ---- | A polynomial over the finite field given as a list of coefficients.-type Polyn = [FField] ---- | Evaluates the polynomial at a given point.-evalPolynomial :: Polyn -> FField -> FField-evalPolynomial coeffs x - = foldr (\c res -> c + (x * res)) 0 coeffs
src/Crypto/SecretSharing/Internal.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, GeneralizedNewtypeDeriving #-} +{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, GeneralizedNewtypeDeriving, TemplateHaskell #-} ----------------------------------------------------------------------------- -- | -- Module : Crypto.SecretSharing.Internal@@ -6,7 +6,7 @@ -- License : LGPL -- -- Maintainer : Peter Robinson <peter.robinson@monoid.at>--- Stability : stable+-- Stability : experimental -- Portability : portable -- -----------------------------------------------------------------------------@@ -19,7 +19,6 @@ import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as BLC import qualified Data.List as L-import Data.Maybe import Data.Char import Data.Vector( Vector ) import qualified Data.Vector as V@@ -29,9 +28,7 @@ import Data.Binary( Binary ) import GHC.Generics import Data.FiniteField.PrimeField as PF--import Crypto.SecretSharing.FiniteField-import Crypto.SecretSharing.Prime+import Data.FiniteField.Base(FiniteField,order) import System.Random.Dice @@ -72,11 +69,11 @@ "encode: require n < " ++ show prime ++ " and m<=n." | BL.null bstr = return [] | otherwise = do- let bytes = map fromIntegral $ BL.unpack bstr- let len = max 1 ((length bytes) * (m-1))+ let len = max 1 ((fromIntegral $ BL.length bstr) * (m-1)) coeffs <- (groupInto (m-1) . map fromIntegral . take len ) `liftM` (getDiceRolls prime len)- let byteVecs = zipWith (encodeByte m n) coeffs bytes+ let byteVecs = zipWith (encodeByte m n) coeffs $+ map fromIntegral $ BL.unpack bstr return [ Share $ map (V.! (i-1)) byteVecs | i <- [1..n] ] @@ -93,7 +90,7 @@ let byteVecs = map (V.fromList . theShare) shares in let byteShares = [ map ((V.! (i-1))) byteVecs | i <- [1..origLength] ] in BL.pack . map (fromInteger . PF.toInteger . number) - . catMaybes . map decodeByte $ byteShares+ . map decodeByte $ byteShares encodeByte :: Int -> Int -> Polyn -> FField -> Vector ByteShare@@ -104,15 +101,17 @@ ] -decodeByte :: [ByteShare] -> Maybe FField+decodeByte :: [ByteShare] -> FField decodeByte ss = let m = reconstructionThreshold $ head ss in if length ss < m- then Nothing+ then throw $ AssertionFailed "decodeByte: insufficient number of shares for reconstruction!" else- let shares = take m ss in - let pts = map (\s -> (fromIntegral $ shareId s,fromIntegral $ shareValue s)) shares in- Just $ polyInterp pts 0+ let shares = take m ss + pts = map (\s -> (fromIntegral $ shareId s,fromIntegral $ shareValue s)) + shares + in+ polyInterp pts 0 -- | Groups a list into blocks of certain size. Running time: /O(n)/@@ -124,4 +123,25 @@ if L.null ss then [fs] else fs : groupInto num ss +++-- | A finite prime field. All computations are performed in this field.+newtype FField = FField { number :: $(primeField $ fromIntegral 1021) }+ deriving(Show,Read,Ord,Eq,Num,Fractional,Generic,Typeable,FiniteField)+ ++-- | The size of the finite field+prime :: Int+prime = fromInteger $ order (0 :: FField)+++-- | A polynomial over the finite field given as a list of coefficients.+type Polyn = [FField] ++-- | Evaluates the polynomial at a given point.+evalPolynomial :: Polyn -> FField -> FField+evalPolynomial coeffs x =+ foldr (\c res -> c + (x * res)) 0 coeffs+-- let clist = zipWith (\pow c -> (\x -> c * (x^pow))) [0..] coeffs +-- in L.foldl' (+) 0 [ c x | c <- clist ]
− src/Crypto/SecretSharing/Prime.hs
@@ -1,17 +0,0 @@--------------------------------------------------------------------------------- |--- Module : Crypto.SecretSharing.Prime--- Copyright : Peter Robinson 2014--- License : LGPL--- --- Maintainer : Peter Robinson <peter.robinson@monoid.at>--- Stability : stable--- Portability : portable--- --------------------------------------------------------------------------------module Crypto.SecretSharing.Prime( prime )-where--- | Determines the size of the finite field and the maximum number of shares.-prime :: Int-prime = 1021
tests/Tests.hs view
@@ -12,8 +12,6 @@ import qualified Data.ByteString.Lazy as B import Crypto.SecretSharing.Internal-import Crypto.SecretSharing.FiniteField-import Crypto.SecretSharing.Prime instance Arbitrary ByteString where arbitrary = fmap B.pack arbitrary