]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-gmp.c
don't use clock_gettime, OS X sucks
[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/urandom", "rb");
41         if(!f)
42                 f = fopen("/dev/random", "rb");
43         if(f)
44         {
45                 unsigned char buf[256];
46                 setbuf(f, NULL);
47                 if(fread(buf, sizeof(buf), 1, f) == 1)
48                 {
49                         mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
50                         gmp_randseed(RANDSTATE, temp.z);
51                 }
52                 fclose(f);
53         }
54 }
55
56 void d0_bignum_SHUTDOWN(void)
57 {
58         d0_bignum_clear(&temp);
59         gmp_randclear(RANDSTATE);
60 }
61
62 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
63 {
64         static unsigned char numbuf[65536];
65         size_t count = 0;
66         numbuf[0] = mpz_sgn(bignum->z) & 3;
67         if((numbuf[0] & 3) != 0) // nonzero
68         {
69                 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
70                 if(count > sizeof(numbuf) - 1)
71                         return 0;
72                 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
73         }
74         return d0_iobuf_write_packet(buf, numbuf, count + 1);
75 }
76
77 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
78 {
79         static unsigned char numbuf[65536];
80         size_t count = sizeof(numbuf);
81         if(!d0_iobuf_read_packet(buf, numbuf, &count))
82                 return NULL;
83         if(count < 1)
84                 return NULL;
85         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
86         if(numbuf[0] & 3) // nonzero
87         {
88                 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
89                 if(numbuf[0] & 2) // negative
90                         mpz_neg(bignum->z, bignum->z);
91         }
92         else // zero
93         {
94                 mpz_set_ui(bignum->z, 0);
95         }
96         return bignum;
97 }
98
99 d0_bignum_t *d0_bignum_new(void)
100 {
101         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
102         mpz_init(b->z);
103         return b;
104 }
105
106 void d0_bignum_free(d0_bignum_t *a)
107 {
108         mpz_clear(a->z);
109         d0_free(a);
110 }
111
112 void d0_bignum_init(d0_bignum_t *b)
113 {
114         mpz_init(b->z);
115 }
116
117 void d0_bignum_clear(d0_bignum_t *a)
118 {
119         mpz_clear(a->z);
120 }
121
122 size_t d0_bignum_size(const d0_bignum_t *r)
123 {
124         return mpz_sizeinbase(r->z, 2);
125 }
126
127 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
128 {
129         return mpz_cmp(a->z, b->z);
130 }
131
132 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
133 {
134         if(!r) r = d0_bignum_new(); if(!r) return NULL;
135         mpz_sub(temp.z, max->z, min->z);
136         mpz_urandomm(r->z, RANDSTATE, temp.z);
137         mpz_add(r->z, r->z, min->z);
138         return r;
139 }
140
141 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
142 {
143         if(!r) r = d0_bignum_new(); if(!r) return NULL;
144         mpz_urandomb(r->z, RANDSTATE, n);
145         return r;
146 }
147
148 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
149 {
150         if(!r) r = d0_bignum_new(); if(!r) return NULL;
151         mpz_urandomb(r->z, RANDSTATE, n-1);
152         mpz_setbit(r->z, n-1);
153         return r;
154 }
155
156 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
157 {
158         return d0_bignum_int(r, 0);
159 }
160
161 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
162 {
163         return d0_bignum_int(r, 1);
164 }
165
166 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
167 {
168         if(!r) r = d0_bignum_new(); if(!r) return NULL;
169         mpz_set_si(r->z, n);
170         return r;
171 }
172
173 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
174 {
175         if(r == a)
176                 return r; // trivial
177         if(!r) r = d0_bignum_new(); if(!r) return NULL;
178         mpz_set(r->z, a->z);
179         return r;
180 }
181
182 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
183 {
184         if(!r) r = d0_bignum_new(); if(!r) return NULL;
185         mpz_neg(r->z, a->z);
186         return r;
187 }
188
189 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
190 {
191         if(!r) r = d0_bignum_new(); if(!r) return NULL;
192         if(n > 0)
193                 mpz_mul_2exp(r->z, a->z, n);
194         else if(n < 0)
195                 mpz_fdiv_q_2exp(r->z, a->z, -n);
196         else
197                 mpz_set(r->z, a->z);
198         return r;
199 }
200
201 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
202 {
203         if(!r) r = d0_bignum_new(); if(!r) return NULL;
204         mpz_add(r->z, a->z, b->z);
205         return r;
206 }
207
208 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
209 {
210         if(!r) r = d0_bignum_new(); if(!r) return NULL;
211         mpz_sub(r->z, a->z, b->z);
212         return r;
213 }
214
215 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
216 {
217         if(!r) r = d0_bignum_new(); if(!r) return NULL;
218         mpz_mul(r->z, a->z, b->z);
219         return r;
220 }
221
222 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
223 {
224         if(!q && !m)
225                 m = d0_bignum_new();
226         if(q)
227                 if(m)
228                         mpz_fdiv_qr(q->z, m->z, a->z, b->z);
229                 else
230                         mpz_fdiv_q(q->z, a->z, b->z);
231         else
232                 mpz_fdiv_r(m->z, a->z, b->z);
233         if(m)
234                 return m;
235         else
236                 return q;
237 }
238
239 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)
240 {
241         r = d0_bignum_add(r, a, b);
242         mpz_fdiv_r(r->z, r->z, m->z);
243         return r;
244 }
245
246 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)
247 {
248         r = d0_bignum_mul(r, a, b);
249         mpz_fdiv_r(r->z, r->z, m->z);
250         return r;
251 }
252
253 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)
254 {
255         if(!r) r = d0_bignum_new(); if(!r) return NULL;
256         mpz_powm(r->z, a->z, b->z, m->z);
257         return r;
258 }
259
260 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
261 {
262         // here, r MUST be set, as otherwise we cannot return error state!
263         return mpz_invert(r->z, a->z, m->z);
264 }
265
266 int d0_bignum_isprime(d0_bignum_t *r, int param)
267 {
268         return mpz_probab_prime_p(r->z, param);
269 }
270
271 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)
272 {
273         if(!r) r = d0_bignum_new(); if(!r) return NULL;
274         if(s)
275                 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
276         else if(t)
277                 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
278         else
279                 mpz_gcd(r->z, a->z, b->z);
280         return r;
281 }
282
283 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
284 {
285         return mpz_get_str(NULL, base, x->z);
286 }