Answer the question
In order to leave comments, you need to log in
How to raise a biginteger value to a biginteger?
Good afternoon, there is g ^ m * r ^ n mod n ^ 2, the main problem is exponentiation, since you will need to use numbers from 512, 1024 bits.
I tried to make a crutch BigInteger.ModPow(r, n, bigmod()), where bigmod() is a fixed number 1000000.... But it doesn't always work, because if r^n>bigmod() we get a calculation error, to solve it, you need to calculate the approximate length of r^n , while I don’t know how to do it without clogging up the memory.
If you use the fast exponentiation algorithm -> clogs memory and takes a very long time.
To check the correctness, I use these values (see the picture, in the cipher text header, an example of calculation c)
Who faced a similar problem and how did they decide which algorithms, example code.
Answer the question
In order to leave comments, you need to log in
An excellent property of the module is that you can take the module at each stage during calculations (if there are no divisions, in this case you need to look for the inverse modulo). Therefore, the original expression can be rewritten as follows: (g^m mod n^2) * (r^n mod n^2) mod n^2
And the code for calculating this is:
nn = n.multiply(n);
res = g.modPow(m, nn).multiply(r.modPow(n, nn)).mod(nn);
No one in real implementations does such garbage. You understand the basics of cryptography, but you forgot to study the section "modular arithmetic".
https://ru.wikipedia.org/wiki/Raising_to_a_power...
The number itself is not needed. We need the modulo remainder.
Otherwise, you will need to implement a class where the numbers of the required bit depth for 1024 bits will be packed, you will need 16 longs. Well and further - to implement arithmetic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question