Question

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

EDIT:

Changed to PRIME = int(re.sub('\s+', '', N), 16)

Was it helpful?

Solution 2

Quite frankly what you have is pretty decent. The only obvious change would be your final conversion: you can drop the "0x{0}".format bit and just pass the replaced string as you give int the base 16.

Other than that, you could always hide it a function:

def PrimeInt(string):
    return int(string.replace('\n','').replace(' ',''), 16)

PRIME = PrimeInt("""
        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
        ''')

OTHER TIPS

From the PEP 8 Style Guide

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

So leave the constant as-is, as its more readable than any other un-obvious hacks

And most importantly Readability counts.

Nothing is above the the core principles of Python

but there MUST be a more elegant way of doing PRIME = int('0x{0}'.format(N.replace('\n', '').replace(' ', '')), 16)...

If you insist

PRIME=long('17976931348623159077083915679378745319786029604875601'
          '17064444236841971802161585193689478337958649255415021805654859805'
          '03646440548199239100050792877003355816639229553136239076508735759'
          '91482257486257500742530207744771258955095793777842444242661733472'
          '7629299387668709205606050270810842907692932019128194467627007L')

If you want something to look nice maybe you can convert from a string to an integer. Its probably not best practice though, as I've never actually seen it used in practice, but I think its a bit more clear than the original method because of the limited conversions

number = (
    "179769313486231590770839156793787453197860296048756011"
    "706444423684197180216158519368947833795864925541502180"
    "565485980503646440548199239100050792877003355816639229"
    "553136239076508735759914822574862575007425302077447712"
    "589550957937778424442426617334727629299387668709205606"
    "050270810842907692932019128194467627007"
)
number = int(number)
179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007L
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top