In Python, "float" numbers use by default the floating point numbers available more directly on the CPU- think that on all platforms where Python is supported, this is 64bit floats (IEEE 754).
However, in addition to "getting big", floating point numbers have the feature of losing accuracy - long before they result in an error by "too big a result".
You read a theoretical encryption algorithm in some book -but applying that to real-world computing isn’t just "typing the formula" - there are several levels of numerical computation problems that have to be understood - if your text is complete enough, and not only with the mathematical theoretical aspect of the algorithm, you will see that later on there is a whole adaptation of numerical spaces so that the calculations can be made with integers. In the "real world" cryptography libraries, developed and maintained over decades by various experts do this.
Now, just for you to "see it working", Python has a package that allows floating point numbers with arbitrary precision that can possibly meet your formula without error, just typing as is.
For that, instead of the float
, you will use the class decimal.Decimal
. This class has a standard precision of 28 decimal digits - it will certainly be little for this formula - but maybe a 2000 digits will suffice - the code below will alter the standard precision of the Decimal, and from there you do all your operations (ALL) using this class instead of the float - and it is possible that the theoretical algorithm works as it is in the book. For this it is necessary to "convert" every numerical value that exists to a Decimal
, with a call to the class:
from decimal import Decimal, getcontext, setcontext
ctx = getcontext()
ctx.prec = 2000
setcontext(ctx)
...
# seu código vai aqui - todos os números devem ser do tipo `a = Decimal(<numero>)`
# - princpalmente o código que gera o `k_primo` e o `phi` que você usa na fórmula
...
lista.append(Decimal(msg[x-1])**(K_Primo * phi)+1)
(Also, of course, your message items have to be numerical - you can convert between numbers and characters with the function ord
(character -> integer number) and chr
(number -> character) ).
In fact, depending on the values of "K_primo" that there works neither with 2000 digits, nor with 2 million - in this case you have to continue until a chapter more advanced than at least te gives the formula using exponentiation with module - for example: 2 ** (1_000_000) is already a 300-digit number, which takes several seconds to compute. And if you are talking about ASCII codes in the values of your message would be something like "97 ** 1_000_000" - just for one character a number with ~2 million digits
and it takes minutes to calculate.
On the other hand, one of the first steps from "let’s make it real" to cryptography is to use module exponentiation in the formulas - https://pt.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation - This reduces the number of digits needed from millions to a few dozen and makes it possible to use formulas with numbers as small as 32-bit integers. (The function pow
Python exponentiation with module, simply pass a third parameter in the call)
As for trying to use the original formula only with decimal. Decimal, just to "see it working", good luck - and remember this is only good for demonstration - real encryption is much more complex, and it is very important to use tools that have proven to work already. (in particular, this approach will be too slow for any real application)
And just to close, the title of your question Como aumentar o número de floats no python
It doesn’t even make sense - the "number of possible floats" doesn’t have to be "increased" - the language uses the IEEE 754 standard (theoretically, depending on the architecture) - and you would never have the memory to "create a float of each" to want to "increase their number". The most appropriate formulation of the question would be "how to increase the numerical range that can be represented by floats" - and, as answered, for the type "float" this is impossible - however the type "decimal.Decimal" is flexible.
(and, of course, the number digit size has nothing to do with the "message size" as you imply in the question text, but with the message character code you are encoding)
You’re probably bumping into the edge of your platform, so you can check with
sys.float_info
. You can try to work around this problem using the moduledecimal
.– Lucas Maraal