def decrypt(c, n, g, lamb, miu): n2 = n * n if integer.gcd(c, n2) != 1: print("error") if c < 1 or c >= n2 or integer.gcd(c, n2) != 1: raise Exception("cipher c must be in Group Z_*_n^2") m_bar = integer.mod(funcL(integer.fast_pow(c, lamb, n2), n) * miu, n) return m_bar
def test_gcd(self): actual = integer.gcd(4, 5) expected = 1 assert actual == expected actual = integer.gcd(3, 6) expected = 3 assert actual == expected
def keyGen(sbit): p = integer.randprime(int(sbit/2)) q = integer.randprime(int(sbit/2)) while integer.gcd(p*q, (p-1)*(q-1)) != 1: p = integer.randprime(int(sbit/2)) q = integer.randprime(int(sbit/2)) n = p * q n2 = n * n lamb = integer.lcm(p - 1, q - 1) g = sampleGen(n2) while integer.gcd(funcL(integer.fast_pow(g, lamb, n2), n), n) != 1: g = sampleGen(n2) miu = integer.inverse(funcL(integer.fast_pow(g, lamb, n2), n), n) ''' lamb = (p - 1) * (q - 1) g = n + 1 miu = inverse(lamb, n, lamb) ''' return n, g, lamb, miu
def encrypt(m, n, g): if m < 0 or m >= n: raise Exception("message m must be not less than 0 and less than n") r = integer.random.randint(1, n - 1) n2 = int(n**2) while integer.gcd(r, n2) != 1: r = integer.random.randint(1, n - 1) ''' r = randprime(int(math.log2(n))) while r > n - 1: r = randprime(int(math.log2(n))) n2 = n * n ''' c = integer.mod(integer.fast_pow(g, m, n2) * integer.fast_pow(r, n, n2), n2) return c
def sampleGen(n): g = integer.random.randint(1, n - 1) while integer.gcd(g, n) != 1: g = integer.random.randint(1, n - 1) return g