Ask Your Question
1

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

asked 2015-12-07 23:42:57 +0200

Chin gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-12-08 00:39:42 +0200

tmonteil gravatar image

When you write (say):

sage: mod(7,3)
1

you define an element of the ring of integers modulo 3, not an integer:

sage: mod(7,3).parent()
Ring of integers modulo 3

In particular, there is no adition between elements of incompatible such rings:

sage: mod(7,3) + mod(5,2)
TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo 3' and 'Ring of integers modulo 2'

If you want an integer, you should use %:

sage: 7 % 3
1
sage: (7 % 3).parent()
Integer Ring
sage: 7%3 + 5%2
2
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-12-07 23:42:57 +0200

Seen: 2,234 times

Last updated: Dec 08 '15