Exemplo n.º 1
0
int millerrabin(BIGNUM *bn_n, int maxitr, FILE *primesfile, int num_idnt){
	int s = 0;
	BIGNUM *bn_r = NULL;
	BIGNUM *bn_n_1 = NULL;
	BN_CTX *bn_ctx = NULL;
	BIGNUM *bn_a = NULL;
	BIGNUM *bn_y = NULL;
	BIGNUM *bn_1 = NULL;
	int i = 0;
	int j = 0;

	bn_a = BN_new();
	bn_y = BN_new();
	bn_r = BN_new();
	bn_1 = BN_new();
	BN_one(bn_1);
	bn_ctx = BN_CTX_new();
	bn_n_1 = BN_new();
	BN_CTX_init(bn_ctx);
	fseek(primesfile, 0 ,SEEK_SET);
	s = compute_sr(bn_n, bn_r, bn_n_1, bn_ctx);
	if(s == -1){
		return -1;
	}
	
	if(num_idnt == 0){
		fprintf(stdout, "n = %s\n", BN_bn2dec(bn_n));
	}
	printIndents(num_idnt);
	fprintf(stdout, "  n-1 = %s\n", BN_bn2dec(bn_n_1));
	printIndents(num_idnt);
	fprintf(stdout, "  s = %d\n", s);
	printIndents(num_idnt);
	fprintf(stdout, "  r = %s\n", BN_bn2dec(bn_r));
	
	for(i = 1; i <= maxitr; i++){
		printIndents(num_idnt);
		fprintf(stdout, "  Itr %d of %d, ", i, maxitr);
		
		ithPrime(i, primesfile, bn_a);
		if(BN_cmp(bn_a, bn_n_1) == 1){
			return -1;
		}
		
		compute_y(bn_y, bn_a, bn_r, bn_n, bn_ctx);
		
		
		if(BN_cmp(bn_y, bn_1) != 0 && BN_cmp(bn_y, bn_n_1) != 0){
			fprintf(stdout, "a = %s, y = %s\n", BN_bn2dec(bn_a), BN_bn2dec(bn_y));
			for(j = 1; j <= s - 1; j++){
				BN_mod_mul(bn_y, bn_y, bn_y, bn_n, bn_ctx);
				printIndents(num_idnt);
				fprintf(stdout, "    j = %d of %d, y = %s", j, s - 1, BN_bn2dec(bn_y));
				if(BN_cmp(bn_y, bn_n_1) == 0){
					fprintf(stdout, " (which is n-1)\n");
					break;
				}
				putchar('\n');
				
				if(BN_cmp(bn_y, bn_1) == 0){
					return 0;
				}
			}
			
			if(BN_cmp(bn_y, bn_n_1) != 0){
				printIndents(num_idnt);
				fprintf(stdout, "Miller-Rabin found a strong witness %s\n", BN_bn2dec(bn_a));
				return 0;
			}
		}
		else{
			if(BN_cmp(bn_y, bn_n_1) == 0){
				fprintf(stdout, "a = %s, y = %s (which is n-1)\n", BN_bn2dec(bn_a), BN_bn2dec(bn_y));
			}
			else{
				fprintf(stdout, "a = %s, y = %s\n", BN_bn2dec(bn_a), BN_bn2dec(bn_y));
			}
		}
		
		
	}
	printIndents(num_idnt);
	fprintf(stdout, "Miller-Rabin declares n to be a prime number\n");	
	return 1;
	
	BN_free(bn_1);
	BN_free(bn_a);
	BN_free(bn_y);
	BN_free(bn_r);
	BN_CTX_free(bn_ctx);
}
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
    /* Declaration of input and output arguments. */
    double *x, *u, **p, *dx, *y, *t;
    int     i, np, nu, nx;
    const mxArray *auxvar = NULL; /* Cell array of additional data. */
    
    if (nrhs < 3) {
        mexErrMsgIdAndTxt("IDNLGREY:ODE_FILE:InvalidSyntax",
        "At least 3 inputs expected (t, u, x).");
    }
    
    /* Determine if auxiliary variables were passed as last input.  */
    if ((nrhs > 3) && (mxIsCell(prhs[nrhs-1]))) {
        /* Auxiliary variables were passed as input. */
        auxvar = prhs[nrhs-1];
        np = nrhs - 4; /* Number of parameters (could be 0). */
    } else {
        /* Auxiliary variables were not passed. */
        np = nrhs - 3; /* Number of parameters. */
    }
    
    /* Determine number of inputs and states. */
    nx = mxGetNumberOfElements(prhs[1]); /* Number of states. */
    nu = mxGetNumberOfElements(prhs[2]); /* Number of inputs. */
    
    /* Obtain double data pointers from mxArrays. */
    t = mxGetPr(prhs[0]);  /* Current time value (scalar). */
    x = mxGetPr(prhs[1]);  /* States at time t. */
    u = mxGetPr(prhs[2]);  /* Inputs at time t. */
    
    p = mxCalloc(np, sizeof(double*));
    for (i = 0; i < np; i++) {
        p[i] = mxGetPr(prhs[3+i]); /* Parameter arrays. */
    }
    
    /* Create matrix for the return arguments. */
    plhs[0] = mxCreateDoubleMatrix(nx, 1, mxREAL);
    plhs[1] = mxCreateDoubleMatrix(NY, 1, mxREAL);
    dx      = mxGetPr(plhs[0]); /* State derivative values. */
    y       = mxGetPr(plhs[1]); /* Output values. */
    
    /*
      Call the state and output update functions.
      
      Note: You may also pass other inputs that you might need,
      such as number of states (nx) and number of parameters (np).
      You may also omit unused inputs (such as auxvar).
      
      For example, you may want to use orders nx and nu, but not time (t)
      or auxiliary data (auxvar). You may write these functions as:
          compute_dx(dx, nx, nu, x, u, p);
          compute_y(y, nx, nu, x, u, p);
    */
    
    /* Call function for state derivative update. */
    compute_dx(dx, t[0], x, u, p, auxvar);
    
    /* Call function for output update. */
    compute_y(y, t[0], x, u, p, auxvar);
    
    /* Clean up. */
    mxFree(p);
}