modpow biginteger javascript
Frecuentes
Visto 2,214 equipos
1
I look for a good algorithme JavaScript because I tried this with node.js :
function modpow_3(a,n, module){
var u = BigInt('1');
var e = equals(a, u);
if( e) return a;
if(equalsZero(a)) return a;
if(pair(n)){
x= modpow_2(a, (divide(n, BigInt('2'))));
return mod(multiply(x,x), module);
}else{
x= modpow_2(a, (divide(subs(n, BigInt(1) ), BigInt('2'))));
return mod(multiply(multiply(x,x), a), module);
}
}
But I have an error : RangeError: Maximum call stack size exceeded
1 Respuestas
0
Prueba algo como esto ...
const prime = 101n
function modPow(expo, base, p=prime) {
// "expo" needs to be of type BigInt
let x = BigInt(base) % p, res = expo & 1n? x: 1n
do {
x = x**2n % p
if (expo & 2n) res = res * x % p
} while (expo /= 2n)
return res
}
const res = modPow(9n, 2n)
contestado el 18 de mayo de 23 a las 15:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript node.js algorithm or haz tu propia pregunta.
Did you try with an iterative version, for instance something like that es.wikipedia.org/wiki/… ? - user189
You could use a library that does it for you, like Crunch (crunch.secureroom.net). It includes modular exponentiation code. Or look at Chapter 14 in the Handbook of Applied Cryptography, it has lots of algorithms, and is available free online. - Nenad Vukicevic
one thing that i notice is that BigInt(1) where 1 is not in '' - Vikram Bhat
mira aquí stackoverflow.com/q/18577076/2521214 for mine implementation of modpow in C++. I am not JAVA coder but most likely you have some problem inside modpow_2 and also I do not see the logic behind your pow computation (missing some form of loop but it can be there in form of that recursion but you do not provide modpow_2 code so hard to say), btw instead of divide by 2 you can use bit shift (on bigint the compiler can not do that for you via optimizations) - Spektre