exponentiation

Modular Exponentiation in Java

狂风中的少年 提交于 2019-12-05 12:06:13
I need a way to calculate: (g^u * y^v) mod p in Java. I've found this algorithm for calculating (g^u) mod p: int modulo(int a,int b,int c) { long x=1 long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return (int) x%c; } and it works great, but I can't seem to find a way to do this for (g^u * y^v) mod p as my math skills are lackluster. To put it in context, it's for a java implementation of a "reduced" DSA - the verifying part requires this to be solved. Assuming that the two factors will not overflow, I believe you can simplify an expression like

What is the Prolog operator ^?

≡放荡痞女 提交于 2019-12-05 06:16:54
What is the Prolog operator ^ ? Looking at The Prolog Built-in Directive op gives a list of the built-in operators. I see ** is exponentiation /\ is or but what is ^ ? Each of the three current answers are of value and I learned something: Roy for the book false for the examples I accepted the answer by CapelliC because it made clear that ^/2 has multiple meanings depending on context which instantly cleared up my confusion. In Prolog, most symbols can be used 'uninterpreted', at syntactic level, in particular after a op/3 declaration, any atom can be used as operator . Then you can use, for

Raise 10 to a power in javascript, are there better ways than this

早过忘川 提交于 2019-12-04 22:57:42
I have a need to create an integer value to a specific power (that's not the correct term, but basically I need to create 10, 100, 1000, etc.) The "power" will be specified as a function parameter. I came up with a solution but MAN does it feel hacky and wrong. I'd like to learn a better way if there is one, maybe one that isn't string based? Also, eval() is not an option. Here is what I have at this time: function makeMultiplierBase(precision) { var numToParse = '1'; for(var i = 0; i < precision; i++) { numToParse += '0'; } return parseFloat(numToParse); } I also just came up with this non

Minimal addition-chain exponentiation

血红的双手。 提交于 2019-12-04 21:38:49
问题 I know it has been proven NP-complete, and that's ok. I'm currently solving it with branch and bound where I set the initial upper limit at the number of multiplications it would take the normal binary square/multiply algorithm, and it does give the right answers, but I'm not satisfied with the running time (it can take several seconds for numbers around 200). This being an NP-complete problem, I'm not expecting anything spectacular; but there are often tricks to get the Actual Time under

Modular exponentiation fails for large mod in C++

早过忘川 提交于 2019-12-04 13:35:43
问题 This is the code I'm using for calculating (n^p)%mod . Unfortunately, it fails for large values of mod (in my case mod = 10000000000ULL ) when I call it from main() method. Any idea; why? ull powMod(ull n, ull p, ull mod) { ull ans = 1; n = n%mod; while(p) { if(p%2 == 1) { ans = (ans*n)%mod; } n = (n*n)%mod; p /= 2; } return ans; } Here, ull is a typedef for unsigned long long . 回答1: Yes you can do it in C++. As others pointed out you cannot do it directly . Using a little drop of number

Why does numpy.power return 0 for small exponents while math.pow returns the correct answer?

烈酒焚心 提交于 2019-12-04 02:42:59
问题 In [25]: np.power(10,-100) Out[25]: 0 In [26]: math.pow(10,-100) Out[26]: 1e-100 I would expect both the commands to return 1e-100. This is not a precision issue either, since the issue persists even after increasing precision to 500. Is there some setting which I can change to get the correct answer? 回答1: Oh, it's much "worse" than that: In [2]: numpy.power(10,-1) Out[2]: 0 But this is a hint to what's going on: 10 is an integer, and numpy.power doesn't coerce the numbers to floats. But this

Minimal addition-chain exponentiation

瘦欲@ 提交于 2019-12-03 13:35:25
I know it has been proven NP-complete, and that's ok. I'm currently solving it with branch and bound where I set the initial upper limit at the number of multiplications it would take the normal binary square/multiply algorithm, and it does give the right answers, but I'm not satisfied with the running time (it can take several seconds for numbers around 200). This being an NP-complete problem, I'm not expecting anything spectacular; but there are often tricks to get the Actual Time under control somewhat. Are there faster ways to do this in practice? If so, what are they? This looks like

Fastest modular exponentiation in JavaScript

丶灬走出姿态 提交于 2019-12-03 05:59:26
问题 My problem is to compute (g^x) mod p quickly in JavaScript, where ^ is exponentiation, mod is the modulo operation. All inputs are nonnegative integers, x has about 256 bits, and p is a prime number of 2048 bits, and g may have up to 2048 bits. Most of the software I've found that can do this in JavaScript seems to use the JavaScript BigInt library (http://www.leemon.com/crypto/BigInt.html). Doing a single exponentiation of such size with this library takes about 9 seconds on my slow browser

How to do exponentiation in clojure?

六月ゝ 毕业季﹏ 提交于 2019-12-03 02:06:34
问题 How can I do exponentiation in clojure? For now I'm only needing integer exponentiation, but the question goes for fractions too. 回答1: classic recursion (watch this, it blows stack) (defn exp [x n] (if (zero? n) 1 (* x (exp x (dec n))))) tail recursion (defn exp [x n] (loop [acc 1 n n] (if (zero? n) acc (recur (* x acc) (dec n))))) functional (defn exp [x n] (reduce * (repeat n x))) sneaky (also blows stack, but not so easily) (defn exp-s [x n] (let [square (fn[x] (* x x))] (cond (zero? n) 1

Fastest modular exponentiation in JavaScript

左心房为你撑大大i 提交于 2019-12-02 18:30:15
My problem is to compute (g^x) mod p quickly in JavaScript, where ^ is exponentiation, mod is the modulo operation. All inputs are nonnegative integers, x has about 256 bits, and p is a prime number of 2048 bits, and g may have up to 2048 bits. Most of the software I've found that can do this in JavaScript seems to use the JavaScript BigInt library ( http://www.leemon.com/crypto/BigInt.html ). Doing a single exponentiation of such size with this library takes about 9 seconds on my slow browser (Firefox 3.0 with SpiderMonkey). I'm looking for a solution which is at least 10 times faster. The