packages feed

Paillier (empty) → 0.1.0.1

raw patch · 5 files changed

+200/−0 lines, 5 filesdep +HUnitdep +Paillierdep +QuickChecksetup-changed

Dependencies added: HUnit, Paillier, QuickCheck, base, crypto-numbers, crypto-random, test-framework, test-framework-quickcheck2, test-framework-th

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Li-Ting Tsai++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Li-Ting Tsai nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Paillier.cabal view
@@ -0,0 +1,39 @@++name:                Paillier+version:             0.1.0.1+synopsis:            a simple Paillier cryptosystem    +description:         a simple Paillier cryptosystem        +license:             BSD3+license-file:        LICENSE+author:              Li-Ting Tsai+maintainer:          and.liting@gmail.com+category:            Math+build-type:          Simple+cabal-version:       >=1.10++source-repository this+ type:     git+ location: https://github.com/onemouth/HsPaillier.git+ tag: 0.1.0.1++library+ hs-source-dirs: src+ exposed-modules: Crypto.Paillier+ default-language: Haskell2010+ build-depends: base >=4.6 && <4.7, crypto-numbers >= 0.2.2, crypto-random >= 0.0.7++--executable paillier+--  main-is: Main.hs            +--  build-depends:       base >=4.6 && <4.7, cmdargs >= 0.10.5, Paillier >= 0.1.0.0+--  default-language:    Haskell2010++Test-suite test-Paillier+  type: exitcode-stdio-1.0+  hs-source-dirs: src,test+  main-is: TestMain.hs+  build-depends: QuickCheck >= 2.6, HUnit, test-framework-th, test-framework, +                test-framework-quickcheck2,+                base >=4.6 && <4.7, crypto-numbers >= 0.2.2, crypto-random >= 0.0.7,+                Paillier+  default-language: Haskell2010+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Crypto/Paillier.hs view
@@ -0,0 +1,100 @@+{-# LANGUAGE CPP #-}+module Crypto.Paillier where++import Data.Maybe+import Crypto.Random+import Crypto.Number.Prime+import Crypto.Number.Generate (generateOfSize, generateBetween)+import Crypto.Number.ModArithmetic++#if 0+import System.IO+#endif++type PlainText = Integer ++type CipherText = Integer++data PubKey = PubKey{  bits :: Int  -- ^ e.g., 2048+                     , n_modulo :: Integer -- ^ n = pq+                     , generator :: Integer -- ^ generator = n+1+                     , n_square :: Integer -- ^ n^2+                    } deriving (Show)++data PrvKey = PrvKey{  lambda :: Integer -- ^ lambda(n) = lcm(p-1, q-1)+                     , x :: Integer+                    } deriving (Show)++++genKey :: Int -> IO (PubKey, PrvKey)+genKey nBits = do+    -- | choose random primes+    pool <- createEntropyPool+    let rng = cprgCreate pool :: SystemRNG+    let (p, rng1) = generatePrime rng (nBits `div` 2)+    let (q, rng2) = generatePrime rng1 (nBits `div` 2)+    -- | public key parameters+    let modulo = p*q+    let g = modulo+1+    let square = modulo*modulo+    -- | private key parameters+    --let phi_n = (p-1)*(q-1)+    let phi_n = lcm (p-1) (q-1)+    let maybeU = inverse (((expSafe g phi_n square) - 1) `div` modulo) modulo+    --let maybeU = inverse phi_n modulo+    if isNothing maybeU then+       error "genKey failed." +    else+        return (PubKey{bits=nBits, n_modulo=modulo, generator=g, n_square=square}+           ,PrvKey{lambda=phi_n, x=(fromJust maybeU)})++-- | deterministic version of encryption+_encrypt :: PubKey -> PlainText -> Integer -> CipherText+_encrypt pubKey plaintext r = +    result+    where result = (g_m*r_n) `mod` n_2+          n_2 = n_square pubKey+          g_m = expSafe (generator pubKey) plaintext n_2+          r_n = expSafe r (n_modulo pubKey) n_2++generateR :: SystemRNG -> PubKey -> Integer -> Integer+generateR rng pubKey guess =+    if guess >= (n_modulo pubKey) || ((gcd (n_modulo pubKey) guess) > 1) then+        generateR nextRng pubKey nextGuess+    else+        guess++    where (nextGuess, nextRng) = generateBetween rng 1 ((n_modulo pubKey) -1)++encrypt :: PubKey -> PlainText -> IO CipherText+encrypt pubKey plaintext = do+    pool <- createEntropyPool+    let rng = cprgCreate pool :: SystemRNG+#if 0+    hSetBuffering stdout NoBuffering+    putStrLn "get r..."+#endif+    let r = generateR rng pubKey (n_modulo pubKey)+#if 0+    putStrLn $ "r=" ++ (show r)+#endif+    return $ _encrypt pubKey plaintext r ++decrypt :: PrvKey -> PubKey -> CipherText -> PlainText+decrypt prvKey pubKey ciphertext = +    let c_lambda = expSafe ciphertext (lambda prvKey) (n_square pubKey)+        l_c_lamdba = (c_lambda - 1) `div` (n_modulo pubKey)+    in  (l_c_lamdba) * (x prvKey) `mod` (n_modulo pubKey)++-- | ciphetext muliplication is known as homomorphic addition of plaintexts+cipherMul :: PubKey -> CipherText -> CipherText -> CipherText+cipherMul pubKey c1 c2 = (c1*c2) `mod` (n_square pubKey)++-- | Homomorphic multiplication of plaintexts+-- An encrypted plaintext raised to the power of another plaintext will decrypt to the product of the two plaintexts.+cipherExp :: PubKey -> CipherText -> PlainText -> CipherText+cipherExp pubKey c1 p1 = expSafe c1 p1 (n_square pubKey)+++
+ test/TestMain.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE TemplateHaskell, FlexibleInstances #-}++import Test.QuickCheck+import Test.QuickCheck.Monadic+import Test.Framework+import Test.Framework.TH+import Test.Framework.Providers.QuickCheck2++import Crypto.Paillier+++instance Show (IO (PubKey, PrvKey))++instance Arbitrary (IO (PubKey, PrvKey)) where+    arbitrary = do+        nBits <- frequency [(2,return 512), (1,return 1024)]+        return $ genKey nBits+++prop_fixed_plaintext :: IO (PubKey, PrvKey) -> Property +prop_fixed_plaintext keys = monadicIO $ do +    (pub, prv) <- run keys+    let plaintext = 37+    ciphertext <- run $ encrypt pub plaintext+    let plaintext' = decrypt prv pub ciphertext+    assert $ plaintext == plaintext'++main :: IO ()+main = $(defaultMainGenerator)