Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo x' and 'Ring of integers modulo y'

I am trying to decrypt a RSA message using the Chinese Remainder Theorem:

p = random_prime(2^512-1, False, 2^511)
q = random_prime(2^512-1, False, 2^511)
n = p * q
phi = (p - 1) * (q - 1)
m = 235432543543345
e = 65537
bezout = xgcd(e, phi)
d = Integer(mod(bezout[1], phi))
c = 0
m_decrypted = 0

def encrypt_rsa():
    global c
    c = mod(m ^ e, n)
    print 'c = ', c

def decrypt_rsa_crt():
    global m_decrypted
    c1 = p * inverse_mod(p % q, q)
    c2 = q * inverse_mod(q % p, p)
    n1 = ((c % p) ^ (d % (p - 1))) % p
    n2 = ((c % q) ^ (d % (q - 1))) % q
    m_decrypted = (n1 * c1 + n2 * c2) % n

encrypt_rsa()
decrypt_rsa_crt()
print 'm_decrypted = ', m_decrypted

However I'm getting this error:

Error in lines 40-40
Traceback (most recent call last):
  File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 1, in <module>
  File "", line 15, in decrypt_rsa_crt
  File "sage/structure/element.pyx", line 1700, in sage.structure.element.RingElement.__add__ (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/element.c:16570)
    return coercion_model.bin_op(left, right, add)
  File "sage/structure/coerce.pyx", line 1070, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (/projects/sage/sage-6.9/src/build/cythonized/sage/structure/coerce.c:9739)
    raise TypeError(arith_error_message(x,y,op))
TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo 13171665913556629928579831399279531345454405227507363358767433731944129138880990155192020899186190552971738190528920869626603617749599336864307309151600773' and 'Ring of integers modulo 7040259687366349269925712273121165166901616380486072254231627613746850969231618371638459623994615760442997840722561781585598181676625437161073288718538017'

The line of the error is the m_decrypted = (n1 * c1 + n2 * c2) % n line. I'm not sure what I'm doing wrong here and how to fix it?