modular-arithmetic

Simplify modular exponentiation C++

穿精又带淫゛_ 提交于 2020-08-20 12:24:54
问题 I am attempting to write the decryption function for an RSA encryption system, everything seemed to be working fine for very small numbers, however sometimes the output just isn't correct (I think that the cause may be a floating point error or some kind of stack overflow). The process which is causing me problems can be simplified to (11^23) mod 187 but I will include the full code in case anybody wants to see it. I know that the answer should be 88 as it is the example used in Appendix J of

Modular opertation (%) provides false output

百般思念 提交于 2020-07-15 08:20:40
问题 With a function, getNextIdx , I want to receive a new index for an array that depends on the current index and the value of the array at that index. I want the function to return the new index by summing the current index with the value of the array at that index, modular to the array size. #include<vector> using namespace std; int getNextIdx(int currentIdx, vector<int> array) { int jump = array[currentIdx]; int nextIdx = (currentIdx + jump) % array.size(); return (nextIdx >= 0) ? nextIdx :

Can this script have better performance using modular exponentiation?

人走茶凉 提交于 2020-01-24 01:37:16
问题 def f(a, b, c): return ((a ** b)-1) // c % b Can this script be faster in some way? (I have been looking for something with modular exponentiation): pow(a, b, c) == a ** b % c but this above script doesn't seem to be improvable like that. Does anyone know a way to speedup the above script? Thanks in advance. Edit: The second script is not at all the same as the first one, it is just meant to show what kind of optimization I had in mind. Edit: I didn't put the exact equation in becouse I

How to implement modular exponentiation?

雨燕双飞 提交于 2020-01-05 05:42:12
问题 I am trying to calculate something like this: a^b mod c, where all three numbers are large. Things I've tried: Python's pow() function is taking hours and has yet to produce a result. (if someone could tell me how it's implemented that would be very helpful!) A right-to-left binary method that I implemented, with O(log e) time, would take about 30~40 hours (don't wanna wait that long). Various recursion methods are producing segmentation faults (after I changed the recursion limits) Any

Finding binomial coefficient for large n and k modulo m

懵懂的女人 提交于 2019-12-30 06:58:08
问题 I want to compute nCk mod m with following constraints: n<=10^18 k<=10^5 m=10^9+7 I have read this article: Calculating Binomial Coefficient (nCk) for large n & k But here value of m is 1009. Hence using Lucas theorem, we need only to calculate 1009*1009 different values of aCb where a,b<=1009 How to do it with above constraints. I cannot make a array of O(m*k) space complexity with given constraints. Help! 回答1: Just use the fact that (n, k) = n! / k! / (n - k)! = n*(n-1)*...*(n-k+1)/[k*(k-1)

Built-in mod ('%') vs custom mod function: improve the performance of modulus operation

折月煮酒 提交于 2019-12-29 04:03:47
问题 Recently I came to know that the mod('%') operator is very slow. So I made a function which will work just like a%b. But is it faster than the mod operator? Here's my function int mod(int a, int b) { int tmp = a/b; return a - (b*tmp); } 回答1: According to Chandler Carruth's benchmarks at CppCon 2015, the fastest modulo operator (on x86, when compiled with Clang) is: int fast_mod(const int input, const int ceil) { // apply the modulo operator only when needed // (i.e. when the input is greater

Montgomery multiplication VHDL Implementation

若如初见. 提交于 2019-12-24 10:49:48
问题 I am trying to create a modular arithmetic operation in this case: x*y mod n As far as I have read the fastest way to do it is using the Montgomery multiplication, but I cant understand how that is actually done in other to implement it in hardware using VHDL. Has someone been able to do it or have any library that enables me to use it? 回答1: A basic shift and add modular multiplication can be found here as a part of this open cores project. Here's another project that is using the Montgomery