find modulus exponentiation for biginteger in matlab - matlab

I want to calculate modulo exponentiation for Biginteger in Matlab.
a^b mod n = d
Example:
a=java.math.BigInteger('878');
b=java.math.BigInteger('8097');
c=java.math.BigInteger('961');
d=modPow(a,b,n)
How can I calculate unknown n in this Biginteger equation if a, b, d are given?
a=35367
b=453467
a^b mod n =16
or
modPow(a,b,n)=16
n=?
If the answer to the equation is equal to, for example, 16 and we want to find the value of n, how do we write the modulo equation to solve the values of n in Matlab, even for larger numbers?
It can be defined that the number n be found up to 1000000 or 10^6.
Thanks.

Related

Displaying integers with lagrange four square theorem in mathematica or matlab

As I am new on MatLab and Mathematica, I am trying to solve two (easy) problems using one of these two programmes.
"In number theory, Lagrange’s four-square theorem, states that every natural number n can be written as n= a^2+ b^2 + c^2 + d^2, where a, b, c, d are integers.
Given a natural number n, display all possible integers a, b, c, d.
The number of ways to write a natural number
n as the sum of four squares is denoted by r4(n). Using Jacobi's theorem, plot the function r4(n)
and compare it with the function 8n√(log n)."
This is a partial answer using Mathematica build-in functions
PowerRepresentations[n,k,p] gives the distinct representation of the integer n as sum of k non-negative p th integer powers.
Attention: by distinct we mean: if n = n1^p + n2^p + n3^p ... the function returns k-tuples such that n1<=n2<=n3...
Example:
PowerRepresentation[20,4,2]
gives
{{0,0,2,4},{1,1,3,3}}
To get the number of possible representations of integer n as a sum of d squares you can use the SquaresR[d,n] function (your rd(n) functions).
Example:
SquaresR[4,20]
prints
144
However as you explained there is still some works because rd(n) also returns negative solutions and permuted ones.
For instance:
SquaresR[2,20]
returns
8
You must understand 8 as counting without distinction:
4 sign changes:
{2,4},{2,-4},{-2,4},{-2,-4}
times
2 permutations
{2,4},{4,2}

Calculating d value in RSA

I saw a couple questions about this but most of them were answered in unhelpful way or didn't get a proper answer at all. I have these variables:
p = 31
q = 23
e - public key exponent = 223
phi - (p-1)*(q-1) = 660
Now I need to calculate d variable (which I know is equal 367). The problem is that I don't know how. I found this equation on the internet but it doesn't work (or I can't use it):
e⋅d=1modϕ(n)
When I see that equation i think that it means this:
d=(1modϕ(n))/e
But apparently it doesn't because 367 (1modϕ(n))/e = 1%660/223 = 1/223 != 367
Maybe I don't understand and I did something wrong - that's why I ask.
I did some more research and I found second equation:
d=1/e mod ϕ(n)
or
d=e^-1 mod ϕ(n)
But in the end it gives the same result:
1/e mod ϕ(n) = 1/223 % 660 = 1/223 != 367
Then I saw some guy saying that to solve that equation you need extended Euclidean algorithm If anyone knows how to use it to solve that problem then I'd be very thankful if you help me.
If you want to calculate something like a / b mod p, you can't just divide it and take division remainder from it. Instead, you have to find such b-1 that b-1 = 1/b mod p (b-1 is a modular multiplicative inverse of b mod p). If p is a prime, you can use Fermat's little theorem. It states that for any prime p, ap = a mod p <=> a(p - 2) = 1/a mod p. So, instead of a / b mod p, you have to compute something like a * b(p - 2) mod p. b(p - 2) can be computed in O(log(p))
using exponentiation by squaring. If p is not a prime, modular multiplicative inverse exists if and only if GCD(b, p) = 1. Here, we can use extended euclidean algorithm to solve equation bx + py = 1 in logarithmic time. When we have bx + py = 1, we can take it mod p and we have bx = 1 mod p <=> x = 1/b mod p, so x is our b-1. If GCD(b, p) ≠ 1, b-1 mod p doesn't exist.
Using either Fermat's theorem or the euclidean algorithm gives us same result in same time complexity, but the euclidean algorithm can also be used when we want to compute something modulo number that's not a prime (but it has to be coprime with numer want to divide by).

matlab wrong modulo result when the divident is raised to a power

Just wondering... I tried doing by hand (with the multiply and square method) the operation (111^11)mod143 and I got the result 67. I also checked that this is correct, in many online tools. Yet, in matlab plugging:
mod(111^11,143)
gives 127! Is there any particular reason for this? I didn't find anything in the documentation...
The value of 111^11 (about 3.1518e+022) exceeds the maximum integer that is guaranteed to be represented exactly as a double, which is 2^53 (about 9.0072e+015). So the result is spoilt by insufficient numerical precision.
To achieve the correct result, use symbolic computation:
>> syms x y z
>> r = mod(x^y, z);
>> subs(r, [x y z], [111 11 143])
ans =
67
Alternatively, for this specific operation (modulo of a large number that is expressed as a product of small numbers), you can do the computation very easily using the following fact (where ∗ denotes product):
mod(a∗b, z) = mod(mod(a,z)∗mod(b,z), z)
That is, you can apply the modulo operation to factors of your large number and the final result is unchanged. If you choose factors sufficiently small so that they can be represented exactly as double, you can do the computation numerically without any loss of precision.
For example: using the decomposition 111^11 = 111^4*111^4*111^3, since all factors are small enough, gives the correct result:
>> mod((mod(111^4, 143))^2 * mod(111^3, 143), 143)
ans =
67
Similarly, using 111^2 and 111 as factors,
>> mod((mod(111^2, 143))^5 * mod(111, 143), 143)
ans =
67
from the matlab website they recommend using powermod(b, e, m) (b^e mod m)
"If b and m are numbers, the modular power b^e mod m can also be computed by the direct call b^e mod m. However, powermod(b, e, m) avoids the overhead of computing the intermediate result be and computes the modular power much more efficiently." ...
Another way is to use symfun
syms x y z
f = symfun(mod(x^y,z), [x y z])
f(111,11,143)

Matlab log2 equivalent in fortran

I need to translate a code from matlab to fortran 90. What is the best way to implement an equivalent fortran code for the matlab log2 function that dissect floating-point numbers into exponent and mantissa. I need to compute E and F that are described in the matlab documentation :
"[F,E] = log2(X) returns arrays F and E. Argument F is an array of real values, usually in the range 0.5 <= abs(F) < 1. For real X, F satisfies the equation: X = F.*2.^E. Argument E is an array of integers that, for real X, satisfy the equation: X = F.*2.^E."
The Fortran standard has EXPONENT and FRACTION intrinsics that do this dissection. They are elemental, so if you pass them an array you get an array back.

MATLAB: How to make the Hermitean matrix from specific complex vectors?

Is given:
stationary mass ms=1;
Eta-constant eta=0.45;
variable number of repetitions, e.g. N=5;
omega OM=sqrt(ks/ms);
angular frequency om=eta*OM;
time period T=2*pi/om;
upper bound TTT=1.5;
variable for creating function t=0:0.001:TTT;
I made a function like that:
kt=zeros(size(t));
for j=1:2*N+1
n= j-(N+1);
if n==0
k(j)=ks/2;
else
k(j)=i/pi/n;
end
kt=kt+k(j)*exp(i*n*om*t);
end
It’s a Sawtooth wave and there is my problem. From the complex vector kt with value 1x1501 double I have to make the Hermitean matrix for variable N. This means that N can be 5, can be 50, 100, etc. The matrix should look like (picture):
Where k1 is k for N=1, k0 is k for N=0 or k-1 is k for N=-1. Size of matrix is 2*N+1 and 2*N+1.
Thank you for your help and responding!
That's a Toeplitz matrix, you can use the toeplitz command to generate the matrix above. In the general case, this would have been written as:
H = toeplitz(kt(N:end), kt(1:N + 1))
where the first N values in kt correspond to k-N, ... k-1, and the last N + 1 values are k0, ... kN. However, since H is Hermitian, this can be simplified to:
H = toeplitz(kt(N:end));
Try this code:
k=[1 2+i 3+i 4+i 5+i];
N=7;
M=diag(k(1)*ones(N,1));
for j=1:length(k)-1
M=M+diag(k(j+1)*ones(N-j,1),j)+diag(conj(k(j+1))*ones(N-j,1),-j)
end;
Here N should be equal or greater than the length of k array