Home:ALL Converter>How to fix the difference in precision between double data type and uint64

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

Ask Time:2019-07-28T17:34:48         Author:Naseem

Json Formatter

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

Author:Naseem,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57239599/how-to-fix-the-difference-in-precision-between-double-data-type-and-uint64
yy