Paillier 0.1.0.2 → 0.1.0.3
raw patch · 2 files changed
+19/−19 lines, 2 files
Files
- Paillier.cabal +2/−2
- src/Crypto/Paillier.hs +17/−17
Paillier.cabal view
@@ -1,6 +1,6 @@ name: Paillier-version: 0.1.0.2+version: 0.1.0.3 synopsis: a simple Paillier cryptosystem description: a simple Paillier cryptosystem license: BSD3@@ -23,7 +23,7 @@ build-depends: base >=4.6 && <4.7, crypto-numbers >= 0.2.2, crypto-random >= 0.0.7 --executable paillier--- main-is: Main.hs +-- main-is: Main.hs -- build-depends: base >=4.6 && <4.7, cmdargs >= 0.10.5, Paillier >= 0.1.0.0 -- default-language: Haskell2010
src/Crypto/Paillier.hs view
@@ -4,7 +4,7 @@ import Data.Maybe import Crypto.Random import Crypto.Number.Prime-import Crypto.Number.Generate (generateOfSize, generateBetween)+import Crypto.Number.Generate (generateBetween) import Crypto.Number.ModArithmetic #if 0@@ -16,9 +16,9 @@ type CipherText = Integer data PubKey = PubKey{ bits :: Int -- ^ e.g., 2048- , n_modulo :: Integer -- ^ n = pq+ , nModulo :: Integer -- ^ n = pq , generator :: Integer -- ^ generator = n+1- , n_square :: Integer -- ^ n^2+ , nSquare :: Integer -- ^ n^2 } deriving (Show) data PrvKey = PrvKey{ lambda :: Integer -- ^ lambda(n) = lcm(p-1, q-1)@@ -33,7 +33,7 @@ pool <- createEntropyPool let rng = cprgCreate pool :: SystemRNG let (p, rng1) = generatePrime rng (nBits `div` 2)- let (q, rng2) = generatePrime rng1 (nBits `div` 2)+ let (q, _) = generatePrime rng1 (nBits `div` 2) -- public key parameters let modulo = p*q let g = modulo+1@@ -41,31 +41,31 @@ -- 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 ((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)})+ return (PubKey{bits=nBits, nModulo=modulo, generator=g, nSquare=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+ n_2 = nSquare pubKey g_m = expSafe (generator pubKey) plaintext n_2- r_n = expSafe r (n_modulo pubKey) n_2+ r_n = expSafe r (nModulo pubKey) n_2 generateR :: SystemRNG -> PubKey -> Integer -> Integer generateR rng pubKey guess =- if guess >= (n_modulo pubKey) || ((gcd (n_modulo pubKey) guess) > 1) then+ if guess >= nModulo pubKey || (gcd (nModulo pubKey) guess > 1) then generateR nextRng pubKey nextGuess else guess - where (nextGuess, nextRng) = generateBetween rng 1 ((n_modulo pubKey) -1)+ where (nextGuess, nextRng) = generateBetween rng 1 (nModulo pubKey -1) encrypt :: PubKey -> PlainText -> IO CipherText encrypt pubKey plaintext = do@@ -75,7 +75,7 @@ hSetBuffering stdout NoBuffering putStrLn "get r..." #endif- let r = generateR rng pubKey (n_modulo pubKey)+ let r = generateR rng pubKey (nModulo pubKey) #if 0 putStrLn $ "r=" ++ (show r) #endif@@ -83,18 +83,18 @@ 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)+ let c_lambda = expSafe ciphertext (lambda prvKey) (nSquare pubKey)+ l_c_lamdba = (c_lambda - 1) `div` nModulo pubKey+ in l_c_lamdba * x prvKey `mod` nModulo 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)+cipherMul pubKey c1 c2 = c1*c2 `mod` nSquare 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)+cipherExp pubKey c1 p1 = expSafe c1 p1 (nSquare pubKey)