Sunday 8 September 2013

What is the most elegant / pythonic way of introducing large interger constants?

What is the most elegant / pythonic way of introducing large interger
constants?

I have a need to introduce large integer constants (primes for
Diffie-Hellman key exchange)
What is the most pythonic / pretty way of doing so in a PEP8 compliant
manner? Preferably with no backslashes.
My current approach is something like:
N = '''FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1
29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD
EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245
E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED
EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381
FFFFFFFF FFFFFFFF'''
PRIME = int('0x{0}'.format(N.replace('\n', '').replace(' ', '')), 16)
not very pretty, but prettier than:
PRIME =
179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007L

No comments:

Post a Comment