Numbers above 32, 64 bits

1

How to work with numbers above 32, 64 bits? The idea is to generate a large pseudorandom numbers up to 4096, for example, by a pseudorandom number generator that generates numbers of 32.

In C I could allocate a corresponding space, working and putting the numbers there. In Python I thought of using array, but I need the final number to become one. Anyone have any ideas?

    
asked by anonymous 14.04.2018 / 23:42

3 answers

3

Complementing the @Pedro answer a bit, there is no limit value for integers at this time. You can see this by citation in documentation :

  

The sys.maxint constant was removed, since there is no longer a limit   to the value of integers

Translating:

  

The sys.maxint constant has been removed since there is no longer a limit for integer values

Let's say we would have something corresponding to 2^4096 to save. So that would be perfectly calculable in python:

>>> 2**4096
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336L

See this small example in Ideone

    
15.04.2018 / 00:20
1
>>> import random
>>> random.randint(0, 2**64)
6502449964907846195

Python automatically handles large numbers, and you do not have to worry about the maximum bit value of an integer of 32 or 64 bits.

    
14.04.2018 / 23:57
0

Although the random module in Python is very practical, for cryptographic purposes the recommendation is to use the os.urandom() call, which has more randomness guaranteed by the operating system.

On the other hand, it is even more convenient because os.urandom already accepts a parameter with the number of random bytes and returns an object of type bytes - in this case, for 4096 bits, you use os.urandom(512) - in general for whatever use you are going to make the object bytes will be more practical than an integer with 4096 bits.

However, the language supports integers of this size - so if you want the numeric value you can do: int.from_bytes(os.urandom(512), "big") . If you prefer as a text string with 2 hexadecimal digits per byte, you can do os.urandom(512).hex() (convertible back to bytes with bytes.fromhex(minha_string) ).

Now let's assume you have another function of your choice that generates random, fixed-length 32-bit numbers (for example, a call to a hardware device driver) - in which case you you can use the Python "struct" module to join 16 calls to your function in a continuous bytes object with 512 bytes (and can then be converted to an integer, or to a hexadecimal string as above):

struct.pack("=16I", *(random.randrange(0, 2**32) for _ in range(16)))

(In case I used random.randrange as an example, but the recommendation is to actually use os.urandom, as I explained above). Understanding this expression: is a call to the struct.pack function where the first parameter describes the next: plus 16 32-bit integers that will be interpreted using the native endiannes of the machine - little endian in x86_64 - indicated by the " ". Then a generator expression that is deployed by the * operator - each generated element is passed to the function as a positional parameter. The expression in turn simply repeats 16 times, indicated in for _ in range(16) to call the desired function.

    
15.04.2018 / 16:35