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

Subversion Repositories mod_sim_exp

[/] [mod_sim_exp/] [trunk/] [sw/] [ModExp/] [src/] [ModExp.c] - Blame information for rev 101

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, exp_width;
17
        mpz_t m, g0, g1, e0, e1, R, R2, result, tmp, gt0, gt1, gt01;
18
        gmp_randstate_t state;
19
 
20
        gmp_randinit_default(state);
21
        mpz_init(m);
22
        mpz_init(g0);
23
        mpz_init(g1);
24
        mpz_init(e0);
25
        mpz_init(e1);
26
        mpz_init(R);
27
        mpz_init(R2);
28
        mpz_init(result);
29
        mpz_init(tmp);
30
        mpz_init(gt0);
31
        mpz_init(gt1);
32
        mpz_init(gt01);
33
 
34
        printf("-- input generator program\n");
35
        printf("--  generates test values per bit input pair\n");
36
 
37
        while (1){
38
 
39
                //read in base_width
40
                scanf("%d", &base_width);
41
                if (base_width == 0) break;
42
                scanf("%d", &exp_width);
43
 
44
                //generate modulus (must be uneven)
45
                mpz_urandomb(m, state, base_width);
46
                mpz_setbit(m, 0); //uneven
47
 
48
                //generate g0
49
                mpz_urandomb(g0, state, base_width);
50
 
51
                //generate g1
52
                mpz_urandomb(g1, state, base_width);
53
 
54
                //generate e0
55
                mpz_urandomb(e0, state, exp_width);
56
 
57
                //generate e1
58
                mpz_urandomb(e1, state, exp_width);
59
 
60
                //calculate R
61
                mpz_set_ui(R, 2);
62
                mpz_powm_ui(R, R, base_width, m); //R = 2^n mod m
63
 
64
                //calculate R2
65
                mpz_set_ui(R2, 2);
66
                mpz_powm(R2, R, R2, m); //R2 = Rē mod m = 2^2n mod m
67
 
68
                //calc precompute values
69
                mpz_mul(gt0, g0, R);
70
                mpz_powm_ui(gt0, gt0, 1, m);
71
                mpz_mul(gt1, g1, R);
72
                mpz_powm_ui(gt1, gt1, 1, m);
73
                mpz_mul(gt01, g0, g1);
74
                mpz_mul(gt01, gt01, R);
75
                mpz_powm_ui(gt01, gt01, 1, m);
76
 
77
                //calculate result
78
                mpz_powm(result, g0, e0, m);
79
                mpz_powm(tmp, g1, e1, m);
80
                mpz_mul(result, result, tmp);
81
                mpz_powm_ui(result, result, 1, m);
82
 
83
                printf("-- base_width, exp_width, g0, g1, e0, e1, m, R^2, result\n");
84
                printf("%d\n", base_width);
85
                printf("%d\n", exp_width);
86
                gmp_printf("%Zx\n", g0);
87
                gmp_printf("%Zx\n", g1);
88
                gmp_printf("%Zx\n", e0);
89
                gmp_printf("%Zx\n", e1);
90
                gmp_printf("%Zx\n", m);
91
                gmp_printf("%Zx\n", R2);
92
                gmp_printf("%Zx\n", R);
93
                gmp_printf("%Zx\n", gt0);
94
                gmp_printf("%Zx\n", gt1);
95
                gmp_printf("%Zx\n", gt01);
96
                gmp_printf("%Zx\n", result);
97
        }
98
 
99
        mpz_clear(g0);
100
        mpz_clear(g1);
101
        mpz_clear(e0);
102
        mpz_clear(e1);
103
        mpz_clear(m);
104
        mpz_clear(result);
105
        mpz_clear(tmp);
106
        mpz_clear(R);
107
        mpz_clear(R2);
108
        mpz_clear(gt0);
109
        mpz_clear(gt1);
110
        mpz_clear(gt01);
111
 
112
        return EXIT_SUCCESS;
113
}

powered by: WebSVN 2.1.0

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