def __setstate__(self, state):
     n, e = state["n"], state["e"]
     if not state.has_key("d"):
         self.key = _fastmath.rsa_construct(n, e)
     else:
         d = state["d"]
         if not state.has_key("q"):
             self.key = _fastmath.rsa_construct(n, e, d)
         else:
             p, q, u = state["p"], state["q"], state["u"]
             self.key = _fastmath.rsa_construct(n, e, d, p, q, u)
def generate_c(bits, randfunc, progress_func=None):
    # Generate the prime factors of n
    if progress_func:
        progress_func("p,q\n")

    p = q = 1L
    while number.size(p * q) < bits:
        p = pubkey.getPrime(bits / 2, randfunc)
        q = pubkey.getPrime(bits / 2, randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q) = (q, p)
    if progress_func:
        progress_func("u\n")
    u = pubkey.inverse(p, q)
    n = p * q

    e = 65537L
    if progress_func:
        progress_func("d\n")
    d = pubkey.inverse(e, (p - 1) * (q - 1))
    key = _fastmath.rsa_construct(n, e, d, p, q, u)
    obj = RSAobj_c(key)

    ##    print p
    ##    print q
    ##    print number.size(p), number.size(q), number.size(q*p),
    ##    print obj.size(), bits
    assert bits <= 1 + obj.size(), "Generated key is too small"
    return obj