]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-gmp.c
embed a diffie hellmann key exchange
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
1 /*
2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010  Rudolf Polzer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #include "d0_bignum.h"
21
22 #include <gmp.h>
23
24 struct d0_bignum_s
25 {
26         mpz_t z;
27 };
28
29 static gmp_randstate_t RANDSTATE;
30 static d0_bignum_t temp;
31
32 #include <time.h>
33 #include <stdio.h>
34 void d0_bignum_INITIALIZE(void)
35 {
36         FILE *f;
37         d0_bignum_init(&temp);
38         gmp_randinit_mt(RANDSTATE);
39         gmp_randseed_ui(RANDSTATE, time(NULL));
40         f = fopen("/dev/random", "rb");
41         if(f)
42         {
43                 unsigned char buf[256];
44                 setbuf(f, NULL);
45                 if(fread(buf, sizeof(buf), 1, f) == 1)
46                 {
47                         mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
48                         gmp_randseed(RANDSTATE, temp.z);
49                 }
50                 fclose(f);
51         }
52 }
53
54 void d0_bignum_SHUTDOWN(void)
55 {
56         d0_bignum_clear(&temp);
57         gmp_randclear(RANDSTATE);
58 }
59
60 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
61 {
62         static unsigned char numbuf[65536];
63         size_t count = 0;
64         numbuf[0] = mpz_sgn(bignum->z) & 3;
65         if((numbuf[0] & 3) != 0) // nonzero
66         {
67                 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
68                 if(count > sizeof(numbuf) - 1)
69                         return 0;
70                 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
71         }
72         return d0_iobuf_write_packet(buf, numbuf, count + 1);
73 }
74
75 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
76 {
77         static unsigned char numbuf[65536];
78         size_t count = sizeof(numbuf);
79         if(!d0_iobuf_read_packet(buf, numbuf, &count))
80                 return NULL;
81         if(count < 1)
82                 return NULL;
83         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
84         if(numbuf[0] & 3) // nonzero
85         {
86                 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
87                 if(numbuf[0] & 2) // negative
88                         mpz_neg(bignum->z, bignum->z);
89         }
90         else // zero
91         {
92                 mpz_set_ui(bignum->z, 0);
93         }
94         return bignum;
95 }
96
97 d0_bignum_t *d0_bignum_new(void)
98 {
99         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
100         mpz_init(b->z);
101         return b;
102 }
103
104 void d0_bignum_free(d0_bignum_t *a)
105 {
106         mpz_clear(a->z);
107         d0_free(a);
108 }
109
110 void d0_bignum_init(d0_bignum_t *b)
111 {
112         mpz_init(b->z);
113 }
114
115 void d0_bignum_clear(d0_bignum_t *a)
116 {
117         mpz_clear(a->z);
118 }
119
120 size_t d0_bignum_size(const d0_bignum_t *r)
121 {
122         return mpz_sizeinbase(r->z, 2);
123 }
124
125 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
126 {
127         return mpz_cmp(a->z, b->z);
128 }
129
130 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
131 {
132         if(!r) r = d0_bignum_new(); if(!r) return NULL;
133         mpz_sub(temp.z, max->z, min->z);
134         mpz_urandomm(r->z, RANDSTATE, temp.z);
135         mpz_add(r->z, r->z, min->z);
136         return r;
137 }
138
139 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
140 {
141         if(!r) r = d0_bignum_new(); if(!r) return NULL;
142         mpz_urandomb(r->z, RANDSTATE, n);
143         return r;
144 }
145
146 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
147 {
148         if(!r) r = d0_bignum_new(); if(!r) return NULL;
149         mpz_urandomb(r->z, RANDSTATE, n-1);
150         mpz_setbit(r->z, n-1);
151         return r;
152 }
153
154 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
155 {
156         return d0_bignum_int(r, 0);
157 }
158
159 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
160 {
161         return d0_bignum_int(r, 1);
162 }
163
164 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
165 {
166         if(!r) r = d0_bignum_new(); if(!r) return NULL;
167         mpz_set_si(r->z, n);
168         return r;
169 }
170
171 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
172 {
173         if(r == a)
174                 return r; // trivial
175         if(!r) r = d0_bignum_new(); if(!r) return NULL;
176         mpz_set(r->z, a->z);
177         return r;
178 }
179
180 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
181 {
182         if(!r) r = d0_bignum_new(); if(!r) return NULL;
183         mpz_neg(r->z, a->z);
184         return r;
185 }
186
187 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
188 {
189         if(!r) r = d0_bignum_new(); if(!r) return NULL;
190         if(n > 0)
191                 mpz_mul_2exp(r->z, a->z, n);
192         else if(n < 0)
193                 mpz_fdiv_q_2exp(r->z, a->z, -n);
194         else
195                 mpz_set(r->z, a->z);
196         return r;
197 }
198
199 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
200 {
201         if(!r) r = d0_bignum_new(); if(!r) return NULL;
202         mpz_add(r->z, a->z, b->z);
203         return r;
204 }
205
206 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
207 {
208         if(!r) r = d0_bignum_new(); if(!r) return NULL;
209         mpz_sub(r->z, a->z, b->z);
210         return r;
211 }
212
213 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
214 {
215         if(!r) r = d0_bignum_new(); if(!r) return NULL;
216         mpz_mul(r->z, a->z, b->z);
217         return r;
218 }
219
220 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
221 {
222         if(!q && !m)
223                 m = d0_bignum_new();
224         if(q)
225                 if(m)
226                         mpz_fdiv_qr(q->z, m->z, a->z, b->z);
227                 else
228                         mpz_fdiv_q(q->z, a->z, b->z);
229         else
230                 mpz_fdiv_r(m->z, a->z, b->z);
231         if(m)
232                 return m;
233         else
234                 return q;
235 }
236
237 d0_bignum_t *d0_bignum_mod_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
238 {
239         r = d0_bignum_add(r, a, b);
240         mpz_fdiv_r(r->z, r->z, m->z);
241         return r;
242 }
243
244 d0_bignum_t *d0_bignum_mod_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
245 {
246         r = d0_bignum_mul(r, a, b);
247         mpz_fdiv_r(r->z, r->z, m->z);
248         return r;
249 }
250
251 d0_bignum_t *d0_bignum_mod_pow(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
252 {
253         if(!r) r = d0_bignum_new(); if(!r) return NULL;
254         mpz_powm(r->z, a->z, b->z, m->z);
255         return r;
256 }
257
258 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
259 {
260         // here, r MUST be set, as otherwise we cannot return error state!
261         return mpz_invert(r->z, a->z, m->z);
262 }
263
264 int d0_bignum_isprime(d0_bignum_t *r, int param)
265 {
266         return mpz_probab_prime_p(r->z, param);
267 }
268
269 d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const d0_bignum_t *a, const d0_bignum_t *b)
270 {
271         if(!r) r = d0_bignum_new(); if(!r) return NULL;
272         if(s)
273                 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
274         else if(t)
275                 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
276         else
277                 mpz_gcd(r->z, a->z, b->z);
278         return r;
279 }
280
281 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
282 {
283         return mpz_get_str(NULL, base, x->z);
284 }