src/crypto/public_key/rsa.js
async function(m, n, e, d, p, q, u) {
    if (n.cmp(m) <= 0) {
      throw new Error('Data too large.');
    }
    const dq = d.mod(q.subn(1)); // d mod (q-1)
    const dp = d.mod(p.subn(1)); // d mod (p-1)
    const pred = new BN.red(p);
    const qred = new BN.red(q);
    const nred = new BN.red(n);

    let blinder;
    let unblinder;
    if (config.rsa_blinding) {
      unblinder = (await random.getRandomBN(new BN(2), n)).toRed(nred);
      blinder = unblinder.redInvm().redPow(e);
      m = m.toRed(nred).redMul(blinder).fromRed();
    }

    const mp = m.toRed(pred).redPow(dp);
    const mq = m.toRed(qred).redPow(dq);
    const t = mq.redSub(mp.fromRed().toRed(qred));
    const h = u.toRed(qred).redMul(t).fromRed();

    let result = h.mul(p).add(mp).toRed(nred);

    if (config.rsa_blinding) {
      result = result.redMul(unblinder);
    }

    return result.toArrayLike(Uint8Array, 'be', n.byteLength());
  }
async function(m, n, e, d, p, q, u) {
    if (n.cmp(m) <= 0) {
      throw new Error('Data too large.');
    }
    const dq = d.mod(q.subn(1)); // d mod (q-1)
    const dp = d.mod(p.subn(1)); // d mod (p-1)
    const pred = new BN.red(p);
    const qred = new BN.red(q);
    const nred = new BN.red(n);

    let blinder;
    let unblinder;
    if (config.rsa_blinding) {
      unblinder = (await random.getRandomBN(new BN(2), n)).toRed(nred);
      blinder = unblinder.redInvm().redPow(e);
      m = m.toRed(nred).redMul(blinder).fromRed();
    }

    const mp = m.toRed(pred).redPow(dp);
    const mq = m.toRed(qred).redPow(dq);
    const t = mq.redSub(mp.fromRed().toRed(qred));
    const h = u.toRed(qred).redMul(t).fromRed();

    let result = h.mul(p).add(mp).toRed(nred);

    if (config.rsa_blinding) {
      result = result.redMul(unblinder);
    }

    return result.toArrayLike(Uint8Array, 'be', n.byteLength());
  }
async function ( m , n , e , d , p , q , u ) { if ( n . cmp ( m ) <= 0 ) { throw new Error ( 'Data too large.' ) ; } const dq = d . mod ( q . subn ( 1 ) ) ; // d mod (q-1) const dp = d . mod ( p . subn ( 1 ) ) ; // d mod (p-1) const pred = new BN . red ( p ) ; const qred = new BN . red ( q ) ; const nred = new BN . red ( n ) ; let blinder ; let unblinder ; if ( config . rsa_blinding ) { unblinder = ( await random . getRandomBN ( new BN ( 2 ) , n ) ) . toRed ( nred ) ; blinder = unblinder . redInvm ( ) . redPow ( e ) ; m = m . toRed ( nred ) . redMul ( blinder ) . fromRed ( ) ; } const mp = m . toRed ( pred ) . redPow ( dp ) ; const mq = m . toRed ( qred ) . redPow ( dq ) ; const t = mq . redSub ( mp . fromRed ( ) . toRed ( qred ) ) ; const h = u . toRed ( qred ) . redMul ( t ) . fromRed ( ) ; let result = h . mul ( p ) . add ( mp ) . toRed ( nred ) ; if ( config . rsa_blinding ) { result = result . redMul ( unblinder ) ; } return result . toArrayLike ( Uint8Array , 'be' , n . byteLength ( ) ) ; }
Decrypt RSA message
@param {BN} m message
@param {BN} n RSA public modulus
@param {BN} e RSA public exponent
@param {BN} d RSA private exponent
@param {BN} p RSA private prime p
@param {BN} q RSA private prime q
@param {BN} u RSA private inverse of prime q
@returns {BN} RSA Plaintext
@async
Decrypt RSA message