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