コード例 #1
0
//assume c has been initialized
void encrypt_big_num(mpz_t c, mpz_t m){ 

	get_random_r();

	//set r^n mod n^2
	mpz_powm(r_pow_n, r, n, n_square);

	//set big_temp = (n+1)^m mod n^2
	mpz_powm(big_temp, n_plus_1, m, n_square);

	//set c = (n+1)^m*r^n mod n^2
	mpz_mul(c, big_temp, r_pow_n);
	mpz_mod(c, c, n_square);
}
コード例 #2
0
ファイル: sa.c プロジェクト: BackupTheBerlios/yasa-svn
/*
 * Get random double r, with 0 < r < 1.
 */
double 
get_random_r()
{
    double r;
    int ri;

    ri = rand();
    if ((ri == 0) || (ri == RAND_MAX)) {
        printf("get_random: got %d\n", ri);
        return get_random_r();  /* try again */
    }

    r = ((double) ri) / RAND_MAX;
    return r;
}
コード例 #3
0
ファイル: seqsa.c プロジェクト: BackupTheBerlios/yasa-svn
/*
 * Sequential version.
 */
int
seqsa_solver(char *stg_filename, char *ssf_filename)
{
    int malloced;
    int freed;

    int malloced_initial;
    int freed_initial;

    int malloced_select;
    int freed_select;

    struct stg *tg;
    struct ssf_status *status;
    struct ssf *schedule;

    double eps;
    unsigned seed;

    int i;
    double t0;
    double t;

    struct iss *alpha;  /* present solution */
    struct iss *beta;   /* neighbour solution of alpha */

    double cost_alpha;
    double cost_beta;

    double r;
    double bf;  /* Boltzmann Factor p(alpha -> beta) */


    printf("** Sequential Simulated Annealing Solver\n");

    /* Allocate data structures. */
    malloced = 0;
    freed = 0;
    tg = new_task_graph_from_file(stg_filename, &malloced);
    status = new_status(&malloced);
    strncpy(status->name, "YASA Sequential", 20);

    eps = 1e-3;
    seed = 0;
    t0 = 1000.0;

    alpha = NULL;
    beta = NULL;

    /* Solve. */
    srand(seed);
    print_task_graph(tg);

    malloced_initial = 0;
    freed_initial = 0;
    alpha = create_initial_solution(tg, &malloced_initial, &freed_initial);
    printf("malloced %d bytes for initial solution\n", malloced_initial);
    printf("freed %d bytes for initial solution\n", freed_initial);
    printf("difference: %d bytes\n", malloced_initial - freed_initial);

    cost_alpha = cost(tg, alpha);

    i = 0;
    t = t0;
    
    while (t > eps) {
        printf("i = %d, t = %f\n", i, t);

        malloced_select = 0;
        freed_select = 0;
        beta = select_neighbour(tg, alpha, &malloced_select, &freed_select);
        printf("malloced %d bytes for selection\n", malloced_select);
        printf("freed %d bytes for selection\n", freed_select);
        printf("difference: %d bytes\n", malloced_select - freed_select);
        cost_beta = cost(tg, beta);

        if (cost_beta <= cost_alpha) {
            /* TODO alpha := beta */
            cost_alpha = cost_beta;
        } else {
            r = get_random_r();  /* r from (0, 1) */
            bf = boltzmann_factor(t, cost_alpha, cost_beta);
            printf("r = %f, bf = %f\n", r, bf);
            if (r < bf) {
                /* TODO alpha := beta */
                cost_alpha = cost_beta;
            }
        }
        i++;
        t = new_temp(t, i);
    }
    printf("stopping at i = %d, t = %f.\n", i, t);

    /* alpha to schedule */
    //schedule = new_schedule(tg, alpha, &malloced);
    schedule = new_schedule(tg, &malloced);
    printf("malloced %d bytes\n", malloced);

    /* Display Results */
    print_schedule(schedule);
    write_schedule_to_file(ssf_filename, schedule, status);

    /* Free data structures. */
    free_schedule(schedule, &freed);
    // TODO free initial solution
    free_status(status, &freed);
    free_task_graph(tg, &freed);
    printf("freed %d bytes => ", freed);
    if (malloced == freed)
        printf("OK.\n");
    else {
        printf("Error: malloced != freed!\n");
        return (1);
    }

    return (0);
}