Ask Your Question
4

accessing data included in the error message

asked 2011-11-29 12:59:37 +0200

jakub.konieczny gravatar image

updated 2011-11-29 13:00:32 +0200

I am quite new to Sage, so sorry in advance if the question is silly. I have searched the web and found not much that would be related.

I am trying to implement an factorization algorithm using elliptic curves (homework assignment). Say we are trying to factorize some n. The key point in the algorithm is to draw some knowledge from an error in arithmetic on a curve modulo n. More precisely, the error arises when (and only when) we are trying to invert some element in the modulo arithmetic when it is not invertible - and it means that it is not coprime to n, and thus taking their gcd we can find a factor of n.

What I don't know is how to get to the data included in the error message from inside the program.

An example is as follows:

n = 51
K = 720
C = EllipticCurve(Integers(n), [2,49])
P = C([1,1])
try:
    K*P
except ZeroDivisionError:
    raise

In the above code an error arises when computing K*P, which is caught and then thrown once more (since I don't know what else to do with it). The error message says:

Traceback (click to the left of this block for traceback)
...
ZeroDivisionError: Inverse of 34 does not exist (characteristic = 51 =
17*3)

I would like to get my hands on the number 34 appearing in the error message, without actually breaking the computation, so that it can be used in the program.

Ideally, it would work somewhat like that:

n = 51
K = 720
C = EllipticCurve(Integers(n), [2,49])
P = C([1,1])
try:
    K*P
except ZeroDivisionError:
    d = some_magic_function()
    g = gcd(d,n)
    print n, 'has divisor', g

I would appreciate any hints as to how it can be done.

Disclaimer: I have mentioned the question being connected to homework. The assignment was however only to cause the error message to appear, an then copy-paste the interesting number and resume computation. I find this approach somewhat dirty and hoped to look for something neater. I believe I am not doing anything immoral here.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2011-11-29 13:20:19 +0200

DSM gravatar image

updated 2011-11-29 13:20:34 +0200

The current idiom is something like this:

n = 51
K = 720
C = EllipticCurve(Integers(n), [2, 49])
P = C([1,1])

try:
    K*P
except ZeroDivisionError as err:
    d = Integer(err.args[0].split()[2])
    g = gcd(d,n)
    print n, 'has divisor', g

You'll sometimes see "except ZeroDivisionError, e:" which is an older style. The args[0].split()[2] stuff is because the exception which is thrown has a string as the first argument of the args tuple:

sage: err.args
('Inverse of 34 does not exist (characteristic = 51 = 17*3)',)

so it's not so pretty to extract it in this case.

The relevant section of the Python tutorial covers it well.

edit flag offensive delete link more

Comments

Thank you very much! It works just as I hoped it would. I wish more manuals included the "as ... " part of the "except" thing.

jakub.konieczny gravatar imagejakub.konieczny ( 2011-11-29 14:07:43 +0200 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2011-11-29 12:59:37 +0200

Seen: 487 times

Last updated: Nov 29 '11