Пример #1
0
bool miller_rabin(LL n,int s=100) {
	// iterate s times of witness on n
	// return 1 if prime, 0 otherwise
	if(n<2) return 0;
	if(!(n&1)) return n == 2;
	LL u=n-1; int t=0;
	// n-1 = u*2^t
	while(!(u&1)) u>>=1, t++;
	while(s--){
		LL a=randll()%(n-1)+1;
		if(witness(a,n,u,t)) return 0;
	}
	return 1;
}
Пример #2
0
bool Is_prime(int x,int count)
{
	int m=x-1,j=0;
	for(;!(m&0x1);m>>=1)
		j+=1;
	srand((int)time(0));
	
	for(int i=0;i<count;i++)
	{
		int a=rand()%(x-1)+1;
		if(witness(a,m,j,x))
			return false;
	}
	return true;
}
Пример #3
0
/** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
{
    UniValue entry(UniValue::VOBJ);
    entry.push_back(Pair("txid", txin.prevout.hash.ToString()));
    entry.push_back(Pair("vout", (uint64_t)txin.prevout.n));
    UniValue witness(UniValue::VARR);
    for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
        witness.push_back(HexStr(txin.scriptWitness.stack[i].begin(), txin.scriptWitness.stack[i].end()));
    }
    entry.push_back(Pair("witness", witness));
    entry.push_back(Pair("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
    entry.push_back(Pair("sequence", (uint64_t)txin.nSequence));
    entry.push_back(Pair("error", strMessage));
    vErrorsRet.push_back(entry);
}
Пример #4
0
bool _miller_rabin(num_t n, uint32 s, num_t d, uint32 base_no) {
  // n - 1 = d * 2^s.

  const num_t n_prime = montgomery::calc_n_prime(n);
  const num_t r2 = montgomery::calc_r2(n);
  const num_t one = montgomery::init(num_t(1), r2, n, n_prime);
  const num_t minus_one = n - one;

  for (uint32 bi = 0; bi < limits[base_no][1]; ++bi) {
    const num_t base = montgomery::init(num_t(bases[base_no][bi]), r2, n, n_prime);
    if (witness(base, s, d, n, n_prime, one, minus_one)) {
      return false;
    }
  }
  return true;
}
Пример #5
0
int miller_rabin(type n, int s)
{
	type j;
	type a;

	if(n<2) return 0;
	//if(n == 2) return 1;
	if(n != 2 && n%2 == 0) return 0;

	for (j = 0; j < s; j++) {
		a = rand() % (n-1) + 1 ;
		if ( witness(a, n) ) {
			return 0;
		}
	}
	return 1;
}
Пример #6
0
bool miller_rabin(long long n)
{
    if (n == 2) {
        return true;
    }
    
    if (n == 1 || ((n & 1) == 0)) {
        return false;
    }
    
    for (int i = 0; i < 50; i++) {
        long long a = rand() * (n-2) / RAND_MAX + 1;
        if (witness(a, n)) {
            return false;
        }
    }
    return true;
}
Пример #7
0
bool Miller_Rabin(LL n,int S) // n>=2,合数返回1,素数返回0
{
    
    if(n==2) return 0;

    LL x = n - 1, t = 0;
    while ((x & 1) == 0) 
        x >>= 1, t++;
    
    if (t == 0) return 1; // t=0时,n为偶数
    for (int k = 0; k < S; k++)
    {
        LL a = rand() % (n - 1) + 1;
        if (witness(a, n, x, t))
            return 1;
    }
    return 0;
}
Пример #8
0
        /**
         * Method that implements the basic primality test.
         * If witness does not return 1, n is definitely composite.
         * Do this by computing a^i (mod n) and looking for
         * non-trivial square roots of 1 along the way.
         */
        HugeInt witness( const HugeInt & a, const HugeInt & i, const HugeInt & n )
        {
            if( i == 0 )
                return 1;

            HugeInt x = witness( a, i / 2, n );
            if( x == 0 )    // If n is recursively composite, stop
                return 0;

            // n is not prime if we find a non-trivial square root of 1
            HugeInt y = ( x * x ) % n;
            if( y == 1 && x != 1 && x != n - 1 )
                return 0;

            if( i % 2 != 0 )
                y = ( a * y ) % n;

            return y;
        }
Пример #9
0
/**
* @description	素性测试
*
* @param	HugeInt A
* @param	HugeInt i
* @param	HugeInt N
*
* @return	
*/
HugeInt witness(HugeInt A,HugeInt i,HugeInt N) {
	HugeInt X,Y;
	
	if (i < 0 || N <= 0)
		return ;

	if (i == 0)
		return 1;
	
	X = witness(A, i / 2, N);
	if (X == 0)
		return 0;
	
	Y = (X * X) % N;
	if (Y == 1 && X != 1 && X != N -1) 
		return 0;

	if(i % 2 != 0)
		Y = (A * Y) % N;
	
	return Y;
}
Пример #10
0
int BN_is_prime_fasttest(const BIGNUM *a, int checks,
		void (*callback)(int,int,void *),
		BN_CTX *ctx_passed, void *cb_arg,
		int do_trial_division)
	{
	int i, j, ret = -1;
	int k;
	BN_CTX *ctx = NULL;
	BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
	BN_MONT_CTX *mont = NULL;
	const BIGNUM *A = NULL;

	if (BN_cmp(a, BN_value_one()) <= 0)
		return 0;
	
	if (checks == BN_prime_checks)
		checks = BN_prime_checks_for_size(BN_num_bits(a));

	/* first look for small factors */
	if (!BN_is_odd(a))
		return 0;
	if (do_trial_division)
		{
		for (i = 1; i < NUMPRIMES; i++)
			if (BN_mod_word(a, primes[i]) == 0) 
				return 0;
		if (callback != NULL) callback(1, -1, cb_arg);
		}

	if (ctx_passed != NULL)
		ctx = ctx_passed;
	else
		if ((ctx=BN_CTX_new()) == NULL)
			goto err;
	BN_CTX_start(ctx);

	/* A := abs(a) */
	if (a->neg)
		{
		BIGNUM *t;
		if ((t = BN_CTX_get(ctx)) == NULL) goto err;
		BN_copy(t, a);
		t->neg = 0;
		A = t;
		}
	else
		A = a;
	A1 = BN_CTX_get(ctx);
	A1_odd = BN_CTX_get(ctx);
	check = BN_CTX_get(ctx);
	if (check == NULL) goto err;

	/* compute A1 := A - 1 */
	if (!BN_copy(A1, A))
		goto err;
	if (!BN_sub_word(A1, 1))
		goto err;
	if (BN_is_zero(A1))
		{
		ret = 0;
		goto err;
		}

	/* write  A1  as  A1_odd * 2^k */
	k = 1;
	while (!BN_is_bit_set(A1, k))
		k++;
	if (!BN_rshift(A1_odd, A1, k))
		goto err;

	/* Montgomery setup for computations mod A */
	mont = BN_MONT_CTX_new();
	if (mont == NULL)
		goto err;
	if (!BN_MONT_CTX_set(mont, A, ctx))
		goto err;
	
	for (i = 0; i < checks; i++)
		{
		if (!BN_pseudo_rand_range(check, A1))
			goto err;
		if (!BN_add_word(check, 1))
			goto err;
		/* now 1 <= check < A */

		j = witness(check, A, A1, A1_odd, k, ctx, mont);
		if (j == -1) goto err;
		if (j)
			{
			ret=0;
			goto err;
			}
		if (callback != NULL) callback(1,i,cb_arg);
		}
	ret=1;
err:
	if (ctx != NULL)
		{
		BN_CTX_end(ctx);
		if (ctx_passed == NULL)
			BN_CTX_free(ctx);
		}
	if (mont != NULL)
		BN_MONT_CTX_free(mont);

	return(ret);
	}
Пример #11
0
int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
                            int do_trial_division, BN_GENCB *cb)
{
    int i, j, ret = -1;
    int k;
    BN_CTX *ctx = NULL;
    BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
    BN_MONT_CTX *mont = NULL;

    if (BN_cmp(a, BN_value_one()) <= 0)
        return 0;

    if (checks == BN_prime_checks)
        checks = BN_prime_checks_for_size(BN_num_bits(a));

    /* first look for small factors */
    if (!BN_is_odd(a))
        /* a is even => a is prime if and only if a == 2 */
        return BN_is_word(a, 2);
    if (do_trial_division) {
        for (i = 1; i < NUMPRIMES; i++) {
            BN_ULONG mod = BN_mod_word(a, primes[i]);
            if (mod == (BN_ULONG)-1)
                goto err;
            if (mod == 0)
                return BN_is_word(a, primes[i]);
        }
        if (!BN_GENCB_call(cb, 1, -1))
            goto err;
    }

    if (ctx_passed != NULL)
        ctx = ctx_passed;
    else if ((ctx = BN_CTX_new()) == NULL)
        goto err;
    BN_CTX_start(ctx);

    A1 = BN_CTX_get(ctx);
    A1_odd = BN_CTX_get(ctx);
    check = BN_CTX_get(ctx);
    if (check == NULL)
        goto err;

    /* compute A1 := a - 1 */
    if (!BN_copy(A1, a))
        goto err;
    if (!BN_sub_word(A1, 1))
        goto err;
    if (BN_is_zero(A1)) {
        ret = 0;
        goto err;
    }

    /* write  A1  as  A1_odd * 2^k */
    k = 1;
    while (!BN_is_bit_set(A1, k))
        k++;
    if (!BN_rshift(A1_odd, A1, k))
        goto err;

    /* Montgomery setup for computations mod a */
    mont = BN_MONT_CTX_new();
    if (mont == NULL)
        goto err;
    if (!BN_MONT_CTX_set(mont, a, ctx))
        goto err;

    for (i = 0; i < checks; i++) {
        if (!BN_priv_rand_range(check, A1))
            goto err;
        if (!BN_add_word(check, 1))
            goto err;
        /* now 1 <= check < a */

        j = witness(check, a, A1, A1_odd, k, ctx, mont);
        if (j == -1)
            goto err;
        if (j) {
            ret = 0;
            goto err;
        }
        if (!BN_GENCB_call(cb, 1, i))
            goto err;
    }
    ret = 1;
 err:
    if (ctx != NULL) {
        BN_CTX_end(ctx);
        if (ctx_passed == NULL)
            BN_CTX_free(ctx);
    }
    BN_MONT_CTX_free(mont);

    return ret;
}
Пример #12
0
//是否为素数,费马小定理
int isPrime(HugeInt N) {
	return witness(Random(2,N -2),N -1 , N) == 1;
}
Пример #13
0
int main(int argc, char **argv) {
    mpz_t x;            /* witness */
    unsigned int e;     /* random boolean vector (challenge) */
    mpz_t y;            /* response */
    int proof;
    int sockfd;         /* socket file descriptor */
    int index;
    int j;

    char buf[MAXDATASIZE];

    printf("Feige-Fiat-Shamir ZKP implementation\n");

    if (argc != 2) {
        fprintf(stderr, "ZKP Server Usage: %s hostname\n", argv[0]);
        exit(1);
    } else {
        fastseed = TRUE;
        // printf("Warning: fastseed enabled, using a bad random seed value!\n");
    }

    printf("\n");

    mpz_init(rndseed);

    mpz_init(n);
    mpz_set_str(n, N, 10);

    /* Connect to server, T times */
    for (j=0; j < T; j++) {
        printf("Iteration %d: ", j);
        timestamp(stdout);
        printf("\n");

        mpz_init(x);
        mpz_init(y);
        e     = 0;
        proof = 0;
        index = 0;

        // gmp_printf("n = %Zd\n", n);
        // printf("Computing keys.\n");
        compute_keys();                /* Prover chooses public/private keys    */

        sockfd = zkp_connect(argv[1]);

        if (sockfd == 2) {
            return(0);
        }

        /* Prover sends public key, i[K] */
        itoa(K, buf, 10);
        zkp_send(buf, sockfd); // 5 = K
        printf("Sent 1.\n");

        for (index = 0; index < K; index++) {
            // gmp_printf("Sending: %i %Zd\n", index, i[index]);
            mpz_get_str(buf, 10, i[index]);
            zkp_send(buf, sockfd);
            sleep(1);
        }

        /* Prover sends the witness */
        witness(x);
        mpz_get_str(buf, 10, x);
        zkp_send(buf, sockfd);
        /* end, Prover sends the witness */

        /* Verifier/server sends the challenge, we receive */
        zkp_recv(sockfd, buf);
        e = atoi(buf);
        printf("Client: got e: %i\n", e);
        /* End server sends the challenge */

        /* Send the response to the server */
        response(y, e);                /* Prover sends the response             */
        mpz_get_str(buf, 10, y);
        zkp_send(buf, sockfd);

        /* Verifier/server sends the authentication response */
        if (!zkp_recv(sockfd, buf)) {
            printf("Client ZKP: Error receiving client data.\n");
            return(0);
        }
        proof = atoi(buf);

        zkp_disconnect(sockfd);

        if (proof) {
            printf("Authentication successful!\n");
        } else {
            printf("Authentication failed!\n");
        }

        mpz_clear(x);
        mpz_clear(y);

        printf("Iteration %d finished at: ", j);
        timestamp(stdout);
        printf("\n\n");

    }

    mpz_clear(n);
    mpz_clear(rndseed);
    return (1);
}
Пример #14
0
/**
 * Deterministic Miller-Rabin to see if a number is prime.
*/
inline bool pollards::IsPrime(cpp_int number) {

    if (number < 1373653) {
        return is_prime(number);
    }
    // write n−1 as 2^s*d by factoring powers of 2 from n−1
    int count_exponent;
    cpp_int count_rest;

    count_rest = number / 2;
    count_exponent = 1;
    while (!(count_rest & 1)) {
        count_rest /= 2;
        ++count_exponent;
    }

    if (number < 9080191) {
        if (witness(31, number, count_exponent, count_rest)) return false;
        if (witness(73, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("4759123141")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(61, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("2152302898747")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("3474749660383")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        if (witness(13, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("341550071728321")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        if (witness(13, number, count_exponent, count_rest)) return false;
        if (witness(17, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("3825123056546413051")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        if (witness(13, number, count_exponent, count_rest)) return false;
        if (witness(17, number, count_exponent, count_rest)) return false;
        if (witness(19, number, count_exponent, count_rest)) return false;
        if (witness(23, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("18446744073709551616")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        if (witness(13, number, count_exponent, count_rest)) return false;
        if (witness(17, number, count_exponent, count_rest)) return false;
        if (witness(19, number, count_exponent, count_rest)) return false;
        if (witness(23, number, count_exponent, count_rest)) return false;
        if (witness(29, number, count_exponent, count_rest)) return false;
        if (witness(31, number, count_exponent, count_rest)) return false;
        if (witness(37, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("318665857834031151167461")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        if (witness(13, number, count_exponent, count_rest)) return false;
        if (witness(17, number, count_exponent, count_rest)) return false;
        if (witness(19, number, count_exponent, count_rest)) return false;
        if (witness(23, number, count_exponent, count_rest)) return false;
        if (witness(29, number, count_exponent, count_rest)) return false;
        if (witness(31, number, count_exponent, count_rest)) return false;
        if (witness(37, number, count_exponent, count_rest)) return false;
        return true;
    }
    if (number < cpp_int("3317044064679887385961981")) {

        if (witness(2, number, count_exponent, count_rest)) return false;
        if (witness(3, number, count_exponent, count_rest)) return false;
        if (witness(5, number, count_exponent, count_rest)) return false;
        if (witness(7, number, count_exponent, count_rest)) return false;
        if (witness(11, number, count_exponent, count_rest)) return false;
        if (witness(13, number, count_exponent, count_rest)) return false;
        if (witness(17, number, count_exponent, count_rest)) return false;
        if (witness(19, number, count_exponent, count_rest)) return false;
        if (witness(23, number, count_exponent, count_rest)) return false;
        if (witness(29, number, count_exponent, count_rest)) return false;
        if (witness(31, number, count_exponent, count_rest)) return false;
        if (witness(37, number, count_exponent, count_rest)) return false;
        if (witness(41, number, count_exponent, count_rest)) return false;
        return true;
    }

    // max_witness cant be higher than 5 digits if input is <= 29 digits.
    cpp_dec_float_50 tmp = 2 * pow(log(cpp_dec_float_50(number)), 2);
    int max_witness = tmp.convert_to<int>();    // assuming generalized Riemann hypothesis to be true.
    for (int a = 2; a < max_witness; ++a) {
        if (witness(a, number, count_exponent, count_rest)) {
            return false;
        }
    }
    return true;
}