correct modular inverse

This commit is contained in:
jude
2023-03-04 10:50:49 +00:00
parent ed171bf77f
commit ab629a78b4
3 changed files with 13 additions and 9 deletions

View File

@ -21,10 +21,14 @@ export function mod_inv(a, n) {
while (new_r !== 0n) {
let quotient = r / new_r;
let t_temp = t;
t = new_t;
new_t = t - quotient * new_t;
new_t = t_temp - quotient * new_t;
let r_temp = r;
r = new_r;
new_r = r - quotient * new_r;
new_r = r_temp - quotient * new_r;
}
if (t < 0) {

View File

@ -6,10 +6,10 @@ let p, q, pubKey, privKey;
class PubKey {
constructor(p, q) {
this.n = p * q;
this.e = 65537;
this.e = 65537n;
}
decrypt(m) {
encrypt(m) {
return mod_exp(m, this.e, this.n);
}
}
@ -17,10 +17,10 @@ class PubKey {
class PrivKey {
constructor(p, q) {
this.n = p * q;
this.d = mod_inv(65537, (q - 1) * (p - 1));
this.d = mod_inv(65537n, (q - 1n) * (p - 1n));
}
encrypt(c) {
decrypt(c) {
return mod_exp(c, this.d, this.n);
}
}