Ask Your Question
1

Where does sagemath's speed in calculating gcd come from? [closed]

asked 2023-12-13 21:28:48 +0100

Monapp gravatar image

Hey, I was wondering how can sagemath can calculate gcds without calculating the numbers at stake? For instance,

sage: from Crypto.Util.number import long_to_bytes, inverse, bytes_to_long
sage: from Crypto.Hash import SHA256
sage: e = 65537
sage: gcd(bytes_to_long(SHA256.new(data=b'first').digest())^e-35211653423, bytes_to_long(SHA256.new(data=b'second').digest())^e-156535482153)

is computed nearly instantly while it will take forever for sage to calculate bytes_to_long(SHA256.new(data=b'first').digest())^e.

Is it perhaps part of symbolic computation?

Thanks :)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Monapp
close date 2023-12-16 10:42:22.031138

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-12-13 23:10:59 +0100

updated 2023-12-13 23:19:18 +0100

On my computer (an old Mac), Sage 10.1.beta3, the gcd computation is slower than the second computation, as common sense would suggest:

sage: %time x = gcd(bytes_to_long(SHA256.new(data=b'first').digest())^e-35211653423, bytes_to_long(SHA256.new(data=b'second').digest())^e-156535482153)
CPU times: user 7.79 s, sys: 191 ms, total: 7.98 s
Wall time: 7.99 s
sage: %time y = bytes_to_long(SHA256.new(data=b'first').digest())^e
CPU times: user 2.7 s, sys: 59.7 ms, total: 2.76 s
Wall time: 2.76 s

What does take a long time is actually printing y. If I did

sage: %time bytes_to_long(SHA256.new(data=b'first').digest())^e  # note: omitting "y=" at the start

the timing would be the same: it prints

CPU times: user 2.7 s, sys: 51.2 ms, total: 2.75 s
Wall time: 2.78 s

and then it takes a long time to actually print the answer. Indeed, going back to y from the first version:

sage: %time len(str(y))
CPU times: user 4min 8s, sys: 425 ms, total: 4min 9s
Wall time: 4min 9s
5038462

Computing the string representation of y takes 4 minutes. (The gcd is 1, and its string representation is pretty quick to compute ;)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2023-12-13 21:28:48 +0100

Seen: 187 times

Last updated: Dec 13 '23