Skip to content

Conversation

krishnajalan
Copy link

this function run in log(N) time complexity and log(N) space complexity to find the modular exponent and can be used cryptographic encryption to find the modular exponent of a really large prime numer

this function run in log(N) time complexity and log(N) space complexity to find the modular exponent and can be used cryptographic encryption to find the modular exponent of a really large prime numer
@coveralls
Copy link

Coverage Status

Coverage decreased (-2.3%) to 95.75% when pulling ac92910 on krishnajalan:patch-1 into f27b20f on warner:master.

@coveralls
Copy link

coveralls commented Aug 28, 2020

Coverage Status

Coverage decreased (-0.2%) to 97.772% when pulling 9226ce3 on krishnajalan:patch-1 into f27b20f on warner:master.

@tomato42
Copy link
Member

how much faster it is than the built-in pow()? how much faster it is than pow() when gmpy2 is used?

I've done some quick benchmarks and it's at least 4 times slower with the native ints on python3, do you see something different?

removed an extra for loop and merge its content in first loop
so must increase performance
@krishnajalan
Copy link
Author

how much faster it is than the built-in pow()? how much faster it is than pow() when gmpy2 is used?

I've done some quick benchmarks and it's at least 4 times slower with the native ints on python3, do you see something different?

now made some tweeks might be faster now
and this method is the one used for encrytion and decryption algorythm
this is better than built in pow as this can handle much larger prime number (more than 100 digit number)

@tomato42
Copy link
Member

now it doesn't work, ans=1 is missing

also, it is slower than pow:

a = 2
x = 0x2341b79adf72ef90a532158BFD3E2B9C8CF51558903BA0D0F8435A021FFF5E91479E
p = 0xEEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3

In [9]: %timeit fast_modular_exponentiation(a, x, p)
7.01 ms ± 2.65 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [10]: %timeit pow(a, x, p)
638 µs ± 2.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

@krishnajalan
Copy link
Author

krishnajalan commented Aug 31, 2020

now this is fast please review this code

a = 2
x = 0x2341b79adf72ef90a532158BFD3E2B9C8CF51558903BA0D0F8435A021FFF5E91479E
p = 0xEEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3

%timeit fast_modular_exponentiation(a, x, p)
2.12 µs ± 6.39 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit pow(a, x, p)
798 µs ± 4.51 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

@tomato42
Copy link
Member

it's also providing incorrect result:

In [8]: pow(a, x, p)
Out[8]: 156585191525109838650514497544841306938118786727686521188924477616074565750827971867070903274582757259308907082099809400189995273960711551098573085830248220735659354382108924600679178686704643615034564481430785405384599047848897245930269730454359626054001725047726611533932270804253803224884355215811899509232

In [9]: fast_modular_exponentiation(a, x, p)
Out[9]: 1

@krishnajalan
Copy link
Author

krishnajalan commented Aug 31, 2020

now this is fast please review this code

a = 2
x = 0x2341b79adf72ef90a532158BFD3E2B9C8CF51558903BA0D0F8435A021FFF5E91479E
p = 0xEEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3

%timeit fast_modular_exponentiation(a, x, p)
2.12 µs ± 6.39 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit pow(a, x, p)
798 µs ± 4.51 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

@tomato42
Copy link
Member

now this is fast please review this code

a = 2
x = 0x2341b79adf72ef90a532158BFD3E2B9C8CF51558903BA0D0F8435A021FFF5E91479E
p = 0xEEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3

%timeit fast_modular_exponentiation(a, x, p)
2.12 µs ± 6.39 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit pow(a, x, p)
798 µs ± 4.51 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

this is more than 100 time faster

return 1 would be even faster, doesn't make it particularly useful, does it?

  1. why are you talking about prime numbers? this is trying to use the basic square and multiply algorithm
  2. why you think that the built-in pow doesn't use this or even more efficient algorithm? and why do you think that pow can't handle large numbers (not that 100 decimal digits is large...)
  3. this code is not py2 and py3 compatible
  4. you're modifying code that is explicitly marked as deprecated and unused while not removing the deprecation and updating rest of the library code to start using it
  5. this code is missing any test coverage
  6. it's not following the formatting guidelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants