Paillier
ペイエ暗号 (加法準同型性を満たす)

Haskell コード

1
import Crypto.Number.ModArithmetic
2
3
l :: Integer -> Integer
4
l x = (x - 1) `div` n
5
6
solve :: Integer -> Integer
7
solve c = (l (expSafe c lambda (n^2)) * mu) `mod` n
8
where
9
lambda = lcm (p-1) (q-1)
10
mu = inverseCoprimes (l (expSafe g lambda (n^2))) n
Copied!

具体例

1
import Crypto.Number.ModArithmetic
2
import Crypto.Number.Serialize
3
4
import Data.Text
5
import Data.Text.Encoding
6
7
p, q, n, g, c :: Integer
8
p = 310013024566643256138761337388255591613
9
q = 319848228152346890121384041219876391791
10
n = 99157116611790833573985267443453374677300242114595736901854871276546481648883
11
g = 99157116611790833573985267443453374677300242114595736901854871276546481648884
12
c = 2433283484328067719826123652791700922735828879195114568755579061061723786565164234075183183699826399799223318790711772573290060335232568738641793425546869
13
14
l :: Integer -> Integer
15
l x = (x - 1) `div` n
16
17
solve :: Integer -> Integer
18
solve c = (l (expSafe c lambda (n^2)) * mu) `mod` n
19
where
20
lambda = lcm (p-1) (q-1)
21
mu = inverseCoprimes (l (expSafe g lambda (n^2))) n
22
23
toDisplay :: Integer -> Text
24
toDisplay = decodeUtf8 . i2osp
Copied!
実行結果
1
*Main> solve c
2
8483734412270322850839331621532480687141757
3
4
*Main> toDisplay $ solve c
5
"actf{crypto_lives}"
Copied!

参考リソース

Last modified 2yr ago