OpenCores
URL https://opencores.org/ocsvn/mod_sim_exp/mod_sim_exp/trunk

Subversion Repositories mod_sim_exp

[/] [mod_sim_exp/] [trunk/] [sw/] [ModMult/] [src/] [ModMult.c] - Blame information for rev 73

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 JonasDC
/*
2
 ============================================================================
3
 Name        : ModExp.c
4
 Author      :
5
 Version     :
6
 Copyright   : Your copyright notice
7
 Description : Hello World in C, Ansi-style
8
 ============================================================================
9
 */
10
 
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <gmp.h>
14
 
15
int main(void) {
16
        unsigned int base_width;
17
        mpz_t x, m, r, R, R2;
18
        gmp_randstate_t state;
19
 
20
        gmp_randinit_mt(state);
21
        mpz_init(x);
22
        mpz_init(m);
23
        mpz_init(r);
24
        mpz_init(R);
25
        mpz_init(R2);
26
 
27
        printf("-- input generator program\n");
28
        printf("--  generates test values for multiplier testbench\n");
29
 
30
        while (1){
31
 
32
                //read in base_width
33
                scanf("%d", &base_width);
34
                if (base_width == 0) break;
35
 
36
                //generate modulus (must be uneven)
37
                mpz_urandomb(m, state, base_width);
38
                mpz_setbit(m, 0); //uneven
39
 
40
                //generate x
41
                mpz_urandomb(x, state, base_width);
42
 
43
                //calculate R
44
                mpz_set_ui(R, 2);
45
                mpz_powm_ui(R, R, base_width, m); //R = 2^n mod m
46
 
47
                //calculate R2
48
                mpz_set_ui(R2, 2);
49
                mpz_powm(R2, R, R2, m); //R2 = Rē mod m = 2^2n mod m
50
 
51
                //calculate result
52
                mpz_mul(r, x, R);
53
                mpz_powm_ui(r, r, 1, m);
54
 
55
                printf("-- x, y, m, result\n");
56
                gmp_printf("%Zx\n", x);
57
                gmp_printf("%Zx\n", R2);
58
                gmp_printf("%Zx\n", m);
59
                gmp_printf("%Zx\n", r);
60
        }
61
 
62
        mpz_clear(x);
63
        mpz_clear(R2);
64
        mpz_clear(m);
65
        mpz_clear(r);
66
        mpz_clear(R);
67
        gmp_randclear(state);
68
 
69
        return EXIT_SUCCESS;
70
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.