Add an internal-representation no-op function.
[u/mdw/catacomb] / share.c
1 /* -*-c-*-
2 *
3 * $Id: share.c,v 1.6 2001/02/03 16:05:41 mdw Exp $
4 *
5 * Shamir's secret sharing
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: share.c,v $
33 * Revision 1.6 2001/02/03 16:05:41 mdw
34 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
35 * it. Note that the macro version @MP_DROP@ doesn't do this.
36 *
37 * Revision 1.5 2000/12/06 20:30:10 mdw
38 * Change secret sharing interface: present the secret at share
39 * construction time.
40 *
41 * Revision 1.4 2000/10/08 12:16:17 mdw
42 * Use @MP_EQ@ instead of @MP_CMP@.
43 *
44 * Revision 1.3 2000/06/24 18:29:05 mdw
45 * Interface change: allow shares to be extracted from a context on demand,
46 * rather than building them all up-front.
47 *
48 * Revision 1.2 2000/06/18 23:05:19 mdw
49 * Minor performance tweak: use Barrett reduction rather than Montgomery.
50 * Fast secret sharing isn't done here, though: see `gfshare' instead.
51 *
52 * Revision 1.1 2000/06/17 12:09:38 mdw
53 * Shamir's secret sharing system.
54 *
55 */
56
57 /*----- Header files ------------------------------------------------------*/
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include "grand.h"
64 #include "mp.h"
65 #include "mpint.h"
66 #include "mpbarrett.h"
67 #include "mprand.h"
68 #include "pgen.h"
69 #include "rabin.h"
70 #include "share.h"
71
72 /*----- Main code ---------------------------------------------------------*/
73
74 /* --- @share_create@ --- *
75 *
76 * Arguments: @share *s@ = pointer to share context to initialize
77 * @unsigned t@ = threshold for the system
78 *
79 * Returns: ---
80 *
81 * Use: Initializes a sharing context.
82 */
83
84 void share_create(share *s, unsigned t)
85 {
86 s->t = t;
87 s->i = 0;
88 s->p = 0;
89 s->v = 0;
90 }
91
92 /* --- @share_destroy@ --- *
93 *
94 * Arguments: @share *s@ = pointer to share context to destroy
95 *
96 * Returns: ---
97 *
98 * Use: Disposes of a sharing context. All memory is freed, all
99 * integers are dropped.
100 */
101
102 void share_destroy(share *s)
103 {
104 unsigned i;
105
106 /* --- Dispose of the share vector --- */
107
108 if (s->v) {
109 for (i = 0; i < s->t; i++)
110 mp_drop(s->v[i].y);
111 xfree(s->v);
112 }
113
114 /* --- Other stuff --- */
115
116 mp_drop(s->p);
117 }
118
119 /* --- @share_mkshares@ --- *
120 *
121 * Arguments: @share *s@ = pointer to share context to fill in
122 * @grand *r@ = pointer to random number source
123 * @mp *n@ = the secret to share
124 *
125 * Returns: ---
126 *
127 * Use: Initializes a sharing context to be able to create shares.
128 * The context structure is expected to be mostly filled in. In
129 * particular, @t@ must be initialized. If @p@ is zero, a prime
130 * number of appropriate size is generated automatically. If
131 * @v@ is zero, a vector of appropriate size is allocated. You
132 * should use the macro @SHARE_INIT@ or @share_create@ to
133 * construct sharing contexts.
134 */
135
136 void share_mkshares(share *s, grand *r, mp *n)
137 {
138 unsigned i;
139
140 /* --- If there's no prime, construct one --- */
141
142 if (!s->p) {
143 pgen_filterctx pf;
144 rabin pr;
145 mp *p;
146 unsigned bits = (mp_octets(n) + 1) * 8;
147
148 pf.step = 2;
149 p = mprand(MP_NEW, bits, r, 1);
150 s->p = pgen("p", p, p, 0, 0, 0, pgen_filter, &pf,
151 rabin_iters(bits), pgen_test, &pr);
152 }
153
154 /* --- Construct the polynomial --- */
155
156 if (!s->v)
157 s->v = xmalloc(s->t * sizeof(share_pt));
158 for (i = 0; i < s->t - 1; i++)
159 s->v[i].y = mprand_range(MP_NEWSEC, s->p, r, 0);
160 s->v[s->t - 1].y = mp_copy(n);
161 }
162
163 /* --- @share_get@ --- *
164 *
165 * Arguments: @share *s@ = pointer to share conext
166 * @mp *d@ = destination for the share
167 * @unsigned x@ = share index to fetch
168 *
169 * Returns: The share, as requested.
170 *
171 * Use: Extracts a share from the system. You may extract @MPW_MAX@
172 * shares, or @s->p@ shares from the system, whichever is
173 * smaller. Shares are indexed from 0.
174 */
175
176 mp *share_get(share *s, mp *d, unsigned x)
177 {
178 mpbarrett mb;
179 mpw uw = x + 1;
180 mp u;
181 unsigned i;
182
183 /* --- Various bits of initialization --- */
184
185 mp_build(&u, &uw, &uw + 1);
186 mp_drop(d);
187
188 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
189
190 d = MP_ZERO;
191 mpbarrett_create(&mb, s->p);
192 for (i = 0; i < s->t; i++) {
193 d = mp_mul(d, d, &u);
194 d = mp_add(d, d, s->v[i].y);
195 d = mpbarrett_reduce(&mb, d, d);
196 }
197 mpbarrett_destroy(&mb);
198
199 return (d);
200 }
201
202 /* --- @share_add@ --- *
203 *
204 * Arguments: @share *s@ = pointer to sharing context
205 * @unsigned x@ = which share number this is
206 * @mp *y@ = the share value
207 *
208 * Returns: Number of shares required before recovery may be performed.
209 *
210 * Use: Adds a share to the context. The context must have been
211 * initialized with the correct prime @p@ and threshold @t@.
212 */
213
214 unsigned share_add(share *s, unsigned x, mp *y)
215 {
216 /* --- If no vector has been allocated, create one --- */
217
218 if (!s->v) {
219 unsigned i;
220 s->v = xmalloc(s->t * sizeof(share_pt));
221 s->i = 0;
222 for (i = 0; i < s->t; i++)
223 s->v[i].y = 0;
224 }
225
226 assert(((void)"Share context is full", s->i < s->t));
227
228 /* --- Store the share in the vector --- */
229
230 s->v[s->i].x = x + 1;
231 s->v[s->i].y = mp_copy(y);
232 s->i++;
233
234 /* --- Done --- */
235
236 return (s->t - s->i);
237 }
238
239 /* --- @share_combine@ --- *
240 *
241 * Arguments: @share *s@ = pointer to share context
242 *
243 * Returns: The secret, as a multiprecision integer.
244 *
245 * Use: Reconstructs a secret, given enough shares.
246 */
247
248 mp *share_combine(share *s)
249 {
250 mp *a = MP_ZERO;
251 mpbarrett mb;
252 unsigned i, j;
253 mp ii, jj;
254 mpw iiw, jjw;
255 mp *m = MP_NEW;
256
257 /* --- Sanity checking --- */
258
259 assert(((void)"Not enough shares yet", s->i == s->t));
260
261 /* --- Initialization --- */
262
263 mpbarrett_create(&mb, s->p);
264 mp_build(&ii, &iiw, &iiw + 1);
265 mp_build(&jj, &jjw, &jjw + 1);
266
267 /* --- Grind through the shares --- */
268
269 for (i = 0; i < s->t; i++) {
270 mp *c = MP_ONE;
271
272 iiw = s->v[i].x;
273 for (j = 0; j < s->t; j++) {
274 if (i == j)
275 continue;
276 jjw = s->v[j].x;
277 if (s->v[j].x >= s->v[i].x)
278 m = mp_sub(m, &jj, &ii);
279 else {
280 m = mp_sub(m, &ii, &jj);
281 m = mp_sub(m, s->p, m);
282 }
283 mp_gcd(0, 0, &m, s->p, m);
284 c = mp_mul(c, c, &jj);
285 c = mpbarrett_reduce(&mb, c, c);
286 c = mp_mul(c, c, m);
287 c = mpbarrett_reduce(&mb, c, c);
288 }
289 c = mp_mul(c, c, s->v[i].y);
290 c = mpbarrett_reduce(&mb, c, c);
291 a = mp_add(a, a, c);
292 mp_drop(c);
293 }
294
295 a = mpbarrett_reduce(&mb, a, a);
296 mp_drop(m);
297 mpbarrett_destroy(&mb);
298 return (a);
299 }
300
301 /*----- Test rig ----------------------------------------------------------*/
302
303 #ifdef TEST_RIG
304
305 #include "fibrand.h"
306
307 static int verify(grand *r)
308 {
309 unsigned n = r->ops->range(r, 16) + 8;
310 unsigned t = r->ops->range(r, n - 1) + 1;
311 unsigned len = r->ops->range(r, 160);
312
313 mp **v = xmalloc(t * sizeof(mp *));
314 unsigned *p = xmalloc(n * sizeof(unsigned));
315 mp *sec = mprand(MP_NEW, len, r, 0);
316 share s;
317 mp *pp;
318 mp *ss;
319 unsigned i;
320
321 int ok = 1;
322
323 for (i = 0; i < n; i++)
324 p[i] = i;
325 for (i = 0; i < t; i++) {
326 unsigned long j = r->ops->range(r, n - i);
327 unsigned x = p[i];
328 p[i] = p[i + j];
329 p[i + j] = x;
330 }
331
332 share_create(&s, t);
333 share_mkshares(&s, r, sec);
334 for (i = 0; i < t; i++)
335 v[i] = share_get(&s, MP_NEW, p[i]);
336 pp = mp_copy(s.p);
337 share_destroy(&s);
338 assert(mparena_count(MPARENA_GLOBAL) + mparena_count(MPARENA_SECURE) == t + 2);
339
340 share_create(&s, t);
341 s.p = pp;
342 for (i = 0; i < t; i++)
343 share_add(&s, p[i], v[i]);
344 ss = share_combine(&s);
345 share_destroy(&s);
346
347 if (!MP_EQ(sec, ss)) {
348 ok = 0;
349 fprintf(stderr, "\nbad recombination of shares\n");
350 };
351
352 mp_drop(sec);
353 mp_drop(ss);
354
355 for (i = 0; i < t; i++)
356 mp_drop(v[i]);
357
358 xfree(v);
359 xfree(p);
360
361 assert(mparena_count(MPARENA_GLOBAL) + mparena_count(MPARENA_SECURE) == 0);
362 return (ok);
363 }
364
365 int main(void)
366 {
367 grand *r = fibrand_create(0);
368 unsigned i;
369 int ok = 1;
370
371 fputs("share: ", stdout);
372 for (i = 0; i < 40; i++) {
373 if (!verify(r))
374 ok = 0;
375 fputc('.', stdout);
376 fflush(stdout);
377 }
378
379 if (ok)
380 fputs(" ok\n", stdout);
381 else
382 fputs(" failed\n", stdout);
383 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
384 }
385
386 #endif
387
388 /*----- That's all, folks -------------------------------------------------*/