Mpz_powm giving wrong answers when hash is big? - rsa

I'm now in trial and error phase, so I assigning some small numbers to the variables as I know the calculation is correct.
In my encryption function, I got this function;
hexValues = 65;
d = 2753;
n = 3233;
mpz_powm(sigfile, hexValues, d, n); //sigfile = hexvalues^d % n = 588
The power mod function above give me a value of 588
And in my decryption function
sigfile = 588; //this is where i read sigfile value from encryption function
e = 17;
N = 3233;
mpz_powm(answer, sigfile, e, N); //answer = sigfile^e %N
And obviously I got 65 because I just apply reverse formula on the numbers.
But the issue here is, if I change hexValues(65) from my encryption to 6555, I will have different results from both of the functions.
Let alone to test it with an actual hash value.
Encryption of 6555 as the hash value will give me 2448 but Decryption gives me 89 instead.
*This is an assignment.
*Yes, the variables are intialized with mpz datatype.

Related

How to fix the difference in precision between double data type and uint64

I'm in the process of implementing Three Fish block cipher using MATLAB. At first, I implemented the algorithm on uint8 numbers to validate my code. Every thing was OK and the decryption was successful. But when I replaced the numbers to uint64 the plain text did not retrieved correctly.
I traced the rounds results again and over again to find the reason, but I couldn't find it so far. There is difference in the first four digits between encryption and decryption, that is, along the rounds x encrypted as 9824265115183455531, but it decrypts as 9824265115183455488.
I think the reason behind this difference is in the functions AddMod64 and SubMod64 to find arithmetic modulo 2 to the power 64. but really I could not fix it so far.
I know that
double(2^64) = 18446744073709552000
and
uint64(2^64) = 18446744073709551615 % z = ( x + y ) % 2^64
function z = AddMod64(x , y)
m = uint64(2^64);
z = double(mod(mod(double(x),m)+mod(double(y),m),m));
end
% z = (x - y ) % 2^64
function z = SubMod64(x , y)
m = uint64(2^64);
z = double(mod(mod(double(x),m) - mod(double(y),m),m));
end
double(2^64) is already the wrong result, the double type can hold only up to 2^52-1 as an integer without rounding.
Also, when you do uint64(2^64), the power is computed using double, giving the wrong result, which you then cast to uint64. And because the maximum value that a uint64 van hold is 2^64-1, that whole operation is wrong.
Use maxint instead:
m = maxint('uint64');
To do modulo addition in MATLAB is rather tricky, because MATLAB does saturated arithmetic with integers. You need to test for overflow before doing the computation.
if x > m - y
x = y - (m - x + 1);
else
x = x + y
end

Is it possible to compute all the values at the same time when writing a script?

When defining a function, say TEST = #(t) t.^2. If the input is a vector, say [1,2,3,4], TEST([1,2,3,4]) = [1,4,9,16].
Can we do similar thing if the function defined is in script form? What I mean is that if I have a script, say TEST.m such that ret = TEST(x,y,z) which outputs a value when knowing numerical values of x, y and z. Suppose I want to calculate 100 different values of z ranging from 1 to 100 when x, y are fixed, say at 0, 1 respectively. Is it possible to output TEST(0,1,1:1:100) without writing a for loop or changing any contents of the script TEST.m?
The reason to ask such question comes from the computation time. Usually, the script I have may be a little complicated so that the calculate of a single value may take few minutes to go. Writing for-loop to output it can be very time-consuming. I think of writing parfor loop, but the computation time is still long to me for further uses. I wonder if I can calculate all the 100 values at a time. I am a starter of programmer, and I hope I can get satisfactory answers after this post. Thanks for all your help.
You cab define a new anonymous function to get the fixed values as parameters, and the vector as input. Then use arrayfun to compute it on all values of the array.
Say you have this functions:
function ret = TEST(x,y,z)
ret = f(x)+g(y)+h(z);
end
function r = f(x)
r = x^2;
end
function r = g(y)
r = y^3;
end
function r = h(z)
r = z^4;
end
And you call it from:
x = 2;
y = 3;
z = 1:5;
T = #(z) TEST(x,y,z);
arrayfun(T,z)
So T is a new function that treat x and y as constants, and only have z as input. Then arrayfun takes T and compute it for every element in z, and you get:
ans =
32 47 112 287 656
Now, you can use arrayfun with more vectors, like [a,b,c] = arrayfun(T,z,w,v), if x and y can stay constant.
Hopes it answers your question ;)

MATLAB convert big-endian order bytes into floating point values

I have the following bytes stored in a vector:
data = [189 33 136 147]
These 4 bytes represent a single float in Big-endian order. How can I get this number in MATLAB?
I will need to concatenate and convert. I tried:
x = typecast(str2num(sprintf('%d%d%d%d',data(1),data(2),data(3),data(4))), 'single')
To no avail (I got x = []).
great example here:
>> dataL = typecast(uint8([189, 33, 136, 147]), 'uint32')
dataL =
2475172285
>> dataF = double(dataL)
dataF =
2.4752e+09
big to little, try swapbytes
>> dataLbig = swapbytes(dataL)
dataLbig =
3173091475
>> dataFbig = double(dataLbig)
dataFbig =
3.1731e+09
Is this what you were expecting?
I'll leave this here in case it's useful for anyone. As #MarkMikofski showed, using typecast and swapbytes is the standard method for this. However, if you your data is already floating-point, these functions can be inefficient in some cases. I use the following utility function in my video-encoding/decoding tools:
function x = Bit32toDouble(y)
n = numel(y);
if n >= 65536
x = double(swapbytes(typecast(uint8(y(:)),'uint32'))).';
elseif n > 4
x = sum(bsxfun(#times,[16777216;65536;256;1],reshape(y(:),4,n/4)));
else
x = sum([16777216;65536;256;1].*y(:));
end
There are separate cases depending on the number of bytes passed in. Only when a large amount of data is processed at once is the typecast/swapbytes most efficient. If the function is called repeatedly with smaller inputs, as is common in my application, the other cases are much faster because they do every thing in Matlab's native floating-point.

Linear Feedback Shift Register algorithm

I'm trying to code my own implementation of Linear Feedback Shift Register on Matlab in order to generate a pseudo-random sequence of numbers. Suppose I need to generate a sequence from 1 to 16,384 (2^14) in random order, my initial state is number 329 and the tap is 7.
This is the code I've got so far:
function [rndV] = lfsr(limit, init, tap)
X = -1;
rndV = init;
bits = nextpow2(limit);
while(X ~= init)
if(X == -1)
X = init;
end
a = bitget(X, bits);
b = bitget(X, tap);
X = bitshift(X,1,bits);
X = bitset(X,1,bitxor(a,b));
rndV = [rndV X];
end
end
The parameters are:
limit = 16,384
init = 329
tap = 7
If I get right LFSR, must the algorithm loop until the initial state is found again? Does this loop must generate all numbers between 1 and 16,384 in random order?
Something is wrong on my code or maybe I misunderstood LFSR algorithm, but I'm getting just 22 numbers in random order, then the initial state (329) is found again.
I want to achieve the same as described here but in matlab.Thanks!
only a primitive polynomial can got full range of random numbers. check here for primpolyes, or here if you need bigger order of LSFR

Advice in Understanding this Key generating Algorith for Blowfish Algorithm

The code is for MATLAB and i want to understand what exactly they are trying to do. I m new to matlab so need some advice. Please Help
function [key] = keyGen(n)
n = n*8;
% n = 2048*2048*16;
% n = 24 * 24 * 8;
bin_x = zeros(n,1,'uint8');
r = 3.9999998;
bin_x_N_Minus_1 = 0.300001;
x_N = 0;
tic
for ind = 2 : n
x_N = 1 - 2* bin_x_N_Minus_1 * bin_x_N_Minus_1;
if (x_N > 0.0)
bin_x(ind-1) = 1;
end
bin_x_N_Minus_1 = x_N;
end
toc
%save bin_sec bin_x;
t = uint8(0);
key = zeros(n/8,1,'uint8');
for ind1 = 1 : n/8
for ind2 = 1 : 8
key(ind1) = key(ind1) + bin_x(ind2*ind1)* 2 ^ (ind2-1);
end
end
The parameter n is the number of bytes in the key. n*8 is to convert that into a number of bits. bin_x is used to store the binary representation of the key. bin_x_N_Minus_1 is the value which we use to calculate the next bit.
In the first for loop, we loop through the bits in the key (the first bit is always a 0). We calculate x_N using that formula (bin_x_N_Minus_1 is the previous value of x_N). If x_N is positive, the corresponding bit in the key is 1, otherwise it is a zero.
tic and toc are used to time how long this for loop takes.
The second for loop converts the bits of the key into bytes, and stores them in output array, key. The Kth entry in key is the 8 bit number represented by taking every (N/8)th entry in bin_x, starting from K.
The variables r and t are unused.