lib/bigint.js
function millerRabin(x,b) {
    var i,j,k,s;

    if (mr_x1.length!=x.length) {
      mr_x1=dup(x);
      mr_r=dup(x);
      mr_a=dup(x);
    }

    copy_(mr_a,b);
    copy_(mr_r,x);
    copy_(mr_x1,x);

    addInt_(mr_r,-1);
    addInt_(mr_x1,-1);

    //s=the highest power of two that divides mr_r

    /*
    k=0;
    for (i=0;i<mr_r.length;i++)
      for (j=1;j<mask;j<<=1)
        if (x[i] & j) {
          s=(k<mr_r.length+bpe ? k : 0); 
           i=mr_r.length;
           j=mask;
        } else
          k++;
    */

    /* http://www.javascripter.net/math/primes/millerrabinbug-bigint54.htm */
    if (isZero(mr_r)) return 0;
    for (k=0; mr_r[k]==0; k++);
    for (i=1,j=2; mr_r[k]%j==0; j*=2,i++ );
    s = k*bpe + i - 1;
    /* end */

    if (s)                
      rightShift_(mr_r,s);

    powMod_(mr_a,mr_r,x);

    if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) {
      j=1;
      while (j<=s-1 && !equals(mr_a,mr_x1)) {
        squareMod_(mr_a,x);
        if (equalsInt(mr_a,1)) {
          return 0;
        }
        j++;
      }
      if (!equals(mr_a,mr_x1)) {
        return 0;
      }
    }
    return 1;  
  }
function millerRabin(x,b) {
    var i,j,k,s;

    if (mr_x1.length!=x.length) {
      mr_x1=dup(x);
      mr_r=dup(x);
      mr_a=dup(x);
    }

    copy_(mr_a,b);
    copy_(mr_r,x);
    copy_(mr_x1,x);

    addInt_(mr_r,-1);
    addInt_(mr_x1,-1);

    //s=the highest power of two that divides mr_r

    /*
    k=0;
    for (i=0;i<mr_r.length;i++)
      for (j=1;j<mask;j<<=1)
        if (x[i] & j) {
          s=(k<mr_r.length+bpe ? k : 0); 
           i=mr_r.length;
           j=mask;
        } else
          k++;
    */

    /* http://www.javascripter.net/math/primes/millerrabinbug-bigint54.htm */
    if (isZero(mr_r)) return 0;
    for (k=0; mr_r[k]==0; k++);
    for (i=1,j=2; mr_r[k]%j==0; j*=2,i++ );
    s = k*bpe + i - 1;
    /* end */

    if (s)                
      rightShift_(mr_r,s);

    powMod_(mr_a,mr_r,x);

    if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) {
      j=1;
      while (j<=s-1 && !equals(mr_a,mr_x1)) {
        squareMod_(mr_a,x);
        if (equalsInt(mr_a,1)) {
          return 0;
        }
        j++;
      }
      if (!equals(mr_a,mr_x1)) {
        return 0;
      }
    }
    return 1;  
  }
function millerRabin ( x , b ) { var i , j , k , s ; if ( mr_x1 . length != x . length ) { mr_x1 = dup ( x ) ; mr_r = dup ( x ) ; mr_a = dup ( x ) ; } copy_ ( mr_a , b ) ; copy_ ( mr_r , x ) ; copy_ ( mr_x1 , x ) ; addInt_ ( mr_r , - 1 ) ; addInt_ ( mr_x1 , - 1 ) ; //s=the highest power of two that divides mr_r /*
    k=0;
    for (i=0;i<mr_r.length;i++)
      for (j=1;j<mask;j<<=1)
        if (x[i] & j) {
          s=(k<mr_r.length+bpe ? k : 0); 
           i=mr_r.length;
           j=mask;
        } else
          k++;
    */ /* http://www.javascripter.net/math/primes/millerrabinbug-bigint54.htm */ if ( isZero ( mr_r ) ) return 0 ; for ( k = 0 ; mr_r [ k ] == 0 ; k ++ ) ; for ( i = 1 , j = 2 ; mr_r [ k ] % j == 0 ; j *= 2 , i ++ ) ; s = k * bpe + i - 1 ; /* end */ if ( s ) rightShift_ ( mr_r , s ) ; powMod_ ( mr_a , mr_r , x ) ; if ( ! equalsInt ( mr_a , 1 ) && ! equals ( mr_a , mr_x1 ) ) { j = 1 ; while ( j <= s - 1 && ! equals ( mr_a , mr_x1 ) ) { squareMod_ ( mr_a , x ) ; if ( equalsInt ( mr_a , 1 ) ) { return 0 ; } j ++ ; } if ( ! equals ( mr_a , mr_x1 ) ) { return 0 ; } } return 1 ; }
does a single round of Miller-Rabin base b consider x to be a possible prime? x and b are bigInts with b<x
does a single round of Miller - Rabin base b consider x to be a possible prime? x and b are bigInts with b<x