modExp( b, e, n )

Version 1.0.0.0 (416 Bytes) by John Harakas
Modular exponentiation. Very efficient (until you overflow).
104 Downloads
Updated 2 May 2016

View License

function y = modExp( b, e, n )
% Fast modular exponentiation e.g.: y = b^e mod n
% Applied Cryptography: Protocols, Algorithms, and Source Code in C,
% page 245
if (n == 1)
y = 0;
return
end
y = uint64(1);
b = mod(b,n);
while ( e > 0 )
if ( mod(e,2) == 1)
y = mod((y * b),n);
end
e = bitshift(e,-1);
b = mod( (b * b),n ) ;
end
end

Cite As

John Harakas (2026). modExp( b, e, n ) (https://www.mathworks.com/matlabcentral/fileexchange/56888-modexp-b-e-n), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Encryption / Cryptography in Help Center and MATLAB Answers
Version Published Release Notes
1.0.0.0