UNPKG

@stellot/crypto

Version:

Crypto libraries for front and backend

22 lines (21 loc) 838 B
import { BigInteger as BigInt } from 'jsbn'; import * as Errors from './errors'; import { parseBigInt } from './utils'; import EncryptionElGamal from './encryption'; import * as Utils from "./utils"; export default class DecryptionElGamal extends EncryptionElGamal { constructor(p, g, y, x) { super(p, g, y); this.x = parseBigInt(x); } decrypt(m) { if (!this.x) throw new Errors.MissingPrivateKeyError(); const p = this.p; const r = Utils.getRandomBigInt(Utils.BIG_TWO, this.p.subtract(BigInt.ONE)); const aBlind = this.g.modPow(r, p).multiply(m.a).remainder(p); const ax = aBlind.modPow(this.x, p); const plaintextBlind = ax.modInverse(p).multiply(m.b).remainder(p); return this.y.modPow(r, p).multiply(plaintextBlind).remainder(p); } }