]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-openssl.c
use is_prime_fasttest for the quick primality test
[xonotic/d0_blind_id.git] / d0_bignum-openssl.c
1 /*
2  * FILE:        d0_bignum-openssl.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Format:commit %H$
33  * $Id$
34  */
35
36 /* NOTE: this file links against openssl (http://www.openssl.org), which is
37  * under the OpenSSL License. You may have to abide to its terms too if you use
38  * this file.
39  * To alternatively link to GMP, provide the option --without-openssl to
40  * ./configure.
41  */
42
43 #include "d0_bignum.h"
44
45 #include <assert.h>
46 #include <string.h>
47 #include <openssl/bn.h>
48
49 struct d0_bignum_s
50 {
51         BIGNUM z;
52 };
53
54 static d0_bignum_t temp;
55 static BN_CTX *ctx;
56
57 #include <time.h>
58 #include <stdio.h>
59
60 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
61 {
62         ctx = BN_CTX_new();
63         d0_bignum_init(&temp);
64         return 1;
65 }
66
67 void d0_bignum_SHUTDOWN(void)
68 {
69         d0_bignum_clear(&temp);
70         BN_CTX_free(ctx);
71         ctx = NULL;
72 }
73
74 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
75 {
76         static unsigned char numbuf[65536];
77         size_t count = 0;
78         numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
79         if((numbuf[0] & 3) != 0) // nonzero
80         {
81                 count = BN_num_bytes(&bignum->z);
82                 if(count > sizeof(numbuf) - 1)
83                         return 0;
84                 BN_bn2bin(&bignum->z, numbuf+1);
85         }
86         return d0_iobuf_write_packet(buf, numbuf, count + 1);
87 }
88
89 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
90 {
91         static unsigned char numbuf[65536];
92         size_t count = sizeof(numbuf);
93         if(!d0_iobuf_read_packet(buf, numbuf, &count))
94                 return NULL;
95         if(count < 1)
96                 return NULL;
97         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
98         if(numbuf[0] & 3) // nonzero
99         {
100                 BN_bin2bn(numbuf+1, count-1, &bignum->z);
101                 if(numbuf[0] & 2) // negative
102                         BN_set_negative(&bignum->z, 1);
103         }
104         else // zero
105         {
106                 BN_zero(&bignum->z);
107         }
108         return bignum;
109 }
110
111 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
112 {
113         size_t count;
114         count = BN_num_bytes(&bignum->z);
115         if(count > bufsize)
116                 return -1;
117         if(bufsize > count)
118         {
119                 // pad from left (big endian numbers!)
120                 memset(buf, 0, bufsize - count);
121                 buf += bufsize - count;
122         }
123         BN_bn2bin(&bignum->z, buf);
124         return bufsize;
125 }
126
127 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
128 {
129         size_t count;
130         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
131         BN_bin2bn(buf, bufsize, &bignum->z);
132         return bignum;
133 }
134
135 d0_bignum_t *d0_bignum_new(void)
136 {
137         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
138         BN_init(&b->z);
139         return b;
140 }
141
142 void d0_bignum_free(d0_bignum_t *a)
143 {
144         BN_free(&a->z);
145         d0_free(a);
146 }
147
148 void d0_bignum_init(d0_bignum_t *b)
149 {
150         BN_init(&b->z);
151 }
152
153 void d0_bignum_clear(d0_bignum_t *a)
154 {
155         BN_free(&a->z);
156 }
157
158 size_t d0_bignum_size(const d0_bignum_t *r)
159 {
160         return BN_num_bits(&r->z);
161 }
162
163 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
164 {
165         return BN_cmp(&a->z, &b->z);
166 }
167
168 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
169 {
170         if(!r) r = d0_bignum_new(); if(!r) return NULL;
171         BN_sub(&temp.z, &max->z, &min->z);
172         BN_rand_range(&r->z, &temp.z);
173         BN_add(&r->z, &r->z, &min->z);
174         return r;
175 }
176
177 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
178 {
179         if(!r) r = d0_bignum_new(); if(!r) return NULL;
180         BN_rand(&r->z, n, -1, 0);
181         return r;
182 }
183
184 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
185 {
186         if(!r) r = d0_bignum_new(); if(!r) return NULL;
187         BN_rand(&r->z, n, 0, 0);
188         return r;
189 }
190
191 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
192 {
193         if(!r) r = d0_bignum_new(); if(!r) return NULL;
194         BN_zero(&r->z);
195         return r;
196 }
197
198 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
199 {
200         if(!r) r = d0_bignum_new(); if(!r) return NULL;
201         BN_one(&r->z);
202         return r;
203 }
204
205 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
206 {
207         if(!r) r = d0_bignum_new(); if(!r) return NULL;
208         BN_set_word(&r->z, n);
209         return r;
210 }
211
212 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
213 {
214         if(r == a)
215                 return r; // trivial
216         if(!r) r = d0_bignum_new(); if(!r) return NULL;
217         BN_copy(&r->z, &a->z);
218         return r;
219 }
220
221 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
222 {
223         if(!r) r = d0_bignum_new(); if(!r) return NULL;
224         if(r != a)
225                 BN_copy(&r->z, &a->z);
226         BN_set_negative(&r->z, !BN_is_negative(&r->z));
227         return r;
228 }
229
230 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
231 {
232         if(!r) r = d0_bignum_new(); if(!r) return NULL;
233         if(n > 0)
234                 BN_lshift(&r->z, &a->z, n);
235         else if(n < 0)
236                 BN_rshift(&r->z, &a->z, -n);
237         else if(r != a)
238                 BN_copy(&r->z, &a->z);
239         return r;
240 }
241
242 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
243 {
244         if(!r) r = d0_bignum_new(); if(!r) return NULL;
245         BN_add(&r->z, &a->z, &b->z);
246         return r;
247 }
248
249 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
250 {
251         if(!r) r = d0_bignum_new(); if(!r) return NULL;
252         BN_sub(&r->z, &a->z, &b->z);
253         return r;
254 }
255
256 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
257 {
258         if(!r) r = d0_bignum_new(); if(!r) return NULL;
259         BN_mul(&r->z, &a->z, &b->z, ctx);
260         return r;
261 }
262
263 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
264 {
265         if(!q && !m)
266                 m = d0_bignum_new();
267         if(q)
268         {
269                 if(m)
270                         BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
271                 else
272                         BN_div(&q->z, NULL, &a->z, &b->z, ctx);
273                 assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
274         }
275         else
276                 BN_nnmod(&m->z, &a->z, &b->z, ctx);
277         if(m)
278                 return m;
279         else
280                 return q;
281 }
282
283 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)
284 {
285         if(!r) r = d0_bignum_new(); if(!r) return NULL;
286         BN_mod_add(&r->z, &a->z, &b->z, &m->z, ctx);
287         return r;
288 }
289
290 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)
291 {
292         if(!r) r = d0_bignum_new(); if(!r) return NULL;
293         BN_mod_mul(&r->z, &a->z, &b->z, &m->z, ctx);
294         return r;
295 }
296
297 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)
298 {
299         if(!r) r = d0_bignum_new(); if(!r) return NULL;
300         BN_mod_exp(&r->z, &a->z, &b->z, &m->z, ctx);
301         return r;
302 }
303
304 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
305 {
306         // here, r MUST be set, as otherwise we cannot return error state!
307         return !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
308 }
309
310 int d0_bignum_isprime(d0_bignum_t *r, int param)
311 {
312         if(param <= 0)
313                 return BN_is_prime_fasttest(&r->z, 1, NULL, ctx, NULL, 1);
314         else
315                 return BN_is_prime(&r->z, param, NULL, ctx, NULL);
316 }
317
318 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)
319 {
320         if(!r) r = d0_bignum_new(); if(!r) return NULL;
321         if(s)
322                 assert(!"Extended gcd not implemented");
323         else if(t)
324                 assert(!"Extended gcd not implemented");
325         else
326                 BN_gcd(&r->z, &a->z, &b->z, ctx);
327         return r;
328 }
329
330 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
331 {
332         if(base == 10)
333                 return BN_bn2dec(&x->z);
334         else if(base == 16)
335                 return BN_bn2hex(&x->z);
336         else
337                 assert(!"Other bases not implemented");
338 }