big integer speed (VPI and symbolic)
Show older comments
I am doing integer exponentiation modulo a prime, with large integers. This just out of curiosity (crypto), I am a real novice in all this. What I am doing, is the following: take a big integer 'g', and subsequently multiply it by itself to create the exponentiation (g^1, g^2, g^3 ...). All of this modulo a big prime 'p'. When I run the following code in Matlab, it takes 1.4 seconds for a table of 1000 exponentiations. This is on a core i5, Matlab 2013b. Inspection with the profiler showed that the biggest chunk of time is due to the mod() line.
====================================
p = sym('13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171')
g = sym('11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568')
nr = 1000
gv = cell(nr,1);
gv{1} = g;
gt = 1;
for ii = 2:nr
gt = mod(gt*g,p);
gv{ii} = gt;
end
t = toc
====================================
I want to create a table of around 1000000 entries, so 1000 times as big as this one. Because this would take a lot of time, I also tried something in iPython (I'm also a novice in iPython):
====================================
t0 = time.time()
p = mpz('13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171')
g = mpz('11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568')
nr = 1000000
table = {}
gv = 1
for ii in range (0,nr):
gv = gmpy2.f_mod(gv*g,p)
table[ii] = gv
print(time.time()-t0)
====================================
This only took 1.5 seconds for all 1000000 entries. I can not explain this huge difference in speed. I also tried the VPI tool: VPI But that is even slower than the symbolic toolbox.
Can anyone give some insight on this? Am I doing something wrong here or is Python just faster in this case?
Kind regards, Bart
Accepted Answer
More Answers (2)
Yair Altman
on 21 Feb 2014
2 votes
@Bart - you might also try the following alternatives:
- Advanpix Multi-Precision Computing Toolbox (MCT). Commercial. Reportedly 40-200x faster than Symbolic Toolbox's vpa. It uses GMP and MPFR, among others.
- Ben Barrowes' Multiple Precision Toolbox (MPT). FEX open-source. This toolbox also relies on GMP and MPFR, however it appears not to have updated since 2006 whereas MCT is fully up-to-date.
Akram Choukri
on 9 Jan 2019
0 votes
how to convert str to vpi ??
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!