correct modular inverse
This commit is contained in:
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user