def egVerification(p, m, a, r, s, y): if r < 1 or r >= p: return False v1 = (expm(p, y, r) * expm(p, r, s)) % p v2 = expm(p, a, m) return v1 == v2
def egEnc(p, a, y, m): if m < 0 or m >= p: raise Exception('m is outside range: 0 <= m < p') k = randbelow(p - 1) while k < 1: k = randbelow(p - 1) c1 = expm(p, a, k) c2 = m * expm(p, y, k) % p return (c1, c2)
def order(p, f, a): t = p - 1 for pi in f: if expm(p, a, t // pi) == 1: t = t // pi return t
def egKey(s): p, a = pair(s) x = randbelow(p - 1) while x < 1: x = randbelow(p - 1) y = expm(p, a, x) return (p, a, x, y)
def egSignature(p, m, a, x): # if m < 0 or m >= p - 1: # raise Exception('m is outside range: 0 <= m < p-1') k = randbelow(p - 1) while k < 1 or gcd(k, p - 1) != 1: k = randbelow(p - 1) r = expm(p, a, k) s = invm(p - 1, k) * (m - x * r) % (p - 1) return (r, s)
def egDec(p, x, c1, c2): return expm(p, c1, (p - 1 - x)) * c2 % p
def log(p, a, x): i = 0 while expm(p, a, i) != x: i += 1 return i
def rsaAppValidate(n, s, e, m): m = hashlib.sha1(str(m).encode('utf-8')) return int(m.hexdigest(), 16) == expm(n, s, e)
def rsaAppSignature(n, d, m): m = hashlib.sha1(str(m).encode('utf-8')) sha1 = int(m.hexdigest(), 16) return expm(n, sha1, d)
def rsaRecVerification(m, n, s, e, k): return okSimple(len(bin(m)[2:]), k, expm(n, s, e))
def rsaRecRecovery(n, s, e, k): x = expm(n, s, e) m_len = len(bin(x)[2:]) - k return removeSimple(m_len, k, x)
def rsaRecSignature(n, d, m, k): m_len = len(bin(m)[2:]) return expm(n, addSimple(m_len, k, m), d)